1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
package org.apache.commons.xmlio.out; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | 0 | public final class XMLEncode { |
33 | |
|
34 | |
private final static int CDATA_BLOCK_THRESHOLD_LENGTH = 12; |
35 | |
private final static char DEFAULT_QUOTE_CHAR = '"'; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
public final static boolean isWhiteSpace(String text) { |
41 | 0 | for (int i = 0; i < text.length(); i++) { |
42 | 0 | char c = text.charAt(i); |
43 | 0 | if (Character.isWhitespace(c)) { |
44 | 0 | continue; |
45 | |
} else { |
46 | 0 | return false; |
47 | |
} |
48 | |
} |
49 | 0 | return true; |
50 | |
} |
51 | |
|
52 | |
|
53 | |
public final static String xmlEncodeTextForAttribute(String text, char quoteChar) { |
54 | 0 | if (text == null) |
55 | 0 | return null; |
56 | 0 | return xmlEncodeTextAsPCDATA(text, true, quoteChar); |
57 | |
} |
58 | |
|
59 | |
|
60 | |
public final static String xmlEncodeText(String text) { |
61 | 0 | if (text == null) |
62 | 0 | return null; |
63 | 0 | if (!needsEncoding(text)) { |
64 | 0 | return text; |
65 | |
} else { |
66 | |
|
67 | 0 | if (text.length() > CDATA_BLOCK_THRESHOLD_LENGTH) { |
68 | 0 | String cdata = xmlEncodeTextAsCDATABlock(text); |
69 | 0 | if (cdata != null) { |
70 | 0 | return cdata; |
71 | |
} |
72 | |
} |
73 | |
} |
74 | |
|
75 | 0 | return xmlEncodeTextAsPCDATA(text); |
76 | |
} |
77 | |
|
78 | |
|
79 | |
public final static String xmlEncodeTextAsPCDATA(String text) { |
80 | 0 | if (text == null) |
81 | 0 | return null; |
82 | 0 | return xmlEncodeTextAsPCDATA(text, false); |
83 | |
} |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
public final static String xmlEncodeTextAsPCDATA(String text, boolean forAttribute) { |
90 | 0 | return xmlEncodeTextAsPCDATA(text, forAttribute, DEFAULT_QUOTE_CHAR); |
91 | |
} |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
public final static String xmlEncodeTextAsPCDATA(String text, boolean forAttribute, char quoteChar) { |
99 | 0 | if (text == null) |
100 | 0 | return null; |
101 | |
char c; |
102 | 0 | StringBuffer n = new StringBuffer(text.length() * 2); |
103 | 0 | for (int i = 0; i < text.length(); i++) { |
104 | 0 | c = text.charAt(i); |
105 | 0 | switch (c) { |
106 | |
case '&' : |
107 | 0 | n.append("&"); |
108 | 0 | break; |
109 | |
case '<' : |
110 | 0 | n.append("<"); |
111 | 0 | break; |
112 | |
case '>' : |
113 | 0 | n.append(">"); |
114 | 0 | break; |
115 | |
case '"' : |
116 | 0 | if (forAttribute) |
117 | 0 | n.append("""); |
118 | |
else |
119 | 0 | n.append(c); |
120 | 0 | break; |
121 | |
case '\'' : |
122 | 0 | if (forAttribute) |
123 | 0 | n.append("'"); |
124 | |
else |
125 | 0 | n.append(c); |
126 | 0 | break; |
127 | |
default : |
128 | |
{ |
129 | 0 | n.append(c); |
130 | |
break; |
131 | |
} |
132 | |
} |
133 | |
} |
134 | |
|
135 | 0 | if (forAttribute) { |
136 | 0 | n.append(quoteChar); |
137 | 0 | n.insert(0, quoteChar); |
138 | |
} |
139 | |
|
140 | 0 | return n.toString(); |
141 | |
} |
142 | |
|
143 | |
|
144 | |
public final static String xmlEncodeTextAsCDATABlock(String text) { |
145 | 0 | if (text == null) |
146 | 0 | return null; |
147 | 0 | if (isCompatibleWithCDATABlock(text)) { |
148 | 0 | return "<![CDATA[" + text + "]]>"; |
149 | |
} else { |
150 | 0 | return null; |
151 | |
} |
152 | |
} |
153 | |
|
154 | |
|
155 | |
public final static boolean needsEncoding(String text) { |
156 | 0 | return needsEncoding(text, false); |
157 | |
} |
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
public final static boolean needsEncoding(String data, boolean checkForAttr) { |
165 | 0 | if (data == null) |
166 | 0 | return false; |
167 | |
char c; |
168 | 0 | for (int i = 0; i < data.length(); i++) { |
169 | 0 | c = data.charAt(i); |
170 | 0 | if (c == '&' || c == '<' || (checkForAttr && (c == '"' || c == '\''))) |
171 | 0 | return true; |
172 | |
} |
173 | 0 | return false; |
174 | |
} |
175 | |
|
176 | |
|
177 | |
public final static boolean isCompatibleWithCDATABlock(String text) { |
178 | 0 | if (text == null) |
179 | 0 | return false; |
180 | 0 | return (text.indexOf("]]>") == -1); |
181 | |
} |
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
public final static String xmlDecodeTextToCDATA(String pcdata) { |
187 | 0 | if (pcdata == null) |
188 | 0 | return null; |
189 | |
char c, c1, c2, c3, c4, c5; |
190 | 0 | StringBuffer n = new StringBuffer(pcdata.length()); |
191 | 0 | for (int i = 0; i < pcdata.length(); i++) { |
192 | 0 | c = pcdata.charAt(i); |
193 | 0 | if (c == '&') { |
194 | 0 | c1 = lookAhead(1, i, pcdata); |
195 | 0 | c2 = lookAhead(2, i, pcdata); |
196 | 0 | c3 = lookAhead(3, i, pcdata); |
197 | 0 | c4 = lookAhead(4, i, pcdata); |
198 | 0 | c5 = lookAhead(5, i, pcdata); |
199 | |
|
200 | 0 | if (c1 == 'a' && c2 == 'm' && c3 == 'p' && c4 == ';') { |
201 | 0 | n.append("&"); |
202 | 0 | i += 4; |
203 | 0 | } else if (c1 == 'l' && c2 == 't' && c3 == ';') { |
204 | 0 | n.append("<"); |
205 | 0 | i += 3; |
206 | 0 | } else if (c1 == 'g' && c2 == 't' && c3 == ';') { |
207 | 0 | n.append(">"); |
208 | 0 | i += 3; |
209 | 0 | } else if (c1 == 'q' && c2 == 'u' && c3 == 'o' && c4 == 't' && c5 == ';') { |
210 | 0 | n.append("\""); |
211 | 0 | i += 5; |
212 | 0 | } else if (c1 == 'a' && c2 == 'p' && c3 == 'o' && c4 == 's' && c5 == ';') { |
213 | 0 | n.append("'"); |
214 | 0 | i += 5; |
215 | |
} else |
216 | 0 | n.append("&"); |
217 | |
} else |
218 | 0 | n.append(c); |
219 | |
} |
220 | 0 | return n.toString(); |
221 | |
} |
222 | |
|
223 | |
private final static char lookAhead(int la, int offset, String data) { |
224 | |
try { |
225 | 0 | return data.charAt(offset + la); |
226 | 0 | } catch (StringIndexOutOfBoundsException e) { |
227 | 0 | return 0x0; |
228 | |
} |
229 | |
} |
230 | |
|
231 | |
|
232 | |
private final static boolean contains(String text, char[] chars) { |
233 | 0 | if (text == null || chars == null || chars.length == 0) { |
234 | 0 | return false; |
235 | |
} |
236 | 0 | for (int i = 0; i < text.length(); i++) { |
237 | 0 | char c = text.charAt(i); |
238 | 0 | for (int j = 0; j < chars.length; j++) { |
239 | 0 | if (chars[j] == c) { |
240 | 0 | return true; |
241 | |
} |
242 | |
} |
243 | |
} |
244 | 0 | return false; |
245 | |
} |
246 | |
|
247 | |
} |