XMLHelper.java

Go to the documentation of this file.
00001 
00002 //
00003 // XML storage classes version 1.2
00004 //
00005 // XMLHelper.java
00006 //
00007 // Copyright (c) 2004, 2005, 2007 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 package de.mfuchs.xmlstorage;
00040 
00041 
00042 /**
00043  * XMLHelper converts XML string literals.
00044  */
00045 public final class XMLHelper
00046 {
00047     /**
00048      * private constructor to disable instantiation.
00049      */
00050     private XMLHelper()
00051     {
00052     }
00053 
00054     /**
00055      * Encode XML string literals.
00056      */
00057     public static String encodeXMLString(final String s)
00058     {
00059         return encodeXMLString(s, false);
00060     }
00061 
00062     /**
00063      * Encode XML string literals, optionally in CDATA sections.
00064      */
00065     public static String encodeXMLString(final String s, boolean cdata)
00066     {
00067         StringBuffer buffer = new StringBuffer();
00068 
00069         if (cdata) {
00070             buffer.append("<![CDATA[");
00071             buffer.append(s);
00072             buffer.append("]]>");
00073         } else {
00074             for(int i=0; i<s.length(); ++i) {
00075                 char c = s.charAt(i);
00076 
00077                 switch(c) {
00078                   case '<':
00079                     buffer.append("&lt;");
00080                     break;
00081 
00082                   case '>':
00083                     buffer.append("&gt;");
00084                     break;
00085 
00086                   case '&':
00087                     buffer.append("&amp;");
00088                     break;
00089 
00090                   case '"':
00091                     buffer.append("&quot;");
00092                     break;
00093 
00094                   case '\'':
00095                     buffer.append("&apos;");
00096                     break;
00097 
00098                   default:
00099                     buffer.append(c);
00100                 }
00101             }
00102         }
00103 
00104         return buffer.toString();
00105     }
00106 
00107     /**
00108      * Decode XML string literals.
00109      */
00110     public static String decodeXMLString(final String s)
00111     {
00112         StringBuffer buffer = new StringBuffer();
00113 
00114         for(int i=0; i<s.length(); ++i) {
00115             char c = s.charAt(i);
00116 
00117              // handle string literals
00118             if (c == '&') {
00119                 if (s.substring(i+1,i+4).equalsIgnoreCase("lt;")) {
00120                     buffer.append('<');
00121                     i += 3;
00122                 } else if (s.substring(i+1,i+4).equalsIgnoreCase("gt;")) {
00123                     buffer.append('>');
00124                     i += 3;
00125                 } else if (s.substring(i+1,i+5).equalsIgnoreCase("amp;")) {
00126                     buffer.append('&');
00127                     i += 4;
00128                 } else if (s.substring(i+1,i+6).equalsIgnoreCase("quot;")) {
00129                     buffer.append('"');
00130                     i += 5;
00131                 } else if (s.substring(i+1,i+6).equalsIgnoreCase("apos;")) {
00132                     buffer.append('\'');
00133                     i += 5;
00134                 } else
00135                     buffer.append(c);
00136             }
00137              // handle CDATA sections
00138             else if (c=='<' && s.regionMatches(i+1, "![CDATA[", 0, 8)) {
00139                 int e = s.indexOf("]]>", i+9);
00140                 if (e != -1) {
00141                     i += 9;
00142                     int l = e - i;
00143                     buffer.append(s.substring(i, i+l));
00144                     i += 3;
00145                 } else
00146                     buffer.append(c);
00147             } else
00148                 buffer.append(c);
00149         }
00150 
00151         return buffer.toString();
00152     }
00153 
00154     /// remove escape characters from the specified string
00155     public static String unescape(String s)
00156     {
00157         return XMLHelper.unescape(s, '"', '"');
00158     }
00159 
00160     /// remove escape characters from the specified string
00161     public static String unescape(String s, char b, char e)
00162     {
00163     //  if (s.charAt(0) == b)
00164     //      s = s.substring(1);
00165     //
00166     //  if (s.length()>0 && end[-1]==e)
00167     //      s = s.substring(0, s.length()-1);
00168     
00169         if (s.charAt(0) == b)
00170             if (s.length()>1 && s.charAt(s.length()-1)==e)
00171                 s = s.substring(1, s.length()-1);
00172     
00173         return s;
00174     }
00175 }