Advertisement
Guest User

xmlsettings

a guest
Feb 4th, 2012
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.30 KB | None | 0 0
  1.     using System;
  2.     using System.Collections.Generic;
  3.     using System.Linq;
  4.     using System.Text;
  5.     using System.IO;
  6.     using System.Xml.Linq;
  7.  
  8.     namespace Classic6
  9.     {
  10.     public class XmlSettings
  11.     {
  12.         private Dictionary<string, XmlSetting> Values { get; set; }
  13.         private string[] SettingFiles;
  14.         public bool EnableSaving { get; set; }
  15.         public event EventHandler<SettingChangedEventArgs> OnSettingChanged;
  16.         /// <summary>
  17.         /// The location of the XML file that new keys
  18.         /// should be stored in (when a key is added
  19.         /// via XmlSettings["key"] without a file, it
  20.         /// will be saved here.
  21.         /// </summary>
  22.         public static string DefaultFile { get; set; }
  23.  
  24.         public XmlSettings()
  25.         {
  26.             Values = new Dictionary<string, XmlSetting>();
  27.             EnableSaving = false;
  28.         }
  29.  
  30.         public void Load(string SettingsDirectory)
  31.         {
  32.             SettingFiles = Directory.GetFiles(SettingsDirectory, "*.xml", SearchOption.AllDirectories);
  33.             foreach (string file in SettingFiles)
  34.             {
  35.                 try
  36.                 {
  37.                     Stream s = File.Open(file, FileMode.Open);
  38.                     XDocument d = XDocument.Load(s);
  39.                     s.Close();
  40.                     LoadRecursive(d.Root, file, string.Empty);
  41.                 }
  42.                 catch { }
  43.             }
  44.  
  45.             if (string.IsNullOrEmpty(DefaultFile))
  46.                 DefaultFile = Path.Combine(SettingsDirectory, "bla.xml.bak.invalid");
  47.         }
  48.  
  49.         private void LoadRecursive(XElement root, string sourceFile, string path)
  50.         {
  51.             foreach (XElement e in root.Elements())
  52.             {
  53.                 if (e.Elements().Count() != 0)
  54.                     LoadRecursive(e, sourceFile, path + e.Name + ".");
  55.                 foreach (XAttribute a in e.Attributes())
  56.                 {
  57.                     Values[(path + e.Name.LocalName.ToString() + "." +
  58.                         a.Name.LocalName.ToString()).ToLower()] = new XmlSetting(sourceFile, a.Value, true);
  59.                 }
  60.                 if (Values.ContainsKey((path + e.Name.LocalName.ToString()).ToLower()))
  61.                 {
  62.                     if (Values[(path + e.Name.LocalName.ToString()).ToLower()].Value != e.Value)
  63.                     {
  64.                         if (OnSettingChanged != null)
  65.                             OnSettingChanged(this, new SettingChangedEventArgs((path + e.Name.LocalName.ToString()).ToLower(),
  66.                                 Values[(path + e.Name.LocalName.ToString()).ToLower()].Value, e.Value));
  67.                     }
  68.                 }
  69.                 Values[(path + e.Name.LocalName.ToString()).ToLower()] = new XmlSetting(sourceFile, e.Value, false);
  70.             }
  71.         }
  72.  
  73.         public int GetInt(string Key)
  74.         {
  75.             int i = -1;
  76.             if (!int.TryParse(this[Key], out i) && !Key.StartsWith("command") && !Key.Contains("port"))
  77.                 Server.server.Log("Setting error: " + Key + " is not a valid integer.");
  78.             return i;
  79.         }
  80.        
  81.         public bool GetBool(string Key)
  82.         {
  83.             bool b = false;
  84.             if (!bool.TryParse(this[Key], out b))
  85.                 Server.server.Log("Setting error: " + Key + " is not a valid boolean.");
  86.             return b;
  87.         }
  88.  
  89.         public bool ContainsKey(string Key)
  90.         {
  91.             return Values.ContainsKey(Key.ToLower());
  92.         }
  93.  
  94.         public string this[string key]
  95.         {
  96.             get
  97.             {
  98.                 if (!Values.ContainsKey(key.ToLower()))
  99.                     return "";
  100.                 return Values[key.ToLower()].Value;
  101.             }
  102.             set
  103.             {
  104.                 if (OnSettingChanged != null)
  105.                     OnSettingChanged(this, new SettingChangedEventArgs(key, Values.ContainsKey(key.ToLower()) ? Values[key.ToLower()].Value : DefaultFile, value));
  106.                 if (Values.ContainsKey(key))
  107.                     Values[key.ToLower()].Value = value;
  108.                 else
  109.                     Values[key.ToLower()] = new XmlSetting(DefaultFile, value, false);
  110.  
  111.                 if (string.IsNullOrEmpty(DefaultFile))
  112.                     return;
  113.                 if (!EnableSaving)
  114.                     return;
  115.  
  116.                 XDocument d = new XDocument();
  117.  
  118.                 if (File.Exists(Values[key.ToLower()].SourceFile))
  119.                 {
  120.                     Stream s = File.Open(Values[key.ToLower()].SourceFile, FileMode.OpenOrCreate);
  121.                     d = XDocument.Load(s, LoadOptions.PreserveWhitespace);
  122.                     s.Close();
  123.                 }
  124.                 else
  125.                 {
  126.                     d = new XDocument();
  127.                     d.Add(new XElement("Classic6"));
  128.                 }
  129.                 // Locate this property
  130.                 string[] parts = key.ToLower().Split('.');
  131.                 XElement currentElement = d.Root;
  132.                 for (int i = 0; i < parts.Length; i++ )
  133.                 {
  134.                     bool found = false;
  135.                     if (parts.Length - 1 == i)
  136.                     {
  137.                         foreach (XAttribute a in currentElement.Attributes())
  138.                         {
  139.                             if (a.Name.LocalName.ToLower() == parts[i])
  140.                             {
  141.                                 found = true;
  142.                                 break;
  143.                             }
  144.                         }
  145.                     }
  146.                     foreach (XElement e in currentElement.Elements())
  147.                     {
  148.                         if (e.Name.LocalName.ToLower() == parts[i])
  149.                         {
  150.                             currentElement = e;
  151.                             found = true;
  152.                             break;
  153.                         }
  154.                     }
  155.                     if (!found)
  156.                     {
  157.                         XElement el = new XElement(parts[i]);
  158.                         currentElement.Add(el);
  159.                         currentElement = el;
  160.                     }
  161.                 }
  162.                 if (Values[key.ToLower()].IsAttribute)
  163.                     currentElement.SetAttributeValue(parts[parts.Length - 1], Values[key.ToLower()].Value);
  164.                 else
  165.                     currentElement.SetValue(Values[key.ToLower()].Value);
  166.                
  167.                 d.Save(Values[key.ToLower()].SourceFile);
  168.             }
  169.         }
  170.     }
  171.  
  172.     internal class XmlSetting
  173.     {
  174.         public string SourceFile { get; set; }
  175.         public string Value { get; set; }
  176.         public bool IsAttribute { get; set; }
  177.  
  178.         public XmlSetting(string SourceFile, string Value, bool IsAttribute)
  179.         {
  180.             this.SourceFile = SourceFile;
  181.             this.Value = Value;
  182.             this.IsAttribute = IsAttribute;
  183.         }
  184.     }
  185.  
  186.     public class SettingChangedEventArgs : EventArgs
  187.     {
  188.         public string Key { get; set; }
  189.         public string OldValue { get; set; }
  190.         public string NewValue { get; set; }
  191.  
  192.         public SettingChangedEventArgs(string Key, string OldValue, string NewValue)
  193.         {
  194.             this.Key = Key;
  195.             this.OldValue = OldValue;
  196.             this.NewValue = NewValue;
  197.         }
  198.     }
  199.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement