clsLanguageFile.cs

00001 // C# ClassLib
00002 // http://csharp-classlib.sourceforge.net
00003 //
00004 //
00005 // Copyright (C) 2005 Marcel Joachim Kloubert
00006 // 
00007 // This program is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2
00010 // of the License, or (at your option) any later version.
00011 // 
00012 // This program is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 // 
00017 // You should have received a copy of the GNU General Public License
00018 // along with this program; if not, write to the Free Software
00019 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
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 LanguageFile : System.Object
00052         {
00054         private System.Xml.XmlDocument m_oXMLDocument;
00055                 
00057                 private System.Collections.ArrayList m_oLanguageEntries;
00058                 
00060                 private string m_strAuthor;
00062                 private string m_strEmail;
00064                 private string m_strHomepage;
00066                 private string m_strCaptionOriginal;
00068                 private string m_strCaptionEnglish;
00069                 
00071             private const string XML_TAG_ROOT =  "language_file";
00073                 private const string XML_TAG_INFORMATION =  "information";
00075                 private const string XML_TAG_ENTRIES =  "entries";      
00076                 
00078                 private const string XML_TAG_AUTHOR =  "author";
00080                 private const string XML_TAG_EMAIL =  "email";
00082                 private const string XML_TAG_HOMEPAGE =  "homepage";
00084                 private const string XML_TAG_LANGUAGE_CAPTION_ORIGINAL =  "caption_org";
00086                 private const string XML_TAG_LANGUAGE_CAPTION_ENGLISH =  "caption_eng";
00087         
00089                 private const string XML_ATTRIBUTE_ID =  "id";
00091                 private const string XML_ATTRIBUTE_CATEGORY =  "category";
00092                 
00094                 private const string XPATH_ALL_INFORMATION = "/" + XML_TAG_ROOT + "//" + XML_TAG_INFORMATION + "/child::*";
00096                 private const string XPATH_ALL_ENTRIES = "/" + XML_TAG_ROOT + "//" + XML_TAG_ENTRIES + "/child::*";
00097                 
00110                 public LanguageFile ()
00111                 {
00112                         this.InitRest();
00113                         
00114                         return;
00115                 }
00116         
00132                 public LanguageFile (string Language_File)
00133                 {
00134                         this.InitRest();
00135                         
00136                         this.LoadFromFile(Language_File);
00137                         
00138                         return;
00139                 }
00140                 
00155                 public string Author { get { return m_strAuthor; } }
00156                 
00171                 public string Email { get { return m_strEmail; } }
00172                 
00187                 public string Homepage { get { return m_strHomepage; } }
00188                 
00203                 public string OriginalCaption { get { return m_strCaptionOriginal; } }
00204                 
00219                 public string EnglishCaption { get { return m_strCaptionEnglish; } }
00220                 
00240                 public CSharp_ClassLib.Classes.LanguageEntry[] Entries
00241                 {
00242                         get
00243                         {
00244                                 CSharp_ClassLib.Classes.LanguageEntry[] oBuffer = new CSharp_ClassLib.Classes.LanguageEntry[m_oLanguageEntries.Count];
00245                                 m_oLanguageEntries.CopyTo(oBuffer);
00246                                 
00247                                 return oBuffer;
00248                         }
00249                 }
00250                 
00255                 private void InitRest ()
00256                 {
00257                         m_oXMLDocument = null;
00258                         
00259                         m_strAuthor = "";
00260                         m_strEmail = "";
00261                         m_strHomepage = "";
00262                         m_strCaptionOriginal = "";
00263                         m_strCaptionEnglish = "";
00264                         
00265                         m_oLanguageEntries = new System.Collections.ArrayList();
00266                         
00267                         return;
00268                 }
00269                 
00285                 public void LoadFromFile (string Language_File)
00286                 {
00287                         if (!System.IO.File.Exists(Language_File))
00288                                 // file not found!
00289                                 throw ( new System.IO.FileNotFoundException("Language file not found!", Language_File) );
00290                         
00291                 // first create a temp document
00292                         System.Xml.XmlDocument oTEMPXMLDocument = new System.Xml.XmlDocument();                 
00293                         // init a new XPath document
00294                 System.Xml.XPath.XPathDocument oXPathDocument = new System.Xml.XPath.XPathDocument(Language_File);
00295         
00296                 try
00297                 {
00298                         oTEMPXMLDocument.Load(Language_File);
00299         
00300                         // first read infos
00301                         this.ReadLanguageFileInformation(oXPathDocument);
00302                         // then read data
00303                         this.ReadLanguageFileEntries(oXPathDocument);   
00304                         
00305                         m_oXMLDocument = oTEMPXMLDocument;
00306                 }
00307                 catch (System.Exception e)
00308                 {
00309                         throw (e);
00310                 }
00311                 
00312                 return;
00313                 }
00314                 
00323                 private void ReadLanguageFileInformation (System.Xml.XPath.XPathDocument XPath_Document)
00324                 {
00325                         if (null == XPath_Document)
00326                                 // argument is null!
00327                                 throw ( new System.ArgumentNullException("XPath_Document", "Argument must not be null!") );
00328                         
00329                 System.Xml.XPath.XPathNavigator oXMLPathNav = XPath_Document.CreateNavigator();
00330                 System.Xml.XPath.XPathNodeIterator oXMLNodeIterator = oXMLPathNav.Select(XPATH_ALL_INFORMATION);
00331         
00332                 while ( oXMLNodeIterator.MoveNext() )
00333                 {
00334                 switch (oXMLNodeIterator.Current.Name.ToLower().Trim())
00335                 {
00336                         // author name
00337                         case XML_TAG_AUTHOR:
00338                                 m_strAuthor = oXMLNodeIterator.Current.Value.Trim();
00339                                 break;
00340                         
00341                         // author email
00342                         case XML_TAG_EMAIL:
00343                                 m_strEmail = oXMLNodeIterator.Current.Value.Trim();
00344                                 break;
00345                         
00346                         // author homepage
00347                         case XML_TAG_HOMEPAGE:
00348                                 m_strHomepage = oXMLNodeIterator.Current.Value.Trim();
00349                                 break;
00350                         
00351                         // original caption of the language
00352                         case XML_TAG_LANGUAGE_CAPTION_ORIGINAL:
00353                                 m_strCaptionOriginal = oXMLNodeIterator.Current.Value.Trim();
00354                                 break;
00355                                 
00356                         // english caption of the language
00357                         case XML_TAG_LANGUAGE_CAPTION_ENGLISH:
00358                                 m_strCaptionEnglish = oXMLNodeIterator.Current.Value.Trim();
00359                                 break;
00360                 }            
00361                 }
00362         
00363                 return;                 
00364                 }
00365         
00374                 private void ReadLanguageFileEntries (System.Xml.XPath.XPathDocument XPath_Document)
00375                 {
00376                         if (null == XPath_Document)
00377                                 // argument is null!
00378                                 throw ( new System.ArgumentNullException("XPath_Document", "Argument must not be null!") );
00379                         
00380                 try
00381                 {
00382                                 // Create temporary language entry list
00383                         System.Collections.ArrayList m_oTempLanguageEntries = new System.Collections.ArrayList();
00384         
00385                         System.Xml.XPath.XPathNavigator oXMLPathNav = XPath_Document.CreateNavigator();
00386                         System.Xml.XPath.XPathNodeIterator oXMLNodeIterator = oXMLPathNav.Select(XPATH_ALL_ENTRIES);
00387                 
00388                         // Temporary language entry
00389                         CSharp_ClassLib.Classes.LanguageEntry oTMPEntry;
00390         
00391                         while ( oXMLNodeIterator.MoveNext() )
00392                         {
00393                         switch ( oXMLNodeIterator.Current.Name.ToLower().Trim() )
00394                         {
00395                                 case "entry":
00396                                 // first store data in a temporary LanguageEntry instance
00397                                 oTMPEntry = new CSharp_ClassLib.Classes.LanguageEntry();
00398                                 oTMPEntry.ID = oXMLNodeIterator.Current.GetAttribute(XML_ATTRIBUTE_ID, "");
00399                                 oTMPEntry.Category = oXMLNodeIterator.Current.GetAttribute(XML_ATTRIBUTE_CATEGORY, "");
00400                                 oTMPEntry.Value = oXMLNodeIterator.Current.Value;
00401                     
00402                                 // add temp instance to list
00403                                 m_oTempLanguageEntries.Add(oTMPEntry);
00404                                 break;
00405                         }            
00406                         }
00407                         
00408                         m_oLanguageEntries = m_oTempLanguageEntries;
00409                 }
00410                 catch (System.Exception e)
00411                 {
00412                         throw(e);
00413                 }
00414                 
00415                 return;                 
00416                 }
00417         
00430         public string GetValue (string ID, bool ThrowException)
00431         {       
00432                 return this.GetValue(ID, "", ThrowException);
00433         }
00434                 
00447         public string GetValue (string ID, string Category, bool ThrowException)
00448         {       
00449                 int i = 0;
00450                 
00451                 for (i = 0; i < this.m_oLanguageEntries.Count; i++)
00452                 {
00453                 if ( (((CSharp_ClassLib.Classes.LanguageEntry)m_oLanguageEntries[i]).Category == Category) && (((CSharp_ClassLib.Classes.LanguageEntry)m_oLanguageEntries[i]).ID == ID) )
00454                         // entry found
00455                         return ((CSharp_ClassLib.Classes.LanguageEntry)m_oLanguageEntries[i]).Value;
00456                 }
00457         
00458                 // entry not found => use Default value or throw exception
00459                 if (ThrowException)
00460                         throw ( new System.ArgumentOutOfRangeException("ID; Category", "Entry was not found!") );
00461                 
00462                 return "";
00463         }
00464                 
00475         public string GetValue (string ID, string Category, string Default)
00476         {       
00477                 int i = 0;
00478                 
00479                 for (i = 0; i < this.m_oLanguageEntries.Count; i++)
00480                 {
00481                 if ( (((CSharp_ClassLib.Classes.LanguageEntry)m_oLanguageEntries[i]).Category == Category) && (((CSharp_ClassLib.Classes.LanguageEntry)m_oLanguageEntries[i]).ID == ID) )
00482                         // entry found
00483                         return ((CSharp_ClassLib.Classes.LanguageEntry)m_oLanguageEntries[i]).Value;
00484                 }
00485         
00486                 // entry not found => use Default value
00487                 return Default;
00488         }
00489                 
00500         public string GetValue (string ID, string Category)
00501         {       
00502                 return this.GetValue(ID, Category, "");
00503         }
00504                 
00513         public string GetValue (string ID)
00514         {       
00515                 return this.GetValue(ID, "", "");
00516         }
00517         }
00518 
00541         public class LanguageEntry : System.Object
00542         {
00544         string m_strID = "";
00546         string m_strCategory = "";
00548         string m_strValue = "";
00549                 
00562         public LanguageEntry()
00563         {
00564                 Category = "";
00565                 ID = "";
00566                 Value = "";
00567             
00568                 return;
00569         }
00570         
00585         public LanguageEntry(string category)
00586         {
00587                 Category = category;
00588                 ID = "";
00589                 Value = "";
00590             
00591                 return;
00592         }
00593         
00609         public LanguageEntry(string category, string id)
00610         {
00611                 Category = category;
00612                 ID = id;
00613                 Value = "";
00614             
00615                 return;
00616         }
00617         
00634         public LanguageEntry(string category, string id, string value)
00635         {
00636                 Category = category;
00637                 ID = id;
00638                 Value = value;
00639             
00640                 return;
00641         }
00642         
00660         public string ID
00661         {
00662                 get { return m_strID; }
00663                 set { m_strID = value; return; }
00664         }
00665         
00683         public string Category
00684         {
00685                 get { return m_strCategory; }
00686                 set { m_strCategory = value; return; }
00687         }
00688         
00706         public string Value
00707         {
00708                 get { return m_strValue; }
00709                 set { m_strValue = value; return; }
00710         }
00711         }       
00712 }

Generated on Thu Jun 2 08:41:49 2005 for C# ClassLib by  doxygen 1.4.1