• Main Page
  • Namespaces
  • Classes
  • Files
  • Directories
  • File List

XMLHelper.cs

Go to the documentation of this file.
00001 
00002 //
00003 // XML storage classes version 1.5
00004 //
00005 // XMLHelper.cs
00006 //
00007 // Copyright (c) 2010, 2014 Martin Fuchs <martin-fuchs@gmx.net>
00008 //
00009 
00010 
00011 /*
00012 
00013  All rights reserved.
00014 
00015  Redistribution and use in source and binary forms, with or without
00016  modification, are permitted provided that the following conditions are met:
00017 
00018  * Redistributions of source code must retain the above copyright
00019     notice, this list of conditions and the following disclaimer.
00020  * Redistributions in binary form must reproduce the above copyright
00021     notice, this list of conditions and the following disclaimer in
00022     the documentation and/or other materials provided with the
00023     distribution.
00024 
00025  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00026  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00028  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00029  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00030  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00031  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00032  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00033  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00034  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00035  POSSIBILITY OF SUCH DAMAGE.
00036 
00037 */
00038 
00039 using System.Text;
00040 
00041 namespace XMLStorage
00042 {
00043 
00044     /**
00045      * XMLHelpder converts XML string literals.
00046      */
00047     public class XMLHelper
00048     {
00049         /**
00050          * private constructor to disable instantiation.
00051          */
00052         private XMLHelper()
00053         {
00054         }
00055 
00056         /**
00057          * Encode XML string literals.
00058          */
00059         public static string encodeXMLString(string s)
00060         {
00061             return encodeXMLString(s, false);
00062         }
00063 
00064         /**
00065          * Encode XML string literals, optionally in CDATA sections.
00066          */
00067         public static string encodeXMLString(string s, bool cdata)
00068         {
00069             StringBuilder buffer = new StringBuilder();
00070 
00071             if (cdata)
00072             {
00073                 buffer.Append("<![CDATA[");
00074                 buffer.Append(s);
00075                 buffer.Append("]]>");
00076             }
00077             else
00078             {
00079                 foreach(char c in s)
00080                 {
00081                     switch (c)
00082                     {
00083                         case '<':
00084                             buffer.Append("&lt;");
00085                             break;
00086 
00087                         case '>':
00088                             buffer.Append("&gt;");
00089                             break;
00090 
00091                         case '&':
00092                             buffer.Append("&amp;");
00093                             break;
00094 
00095                         case '"':
00096                             buffer.Append("&quot;");
00097                             break;
00098 
00099                         case '\'':
00100                             buffer.Append("&apos;");
00101                             break;
00102 
00103                         default:
00104                             buffer.Append(c);
00105                             break;
00106                     }
00107                 }
00108             }
00109 
00110             return buffer.ToString();
00111         }
00112 
00113         /**
00114          * Decode XML string literals.
00115          */
00116         public static string decodeXMLString(string s)
00117         {
00118             StringBuilder buffer = new StringBuilder();
00119 
00120             for(int i = 0; i < s.Length; ++i)
00121             {
00122                 char c = s[i];
00123 
00124                 // handle string literals
00125                 if (c == '&')
00126                 {
00127                     if (string.Compare(s, i+1, "lt;", 0, 3, true) == 0)
00128                     {
00129                         buffer.Append('<');
00130                         i += 3;
00131                     }
00132                     if (string.Compare(s, i+1, "gt;", 0, 3, true) == 0)
00133                     {
00134                         buffer.Append('>');
00135                         i += 3;
00136                     }
00137                     if (string.Compare(s, i+1, "amp;", 0, 4, true) == 0)
00138                     {
00139                         buffer.Append('&');
00140                         i += 4;
00141                     }
00142                     if (string.Compare(s, i+1, "quot;", 0, 5, true) == 0)
00143                     {
00144                         buffer.Append('"');
00145                         i += 5;
00146                     }
00147                     if (string.Compare(s, i+1, "apos;", 0, 5, true) == 0)
00148                     {
00149                         buffer.Append('\'');
00150                         i += 5;
00151                     }
00152                     else
00153                         buffer.Append(c);
00154                 }
00155                 // handle CDATA sections
00156                 else if (c=='<' && string.Compare(s, i+1, "![CDATA[", 0, 8)==0)
00157                 {
00158                     int e = s.IndexOf("]]>", i+9);
00159                     if (e != -1)
00160                     {
00161                         i += 9;
00162                         int l = e - i;
00163                         buffer.Append(s.Substring(i, l));
00164                         i += 3;
00165                     }
00166                     else
00167                         buffer.Append(c);
00168                 }
00169                 else
00170                     buffer.Append(c);
00171             }
00172 
00173             return buffer.ToString();
00174         }
00175 
00176         /// remove escape characters from the specified string
00177         public static string unescape(string s)
00178         {
00179             return XMLHelper.unescape(s, '"', '"');
00180         }
00181 
00182         /// remove escape characters from the specified string
00183         public static string unescape(string s, char b, char e)
00184         {
00185             //  if (s.charAt(0) == b)
00186             //      s = s.substring(1);
00187             //
00188             //  if (s.Length>0 && end[-1]==e)
00189             //      s = s.substring(0, s.Length-1);
00190 
00191             if (s[0] == b)
00192                 if (s.Length>1 && s[s.Length-1]==e)
00193                     s = s.Substring(1, s.Length-2);
00194 
00195             return s;
00196         }
00197     }
00198 
00199 } // namespace XMLStorage

Generated on Tue Oct 28 2014 00:05:07 for XMLStorage/C# by  doxygen 1.7.2