Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 package de.mfuchs.xmlstorage;
00040
00041
00042
00043
00044
00045 public final class XMLHelper
00046 {
00047
00048
00049
00050 private XMLHelper()
00051 {
00052 }
00053
00054
00055
00056
00057 public static String encodeXMLString(final String s)
00058 {
00059 return encodeXMLString(s, false);
00060 }
00061
00062
00063
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("<");
00080 break;
00081
00082 case '>':
00083 buffer.append(">");
00084 break;
00085
00086 case '&':
00087 buffer.append("&");
00088 break;
00089
00090 case '"':
00091 buffer.append(""");
00092 break;
00093
00094 case '\'':
00095 buffer.append("'");
00096 break;
00097
00098 default:
00099 buffer.append(c);
00100 }
00101 }
00102 }
00103
00104 return buffer.toString();
00105 }
00106
00107
00108
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
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
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
00155 public static String unescape(String s)
00156 {
00157 return XMLHelper.unescape(s, '"', '"');
00158 }
00159
00160
00161 public static String unescape(String s, char b, char e)
00162 {
00163
00164
00165
00166
00167
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 }