Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/24/09 17:04:02 (15 years ago)
Author:
epitzer
Message:

Incorporate settings infrastructure. (#506)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/New Persistence Exploration/Persistence/Persistence/Core/ConfigurationService.cs

    r1400 r1406  
    11using System;
     2using System.Collections.Specialized;
     3using System.IO;
     4using System.Linq;
    25using System.Collections.Generic;
    36using System.Reflection;
     7using System.Text;
     8using HeuristicLab.Persistence.Default.Xml;
    49using HeuristicLab.Persistence.Interfaces;
    510
     
    914
    1015    private static ConfigurationService instance;
    11     private Dictionary<IFormat, Configuration> customConfigurations;
     16    private readonly Dictionary<IFormat, Configuration> customConfigurations;
    1217    public readonly Dictionary<IFormat, List<IFormatter>> Formatters;
    1318    public readonly List<IDecomposer> Decomposers;
     
    2429      Formatters = new Dictionary<IFormat, List<IFormatter>>();
    2530      Decomposers = new List<IDecomposer>();
    26       customConfigurations = new Dictionary<IFormat, Configuration>();
     31      customConfigurations = new Dictionary<IFormat, Configuration>();     
    2732      Reset();
     33      LoadSettings();
     34    }
     35
     36    public void LoadSettings() {
     37      if ( String.IsNullOrEmpty(Properties.Settings.Default.customConfigurations) ||
     38        String.IsNullOrEmpty(Properties.Settings.Default.customConfigurationsTypeCache) )
     39        return;
     40      DeSerializer deSerializer = new DeSerializer(
     41        XmlParser.ParseTypeCache(
     42        new StringReader(
     43          Properties.Settings.Default.customConfigurationsTypeCache)));
     44      XmlParser parser = new XmlParser(
     45        new StringReader(
     46          Properties.Settings.Default.customConfigurations));
     47      var newCustomConfigurations = (Dictionary<IFormat, Configuration>)
     48        deSerializer.DeSerialize(parser);     
     49      foreach ( var config in newCustomConfigurations ) {
     50        customConfigurations[config.Key] = config.Value;
     51      }
     52    }
     53
     54    public void SaveSettings() {     
     55      Serializer serializer = new Serializer(
     56        customConfigurations,
     57        GetDefaultConfig(XmlFormat.Instance),
     58        "CustomConfigurations");
     59      XmlGenerator generator = new XmlGenerator();
     60      StringBuilder configurationString = new StringBuilder();
     61      foreach (ISerializationToken token in serializer) {
     62        configurationString.Append(generator.Format(token));
     63      }
     64      StringBuilder configurationTypeCacheString = new StringBuilder();
     65      foreach (string s in generator.Format(serializer.TypeCache))
     66        configurationTypeCacheString.Append(s);
     67      Properties.Settings.Default.customConfigurations =
     68        configurationString.ToString();
     69      Properties.Settings.Default.customConfigurationsTypeCache =
     70        configurationTypeCacheString.ToString();
     71      Properties.Settings.Default.Save();
    2872    }
    2973
    3074    public void Reset() {     
    3175      customConfigurations.Clear();
     76      Formatters.Clear();
     77      Decomposers.Clear();
    3278      Assembly defaultAssembly = Assembly.GetExecutingAssembly();
    3379      DiscoverFrom(defaultAssembly);
     
    77123    public void DefineConfiguration(IFormat format, Configuration configuration) {
    78124      customConfigurations[format] = configuration;
     125      SaveSettings();
    79126    }
    80127
Note: See TracChangeset for help on using the changeset viewer.