00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 using System;
00023 using System.IO;
00024 using System.Xml;
00025 using System.Xml.XPath;
00026
00027 namespace CSharp_ClassLib.Classes
00028 {
00051 public class PreferencesFile : System.Object
00052 {
00054 private System.Xml.XmlDocument m_oXMLDocument;
00055
00057 private System.Collections.ArrayList m_oPreferencesEntries;
00058
00060 private const string XML_TAG_ROOT = "preferences_file";
00062 private const string XML_TAG_ENTRIES = "entries";
00063
00065 private const string XML_ATTRIBUTE_ID = "id";
00067 private const string XML_ATTRIBUTE_CATEGORY = "category";
00068
00070 private const string XPATH_ALL_ENTRIES = "/" + XML_TAG_ROOT + "//" + XML_TAG_ENTRIES + "/child::*";
00071
00084 public PreferencesFile ()
00085 {
00086 this.InitRest();
00087
00088 return;
00089 }
00090
00106 public PreferencesFile (string Preferences_File)
00107 {
00108 this.InitRest();
00109
00110 this.LoadFromFile(Preferences_File);
00111
00112 return;
00113 }
00114
00134 public CSharp_ClassLib.Classes.PreferencesEntry[] Entries
00135 {
00136 get
00137 {
00138 CSharp_ClassLib.Classes.PreferencesEntry[] oBuffer = new CSharp_ClassLib.Classes.PreferencesEntry[m_oPreferencesEntries.Count];
00139 m_oPreferencesEntries.CopyTo(oBuffer);
00140
00141 return oBuffer;
00142 }
00143 }
00144
00149 private void InitRest ()
00150 {
00151 m_oXMLDocument = null;
00152
00153 m_oPreferencesEntries = new System.Collections.ArrayList();
00154
00155 return;
00156 }
00157
00173 public void LoadFromFile (string Preferences_File)
00174 {
00175 if (!System.IO.File.Exists(Preferences_File))
00176
00177 throw ( new System.IO.FileNotFoundException("Preferences file not found!", Preferences_File) );
00178
00179
00180 System.Xml.XmlDocument oTEMPXMLDocument = new System.Xml.XmlDocument();
00181
00182 System.Xml.XPath.XPathDocument oXPathDocument = new System.Xml.XPath.XPathDocument(Preferences_File);
00183
00184 try
00185 {
00186 oTEMPXMLDocument.Load(Preferences_File);
00187
00188
00189 this.ReadPreferencesFileEntries(oXPathDocument);
00190
00191 m_oXMLDocument = oTEMPXMLDocument;
00192 }
00193 catch (System.Exception e)
00194 {
00195 throw (e);
00196 }
00197
00198 return;
00199 }
00200
00209 private void ReadPreferencesFileEntries (System.Xml.XPath.XPathDocument XPath_Document)
00210 {
00211 if (null == XPath_Document)
00212
00213 throw ( new System.ArgumentNullException("XPath_Document", "Argument must not be null!") );
00214
00215 try
00216 {
00217
00218 System.Collections.ArrayList m_oTempPreferencesEntries = new System.Collections.ArrayList();
00219
00220 System.Xml.XPath.XPathNavigator oXMLPathNav = XPath_Document.CreateNavigator();
00221 System.Xml.XPath.XPathNodeIterator oXMLNodeIterator = oXMLPathNav.Select(XPATH_ALL_ENTRIES);
00222
00223
00224 CSharp_ClassLib.Classes.PreferencesEntry oTMPEntry;
00225
00226 while ( oXMLNodeIterator.MoveNext() )
00227 {
00228 switch ( oXMLNodeIterator.Current.Name.ToLower().Trim() )
00229 {
00230 case "entry":
00231
00232 oTMPEntry = new CSharp_ClassLib.Classes.PreferencesEntry();
00233 oTMPEntry.ID = oXMLNodeIterator.Current.GetAttribute(XML_ATTRIBUTE_ID, "");
00234 oTMPEntry.Category = oXMLNodeIterator.Current.GetAttribute(XML_ATTRIBUTE_CATEGORY, "");
00235 oTMPEntry.Value = oXMLNodeIterator.Current.Value;
00236
00237
00238 m_oTempPreferencesEntries.Add(oTMPEntry);
00239 break;
00240 }
00241 }
00242
00243 m_oPreferencesEntries = m_oTempPreferencesEntries;
00244 }
00245 catch (System.Exception e)
00246 {
00247 throw(e);
00248 }
00249
00250 return;
00251 }
00252
00265 public string GetValue (string ID, bool ThrowException)
00266 {
00267 return this.GetValue(ID, "", ThrowException);
00268 }
00269
00282 public string GetValue (string ID, string Category, bool ThrowException)
00283 {
00284 int i = 0;
00285
00286 for (i = 0; i < this.m_oPreferencesEntries.Count; i++)
00287 {
00288 if ( (((CSharp_ClassLib.Classes.PreferencesEntry)m_oPreferencesEntries[i]).Category == Category) && (((CSharp_ClassLib.Classes.PreferencesEntry)m_oPreferencesEntries[i]).ID == ID) )
00289
00290 return ((CSharp_ClassLib.Classes.PreferencesEntry)m_oPreferencesEntries[i]).Value;
00291 }
00292
00293
00294 if (ThrowException)
00295 throw ( new System.ArgumentOutOfRangeException("ID; Category", "Entry was not found!") );
00296
00297 return "";
00298 }
00299
00310 public string GetValue (string ID, string Category, string Default)
00311 {
00312 int i = 0;
00313
00314 for (i = 0; i < this.m_oPreferencesEntries.Count; i++)
00315 {
00316 if ( (((CSharp_ClassLib.Classes.PreferencesEntry)m_oPreferencesEntries[i]).Category == Category) && (((CSharp_ClassLib.Classes.PreferencesEntry)m_oPreferencesEntries[i]).ID == ID) )
00317
00318 return ((CSharp_ClassLib.Classes.PreferencesEntry)m_oPreferencesEntries[i]).Value;
00319 }
00320
00321
00322 return Default;
00323 }
00324
00335 public string GetValue (string ID, string Category)
00336 {
00337 return this.GetValue(ID, Category, "");
00338 }
00339
00348 public string GetValue (string ID)
00349 {
00350 return this.GetValue(ID, "", "");
00351 }
00352
00363 public void UpdateEntry (string ID, string Category, string NewValue)
00364 {
00365 this.UpdateEntry (ID, Category, NewValue, false);
00366
00367 return;
00368 }
00369
00381 public void UpdateEntry (string ID, string Category, string NewValue, bool AutoAdd)
00382 {
00383 int i = 0;
00384
00385
00386 for (i = 0; i < m_oPreferencesEntries.Count; i++)
00387 {
00388 if ( (((PreferencesEntry)m_oPreferencesEntries[i]).Category == Category) && (((PreferencesEntry)m_oPreferencesEntries[i]).ID == ID) )
00389
00390 {
00391 ((PreferencesEntry)m_oPreferencesEntries[i]).Value = NewValue;
00392
00393 return;
00394 }
00395 }
00396
00397 if (!AutoAdd)
00398
00399 throw ( new System.ArgumentOutOfRangeException("ID; Category", "Entry was not found!") );
00400 else
00401
00402
00403 return;
00404 }
00405
00417 public void AddEntry (string ID, string Category, string NewValue)
00418 {
00419 return;
00420 }
00421 }
00422
00445 public class PreferencesEntry : System.Object
00446 {
00448 string m_strID = "";
00450 string m_strCategory = "";
00452 string m_strValue = "";
00453
00466 public PreferencesEntry()
00467 {
00468 Category = "";
00469 ID = "";
00470 Value = "";
00471
00472 return;
00473 }
00474
00489 public PreferencesEntry(string category)
00490 {
00491 Category = category;
00492 ID = "";
00493 Value = "";
00494
00495 return;
00496 }
00497
00513 public PreferencesEntry(string category, string id)
00514 {
00515 Category = category;
00516 ID = id;
00517 Value = "";
00518
00519 return;
00520 }
00521
00538 public PreferencesEntry(string category, string id, string value)
00539 {
00540 Category = category;
00541 ID = id;
00542 Value = value;
00543
00544 return;
00545 }
00546
00564 public string ID
00565 {
00566 get { return m_strID; }
00567 set { m_strID = value; return; }
00568 }
00569
00587 public string Category
00588 {
00589 get { return m_strCategory; }
00590 set { m_strCategory = value; return; }
00591 }
00592
00610 public string Value
00611 {
00612 get { return m_strValue; }
00613 set { m_strValue = value; return; }
00614 }
00615 }
00616 }