|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| package nu.xom.tests; |
|
23 |
| |
|
24 |
| import nu.xom.Elements; |
|
25 |
| import nu.xom.Namespace; |
|
26 |
| import nu.xom.Node; |
|
27 |
| import nu.xom.Serializer; |
|
28 |
| import nu.xom.Element; |
|
29 |
| import nu.xom.DocType; |
|
30 |
| import nu.xom.Document; |
|
31 |
| import nu.xom.Builder; |
|
32 |
| import nu.xom.Comment; |
|
33 |
| import nu.xom.ParsingException; |
|
34 |
| import nu.xom.ProcessingInstruction; |
|
35 |
| import nu.xom.Attribute; |
|
36 |
| import nu.xom.Text; |
|
37 |
| import nu.xom.UnavailableCharacterException; |
|
38 |
| import nu.xom.ValidityException; |
|
39 |
| import nu.xom.XMLException; |
|
40 |
| |
|
41 |
| import java.io.ByteArrayInputStream; |
|
42 |
| import java.io.ByteArrayOutputStream; |
|
43 |
| import java.io.File; |
|
44 |
| import java.io.IOException; |
|
45 |
| import java.io.BufferedReader; |
|
46 |
| import java.io.InputStream; |
|
47 |
| import java.io.OutputStream; |
|
48 |
| import java.io.StringReader; |
|
49 |
| import java.io.UnsupportedEncodingException; |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| public class SerializerTest extends XOMTestCase { |
|
61 |
| |
|
62 |
| |
|
63 |
| private Builder parser; |
|
64 |
| private final static double version = Double.parseDouble( |
|
65 |
| System.getProperty("java.version").substring(0,3) |
|
66 |
| ); |
|
67 |
| Element root = new Element("root"); |
|
68 |
| Document doc = new Document(root); |
|
69 |
| ByteArrayOutputStream out = new ByteArrayOutputStream(); |
|
70 |
| |
|
71 |
| |
|
72 |
131
| public SerializerTest(String name) {
|
|
73 |
131
| super(name);
|
|
74 |
| } |
|
75 |
| |
|
76 |
| |
|
77 |
131
| protected void setUp() {
|
|
78 |
131
| parser = new Builder();
|
|
79 |
| } |
|
80 |
| |
|
81 |
| |
|
82 |
1
| public void testNFC88592() throws IOException, ParsingException {
|
|
83 |
| |
|
84 |
1
| String data = " Ą˘Ł¤ĽŚ§¨ŠŞŤŹŽŻ°ą˛ł´ľśˇ¸šşťź˝žżŔÁÂĂÄĹĆÇČÉĘËĚÍÎĎĐŃŇÓÔŐÖ×ŘŮÚŰÜÝŢßŕáâăäĺćçčéęëěíîďđńňóôőö÷řůúűüýţ˙";
|
|
85 |
1
| Element root = new Element("a");
|
|
86 |
1
| root.appendChild(data);
|
|
87 |
1
| Document doc = new Document(root);
|
|
88 |
| |
|
89 |
| |
|
90 |
1
| Serializer serializer = new Serializer(out);
|
|
91 |
1
| serializer.setUnicodeNormalizationFormC(true);
|
|
92 |
1
| serializer.write(doc);
|
|
93 |
1
| serializer.flush();
|
|
94 |
1
| out.close();
|
|
95 |
1
| byte[] temp = out.toByteArray();
|
|
96 |
1
| Document roundTrip = parser.build(new ByteArrayInputStream(temp));
|
|
97 |
| |
|
98 |
1
| assertEquals(data, roundTrip.getValue());
|
|
99 |
| |
|
100 |
| } |
|
101 |
| |
|
102 |
| |
|
103 |
1
| public void testMultipleCombiningCharactersWithNFC()
|
|
104 |
| throws ParsingException, IOException { |
|
105 |
| |
|
106 |
| |
|
107 |
1
| String input = "<a>Ḍ̇</a>";
|
|
108 |
| |
|
109 |
1
| String output = "<?xml version=\"1.0\" encoding=\"US-ASCII\"?>\r\n"
|
|
110 |
| + "<a>Ḍ̇</a>\r\n"; |
|
111 |
| |
|
112 |
1
| Document doc = parser.build(input, null);
|
|
113 |
1
| Serializer serializer = new Serializer(out, "US-ASCII");
|
|
114 |
1
| serializer.setUnicodeNormalizationFormC(true);
|
|
115 |
1
| serializer.write(doc);
|
|
116 |
1
| serializer.flush();
|
|
117 |
1
| String result = out.toString("US-ASCII");
|
|
118 |
1
| assertEquals(output, result);
|
|
119 |
| |
|
120 |
| } |
|
121 |
| |
|
122 |
| |
|
123 |
1
| public void testMultipleCombiningCharactersWithDifferentCombiningClassesNFC()
|
|
124 |
| throws ParsingException, IOException { |
|
125 |
| |
|
126 |
| |
|
127 |
| |
|
128 |
| |
|
129 |
1
| String input = "<a>à𝅭֮֮b</a>";
|
|
130 |
| |
|
131 |
1
| String output = "<?xml version=\"1.0\" encoding=\"US-ASCII\"?>\r\n"
|
|
132 |
| + "<a>à𝅭֮֮b</a>\r\n"; |
|
133 |
| |
|
134 |
1
| Document doc = parser.build(input, null);
|
|
135 |
1
| Serializer serializer = new Serializer(out, "US-ASCII");
|
|
136 |
1
| serializer.setUnicodeNormalizationFormC(true);
|
|
137 |
1
| serializer.write(doc);
|
|
138 |
1
| serializer.flush();
|
|
139 |
1
| String result = out.toString("US-ASCII");
|
|
140 |
1
| assertEquals(output, result);
|
|
141 |
| |
|
142 |
| } |
|
143 |
| |
|
144 |
| |
|
145 |
1
| public void testEWithCombiningMacron()
|
|
146 |
| throws ParsingException, IOException { |
|
147 |
| |
|
148 |
| |
|
149 |
1
| String input = "<a>Ē</a>";
|
|
150 |
1
| String output = "<?xml version=\"1.0\" encoding=\"US-ASCII\"?>\r\n<a>Ē</a>\r\n";
|
|
151 |
| |
|
152 |
1
| Document doc = parser.build(input, null);
|
|
153 |
1
| Serializer serializer = new Serializer(out, "US-ASCII");
|
|
154 |
1
| serializer.setUnicodeNormalizationFormC(true);
|
|
155 |
1
| serializer.write(doc);
|
|
156 |
1
| serializer.flush();
|
|
157 |
1
| String result = out.toString("US-ASCII");
|
|
158 |
1
| assertEquals(output, result);
|
|
159 |
| |
|
160 |
| } |
|
161 |
| |
|
162 |
| |
|
163 |
1
| public void testEWithCombiningMacronAndGrave()
|
|
164 |
| throws ParsingException, IOException { |
|
165 |
| |
|
166 |
| |
|
167 |
1
| String input = "<a>Ḕ</a>";
|
|
168 |
1
| String output = "<?xml version=\"1.0\" encoding=\"US-ASCII\"?>\r\n<a>Ḕ</a>\r\n";
|
|
169 |
| |
|
170 |
1
| Document doc = parser.build(input, null);
|
|
171 |
1
| Serializer serializer = new Serializer(out, "US-ASCII");
|
|
172 |
1
| serializer.setUnicodeNormalizationFormC(true);
|
|
173 |
1
| serializer.write(doc);
|
|
174 |
1
| serializer.flush();
|
|
175 |
1
| String result = out.toString("US-ASCII");
|
|
176 |
1
| assertEquals(output, result);
|
|
177 |
| |
|
178 |
| } |
|
179 |
| |
|
180 |
| |
|
181 |
1
| public void testParenthesizedOjeon()
|
|
182 |
| throws ParsingException, IOException { |
|
183 |
| |
|
184 |
| |
|
185 |
1
| String input = "<a>(오전)</a>";
|
|
186 |
1
| String output = "<?xml version=\"1.0\" encoding=\"US-ASCII\"?>\r\n<a>(오전)</a>\r\n";
|
|
187 |
| |
|
188 |
1
| Document doc = parser.build(input, null);
|
|
189 |
1
| Serializer serializer = new Serializer(out, "US-ASCII");
|
|
190 |
1
| serializer.setUnicodeNormalizationFormC(true);
|
|
191 |
1
| serializer.write(doc);
|
|
192 |
1
| serializer.flush();
|
|
193 |
1
| String result = out.toString("US-ASCII");
|
|
194 |
1
| assertEquals(output, result);
|
|
195 |
| |
|
196 |
| } |
|
197 |
| |
|
198 |
| |
|
199 |
1
| public void testNonParenthesizedOjeon()
|
|
200 |
| throws ParsingException, IOException { |
|
201 |
| |
|
202 |
| |
|
203 |
1
| String input = "<a>오전</a>";
|
|
204 |
1
| String output = "<?xml version=\"1.0\" encoding=\"US-ASCII\"?>\r\n<a>오전</a>\r\n";
|
|
205 |
| |
|
206 |
1
| Document doc = parser.build(input, null);
|
|
207 |
1
| Serializer serializer = new Serializer(out, "US-ASCII");
|
|
208 |
1
| serializer.setUnicodeNormalizationFormC(true);
|
|
209 |
1
| serializer.write(doc);
|
|
210 |
1
| serializer.flush();
|
|
211 |
1
| String result = out.toString("US-ASCII");
|
|
212 |
1
| assertEquals(output, result);
|
|
213 |
| |
|
214 |
| } |
|
215 |
| |
|
216 |
| |
|
217 |
1
| public void testOjeon()
|
|
218 |
| throws ParsingException, IOException { |
|
219 |
| |
|
220 |
| |
|
221 |
1
| String input = "<a>전</a>";
|
|
222 |
1
| String output = "<?xml version=\"1.0\" encoding=\"US-ASCII\"?>\r\n<a>전</a>\r\n";
|
|
223 |
| |
|
224 |
1
| Document doc = parser.build(input, null);
|
|
225 |
1
| Serializer serializer = new Serializer(out, "US-ASCII");
|
|
226 |
1
| serializer.setUnicodeNormalizationFormC(true);
|
|
227 |
1
| serializer.write(doc);
|
|
228 |
1
| serializer.flush();
|
|
229 |
1
| String result = out.toString("US-ASCII");
|
|
230 |
1
| assertEquals(output, result);
|
|
231 |
| |
|
232 |
| } |
|
233 |
| |
|
234 |
| |
|
235 |
1
| public void testKannadaVowelSignOO()
|
|
236 |
| throws ParsingException, IOException { |
|
237 |
| |
|
238 |
1
| String input = "<a>ೋ</a>";
|
|
239 |
1
| String output = "<?xml version=\"1.0\" encoding=\"US-ASCII\"?>\r\n<a>ೋ</a>\r\n";
|
|
240 |
| |
|
241 |
1
| Document doc = parser.build(input, null);
|
|
242 |
1
| Serializer serializer = new Serializer(out, "US-ASCII");
|
|
243 |
1
| serializer.setUnicodeNormalizationFormC(true);
|
|
244 |
1
| serializer.write(doc);
|
|
245 |
1
| serializer.flush();
|
|
246 |
1
| String result = out.toString("US-ASCII");
|
|
247 |
1
| assertEquals(output, result);
|
|
248 |
| |
|
249 |
| } |
|
250 |
| |
|
251 |
| |
|
252 |
1
| public void testCDATASectionEndDelimiter() throws IOException {
|
|
253 |
| |
|
254 |
1
| root.appendChild("]]>");
|
|
255 |
1
| Serializer serializer = new Serializer(out, "UTF-8");
|
|
256 |
1
| serializer.setMaxLength(20);
|
|
257 |
1
| serializer.write(doc);
|
|
258 |
1
| String result = out.toString("UTF-8");
|
|
259 |
1
| assertTrue(result.indexOf("]]>") > 0);
|
|
260 |
| |
|
261 |
| } |
|
262 |
| |
|
263 |
| |
|
264 |
1
| public void testXMLSpacePreserve() throws IOException {
|
|
265 |
| |
|
266 |
1
| root.addAttribute(
|
|
267 |
| new Attribute( |
|
268 |
| "xml:space", |
|
269 |
| "http://www.w3.org/XML/1998/namespace", |
|
270 |
| "preserve")); |
|
271 |
1
| String value =
|
|
272 |
| "This is a long sentence with plenty of opportunities for " + |
|
273 |
| "breaking from beginning to end."; |
|
274 |
1
| root.appendChild(value);
|
|
275 |
1
| Serializer serializer = new Serializer(out, "UTF-8");
|
|
276 |
1
| serializer.setMaxLength(20);
|
|
277 |
1
| serializer.write(doc);
|
|
278 |
1
| String result = out.toString("UTF-8");
|
|
279 |
1
| assertTrue(result.indexOf(value) > 0);
|
|
280 |
| |
|
281 |
| } |
|
282 |
| |
|
283 |
| |
|
284 |
| |
|
285 |
| |
|
286 |
| |
|
287 |
| |
|
288 |
| |
|
289 |
| |
|
290 |
| |
|
291 |
1
| public void testUTF16LEBOM() throws IOException {
|
|
292 |
| |
|
293 |
1
| if (version >= 1.3) {
|
|
294 |
| |
|
295 |
1
| Serializer serializer = new Serializer(out, "UTF-16LE");
|
|
296 |
1
| serializer.write(doc);
|
|
297 |
1
| serializer.flush();
|
|
298 |
1
| out.flush();
|
|
299 |
1
| out.close();
|
|
300 |
1
| byte[] data = out.toByteArray();
|
|
301 |
1
| assertEquals('<', (char) data[0]);
|
|
302 |
1
| assertEquals((byte) 0, data[1]);
|
|
303 |
| } |
|
304 |
| |
|
305 |
| } |
|
306 |
| |
|
307 |
| |
|
308 |
| |
|
309 |
| |
|
310 |
| |
|
311 |
| |
|
312 |
| |
|
313 |
| |
|
314 |
| |
|
315 |
1
| public void testUTF16BOM() throws IOException {
|
|
316 |
| |
|
317 |
1
| Serializer serializer = new Serializer(out, "UTF-16");
|
|
318 |
1
| serializer.write(doc);
|
|
319 |
1
| serializer.flush();
|
|
320 |
1
| out.flush();
|
|
321 |
1
| out.close();
|
|
322 |
1
| byte[] data = out.toByteArray();
|
|
323 |
1
| assertEquals((byte) 0xFE, data[0]);
|
|
324 |
1
| assertEquals((byte) 0xFF, data[1]);
|
|
325 |
1
| assertEquals((byte) 0, data[2]);
|
|
326 |
1
| assertEquals('<', (char) data[3]);
|
|
327 |
| |
|
328 |
| } |
|
329 |
| |
|
330 |
| |
|
331 |
| |
|
332 |
| |
|
333 |
| |
|
334 |
| |
|
335 |
| |
|
336 |
| |
|
337 |
| |
|
338 |
1
| public void testUTF16BEBOM() throws IOException {
|
|
339 |
| |
|
340 |
1
| if (version >= 1.3) {
|
|
341 |
| |
|
342 |
1
| Serializer serializer = new Serializer(out, "UTF-16BE");
|
|
343 |
1
| serializer.write(doc);
|
|
344 |
1
| serializer.flush();
|
|
345 |
1
| out.flush();
|
|
346 |
1
| out.close();
|
|
347 |
1
| byte[] data = out.toByteArray();
|
|
348 |
1
| assertEquals((byte) 0, data[0]);
|
|
349 |
1
| assertEquals('<', (char) data[1]);
|
|
350 |
| } |
|
351 |
| |
|
352 |
| } |
|
353 |
| |
|
354 |
| |
|
355 |
1
| public void testXMLSpaceDefault() throws IOException {
|
|
356 |
| |
|
357 |
1
| root.addAttribute(
|
|
358 |
| new Attribute( |
|
359 |
| "xml:space", |
|
360 |
| "http://www.w3.org/XML/1998/namespace", |
|
361 |
| "preserve")); |
|
362 |
1
| Element child1 = new Element("preserve");
|
|
363 |
1
| String value =
|
|
364 |
| "This is a long sentence with plenty of opportunities for " + |
|
365 |
| "breaking from beginning to end."; |
|
366 |
1
| child1.appendChild(value);
|
|
367 |
1
| Element child2 = new Element("default");
|
|
368 |
1
| root.appendChild(child1);
|
|
369 |
1
| root.appendChild(child2);
|
|
370 |
1
| child2.addAttribute(
|
|
371 |
| new Attribute( |
|
372 |
| "xml:space", |
|
373 |
| "http://www.w3.org/XML/1998/namespace", |
|
374 |
| "default")); |
|
375 |
1
| String value2 =
|
|
376 |
| "This is another very long sentence with plenty" + |
|
377 |
| " of opportunities for breaking from beginning to end."; |
|
378 |
1
| child2.appendChild(value2);
|
|
379 |
| |
|
380 |
1
| String value3 =
|
|
381 |
| "This is still another very long sentence with plenty of " + |
|
382 |
| "opportunities for breaking from beginning to end."; |
|
383 |
1
| Element preserveAgain = new Element("test");
|
|
384 |
1
| preserveAgain.appendChild(value3);
|
|
385 |
1
| child2.appendChild(preserveAgain);
|
|
386 |
1
| preserveAgain.addAttribute(
|
|
387 |
| new Attribute( |
|
388 |
| "xml:space", |
|
389 |
| "http://www.w3.org/XML/1998/namespace", |
|
390 |
| "preserve")); |
|
391 |
| |
|
392 |
| |
|
393 |
1
| Serializer serializer = new Serializer(out, "UTF-8");
|
|
394 |
1
| serializer.setMaxLength(20);
|
|
395 |
1
| serializer.write(doc);
|
|
396 |
1
| String result = out.toString("UTF-8");
|
|
397 |
1
| assertTrue(result.indexOf(value) > 0);
|
|
398 |
1
| assertTrue(result.indexOf(value3) > 0);
|
|
399 |
1
| assertEquals(-1, result.indexOf(value2));
|
|
400 |
| |
|
401 |
| } |
|
402 |
| |
|
403 |
| |
|
404 |
1
| public void testXMLSpacePreserveWithIndenting()
|
|
405 |
| throws IOException { |
|
406 |
| |
|
407 |
1
| root.addAttribute(
|
|
408 |
| new Attribute( |
|
409 |
| "xml:space", |
|
410 |
| "http://www.w3.org/XML/1998/namespace", |
|
411 |
| "preserve")); |
|
412 |
1
| root.appendChild(new Element("sameline"));
|
|
413 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
414 |
1
| Serializer serializer = new Serializer(out, "UTF-8");
|
|
415 |
1
| serializer.setIndent(4);
|
|
416 |
1
| serializer.write(doc);
|
|
417 |
1
| String result = out.toString("UTF-8");
|
|
418 |
1
| assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
|
|
419 |
| + "<root xml:space=\"preserve\"><sameline/></root>\r\n", |
|
420 |
| result); |
|
421 |
| |
|
422 |
| } |
|
423 |
| |
|
424 |
| |
|
425 |
1
| public void testXMLSpaceUnspecifiedValueWithIndenting()
|
|
426 |
| throws IOException { |
|
427 |
| |
|
428 |
1
| root.addAttribute(
|
|
429 |
| new Attribute( |
|
430 |
| "xml:space", |
|
431 |
| "http://www.w3.org/XML/1998/namespace", |
|
432 |
| "undefined")); |
|
433 |
1
| root.appendChild(new Element("sameline"));
|
|
434 |
1
| Serializer serializer = new Serializer(out, "UTF-8");
|
|
435 |
1
| serializer.setIndent(4);
|
|
436 |
1
| serializer.write(doc);
|
|
437 |
1
| String result = out.toString("UTF-8");
|
|
438 |
1
| assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
|
|
439 |
| + "<root xml:space=\"undefined\">\r\n <sameline/>\r\n</root>\r\n", |
|
440 |
| result); |
|
441 |
| |
|
442 |
| } |
|
443 |
| |
|
444 |
| |
|
445 |
1
| public void testXMLSpaceDefaultWithIndenting() throws IOException {
|
|
446 |
| |
|
447 |
1
| root.addAttribute(
|
|
448 |
| new Attribute( |
|
449 |
| "xml:space", |
|
450 |
| "http://www.w3.org/XML/1998/namespace", |
|
451 |
| "preserve")); |
|
452 |
1
| Element child = new Element("child");
|
|
453 |
1
| child.addAttribute(
|
|
454 |
| new Attribute( |
|
455 |
| "xml:space", |
|
456 |
| "http://www.w3.org/XML/1998/namespace", |
|
457 |
| "default")); |
|
458 |
1
| root.appendChild(child);
|
|
459 |
1
| Serializer serializer = new Serializer(out, "UTF-8");
|
|
460 |
1
| serializer.setIndent(4);
|
|
461 |
1
| serializer.write(doc);
|
|
462 |
1
| String result = out.toString("UTF-8");
|
|
463 |
1
| assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
|
|
464 |
| + "<root xml:space=\"preserve\">" + |
|
465 |
| "<child xml:space=\"default\"/></root>\r\n", |
|
466 |
| result); |
|
467 |
| |
|
468 |
| } |
|
469 |
| |
|
470 |
| |
|
471 |
1
| public void testXMLSpaceDefaultWithIndentingAndGrandchildren()
|
|
472 |
| throws IOException { |
|
473 |
| |
|
474 |
1
| root.addAttribute(
|
|
475 |
| new Attribute( |
|
476 |
| "xml:space", |
|
477 |
| "http://www.w3.org/XML/1998/namespace", |
|
478 |
| "preserve")); |
|
479 |
1
| Element child = new Element("child");
|
|
480 |
1
| child.addAttribute(
|
|
481 |
| new Attribute( |
|
482 |
| "xml:space", |
|
483 |
| "http://www.w3.org/XML/1998/namespace", |
|
484 |
| "default")); |
|
485 |
1
| root.appendChild(child);
|
|
486 |
1
| child.appendChild(new Element("differentLine"));
|
|
487 |
1
| Serializer serializer = new Serializer(out, "UTF-8");
|
|
488 |
1
| serializer.setIndent(2);
|
|
489 |
1
| serializer.write(doc);
|
|
490 |
1
| String result = out.toString("UTF-8");
|
|
491 |
1
| assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
|
|
492 |
| + "<root xml:space=\"preserve\">" + |
|
493 |
| "<child xml:space=\"default\">\r\n <differentLine/>\r\n" + |
|
494 |
| " </child></root>\r\n", |
|
495 |
| result); |
|
496 |
| |
|
497 |
| } |
|
498 |
| |
|
499 |
| |
|
500 |
1
| public void testDontSerializeXMLNamespace() throws IOException {
|
|
501 |
| |
|
502 |
1
| Element root
|
|
503 |
| = new Element("html", "http://www.w3.org/1999/xhtml"); |
|
504 |
1
| root.addAttribute(
|
|
505 |
| new Attribute( |
|
506 |
| "xml:lang", "http://www.w3.org/XML/1998/namespace", "en")); |
|
507 |
1
| Document doc = new Document(root);
|
|
508 |
1
| Serializer serializer = new Serializer(out, "UTF-8");
|
|
509 |
1
| serializer.write(doc);
|
|
510 |
1
| String result = out.toString("UTF-8");
|
|
511 |
1
| assertEquals(-1, result.indexOf("xmlns:xml"));
|
|
512 |
1
| assertTrue(result.indexOf("xml:lang=") > 1);
|
|
513 |
| |
|
514 |
| } |
|
515 |
| |
|
516 |
1
| public void testDontSerializeNoNamespace() throws IOException {
|
|
517 |
| |
|
518 |
1
| Serializer serializer = new Serializer(out, "UTF-8");
|
|
519 |
1
| serializer.write(doc);
|
|
520 |
1
| String result = out.toString("UTF-8");
|
|
521 |
1
| assertEquals(-1, result.indexOf("xmlns="));
|
|
522 |
| |
|
523 |
| } |
|
524 |
| |
|
525 |
| |
|
526 |
1
| public void testDefaultNamespace() throws IOException {
|
|
527 |
| |
|
528 |
1
| Element root = new Element("root", "http://www.example.com");
|
|
529 |
1
| Document doc = new Document(root);
|
|
530 |
1
| Serializer serializer = new Serializer(out, "UTF-8");
|
|
531 |
1
| serializer.write(doc);
|
|
532 |
1
| String result = out.toString("UTF-8");
|
|
533 |
1
| assertTrue(result.indexOf("xmlns=") > 1);
|
|
534 |
1
| assertTrue(result.indexOf("http://www.example.com") > 1);
|
|
535 |
| |
|
536 |
| } |
|
537 |
| |
|
538 |
| |
|
539 |
1
| public void testEmptyElement() throws IOException {
|
|
540 |
| |
|
541 |
1
| Serializer serializer = new Serializer(out, "UTF-8");
|
|
542 |
1
| serializer.write(doc);
|
|
543 |
1
| String result = out.toString("UTF-8");
|
|
544 |
1
| assertEquals(
|
|
545 |
| "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<root/>\r\n", |
|
546 |
| result |
|
547 |
| ); |
|
548 |
| |
|
549 |
| } |
|
550 |
| |
|
551 |
| |
|
552 |
1
| public void testElementWithText() throws IOException {
|
|
553 |
| |
|
554 |
1
| String data = " test \n\n \n \n hello again";
|
|
555 |
1
| root.appendChild(data);
|
|
556 |
1
| Serializer serializer = new Serializer(out, "UTF-8");
|
|
557 |
1
| serializer.write(doc);
|
|
558 |
1
| String result = out.toString("UTF-8");
|
|
559 |
| |
|
560 |
1
| assertEquals(
|
|
561 |
| "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<root>" |
|
562 |
| + data + "</root>\r\n", |
|
563 |
| result); |
|
564 |
| |
|
565 |
| } |
|
566 |
| |
|
567 |
| |
|
568 |
1
| public void testStaticElementWithText()
|
|
569 |
| throws IOException { |
|
570 |
| |
|
571 |
1
| String data = " test \n\n \n \n hello again";
|
|
572 |
1
| root.appendChild(data);
|
|
573 |
1
| Serializer serializer = new Serializer(out);
|
|
574 |
1
| serializer.write(doc);
|
|
575 |
1
| String result = out.toString("UTF-8");
|
|
576 |
| |
|
577 |
1
| assertEquals(
|
|
578 |
| "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<root>" |
|
579 |
| + data + "</root>\r\n", |
|
580 |
| result); |
|
581 |
| |
|
582 |
| } |
|
583 |
| |
|
584 |
| |
|
585 |
1
| public void testElementWithTextAndCarriageReturns()
|
|
586 |
| throws IOException { |
|
587 |
| |
|
588 |
1
| String data = " test \r\n \n \r hello again";
|
|
589 |
1
| root.appendChild(data);
|
|
590 |
1
| Serializer serializer = new Serializer(out, "UTF-8");
|
|
591 |
1
| serializer.write(doc);
|
|
592 |
1
| String result = out.toString("UTF-8");
|
|
593 |
| |
|
594 |
1
| assertEquals(
|
|
595 |
| "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<root>" |
|
596 |
| + " test 
\n \n 
 hello again" |
|
597 |
| + "</root>\r\n", |
|
598 |
| result); |
|
599 |
| |
|
600 |
| } |
|
601 |
| |
|
602 |
| |
|
603 |
29
| private void serializeParseAndCompare(Document doc)
|
|
604 |
| throws IOException, ParsingException { |
|
605 |
| |
|
606 |
29
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
607 |
29
| Serializer serializer = new Serializer(out, "UTF-8");
|
|
608 |
29
| serializer.write(doc);
|
|
609 |
29
| String result = out.toString("UTF-8");
|
|
610 |
| |
|
611 |
29
| Document resultDoc = parser.build(result, null);
|
|
612 |
29
| XOMTestCase.assertEquals(doc, resultDoc);
|
|
613 |
| |
|
614 |
29
| setOutputStreamSerializeParseAndCompare(doc);
|
|
615 |
| |
|
616 |
| } |
|
617 |
| |
|
618 |
| |
|
619 |
29
| private void setOutputStreamSerializeParseAndCompare(Document doc)
|
|
620 |
| throws IOException, ParsingException { |
|
621 |
| |
|
622 |
29
| Serializer serializer = new Serializer(out);
|
|
623 |
29
| serializer.write(doc);
|
|
624 |
29
| ByteArrayOutputStream out2 = new ByteArrayOutputStream();
|
|
625 |
29
| serializer.setOutputStream(out2);
|
|
626 |
29
| serializer.write(doc);
|
|
627 |
29
| String result = out2.toString("UTF-8");
|
|
628 |
| |
|
629 |
29
| Document resultDoc = parser.build(result, null);
|
|
630 |
29
| XOMTestCase.assertEquals(doc, resultDoc);
|
|
631 |
| |
|
632 |
| } |
|
633 |
| |
|
634 |
| |
|
635 |
1
| public void testComment()
|
|
636 |
| throws IOException, ParsingException { |
|
637 |
| |
|
638 |
1
| String data = " <>&&entity; test \n hello again";
|
|
639 |
1
| root.appendChild(new Comment(data));
|
|
640 |
1
| serializeParseAndCompare(doc);
|
|
641 |
| |
|
642 |
| } |
|
643 |
| |
|
644 |
| |
|
645 |
1
| public void testProcessingInstruction()
|
|
646 |
| throws IOException, ParsingException { |
|
647 |
| |
|
648 |
1
| String data = "<>&&entity; test \n hello again";
|
|
649 |
1
| root.appendChild(new ProcessingInstruction("target", data));
|
|
650 |
1
| serializeParseAndCompare(doc);
|
|
651 |
| |
|
652 |
| } |
|
653 |
| |
|
654 |
1
| public void testBasicElementWithText()
|
|
655 |
| throws IOException, ParsingException { |
|
656 |
| |
|
657 |
1
| String data = " test \n hello again";
|
|
658 |
1
| root.appendChild(data);
|
|
659 |
1
| serializeParseAndCompare(doc);
|
|
660 |
| |
|
661 |
| } |
|
662 |
| |
|
663 |
| |
|
664 |
1
| public void testAttributes()
|
|
665 |
| throws IOException, ParsingException { |
|
666 |
| |
|
667 |
1
| root.addAttribute(new Attribute("test", "sadlkhasdk"));
|
|
668 |
1
| String data = " test \n hello again";
|
|
669 |
1
| root.appendChild(data);
|
|
670 |
1
| serializeParseAndCompare(doc);
|
|
671 |
| |
|
672 |
1
| root.addAttribute(new Attribute("test2", "sadlkhasdk"));
|
|
673 |
1
| serializeParseAndCompare(doc);
|
|
674 |
| |
|
675 |
1
| root.addAttribute(new Attribute("test3", " s adl khasdk "));
|
|
676 |
1
| serializeParseAndCompare(doc);
|
|
677 |
| |
|
678 |
1
| root.addAttribute(new Attribute("xlink:type",
|
|
679 |
| "http://www.w3.org/2001/xlink", " s adl khasdk ")); |
|
680 |
1
| serializeParseAndCompare(doc);
|
|
681 |
| |
|
682 |
| } |
|
683 |
| |
|
684 |
| |
|
685 |
1
| public void testChildElements()
|
|
686 |
| throws IOException, ParsingException { |
|
687 |
| |
|
688 |
1
| serializeParseAndCompare(doc);
|
|
689 |
| |
|
690 |
1
| Element child1 = new Element("child");
|
|
691 |
1
| Element child2 = new Element("child");
|
|
692 |
1
| Element child3 = new Element("child");
|
|
693 |
1
| serializeParseAndCompare(doc);
|
|
694 |
1
| root.appendChild(child1);
|
|
695 |
1
| serializeParseAndCompare(doc);
|
|
696 |
1
| child1.appendChild(child2);
|
|
697 |
1
| serializeParseAndCompare(doc);
|
|
698 |
1
| root.appendChild(child3);
|
|
699 |
1
| serializeParseAndCompare(doc);
|
|
700 |
1
| child3.appendChild("some data");
|
|
701 |
1
| serializeParseAndCompare(doc);
|
|
702 |
1
| child2.appendChild("\nsome data with \n line breaks\n");
|
|
703 |
1
| serializeParseAndCompare(doc);
|
|
704 |
1
| root.insertChild("now let's have some mixed content", 0);
|
|
705 |
1
| serializeParseAndCompare(doc);
|
|
706 |
| |
|
707 |
1
| root.setNamespaceURI("http://www.example.org/");
|
|
708 |
1
| serializeParseAndCompare(doc);
|
|
709 |
1
| child1.setNamespaceURI("http://www.example.org/");
|
|
710 |
1
| serializeParseAndCompare(doc);
|
|
711 |
1
| child2.setNamespaceURI("http://www.example.org/");
|
|
712 |
1
| serializeParseAndCompare(doc);
|
|
713 |
1
| child3.setNamespaceURI("http://www.example.org/");
|
|
714 |
1
| serializeParseAndCompare(doc);
|
|
715 |
1
| child1.setNamespacePrefix("example");
|
|
716 |
1
| serializeParseAndCompare(doc);
|
|
717 |
1
| child2.setNamespacePrefix("perverse");
|
|
718 |
1
| serializeParseAndCompare(doc);
|
|
719 |
| |
|
720 |
| } |
|
721 |
| |
|
722 |
| |
|
723 |
1
| public void testChildElementInNoNamespaceParentInDefaultNamespace()
|
|
724 |
| throws IOException, ParsingException { |
|
725 |
| |
|
726 |
1
| Element root = new Element("root", "http://www.example.org/");
|
|
727 |
1
| Element child1 = new Element("child");
|
|
728 |
1
| root.appendChild(child1);
|
|
729 |
1
| Document doc = new Document(root);
|
|
730 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
731 |
1
| Serializer serializer = new Serializer(out);
|
|
732 |
1
| serializer.write(doc);
|
|
733 |
1
| serializer.flush();
|
|
734 |
1
| String s = out.toString("UTF-8");
|
|
735 |
1
| assertTrue(s.indexOf("xmlns=\"\"") > 0);
|
|
736 |
| |
|
737 |
| } |
|
738 |
| |
|
739 |
| |
|
740 |
1
| public void testSimpleSerialize()
|
|
741 |
| throws IOException, ParsingException { |
|
742 |
| |
|
743 |
1
| Element root = new Element("root");
|
|
744 |
1
| Document doc = new Document(root);
|
|
745 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
746 |
1
| Serializer serializer = new Serializer(out);
|
|
747 |
1
| serializer.write(doc);
|
|
748 |
1
| serializer.flush();
|
|
749 |
1
| String s = out.toString("UTF-8");
|
|
750 |
1
| assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<root/>\r\n", s);
|
|
751 |
| |
|
752 |
| } |
|
753 |
| |
|
754 |
| |
|
755 |
1
| public void testPrologAndEpilog()
|
|
756 |
| throws IOException, ParsingException { |
|
757 |
| |
|
758 |
1
| serializeParseAndCompare(doc);
|
|
759 |
| |
|
760 |
1
| doc.insertChild(new Comment("Hello"), 0);
|
|
761 |
1
| serializeParseAndCompare(doc);
|
|
762 |
1
| doc.insertChild(new DocType("root"), 0);
|
|
763 |
1
| serializeParseAndCompare(doc);
|
|
764 |
1
| doc.insertChild(new ProcessingInstruction("test", "some data"),
|
|
765 |
| 0); |
|
766 |
1
| serializeParseAndCompare(doc);
|
|
767 |
1
| doc.insertChild(new Comment("Goodbye"), 0);
|
|
768 |
1
| serializeParseAndCompare(doc);
|
|
769 |
1
| doc.insertChild(
|
|
770 |
| new ProcessingInstruction("goodbye", "some data"), 0); |
|
771 |
1
| serializeParseAndCompare(doc);
|
|
772 |
1
| doc.appendChild(new Comment("Hello"));
|
|
773 |
1
| serializeParseAndCompare(doc);
|
|
774 |
1
| doc.appendChild(
|
|
775 |
| new ProcessingInstruction("test", "some data")); |
|
776 |
1
| serializeParseAndCompare(doc);
|
|
777 |
| |
|
778 |
| } |
|
779 |
| |
|
780 |
| |
|
781 |
1
| public void testChangeLineSeparator() throws IOException {
|
|
782 |
| |
|
783 |
1
| String breaks =
|
|
784 |
| "This\nstring\rcontains\r\nseveral\r\rweird line breaks."; |
|
785 |
1
| root.appendChild(breaks);
|
|
786 |
1
| root.addAttribute(new Attribute("test", breaks));
|
|
787 |
| |
|
788 |
1
| Serializer serializer = new Serializer(out, "UTF-8");
|
|
789 |
1
| serializer.setLineSeparator("\n");
|
|
790 |
1
| serializer.write(doc);
|
|
791 |
1
| String result = out.toString("UTF-8");
|
|
792 |
1
| assertTrue(result.indexOf('\n') > 0);
|
|
793 |
1
| assertTrue(result + "**\n" + result.indexOf('\r'),
|
|
794 |
| result.indexOf('\r') == -1); |
|
795 |
| |
|
796 |
1
| out = new ByteArrayOutputStream();
|
|
797 |
1
| serializer = new Serializer(out, "UTF-8");
|
|
798 |
1
| serializer.setLineSeparator("\r");
|
|
799 |
1
| serializer.write(doc);
|
|
800 |
1
| result = out.toString("UTF-8");
|
|
801 |
1
| assertTrue(result.indexOf('\r') > 0);
|
|
802 |
1
| assertTrue(result.indexOf('\n') == -1);
|
|
803 |
| |
|
804 |
1
| out = new ByteArrayOutputStream();
|
|
805 |
1
| serializer = new Serializer(out, "UTF-8");
|
|
806 |
1
| serializer.setLineSeparator("\r\n");
|
|
807 |
1
| serializer.write(doc);
|
|
808 |
1
| result = out.toString("UTF-8");
|
|
809 |
1
| assertTrue(result.indexOf("\r\n") > 0);
|
|
810 |
1
| for (int i = 0; i < result.length(); i++) {
|
|
811 |
219
| int c = result.charAt(i);
|
|
812 |
7
| if (c == '\r') assertTrue(result.charAt(i+1) == '\n');
|
|
813 |
| } |
|
814 |
| |
|
815 |
| } |
|
816 |
| |
|
817 |
| |
|
818 |
1
| public void testSettingOutputStreamDoesNotSetLineSeparator()
|
|
819 |
| throws IOException { |
|
820 |
| |
|
821 |
1
| root.appendChild("This\rstring");
|
|
822 |
1
| Serializer serializer = new Serializer(System.out, "UTF-8");
|
|
823 |
| |
|
824 |
1
| serializer.setOutputStream(out);
|
|
825 |
| |
|
826 |
1
| serializer.write(doc);
|
|
827 |
1
| String result = out.toString("UTF-8");
|
|
828 |
1
| assertTrue(result.indexOf("This
string") > 0);
|
|
829 |
| |
|
830 |
| } |
|
831 |
| |
|
832 |
| |
|
833 |
1
| public void testSettingOutputStreamDoesNotUnnecessarilyEscapeLineBreaks()
|
|
834 |
| throws IOException { |
|
835 |
| |
|
836 |
1
| root.appendChild("This\rstring");
|
|
837 |
1
| Serializer serializer = new Serializer(System.out, "UTF-8");
|
|
838 |
| |
|
839 |
1
| serializer.setOutputStream(out);
|
|
840 |
1
| serializer.setLineSeparator("\r\n");
|
|
841 |
1
| serializer.write(doc);
|
|
842 |
1
| String result = out.toString("UTF-8");
|
|
843 |
1
| assertTrue(result.indexOf("This\r\nstring") > 0);
|
|
844 |
| |
|
845 |
| } |
|
846 |
| |
|
847 |
| |
|
848 |
1
| public void testDontChangeLineSeparator() throws IOException {
|
|
849 |
| |
|
850 |
1
| String breaks
|
|
851 |
| = "This\nstring\rcontains\r\rseveral\n\nweird line breaks."; |
|
852 |
1
| String breaksHalfEscaped
|
|
853 |
| = "This\nstring
contains

several" + |
|
854 |
| "\n\nweird line breaks."; |
|
855 |
1
| String breaksEscaped
|
|
856 |
| = "This
string
contains

several" + |
|
857 |
| "

weird line breaks."; |
|
858 |
1
| root.appendChild(breaks);
|
|
859 |
| |
|
860 |
1
| Serializer serializer = new Serializer(out, "UTF-8");
|
|
861 |
1
| serializer.write(doc);
|
|
862 |
1
| String result = out.toString("UTF-8");
|
|
863 |
1
| assertTrue(result.indexOf(breaksHalfEscaped) > 0);
|
|
864 |
| |
|
865 |
1
| root = new Element("root");
|
|
866 |
1
| doc = new Document(root);
|
|
867 |
1
| root.addAttribute(new Attribute("test", breaks));
|
|
868 |
| |
|
869 |
1
| out = new ByteArrayOutputStream();
|
|
870 |
1
| serializer = new Serializer(out, "UTF-8");
|
|
871 |
1
| serializer.write(doc);
|
|
872 |
1
| result = out.toString("UTF-8");
|
|
873 |
1
| assertTrue(result.indexOf(breaksEscaped) > 0);
|
|
874 |
| |
|
875 |
| } |
|
876 |
| |
|
877 |
| |
|
878 |
1
| public void testPreserveBaseURI() throws IOException {
|
|
879 |
| |
|
880 |
1
| Serializer serializer = new Serializer(out, "UTF-8");
|
|
881 |
1
| serializer.setPreserveBaseURI(true);
|
|
882 |
1
| serializer.write(doc);
|
|
883 |
1
| String result = out.toString("UTF-8");
|
|
884 |
1
| assertTrue(result.indexOf("<root") > 1);
|
|
885 |
1
| doc.setBaseURI("http://www.example.com/index.xml");
|
|
886 |
1
| serializer.write(doc);
|
|
887 |
1
| result = out.toString("UTF-8");
|
|
888 |
1
| assertTrue(result.indexOf("<root ") > 1);
|
|
889 |
1
| assertTrue(result.indexOf("xml:base=") > 1);
|
|
890 |
1
| assertTrue(
|
|
891 |
| result.indexOf("http://www.example.com/index.xml") > 1 |
|
892 |
| ); |
|
893 |
| |
|
894 |
| } |
|
895 |
| |
|
896 |
| |
|
897 |
1
| public void testPreserveBaseURIWithChildren() throws IOException {
|
|
898 |
| |
|
899 |
1
| String base = "http://www.example.com/index.xml";
|
|
900 |
1
| root.setBaseURI(base);
|
|
901 |
1
| Element child = new Element("child");
|
|
902 |
1
| child.setBaseURI(base);
|
|
903 |
1
| root.appendChild(child);
|
|
904 |
1
| Serializer serializer = new Serializer(out, "UTF-8");
|
|
905 |
1
| serializer.setPreserveBaseURI(true);
|
|
906 |
1
| serializer.write(doc);
|
|
907 |
1
| String result = out.toString("UTF-8");
|
|
908 |
1
| assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
|
|
909 |
| + "<root xml:base=\"" + base + "\"><child/></root>\r\n", result); |
|
910 |
| |
|
911 |
| } |
|
912 |
| |
|
913 |
| |
|
914 |
1
| public void testPreserveBaseURIDoesntOverrideXMLBase()
|
|
915 |
| throws IOException { |
|
916 |
| |
|
917 |
1
| root.addAttribute(new Attribute("xml:base",
|
|
918 |
| "http://www.w3.org/XML/1998/namespace", |
|
919 |
| "http://www.cafeconleche.org/")); |
|
920 |
1
| Serializer serializer = new Serializer(out, "UTF-8");
|
|
921 |
1
| serializer.setPreserveBaseURI(true);
|
|
922 |
1
| serializer.write(doc);
|
|
923 |
1
| String result = out.toString("UTF-8");
|
|
924 |
1
| assertTrue(result.indexOf("<root") > 1);
|
|
925 |
1
| doc.setBaseURI("http://www.example.com/index.xml");
|
|
926 |
1
| serializer.write(doc);
|
|
927 |
1
| result = out.toString("UTF-8");
|
|
928 |
1
| assertTrue(result.indexOf("<root ") > 1);
|
|
929 |
1
| assertTrue(result.indexOf("xml:base=") > 1);
|
|
930 |
1
| assertTrue(result.indexOf("http://www.cafeconleche.org/") > 1);
|
|
931 |
1
| assertEquals(-1, result.indexOf("http://www.example.com/index.xml"));
|
|
932 |
| |
|
933 |
| } |
|
934 |
| |
|
935 |
| |
|
936 |
1
| public void testSetLineSeparator() {
|
|
937 |
| |
|
938 |
1
| Serializer serializer = new Serializer(System.out);
|
|
939 |
| |
|
940 |
1
| serializer.setLineSeparator("\r");
|
|
941 |
1
| assertEquals("\r", serializer.getLineSeparator());
|
|
942 |
1
| serializer.setLineSeparator("\n");
|
|
943 |
1
| assertEquals("\n", serializer.getLineSeparator());
|
|
944 |
1
| serializer.setLineSeparator("\r\n");
|
|
945 |
1
| assertEquals("\r\n", serializer.getLineSeparator());
|
|
946 |
| |
|
947 |
1
| try {
|
|
948 |
1
| serializer.setLineSeparator("r");
|
|
949 |
0
| fail("Allowed illegal separator character");
|
|
950 |
| } |
|
951 |
| catch (IllegalArgumentException success) { |
|
952 |
1
| assertNotNull(success.getMessage());
|
|
953 |
| } |
|
954 |
| |
|
955 |
1
| try {
|
|
956 |
1
| serializer.setLineSeparator("n");
|
|
957 |
0
| fail("Allowed illegal separator character");
|
|
958 |
| } |
|
959 |
| catch (IllegalArgumentException success) { |
|
960 |
1
| assertNotNull(success.getMessage());
|
|
961 |
| } |
|
962 |
| |
|
963 |
1
| try {
|
|
964 |
1
| serializer.setLineSeparator(" ");
|
|
965 |
0
| fail("Allowed illegal separator character");
|
|
966 |
| } |
|
967 |
| catch (IllegalArgumentException success) { |
|
968 |
1
| assertNotNull(success.getMessage());
|
|
969 |
| } |
|
970 |
| |
|
971 |
1
| try {
|
|
972 |
1
| serializer.setLineSeparator("rn");
|
|
973 |
0
| fail("Allowed illegal separator character");
|
|
974 |
| } |
|
975 |
| catch (IllegalArgumentException success) { |
|
976 |
1
| assertNotNull(success.getMessage());
|
|
977 |
| } |
|
978 |
| |
|
979 |
1
| try {
|
|
980 |
1
| serializer.setLineSeparator("<");
|
|
981 |
0
| fail("Allowed illegal separator character");
|
|
982 |
| } |
|
983 |
| catch (IllegalArgumentException success) { |
|
984 |
1
| assertNotNull(success.getMessage());
|
|
985 |
| } |
|
986 |
| |
|
987 |
1
| try {
|
|
988 |
1
| serializer.setLineSeparator("\u0085");
|
|
989 |
0
| fail("Allowed NEL separator character");
|
|
990 |
| } |
|
991 |
| catch (IllegalArgumentException success) { |
|
992 |
1
| assertNotNull(success.getMessage());
|
|
993 |
| } |
|
994 |
| |
|
995 |
| } |
|
996 |
| |
|
997 |
| |
|
998 |
1
| public void testLowerLimitOfUnicodeInCharacterData()
|
|
999 |
| throws IOException { |
|
1000 |
| |
|
1001 |
1
| root.appendChild("\uD800\uDC00");
|
|
1002 |
1
| Serializer serializer = new Serializer(out, "ISO-8859-1");
|
|
1003 |
1
| serializer.write(doc);
|
|
1004 |
1
| String result = out.toString("ISO-8859-1");
|
|
1005 |
1
| assertTrue(result, result.indexOf("𐀀") > 12);
|
|
1006 |
| |
|
1007 |
| } |
|
1008 |
| |
|
1009 |
| |
|
1010 |
1
| public void testUpperLimitOfUnicodeInCharacterData()
|
|
1011 |
| throws IOException { |
|
1012 |
| |
|
1013 |
1
| root.appendChild("\uDBFF\uDFFD");
|
|
1014 |
1
| Serializer serializer = new Serializer(out, "ISO-8859-1");
|
|
1015 |
1
| serializer.write(doc);
|
|
1016 |
1
| String result = out.toString("ISO-8859-1");
|
|
1017 |
1
| assertTrue(result, result.indexOf("􏿽") > 12);
|
|
1018 |
| |
|
1019 |
| } |
|
1020 |
| |
|
1021 |
| |
|
1022 |
1
| public void testSerializePlane1CharacterInAttributeValue()
|
|
1023 |
| throws IOException { |
|
1024 |
| |
|
1025 |
1
| root.addAttribute(new Attribute("test", "\uD834\uDD1E"));
|
|
1026 |
1
| Serializer serializer = new Serializer(out, "ISO-8859-1");
|
|
1027 |
1
| serializer.write(doc);
|
|
1028 |
1
| String result = out.toString("ISO-8859-1");
|
|
1029 |
1
| assertTrue(result, result.indexOf("𝄞") > 12);
|
|
1030 |
| |
|
1031 |
| } |
|
1032 |
| |
|
1033 |
1
| public void testSerializePlane1CharacterInCharacterData()
|
|
1034 |
| throws IOException { |
|
1035 |
| |
|
1036 |
1
| root.appendChild("\uD834\uDD1E");
|
|
1037 |
1
| Serializer serializer = new Serializer(out, "ISO-8859-1");
|
|
1038 |
1
| serializer.write(doc);
|
|
1039 |
1
| String result = out.toString("ISO-8859-1");
|
|
1040 |
1
| assertTrue(result, result.indexOf("𝄞") > 12);
|
|
1041 |
| |
|
1042 |
| } |
|
1043 |
| |
|
1044 |
| |
|
1045 |
1
| public void testSurrogatePairCountsAsOneCharacterForColumnCount()
|
|
1046 |
| throws IOException { |
|
1047 |
| |
|
1048 |
1
| Element root = new Element("r");
|
|
1049 |
1
| root.appendChild("\uD834\uDD1E");
|
|
1050 |
1
| Document doc = new Document(root);
|
|
1051 |
1
| Serializer serializer = new ColumnSerializer(out);
|
|
1052 |
1
| serializer.write(doc);
|
|
1053 |
| |
|
1054 |
| } |
|
1055 |
| |
|
1056 |
| |
|
1057 |
| private static class ColumnSerializer extends Serializer { |
|
1058 |
| |
|
1059 |
| |
|
1060 |
1
| ColumnSerializer(OutputStream out) {
|
|
1061 |
1
| super(out);
|
|
1062 |
| } |
|
1063 |
| |
|
1064 |
| |
|
1065 |
1
| public void write(Document doc) throws IOException {
|
|
1066 |
| |
|
1067 |
1
| for (int i = 0; i < doc.getChildCount(); i++) {
|
|
1068 |
1
| writeChild(doc.getChild(i));
|
|
1069 |
| } |
|
1070 |
1
| super.flush();
|
|
1071 |
1
| assertEquals(8, super.getColumnNumber());
|
|
1072 |
| |
|
1073 |
| } |
|
1074 |
| |
|
1075 |
| |
|
1076 |
| } |
|
1077 |
| |
|
1078 |
| |
|
1079 |
1
| public void testEscapeAttributeValue() throws IOException {
|
|
1080 |
| |
|
1081 |
1
| root.addAttribute(new Attribute("test", "\u0110"));
|
|
1082 |
1
| Serializer serializer = new Serializer(out, "ISO-8859-1");
|
|
1083 |
1
| serializer.write(doc);
|
|
1084 |
1
| String result = out.toString("ISO-8859-1");
|
|
1085 |
1
| assertTrue(result, result.indexOf("Đ") > 5);
|
|
1086 |
| |
|
1087 |
| } |
|
1088 |
| |
|
1089 |
| |
|
1090 |
1
| public void testLineFeedInAttributeValueWithDefaultOptions()
|
|
1091 |
| throws IOException, ParsingException { |
|
1092 |
| |
|
1093 |
1
| root.addAttribute(new Attribute("test", "\n"));
|
|
1094 |
1
| Serializer serializer = new Serializer(out, "ISO-8859-1");
|
|
1095 |
1
| serializer.write(doc);
|
|
1096 |
1
| out.close();
|
|
1097 |
1
| InputStream in = new ByteArrayInputStream(out.toByteArray());
|
|
1098 |
1
| Document reparsed = parser.build(in);
|
|
1099 |
1
| assertEquals(doc, reparsed);
|
|
1100 |
| |
|
1101 |
| } |
|
1102 |
| |
|
1103 |
| |
|
1104 |
1
| public void testCarriageReturnInAttributeValueWithDefaultOptions()
|
|
1105 |
| throws IOException, ParsingException { |
|
1106 |
| |
|
1107 |
1
| root.addAttribute(new Attribute("test", "\r"));
|
|
1108 |
1
| Serializer serializer = new Serializer(out, "ISO-8859-1");
|
|
1109 |
1
| serializer.write(doc);
|
|
1110 |
1
| out.close();
|
|
1111 |
1
| InputStream in = new ByteArrayInputStream(out.toByteArray());
|
|
1112 |
1
| Document reparsed = parser.build(in);
|
|
1113 |
1
| assertEquals(doc, reparsed);
|
|
1114 |
| |
|
1115 |
| } |
|
1116 |
| |
|
1117 |
| |
|
1118 |
1
| public void testCarriageReturnInTextWithDefaultOptions()
|
|
1119 |
| throws IOException, ParsingException { |
|
1120 |
| |
|
1121 |
1
| root.appendChild("\r");
|
|
1122 |
1
| Serializer serializer = new Serializer(out, "ISO-8859-1");
|
|
1123 |
1
| serializer.write(doc);
|
|
1124 |
1
| out.close();
|
|
1125 |
1
| InputStream in = new ByteArrayInputStream(out.toByteArray());
|
|
1126 |
1
| Document reparsed = parser.build(in);
|
|
1127 |
1
| assertEquals(doc, reparsed);
|
|
1128 |
| |
|
1129 |
| } |
|
1130 |
| |
|
1131 |
| |
|
1132 |
1
| public void testTabInAttributeValueWithDefaultOptions()
|
|
1133 |
| throws IOException, ParsingException { |
|
1134 |
| |
|
1135 |
1
| root.addAttribute(new Attribute("test", "\t"));
|
|
1136 |
1
| Serializer serializer = new Serializer(out, "ISO-8859-1");
|
|
1137 |
1
| serializer.write(doc);
|
|
1138 |
1
| out.close();
|
|
1139 |
1
| InputStream in = new ByteArrayInputStream(out.toByteArray());
|
|
1140 |
1
| Document reparsed = parser.build(in);
|
|
1141 |
1
| assertEquals(doc, reparsed);
|
|
1142 |
| |
|
1143 |
| } |
|
1144 |
| |
|
1145 |
| |
|
1146 |
| |
|
1147 |
| |
|
1148 |
| |
|
1149 |
| |
|
1150 |
| |
|
1151 |
| |
|
1152 |
1
| public void testTabInAttributeValueWithLineSeparator()
|
|
1153 |
| throws IOException, ParsingException { |
|
1154 |
| |
|
1155 |
1
| root.addAttribute(new Attribute("test", "\t"));
|
|
1156 |
1
| Serializer serializer = new Serializer(out, "ISO-8859-1");
|
|
1157 |
1
| serializer.setLineSeparator("\r");
|
|
1158 |
1
| serializer.write(doc);
|
|
1159 |
1
| out.close();
|
|
1160 |
1
| InputStream in = new ByteArrayInputStream(out.toByteArray());
|
|
1161 |
1
| Document reparsed = parser.build(in);
|
|
1162 |
1
| assertEquals(doc, reparsed);
|
|
1163 |
| |
|
1164 |
| } |
|
1165 |
| |
|
1166 |
| |
|
1167 |
| |
|
1168 |
| |
|
1169 |
| |
|
1170 |
| |
|
1171 |
| |
|
1172 |
| |
|
1173 |
| |
|
1174 |
1
| public void testTabInAttributeValueWithIndenting()
|
|
1175 |
| throws IOException, ParsingException { |
|
1176 |
| |
|
1177 |
1
| root.addAttribute(new Attribute("test", "\t"));
|
|
1178 |
1
| Serializer serializer = new Serializer(out, "ISO-8859-1");
|
|
1179 |
1
| serializer.setIndent(2);
|
|
1180 |
1
| serializer.write(doc);
|
|
1181 |
1
| out.close();
|
|
1182 |
1
| InputStream in = new ByteArrayInputStream(out.toByteArray());
|
|
1183 |
1
| Document reparsed = parser.build(in);
|
|
1184 |
1
| String result = reparsed.getRootElement().getAttributeValue("test");
|
|
1185 |
1
| assertEquals("Tab not normalized to space", " ", result);
|
|
1186 |
| |
|
1187 |
| } |
|
1188 |
| |
|
1189 |
| |
|
1190 |
1
| public void testCRLFInAttributeValueWithLineSeparatorCR()
|
|
1191 |
| throws IOException, ParsingException { |
|
1192 |
| |
|
1193 |
1
| root.addAttribute(new Attribute("test", "\r\n"));
|
|
1194 |
1
| Serializer serializer = new Serializer(out, "ISO-8859-1");
|
|
1195 |
1
| serializer.setLineSeparator("\r");
|
|
1196 |
1
| serializer.write(doc);
|
|
1197 |
1
| out.close();
|
|
1198 |
1
| InputStream in = new ByteArrayInputStream(out.toByteArray());
|
|
1199 |
1
| Document reparsed = parser.build(in);
|
|
1200 |
1
| String result = reparsed.getRootElement().getAttributeValue("test");
|
|
1201 |
1
| assertEquals("\r", result);
|
|
1202 |
| |
|
1203 |
| } |
|
1204 |
| |
|
1205 |
| |
|
1206 |
1
| public void testCRLFInAttributeValueWithLineSeparatorLF()
|
|
1207 |
| throws IOException, ParsingException { |
|
1208 |
| |
|
1209 |
1
| root.addAttribute(new Attribute("test", "\r\n"));
|
|
1210 |
1
| Serializer serializer = new Serializer(out, "ISO-8859-1");
|
|
1211 |
1
| serializer.setLineSeparator("\n");
|
|
1212 |
1
| serializer.write(doc);
|
|
1213 |
1
| out.close();
|
|
1214 |
1
| InputStream in = new ByteArrayInputStream(out.toByteArray());
|
|
1215 |
1
| Document reparsed = parser.build(in);
|
|
1216 |
1
| String result = reparsed.getRootElement().getAttributeValue("test");
|
|
1217 |
1
| assertEquals("\n", result);
|
|
1218 |
| |
|
1219 |
| } |
|
1220 |
| |
|
1221 |
1
| public void testLFInAttributeValueWithLineSeparatorCRLF()
|
|
1222 |
| throws IOException, ParsingException { |
|
1223 |
| |
|
1224 |
1
| root.addAttribute(new Attribute("test", "\n"));
|
|
1225 |
1
| Serializer serializer = new Serializer(out, "ISO-8859-1");
|
|
1226 |
1
| serializer.setLineSeparator("\r\n");
|
|
1227 |
1
| serializer.write(doc);
|
|
1228 |
1
| out.close();
|
|
1229 |
1
| InputStream in = new ByteArrayInputStream(out.toByteArray());
|
|
1230 |
1
| Document reparsed = parser.build(in);
|
|
1231 |
1
| String result = reparsed.getRootElement().getAttributeValue("test");
|
|
1232 |
1
| assertEquals("\r\n", result);
|
|
1233 |
| |
|
1234 |
| } |
|
1235 |
| |
|
1236 |
| |
|
1237 |
1
| public void testNotEscapeLinefeedInTextContent()
|
|
1238 |
| throws IOException { |
|
1239 |
| |
|
1240 |
1
| root.appendChild("\r\n");
|
|
1241 |
1
| Serializer serializer = new Serializer(out, "ISO-8859-1");
|
|
1242 |
1
| serializer.write(doc);
|
|
1243 |
1
| out.close();
|
|
1244 |
1
| String result = new String(out.toByteArray(), "ISO-8859-1");
|
|
1245 |
1
| assertEquals(
|
|
1246 |
| "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\r\n<root>
\n</root>\r\n", |
|
1247 |
| result |
|
1248 |
| ); |
|
1249 |
| |
|
1250 |
| } |
|
1251 |
| |
|
1252 |
| |
|
1253 |
1
| public void testCRLFInAttributeValue()
|
|
1254 |
| throws IOException, ParsingException { |
|
1255 |
| |
|
1256 |
1
| root.addAttribute(new Attribute("test", "a\r\na"));
|
|
1257 |
1
| Serializer serializer = new Serializer(out, "ISO-8859-1");
|
|
1258 |
1
| serializer.write(doc);
|
|
1259 |
1
| out.close();
|
|
1260 |
1
| InputStream in = new ByteArrayInputStream(out.toByteArray());
|
|
1261 |
1
| Document reparsed = parser.build(in);
|
|
1262 |
1
| String result = reparsed.getRootElement().getAttributeValue("test");
|
|
1263 |
1
| assertEquals("CRLF not escaped", "a\r\na", result);
|
|
1264 |
| |
|
1265 |
| } |
|
1266 |
| |
|
1267 |
| |
|
1268 |
1
| public void testPunctuationInAttributeValueNonUnicode()
|
|
1269 |
| throws IOException, ParsingException { |
|
1270 |
| |
|
1271 |
1
| root.addAttribute(new Attribute("test", "$()*+,="));
|
|
1272 |
1
| Serializer serializer = new Serializer(out, "ISO-8859-1");
|
|
1273 |
1
| serializer.write(doc);
|
|
1274 |
1
| out.close();
|
|
1275 |
1
| InputStream in = new ByteArrayInputStream(out.toByteArray());
|
|
1276 |
1
| Document reparsed = parser.build(in);
|
|
1277 |
1
| String result = reparsed.getRootElement().getAttributeValue("test");
|
|
1278 |
1
| assertEquals("$()*+,=", result);
|
|
1279 |
| |
|
1280 |
| } |
|
1281 |
| |
|
1282 |
| |
|
1283 |
1
| public void testCRLFInAttributeValueWithIndenting()
|
|
1284 |
| throws IOException, ParsingException { |
|
1285 |
| |
|
1286 |
1
| root.addAttribute(new Attribute("test", "\r\n"));
|
|
1287 |
1
| Serializer serializer = new Serializer(out, "ISO-8859-1");
|
|
1288 |
1
| serializer.setIndent(2);
|
|
1289 |
1
| serializer.write(doc);
|
|
1290 |
1
| out.close();
|
|
1291 |
1
| InputStream in = new ByteArrayInputStream(out.toByteArray());
|
|
1292 |
1
| Document reparsed = parser.build(in);
|
|
1293 |
1
| String result = reparsed.getRootElement().getAttributeValue("test");
|
|
1294 |
1
| assertEquals("CRLF unnecessarily escaped", -1, result.indexOf('\r'));
|
|
1295 |
| |
|
1296 |
| |
|
1297 |
| |
|
1298 |
| } |
|
1299 |
| |
|
1300 |
| |
|
1301 |
1
| public void testCRLFInAttributeValueWithMaxLength()
|
|
1302 |
| throws IOException, ParsingException { |
|
1303 |
| |
|
1304 |
1
| root.addAttribute(new Attribute("test", "\r\n"));
|
|
1305 |
| |
|
1306 |
1
| Serializer serializer = new Serializer(out, "ISO-8859-1");
|
|
1307 |
1
| serializer.setMaxLength(64);
|
|
1308 |
1
| serializer.write(doc);
|
|
1309 |
1
| out.close();
|
|
1310 |
1
| InputStream in = new ByteArrayInputStream(out.toByteArray());
|
|
1311 |
1
| Document reparsed = parser.build(in);
|
|
1312 |
1
| String result = reparsed.getRootElement().getAttributeValue("test");
|
|
1313 |
1
| assertEquals("CRLF unnecessarily escaped", " ", result);
|
|
1314 |
| |
|
1315 |
| } |
|
1316 |
| |
|
1317 |
| |
|
1318 |
1
| public void testCRInTextValueWithLineSeparator()
|
|
1319 |
| throws IOException, ParsingException { |
|
1320 |
| |
|
1321 |
1
| root.appendChild("\r");
|
|
1322 |
1
| Serializer serializer = new Serializer(out, "ISO-8859-1");
|
|
1323 |
1
| serializer.setLineSeparator("\n");
|
|
1324 |
1
| serializer.write(doc);
|
|
1325 |
1
| out.close();
|
|
1326 |
1
| InputStream in = new ByteArrayInputStream(out.toByteArray());
|
|
1327 |
1
| Document reparsed = parser.build(in);
|
|
1328 |
1
| String result = reparsed.getValue();
|
|
1329 |
1
| assertEquals("\n", result);
|
|
1330 |
| |
|
1331 |
| } |
|
1332 |
| |
|
1333 |
| |
|
1334 |
1
| public void testCRLFInTextValueWithLineSeparator()
|
|
1335 |
| throws IOException, ParsingException { |
|
1336 |
| |
|
1337 |
1
| root.appendChild("test \r\n test");
|
|
1338 |
1
| Serializer serializer = new Serializer(out, "ISO-8859-1");
|
|
1339 |
1
| serializer.setLineSeparator("\n");
|
|
1340 |
1
| serializer.write(doc);
|
|
1341 |
1
| out.close();
|
|
1342 |
1
| InputStream in = new ByteArrayInputStream(out.toByteArray());
|
|
1343 |
1
| Document reparsed = parser.build(in);
|
|
1344 |
1
| String result = reparsed.getValue();
|
|
1345 |
1
| assertEquals("test \n test", result);
|
|
1346 |
| |
|
1347 |
| } |
|
1348 |
| |
|
1349 |
| |
|
1350 |
1
| public void testCRInTextWithIndenting()
|
|
1351 |
| throws IOException, ParsingException { |
|
1352 |
| |
|
1353 |
1
| root.appendChild("\r");
|
|
1354 |
1
| Serializer serializer = new Serializer(out, "ISO-8859-1");
|
|
1355 |
1
| serializer.setIndent(2);
|
|
1356 |
1
| serializer.write(doc);
|
|
1357 |
1
| out.close();
|
|
1358 |
1
| InputStream in = new ByteArrayInputStream(out.toByteArray());
|
|
1359 |
1
| Document reparsed = parser.build(in);
|
|
1360 |
1
| String result = reparsed.getValue();
|
|
1361 |
1
| assertEquals("Carriage return unnecessarily escaped",
|
|
1362 |
| -1, result.indexOf('\r')); |
|
1363 |
| |
|
1364 |
| |
|
1365 |
| |
|
1366 |
| |
|
1367 |
| } |
|
1368 |
| |
|
1369 |
| |
|
1370 |
1
| public void testCRInTextWithMaxLength()
|
|
1371 |
| throws IOException, ParsingException { |
|
1372 |
| |
|
1373 |
1
| root.appendChild("\r");
|
|
1374 |
1
| Serializer serializer = new Serializer(out, "ISO-8859-1");
|
|
1375 |
1
| serializer.setMaxLength(64);
|
|
1376 |
1
| serializer.write(doc);
|
|
1377 |
1
| out.close();
|
|
1378 |
1
| InputStream in = new ByteArrayInputStream(out.toByteArray());
|
|
1379 |
1
| Document reparsed = parser.build(in);
|
|
1380 |
1
| String result = reparsed.getValue();
|
|
1381 |
1
| assertEquals("Carriage return unnecessarily escaped", "\n", result);
|
|
1382 |
| |
|
1383 |
| } |
|
1384 |
| |
|
1385 |
| |
|
1386 |
1
| public void testTabInAttributeValueWithMaxLength()
|
|
1387 |
| throws IOException, ParsingException { |
|
1388 |
| |
|
1389 |
1
| root.addAttribute(new Attribute("test", "\t"));
|
|
1390 |
1
| Serializer serializer = new Serializer(out, "ISO-8859-1");
|
|
1391 |
1
| serializer.setMaxLength(64);
|
|
1392 |
1
| serializer.write(doc);
|
|
1393 |
1
| out.close();
|
|
1394 |
1
| InputStream in = new ByteArrayInputStream(out.toByteArray());
|
|
1395 |
1
| Document reparsed = parser.build(in);
|
|
1396 |
1
| String result = reparsed.getRootElement().getAttributeValue("test");
|
|
1397 |
1
| assertEquals("Tab not normalized to space", " ", result);
|
|
1398 |
| |
|
1399 |
| } |
|
1400 |
| |
|
1401 |
| |
|
1402 |
| |
|
1403 |
| |
|
1404 |
| |
|
1405 |
| |
|
1406 |
| |
|
1407 |
| |
|
1408 |
1
| public void testTabInAttributeValueWithZeroMaxLength()
|
|
1409 |
| throws IOException, ParsingException { |
|
1410 |
| |
|
1411 |
1
| root.addAttribute(new Attribute("test", "\t"));
|
|
1412 |
1
| Serializer serializer = new Serializer(out, "ISO-8859-1");
|
|
1413 |
1
| serializer.setMaxLength(0);
|
|
1414 |
1
| serializer.write(doc);
|
|
1415 |
1
| out.close();
|
|
1416 |
1
| InputStream in = new ByteArrayInputStream(out.toByteArray());
|
|
1417 |
1
| Document reparsed = parser.build(in);
|
|
1418 |
1
| String result = reparsed.getRootElement().getAttributeValue("test");
|
|
1419 |
1
| assertEquals("Tab not normalized to space", "\t", result);
|
|
1420 |
| |
|
1421 |
| } |
|
1422 |
| |
|
1423 |
| |
|
1424 |
1
| public void testSetMaxLength() {
|
|
1425 |
1
| Serializer serializer = new Serializer(System.out);
|
|
1426 |
| |
|
1427 |
1
| serializer.setMaxLength(72);
|
|
1428 |
1
| assertEquals(72, serializer.getMaxLength());
|
|
1429 |
1
| serializer.setMaxLength(720);
|
|
1430 |
1
| assertEquals(720, serializer.getMaxLength());
|
|
1431 |
1
| serializer.setMaxLength(1);
|
|
1432 |
1
| assertEquals(1, serializer.getMaxLength());
|
|
1433 |
1
| serializer.setMaxLength(0);
|
|
1434 |
1
| assertEquals(0, serializer.getMaxLength());
|
|
1435 |
1
| serializer.setMaxLength(-1);
|
|
1436 |
1
| assertEquals(0, serializer.getMaxLength());
|
|
1437 |
| |
|
1438 |
| } |
|
1439 |
| |
|
1440 |
| |
|
1441 |
1
| public void testSetIndent() {
|
|
1442 |
| |
|
1443 |
1
| Serializer serializer = new Serializer(System.out);
|
|
1444 |
1
| serializer.setIndent(72);
|
|
1445 |
1
| assertEquals(72, serializer.getIndent());
|
|
1446 |
1
| serializer.setIndent(720);
|
|
1447 |
1
| assertEquals(720, serializer.getIndent());
|
|
1448 |
1
| serializer.setIndent(1);
|
|
1449 |
1
| assertEquals(1, serializer.getIndent());
|
|
1450 |
1
| serializer.setIndent(0);
|
|
1451 |
1
| assertEquals(0, serializer.getIndent());
|
|
1452 |
1
| try {
|
|
1453 |
1
| serializer.setIndent(-1);
|
|
1454 |
0
| fail("Allowed negative indent");
|
|
1455 |
| } |
|
1456 |
| catch (IllegalArgumentException success) { |
|
1457 |
1
| assertNotNull(success.getMessage());
|
|
1458 |
| } |
|
1459 |
| |
|
1460 |
| } |
|
1461 |
| |
|
1462 |
| |
|
1463 |
1
| public void testReallyBigIndent() throws ValidityException, ParsingException, IOException {
|
|
1464 |
| |
|
1465 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
1466 |
1
| Serializer serializer = new Serializer(out);
|
|
1467 |
1
| serializer.setIndent(100);
|
|
1468 |
1
| StringBuffer spaces = new StringBuffer(400);
|
|
1469 |
400
| for (int i = 0; i < 400; i++) spaces.append(' ');
|
|
1470 |
1
| String _400 = spaces.toString();
|
|
1471 |
1
| String _300 = spaces.substring(0, 300);
|
|
1472 |
1
| String _200 = spaces.substring(0, 200);
|
|
1473 |
1
| String _100 = spaces.substring(0, 100);
|
|
1474 |
| |
|
1475 |
1
| String original ="<a><b><c><d><e>Hello</e></d></c></b></a>";
|
|
1476 |
1
| Document doc = parser.build(original, null);
|
|
1477 |
1
| serializer.write(doc);
|
|
1478 |
1
| String expected ="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<a>\r\n"
|
|
1479 |
| + _100 + "<b>\r\n" + _200 + "<c>\r\n" + _300 + "<d>\r\n" + _400 |
|
1480 |
| + "<e>Hello</e>\r\n" + _300 + "</d>\r\n" + _200 + "</c>\r\n" |
|
1481 |
| + _100 + "</b>\r\n</a>\r\n"; |
|
1482 |
1
| String result = new String(out.toByteArray(), "UTF-8");
|
|
1483 |
1
| assertEquals(expected, result);
|
|
1484 |
| |
|
1485 |
| } |
|
1486 |
| |
|
1487 |
| |
|
1488 |
1
| public void testLineLength() throws IOException {
|
|
1489 |
| |
|
1490 |
1
| int length = 40;
|
|
1491 |
1
| String data = "This is a really long string that does not "
|
|
1492 |
| + "contain any line breaks. However, there is lots of " |
|
1493 |
| + "white space so there shouldn't be any trouble wrapping it" |
|
1494 |
| + " into 40 characters or less per line. "; |
|
1495 |
1
| root.appendChild(data);
|
|
1496 |
1
| Serializer serializer = new Serializer(out, "UTF-8");
|
|
1497 |
1
| serializer.setMaxLength(length);
|
|
1498 |
1
| serializer.write(doc);
|
|
1499 |
1
| String result = out.toString("UTF-8");
|
|
1500 |
| |
|
1501 |
1
| BufferedReader reader
|
|
1502 |
| = new BufferedReader(new StringReader(result)); |
|
1503 |
1
| for (String line = reader.readLine();
|
|
1504 |
9
| line != null;
|
|
1505 |
| line = reader.readLine()) { |
|
1506 |
8
| assertTrue(line.length() + ": " + line,
|
|
1507 |
| line.length() <= length); |
|
1508 |
| } |
|
1509 |
| |
|
1510 |
| } |
|
1511 |
| |
|
1512 |
| |
|
1513 |
1
| public void testLineLengthWithSetOutputStream()
|
|
1514 |
| throws IOException { |
|
1515 |
| |
|
1516 |
1
| int length = 40;
|
|
1517 |
1
| String data = "This is a really long string that does not "
|
|
1518 |
| + "contain any line breaks. However, there is lots of " |
|
1519 |
| + "white space so there shouldn't be any trouble wrapping it" |
|
1520 |
| + " into 40 characters or less per line. "; |
|
1521 |
1
| root.appendChild(data);
|
|
1522 |
1
| Serializer serializer = new Serializer(new ByteArrayOutputStream(), "UTF-8");
|
|
1523 |
1
| serializer.setMaxLength(length);
|
|
1524 |
1
| serializer.write(doc);
|
|
1525 |
1
| serializer.setOutputStream(out);
|
|
1526 |
1
| serializer.write(doc);
|
|
1527 |
1
| String result = out.toString("UTF-8");
|
|
1528 |
| |
|
1529 |
1
| BufferedReader reader
|
|
1530 |
| = new BufferedReader(new StringReader(result)); |
|
1531 |
1
| for (String line = reader.readLine();
|
|
1532 |
9
| line != null;
|
|
1533 |
| line = reader.readLine()) { |
|
1534 |
8
| assertTrue(line.length() + ": " + line,
|
|
1535 |
| line.length() <= length); |
|
1536 |
| } |
|
1537 |
| |
|
1538 |
| } |
|
1539 |
| |
|
1540 |
| |
|
1541 |
1
| public void testPrettyXML() throws IOException {
|
|
1542 |
| |
|
1543 |
1
| Element items = new Element("itemSet");
|
|
1544 |
1
| items.appendChild(new Element("item1"));
|
|
1545 |
1
| items.appendChild(new Element("item2"));
|
|
1546 |
1
| Document doc = new Document(items);
|
|
1547 |
1
| Serializer serializer = new Serializer(out);
|
|
1548 |
1
| serializer.setIndent(4);
|
|
1549 |
1
| serializer.write(doc);
|
|
1550 |
1
| serializer.flush();
|
|
1551 |
1
| out.close();
|
|
1552 |
1
| String result = new String(out.toByteArray(), "UTF-8");
|
|
1553 |
1
| assertEquals(
|
|
1554 |
| "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" |
|
1555 |
| + "<itemSet>\r\n <item1/>\r\n <item2/>\r\n" |
|
1556 |
| + "</itemSet>\r\n", |
|
1557 |
| result |
|
1558 |
| ); |
|
1559 |
| |
|
1560 |
| } |
|
1561 |
| |
|
1562 |
| |
|
1563 |
1
| public void testIndentingFromSubclassThatAvoidsWriteElement()
|
|
1564 |
| throws IOException { |
|
1565 |
| |
|
1566 |
1
| Element items = new Element("itemSet");
|
|
1567 |
1
| items.appendChild(new Element("item1"));
|
|
1568 |
1
| items.appendChild(new Element("item2"));
|
|
1569 |
1
| Document doc = new Document(items);
|
|
1570 |
1
| Serializer serializer = new AvoidWriteElement(out);
|
|
1571 |
1
| serializer.setIndent(4);
|
|
1572 |
1
| serializer.write(doc);
|
|
1573 |
1
| serializer.flush();
|
|
1574 |
1
| out.close();
|
|
1575 |
1
| String result = new String(out.toByteArray(), "UTF-8");
|
|
1576 |
1
| assertEquals(
|
|
1577 |
| "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" |
|
1578 |
| + "<itemSet>\r\n <item1></item1>\r\n <item2></item2>\r\n" |
|
1579 |
| + "</itemSet>\r\n", |
|
1580 |
| result |
|
1581 |
| ); |
|
1582 |
| |
|
1583 |
| } |
|
1584 |
| |
|
1585 |
| |
|
1586 |
1
| public void testIndentingFromSubclassThatAvoidsWriteElementWithXMLSpace()
|
|
1587 |
| throws IOException { |
|
1588 |
| |
|
1589 |
1
| Element items = new Element("itemSet");
|
|
1590 |
1
| items.addAttribute(new Attribute("xml:space", Namespace.XML_NAMESPACE, "preserve"));
|
|
1591 |
1
| items.appendChild(new Element("item1"));
|
|
1592 |
1
| items.appendChild(new Element("item2"));
|
|
1593 |
1
| Document doc = new Document(items);
|
|
1594 |
1
| Serializer serializer = new AvoidWriteElement(out);
|
|
1595 |
1
| serializer.setIndent(4);
|
|
1596 |
1
| serializer.write(doc);
|
|
1597 |
1
| serializer.flush();
|
|
1598 |
1
| out.close();
|
|
1599 |
1
| String result = new String(out.toByteArray(), "UTF-8");
|
|
1600 |
1
| assertEquals(
|
|
1601 |
| "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" |
|
1602 |
| + "<itemSet xml:space=\"preserve\"><item1></item1><item2></item2>" |
|
1603 |
| + "</itemSet>\r\n", |
|
1604 |
| result |
|
1605 |
| ); |
|
1606 |
| |
|
1607 |
| } |
|
1608 |
| |
|
1609 |
| |
|
1610 |
| private static class AvoidWriteElement extends Serializer { |
|
1611 |
| |
|
1612 |
2
| public AvoidWriteElement(OutputStream out) {
|
|
1613 |
2
| super(out);
|
|
1614 |
| } |
|
1615 |
| |
|
1616 |
6
| protected void write(Element element) throws IOException {
|
|
1617 |
| |
|
1618 |
6
| writeStartTag(element);
|
|
1619 |
6
| for (int i = 0; i < element.getChildCount(); i++) {
|
|
1620 |
4
| Node child = element.getChild(i);
|
|
1621 |
4
| writeChild(child);
|
|
1622 |
| } |
|
1623 |
6
| writeEndTag(element);
|
|
1624 |
| |
|
1625 |
| } |
|
1626 |
| |
|
1627 |
| } |
|
1628 |
| |
|
1629 |
| |
|
1630 |
1
| public void testIndentAndBreakBeforeComment() throws IOException {
|
|
1631 |
| |
|
1632 |
1
| Element items = new Element("itemSet");
|
|
1633 |
1
| items.appendChild(new Comment("item1"));
|
|
1634 |
1
| Document doc = new Document(items);
|
|
1635 |
1
| Serializer serializer = new Serializer(out);
|
|
1636 |
1
| serializer.setIndent(4);
|
|
1637 |
1
| serializer.write(doc);
|
|
1638 |
1
| serializer.flush();
|
|
1639 |
1
| out.close();
|
|
1640 |
1
| String result = new String(out.toByteArray(), "UTF-8");
|
|
1641 |
1
| assertEquals(
|
|
1642 |
| "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" |
|
1643 |
| + "<itemSet>\r\n <!--item1-->\r\n" |
|
1644 |
| + "</itemSet>\r\n", |
|
1645 |
| result |
|
1646 |
| ); |
|
1647 |
| |
|
1648 |
| } |
|
1649 |
| |
|
1650 |
| |
|
1651 |
1
| public void testOnlyXMLSpaceCountsAsBoundaryWhiteSpace()
|
|
1652 |
| throws IOException { |
|
1653 |
| |
|
1654 |
1
| String emSpace = "\u2003";
|
|
1655 |
1
| Element items = new Element("itemSet");
|
|
1656 |
1
| items.appendChild(emSpace);
|
|
1657 |
1
| items.appendChild(new Element("a"));
|
|
1658 |
| |
|
1659 |
1
| Document doc = new Document(items);
|
|
1660 |
1
| Serializer serializer = new Serializer(out);
|
|
1661 |
1
| serializer.setIndent(4);
|
|
1662 |
1
| serializer.write(doc);
|
|
1663 |
1
| serializer.flush();
|
|
1664 |
1
| out.close();
|
|
1665 |
1
| String result = new String(out.toByteArray(), "UTF-8");
|
|
1666 |
1
| assertEquals(
|
|
1667 |
| "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" |
|
1668 |
| + "<itemSet>\u2003\r\n <a/>\r\n</itemSet>\r\n", |
|
1669 |
| result |
|
1670 |
| ); |
|
1671 |
| |
|
1672 |
| } |
|
1673 |
| |
|
1674 |
| |
|
1675 |
1
| public void testWhiteSpaceBetweenCommentsIsBoundaryWhiteSpace()
|
|
1676 |
| throws IOException { |
|
1677 |
| |
|
1678 |
1
| Element items = new Element("itemSet");
|
|
1679 |
1
| items.appendChild(new Comment("item1"));
|
|
1680 |
1
| items.appendChild(" \r\n ");
|
|
1681 |
1
| items.appendChild(new Comment("item2"));
|
|
1682 |
1
| Document doc = new Document(items);
|
|
1683 |
1
| Serializer serializer = new Serializer(out);
|
|
1684 |
1
| serializer.setIndent(4);
|
|
1685 |
1
| serializer.write(doc);
|
|
1686 |
1
| serializer.flush();
|
|
1687 |
1
| out.close();
|
|
1688 |
1
| String result = new String(out.toByteArray(), "UTF-8");
|
|
1689 |
1
| assertEquals(
|
|
1690 |
| "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" |
|
1691 |
| + "<itemSet>\r\n <!--item1-->\r\n <!--item2-->\r\n" |
|
1692 |
| + "</itemSet>\r\n", |
|
1693 |
| result |
|
1694 |
| ); |
|
1695 |
| |
|
1696 |
| } |
|
1697 |
| |
|
1698 |
| |
|
1699 |
1
| public void testWhiteSpaceBeforeCommentIsBoundaryWhiteSpace()
|
|
1700 |
| throws IOException { |
|
1701 |
| |
|
1702 |
1
| Element items = new Element("itemSet");
|
|
1703 |
1
| items.appendChild(" \r\n ");
|
|
1704 |
1
| items.appendChild(new Comment("item1"));
|
|
1705 |
1
| items.appendChild(new Comment("item2"));
|
|
1706 |
1
| Document doc = new Document(items);
|
|
1707 |
1
| Serializer serializer = new Serializer(out);
|
|
1708 |
1
| serializer.setIndent(4);
|
|
1709 |
1
| serializer.write(doc);
|
|
1710 |
1
| serializer.flush();
|
|
1711 |
1
| out.close();
|
|
1712 |
1
| String result = new String(out.toByteArray(), "UTF-8");
|
|
1713 |
1
| assertEquals(
|
|
1714 |
| "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" |
|
1715 |
| + "<itemSet>\r\n <!--item1-->\r\n <!--item2-->\r\n" |
|
1716 |
| + "</itemSet>\r\n", |
|
1717 |
| result |
|
1718 |
| ); |
|
1719 |
| |
|
1720 |
| } |
|
1721 |
| |
|
1722 |
| |
|
1723 |
1
| public void testWhiteSpaceAfterCommentsIsBoundaryWhiteSpace()
|
|
1724 |
| throws IOException { |
|
1725 |
| |
|
1726 |
1
| Element items = new Element("itemSet");
|
|
1727 |
1
| items.appendChild(new Comment("item1"));
|
|
1728 |
1
| items.appendChild(new Comment("item2"));
|
|
1729 |
1
| items.appendChild(" \r\n ");
|
|
1730 |
1
| Document doc = new Document(items);
|
|
1731 |
1
| Serializer serializer = new Serializer(out);
|
|
1732 |
1
| serializer.setIndent(4);
|
|
1733 |
1
| serializer.write(doc);
|
|
1734 |
1
| serializer.flush();
|
|
1735 |
1
| out.close();
|
|
1736 |
1
| String result = new String(out.toByteArray(), "UTF-8");
|
|
1737 |
1
| assertEquals(
|
|
1738 |
| "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" |
|
1739 |
| + "<itemSet>\r\n <!--item1-->\r\n <!--item2-->\r\n" |
|
1740 |
| + "</itemSet>\r\n", |
|
1741 |
| result |
|
1742 |
| ); |
|
1743 |
| |
|
1744 |
| } |
|
1745 |
| |
|
1746 |
| |
|
1747 |
1
| public void testIndentAndBreakBeforeProcessingInstruction()
|
|
1748 |
| throws IOException { |
|
1749 |
| |
|
1750 |
1
| Element items = new Element("itemSet");
|
|
1751 |
1
| items.appendChild(new ProcessingInstruction("target", "value"));
|
|
1752 |
1
| Document doc = new Document(items);
|
|
1753 |
1
| Serializer serializer = new Serializer(out);
|
|
1754 |
1
| serializer.setIndent(4);
|
|
1755 |
1
| serializer.write(doc);
|
|
1756 |
1
| serializer.flush();
|
|
1757 |
1
| out.close();
|
|
1758 |
1
| String result = new String(out.toByteArray(), "UTF-8");
|
|
1759 |
1
| assertEquals(
|
|
1760 |
| "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" |
|
1761 |
| + "<itemSet>\r\n <?target value?>\r\n" |
|
1762 |
| + "</itemSet>\r\n", |
|
1763 |
| result |
|
1764 |
| ); |
|
1765 |
| |
|
1766 |
| } |
|
1767 |
| |
|
1768 |
| |
|
1769 |
1
| public void testDontBreakLineInElementWithSimpleContent()
|
|
1770 |
| throws IOException { |
|
1771 |
| |
|
1772 |
1
| Element items = new Element("itemSet");
|
|
1773 |
1
| Element item1 = new Element("item1");
|
|
1774 |
1
| items.appendChild(item1);
|
|
1775 |
1
| item1.appendChild("content");
|
|
1776 |
1
| Document doc = new Document(items);
|
|
1777 |
1
| Serializer serializer = new Serializer(out);
|
|
1778 |
1
| serializer.setIndent(4);
|
|
1779 |
1
| serializer.write(doc);
|
|
1780 |
1
| serializer.flush();
|
|
1781 |
1
| out.close();
|
|
1782 |
1
| String result = new String(out.toByteArray(), "UTF-8");
|
|
1783 |
1
| assertEquals(
|
|
1784 |
| "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" |
|
1785 |
| + "<itemSet>\r\n <item1>content</item1>\r\n" |
|
1786 |
| + "</itemSet>\r\n", |
|
1787 |
| result |
|
1788 |
| ); |
|
1789 |
| |
|
1790 |
| } |
|
1791 |
| |
|
1792 |
| |
|
1793 |
1
| public void testPrettyXMLWithSetOutputStream() throws IOException {
|
|
1794 |
| |
|
1795 |
1
| Element items = new Element("itemSet");
|
|
1796 |
1
| items.appendChild(new Element("item1"));
|
|
1797 |
1
| items.appendChild(new Element("item2"));
|
|
1798 |
1
| Document doc = new Document(items);
|
|
1799 |
1
| Serializer serializer = new Serializer(new ByteArrayOutputStream());
|
|
1800 |
1
| serializer.setIndent(4);
|
|
1801 |
1
| serializer.setLineSeparator("\n");
|
|
1802 |
1
| serializer.write(doc);
|
|
1803 |
1
| serializer.setOutputStream(out);
|
|
1804 |
1
| serializer.write(doc);
|
|
1805 |
1
| serializer.flush();
|
|
1806 |
1
| out.close();
|
|
1807 |
1
| String result = new String(out.toByteArray(), "UTF-8");
|
|
1808 |
1
| assertEquals(
|
|
1809 |
| "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
|
1810 |
| + "<itemSet>\n <item1/>\n <item2/>\n" |
|
1811 |
| + "</itemSet>\n", |
|
1812 |
| result |
|
1813 |
| ); |
|
1814 |
| |
|
1815 |
| } |
|
1816 |
| |
|
1817 |
| |
|
1818 |
1
| public void testAmpersandAndLessThanInText() throws IOException {
|
|
1819 |
| |
|
1820 |
1
| root.appendChild("data<data&data");
|
|
1821 |
1
| Serializer serializer = new Serializer(out);
|
|
1822 |
1
| serializer.write(doc);
|
|
1823 |
1
| serializer.flush();
|
|
1824 |
1
| out.close();
|
|
1825 |
1
| String result = new String(out.toByteArray(), "UTF-8");
|
|
1826 |
1
| assertEquals(
|
|
1827 |
| "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" |
|
1828 |
| + "<root>data<data&data" |
|
1829 |
| + "</root>\r\n", |
|
1830 |
| result |
|
1831 |
| ); |
|
1832 |
| |
|
1833 |
| } |
|
1834 |
| |
|
1835 |
| |
|
1836 |
1
| public void testAmpersandAndAngleBracketsInAttributeValue()
|
|
1837 |
| throws IOException { |
|
1838 |
| |
|
1839 |
1
| root.addAttribute(new Attribute("b", "data<data>data&"));
|
|
1840 |
1
| Serializer serializer = new Serializer(out);
|
|
1841 |
1
| serializer.write(doc);
|
|
1842 |
1
| serializer.flush();
|
|
1843 |
1
| out.close();
|
|
1844 |
1
| String result = new String(out.toByteArray(), "UTF-8");
|
|
1845 |
1
| assertEquals(
|
|
1846 |
| "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" |
|
1847 |
| + "<root b=\"data<data>data&\"/>\r\n", |
|
1848 |
| result |
|
1849 |
| ); |
|
1850 |
| |
|
1851 |
| } |
|
1852 |
| |
|
1853 |
| |
|
1854 |
1
| public void testSetNFC() {
|
|
1855 |
| |
|
1856 |
1
| Serializer serializer = new Serializer(System.out);
|
|
1857 |
1
| assertFalse(serializer.getUnicodeNormalizationFormC());
|
|
1858 |
1
| serializer.setUnicodeNormalizationFormC(true);
|
|
1859 |
1
| assertTrue(serializer.getUnicodeNormalizationFormC());
|
|
1860 |
| |
|
1861 |
| } |
|
1862 |
| |
|
1863 |
| |
|
1864 |
1
| public void testNFCInElementContent() throws IOException {
|
|
1865 |
| |
|
1866 |
1
| Element root = new Element("a");
|
|
1867 |
1
| root.appendChild("c\u0327");
|
|
1868 |
1
| Document doc = new Document(root);
|
|
1869 |
1
| Serializer serializer = new Serializer(out);
|
|
1870 |
1
| serializer.setUnicodeNormalizationFormC(true);
|
|
1871 |
1
| serializer.write(doc);
|
|
1872 |
1
| serializer.flush();
|
|
1873 |
1
| out.close();
|
|
1874 |
1
| String result = new String(out.toByteArray(), "UTF-8");
|
|
1875 |
1
| assertEquals(
|
|
1876 |
| "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" |
|
1877 |
| + "<a>\u00E7</a>\r\n", |
|
1878 |
| result |
|
1879 |
| ); |
|
1880 |
| |
|
1881 |
| } |
|
1882 |
| |
|
1883 |
| |
|
1884 |
1
| public void testNFCInConsecutiveTextNodes() throws IOException {
|
|
1885 |
| |
|
1886 |
1
| Element root = new Element("a");
|
|
1887 |
1
| root.appendChild("c");
|
|
1888 |
1
| root.appendChild("\u0327");
|
|
1889 |
1
| Document doc = new Document(root);
|
|
1890 |
1
| Serializer serializer = new Serializer(out);
|
|
1891 |
1
| serializer.setUnicodeNormalizationFormC(true);
|
|
1892 |
1
| serializer.write(doc);
|
|
1893 |
1
| serializer.flush();
|
|
1894 |
1
| out.close();
|
|
1895 |
1
| String result = new String(out.toByteArray(), "UTF-8");
|
|
1896 |
1
| assertEquals(
|
|
1897 |
| "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" |
|
1898 |
| + "<a>\u00E7</a>\r\n", |
|
1899 |
| result |
|
1900 |
| ); |
|
1901 |
| |
|
1902 |
| } |
|
1903 |
| |
|
1904 |
| |
|
1905 |
1
| public void testNoNFCByDefault() throws IOException {
|
|
1906 |
| |
|
1907 |
1
| Element root = new Element("c\u0327");
|
|
1908 |
1
| root.appendChild("c\u0327");
|
|
1909 |
1
| Document doc = new Document(root);
|
|
1910 |
1
| Serializer serializer = new Serializer(out);
|
|
1911 |
1
| serializer.write(doc);
|
|
1912 |
1
| serializer.flush();
|
|
1913 |
1
| out.close();
|
|
1914 |
1
| String result = new String(out.toByteArray(), "UTF-8");
|
|
1915 |
1
| assertEquals(
|
|
1916 |
| "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" |
|
1917 |
| + "<c\u0327>c\u0327</c\u0327>\r\n", |
|
1918 |
| result |
|
1919 |
| ); |
|
1920 |
| |
|
1921 |
| } |
|
1922 |
| |
|
1923 |
| |
|
1924 |
| |
|
1925 |
1
| public void testNFCInAttribute() throws IOException {
|
|
1926 |
| |
|
1927 |
1
| root.addAttribute(new Attribute("c\u0327", "c\u0327"));
|
|
1928 |
1
| Serializer serializer = new Serializer(out);
|
|
1929 |
1
| serializer.setUnicodeNormalizationFormC(true);
|
|
1930 |
1
| serializer.write(doc);
|
|
1931 |
1
| serializer.flush();
|
|
1932 |
1
| out.close();
|
|
1933 |
1
| String result = new String(out.toByteArray(), "UTF-8");
|
|
1934 |
1
| assertEquals(
|
|
1935 |
| "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" |
|
1936 |
| + "<root \u00E7=\"\u00E7\"/>\r\n", |
|
1937 |
| result |
|
1938 |
| ); |
|
1939 |
| |
|
1940 |
| } |
|
1941 |
| |
|
1942 |
| |
|
1943 |
1
| public void testNFCInElementName() throws IOException {
|
|
1944 |
| |
|
1945 |
1
| Element root = new Element("c\u0327");
|
|
1946 |
1
| Document doc = new Document(root);
|
|
1947 |
1
| Serializer serializer = new Serializer(out);
|
|
1948 |
1
| serializer.setUnicodeNormalizationFormC(true);
|
|
1949 |
1
| serializer.write(doc);
|
|
1950 |
1
| serializer.flush();
|
|
1951 |
1
| out.close();
|
|
1952 |
1
| String result = new String(out.toByteArray(), "UTF-8");
|
|
1953 |
1
| assertEquals(
|
|
1954 |
| "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" |
|
1955 |
| + "<\u00E7/>\r\n", |
|
1956 |
| result |
|
1957 |
| ); |
|
1958 |
| |
|
1959 |
| } |
|
1960 |
| |
|
1961 |
| |
|
1962 |
1
| public void testNFCInComment() throws IOException {
|
|
1963 |
| |
|
1964 |
1
| Element root = new Element("a");
|
|
1965 |
1
| Document doc = new Document(root);
|
|
1966 |
1
| doc.insertChild(new Comment("c\u0327hat"), 0);
|
|
1967 |
1
| Serializer serializer = new Serializer(out);
|
|
1968 |
1
| serializer.setUnicodeNormalizationFormC(true);
|
|
1969 |
1
| serializer.write(doc);
|
|
1970 |
1
| serializer.flush();
|
|
1971 |
1
| out.close();
|
|
1972 |
1
| String result = new String(out.toByteArray(), "UTF-8");
|
|
1973 |
1
| assertEquals(
|
|
1974 |
| "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" |
|
1975 |
| + "<!--\u00E7hat-->\r\n" |
|
1976 |
| + "<a/>\r\n", |
|
1977 |
| result |
|
1978 |
| ); |
|
1979 |
| |
|
1980 |
| } |
|
1981 |
| |
|
1982 |
| |
|
1983 |
1
| public void testNFCInProcessingInstruction() throws IOException {
|
|
1984 |
| |
|
1985 |
1
| doc.appendChild(new ProcessingInstruction("c\u0327hat", "c\u0327hat"));
|
|
1986 |
1
| Serializer serializer = new Serializer(out);
|
|
1987 |
1
| serializer.setUnicodeNormalizationFormC(true);
|
|
1988 |
1
| serializer.write(doc);
|
|
1989 |
1
| serializer.flush();
|
|
1990 |
1
| out.close();
|
|
1991 |
1
| String result = new String(out.toByteArray(), "UTF-8");
|
|
1992 |
1
| assertEquals(
|
|
1993 |
| "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" |
|
1994 |
| + "<root/>\r\n" |
|
1995 |
| + "<?\u00E7hat \u00E7hat?>\r\n", |
|
1996 |
| result |
|
1997 |
| ); |
|
1998 |
| |
|
1999 |
| } |
|
2000 |
| |
|
2001 |
| |
|
2002 |
1
| public void testNFCInElementContentWithNonUnicodeEncoding()
|
|
2003 |
| throws IOException { |
|
2004 |
| |
|
2005 |
1
| root.appendChild("c\u0327");
|
|
2006 |
1
| Serializer serializer = new Serializer(out, "ISO-8859-5");
|
|
2007 |
1
| serializer.setUnicodeNormalizationFormC(true);
|
|
2008 |
1
| serializer.write(doc);
|
|
2009 |
1
| serializer.flush();
|
|
2010 |
1
| out.close();
|
|
2011 |
1
| String result = new String(out.toByteArray(), "ISO-8859-5");
|
|
2012 |
1
| assertEquals(
|
|
2013 |
| "<?xml version=\"1.0\" encoding=\"ISO-8859-5\"?>\r\n" |
|
2014 |
| + "<root>ç</root>\r\n", |
|
2015 |
| result |
|
2016 |
| ); |
|
2017 |
| |
|
2018 |
| } |
|
2019 |
| |
|
2020 |
| |
|
2021 |
1
| public void testNFCFollowingEntityReference()
|
|
2022 |
| throws IOException { |
|
2023 |
| |
|
2024 |
1
| root.appendChild("<\u0338");
|
|
2025 |
1
| Serializer serializer = new Serializer(out);
|
|
2026 |
1
| serializer.setUnicodeNormalizationFormC(true);
|
|
2027 |
1
| serializer.write(doc);
|
|
2028 |
1
| serializer.flush();
|
|
2029 |
1
| out.close();
|
|
2030 |
1
| String result = new String(out.toByteArray(), "UTF-8");
|
|
2031 |
1
| assertEquals(
|
|
2032 |
| "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" |
|
2033 |
| + "<root>\u226E</root>\r\n", |
|
2034 |
| result |
|
2035 |
| ); |
|
2036 |
| |
|
2037 |
| } |
|
2038 |
| |
|
2039 |
| |
|
2040 |
1
| public void testNFCWithSetOutputStream()
|
|
2041 |
| throws IOException { |
|
2042 |
| |
|
2043 |
1
| root.appendChild("c\u0327");
|
|
2044 |
1
| Serializer serializer = new Serializer(new ByteArrayOutputStream(), "ISO-8859-5");
|
|
2045 |
1
| serializer.setUnicodeNormalizationFormC(true);
|
|
2046 |
1
| serializer.write(doc);
|
|
2047 |
1
| serializer.setOutputStream(out);
|
|
2048 |
1
| serializer.write(doc);
|
|
2049 |
1
| serializer.flush();
|
|
2050 |
1
| out.close();
|
|
2051 |
1
| String result = new String(out.toByteArray(), "ISO-8859-5");
|
|
2052 |
1
| assertEquals(
|
|
2053 |
| "<?xml version=\"1.0\" encoding=\"ISO-8859-5\"?>\r\n" |
|
2054 |
| + "<root>ç</root>\r\n", |
|
2055 |
| result |
|
2056 |
| ); |
|
2057 |
| |
|
2058 |
| } |
|
2059 |
| |
|
2060 |
| |
|
2061 |
1
| public void testNFCWithKoreanCharacter()
|
|
2062 |
| throws IOException { |
|
2063 |
| |
|
2064 |
1
| root.appendChild("\u1111\u1171\u11B6");
|
|
2065 |
1
| Serializer serializer = new Serializer(new ByteArrayOutputStream());
|
|
2066 |
1
| serializer.setUnicodeNormalizationFormC(true);
|
|
2067 |
1
| serializer.write(doc);
|
|
2068 |
1
| serializer.setOutputStream(out);
|
|
2069 |
1
| serializer.write(doc);
|
|
2070 |
1
| serializer.flush();
|
|
2071 |
1
| out.close();
|
|
2072 |
1
| String result = new String(out.toByteArray(), "UTF-8");
|
|
2073 |
1
| assertEquals(
|
|
2074 |
| "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" |
|
2075 |
| + "<root>\uD4DB</root>\r\n", |
|
2076 |
| result |
|
2077 |
| ); |
|
2078 |
| |
|
2079 |
| } |
|
2080 |
| |
|
2081 |
| |
|
2082 |
1
| public void testNullOutputStream() {
|
|
2083 |
| |
|
2084 |
1
| try {
|
|
2085 |
1
| new Serializer(null);
|
|
2086 |
0
| fail("Allowed null output stream");
|
|
2087 |
| } |
|
2088 |
| catch (NullPointerException success) { |
|
2089 |
1
| assertNotNull(success.getMessage());
|
|
2090 |
| } |
|
2091 |
| |
|
2092 |
| } |
|
2093 |
| |
|
2094 |
| |
|
2095 |
1
| public void testNullOutputStreamWithEncoding()
|
|
2096 |
| throws UnsupportedEncodingException { |
|
2097 |
| |
|
2098 |
1
| try {
|
|
2099 |
1
| new Serializer(null, "UTF-8");
|
|
2100 |
0
| fail("Allowed null output stream");
|
|
2101 |
| } |
|
2102 |
| catch (NullPointerException success) { |
|
2103 |
1
| assertNotNull(success.getMessage());
|
|
2104 |
| } |
|
2105 |
| |
|
2106 |
| } |
|
2107 |
| |
|
2108 |
| |
|
2109 |
1
| public void testNullEncoding()
|
|
2110 |
| throws UnsupportedEncodingException { |
|
2111 |
| |
|
2112 |
1
| try {
|
|
2113 |
1
| new Serializer(System.out, null);
|
|
2114 |
0
| fail("Allowed null encoding");
|
|
2115 |
| } |
|
2116 |
| catch (NullPointerException success) { |
|
2117 |
1
| assertNotNull(success.getMessage());
|
|
2118 |
| } |
|
2119 |
| |
|
2120 |
| } |
|
2121 |
| |
|
2122 |
| |
|
2123 |
| |
|
2124 |
1
| public void testNullDocument() throws IOException {
|
|
2125 |
| |
|
2126 |
1
| Serializer serializer = new Serializer(out, "UTF-16");
|
|
2127 |
1
| try {
|
|
2128 |
1
| serializer.write(null);
|
|
2129 |
0
| fail("Wrote null document");
|
|
2130 |
| } |
|
2131 |
| catch (NullPointerException success) { |
|
2132 |
| |
|
2133 |
| } |
|
2134 |
1
| byte[] result = out.toByteArray();
|
|
2135 |
1
| assertEquals(0, result.length);
|
|
2136 |
| |
|
2137 |
| } |
|
2138 |
| |
|
2139 |
| |
|
2140 |
1
| public void testGetEncoding()
|
|
2141 |
| throws UnsupportedEncodingException { |
|
2142 |
| |
|
2143 |
1
| Serializer serializer = new Serializer(System.out, "ISO-8859-1");
|
|
2144 |
1
| assertEquals("ISO-8859-1", serializer.getEncoding());
|
|
2145 |
| |
|
2146 |
| } |
|
2147 |
| |
|
2148 |
| |
|
2149 |
1
| public void testGetPreserveBaseURI()
|
|
2150 |
| throws UnsupportedEncodingException { |
|
2151 |
| |
|
2152 |
1
| Serializer serializer = new Serializer(System.out, "ISO-8859-1");
|
|
2153 |
1
| assertFalse(serializer.getPreserveBaseURI());
|
|
2154 |
1
| serializer.setPreserveBaseURI(true);
|
|
2155 |
1
| assertTrue(serializer.getPreserveBaseURI());
|
|
2156 |
1
| serializer.setPreserveBaseURI(false);
|
|
2157 |
1
| assertFalse(serializer.getPreserveBaseURI());
|
|
2158 |
| |
|
2159 |
| } |
|
2160 |
| |
|
2161 |
| |
|
2162 |
1
| public void testSerializeDocTypeWithSystemID()
|
|
2163 |
| throws IOException { |
|
2164 |
| |
|
2165 |
1
| Serializer serializer = new Serializer(out);
|
|
2166 |
1
| Document doc = new Document(new Element("a"));
|
|
2167 |
1
| doc.setDocType(new DocType("b", "example.dtd"));
|
|
2168 |
1
| serializer.write(doc);
|
|
2169 |
1
| String result = out.toString("UTF-8");
|
|
2170 |
1
| assertTrue(result.endsWith("<a/>\r\n"));
|
|
2171 |
1
| assertTrue(result.indexOf("<!DOCTYPE b SYSTEM \"example.dtd\">") > 0);
|
|
2172 |
| |
|
2173 |
| } |
|
2174 |
| |
|
2175 |
| |
|
2176 |
1
| public void testSerializeDocTypeWithPublicAndSystemID()
|
|
2177 |
| throws IOException { |
|
2178 |
| |
|
2179 |
1
| Serializer serializer = new Serializer(out);
|
|
2180 |
1
| Document doc = new Document(new Element("a"));
|
|
2181 |
1
| doc.setDocType(new DocType("b",
|
|
2182 |
| "-//W3C//DTD XHTML 1.0 Transitional//EN", "example.dtd")); |
|
2183 |
1
| serializer.write(doc);
|
|
2184 |
1
| String result = out.toString("UTF-8");
|
|
2185 |
1
| assertTrue(result.endsWith("<a/>\r\n"));
|
|
2186 |
1
| assertTrue(result.indexOf(
|
|
2187 |
| "<!DOCTYPE b PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"example.dtd\">") > 0); |
|
2188 |
| |
|
2189 |
| } |
|
2190 |
| |
|
2191 |
| |
|
2192 |
1
| public void testSerializeXMLNamespaceDeclaration()
|
|
2193 |
| throws ParsingException, IOException { |
|
2194 |
| |
|
2195 |
1
| Serializer serializer = new Serializer(out);
|
|
2196 |
1
| Element root = new Element("root");
|
|
2197 |
1
| root.addNamespaceDeclaration("xml", Namespace.XML_NAMESPACE);
|
|
2198 |
1
| Document doc = new Document(root);
|
|
2199 |
1
| serializer.write(doc);
|
|
2200 |
1
| String result = out.toString("UTF-8");
|
|
2201 |
1
| assertTrue(result.endsWith("<root/>\r\n"));
|
|
2202 |
| |
|
2203 |
| } |
|
2204 |
| |
|
2205 |
| |
|
2206 |
1
| public void testSerializeDocTypeWithInternalDTDSubset()
|
|
2207 |
| throws ParsingException, IOException { |
|
2208 |
| |
|
2209 |
1
| Serializer serializer = new Serializer(out);
|
|
2210 |
1
| String data = "<!DOCTYPE root [ <!ELEMENT root EMPTY> ]><test/>";
|
|
2211 |
1
| Builder builder = new Builder();
|
|
2212 |
1
| Document doc = builder.build(data, "http://www.example.com");
|
|
2213 |
1
| serializer.write(doc);
|
|
2214 |
1
| String result = out.toString("UTF-8");
|
|
2215 |
1
| assertTrue(result.endsWith("<test/>\r\n"));
|
|
2216 |
1
| assertTrue(result.indexOf("<!DOCTYPE root [\r\n") > 0);
|
|
2217 |
1
| assertTrue(result.indexOf("\r\n]>\r\n") > 0);
|
|
2218 |
1
| assertTrue(result.indexOf("<!ELEMENT root EMPTY>\r\n") > 0);
|
|
2219 |
| |
|
2220 |
| } |
|
2221 |
| |
|
2222 |
| |
|
2223 |
1
| public void testSerializeInternalDTDSubsetContainingUnavailableCharacter()
|
|
2224 |
| throws ParsingException, IOException { |
|
2225 |
| |
|
2226 |
1
| Serializer serializer = new Serializer(out, "US-ASCII");
|
|
2227 |
1
| String data = "<!DOCTYPE root ["
|
|
2228 |
| + "<!ELEMENT root EMPTY> " |
|
2229 |
| + "<!ATTLIST root attr CDATA 'café creme'> " |
|
2230 |
| + "]><test/>"; |
|
2231 |
1
| Builder builder = new Builder();
|
|
2232 |
1
| Document doc = builder.build(data, "http://www.example.com");
|
|
2233 |
1
| try {
|
|
2234 |
1
| serializer.write(doc);
|
|
2235 |
0
| fail("How'd you serialize é in ASCII?");
|
|
2236 |
| } |
|
2237 |
| catch (UnavailableCharacterException success) { |
|
2238 |
1
| assertTrue(success.getMessage().indexOf("é (é)") > 1);
|
|
2239 |
| } |
|
2240 |
| |
|
2241 |
| } |
|
2242 |
| |
|
2243 |
| |
|
2244 |
1
| public void testLineBreaksInInternalDTDSubset()
|
|
2245 |
| throws ParsingException, IOException { |
|
2246 |
| |
|
2247 |
1
| Serializer serializer = new Serializer(out);
|
|
2248 |
1
| serializer.setLineSeparator("\r");
|
|
2249 |
1
| String data = "<!DOCTYPE root [ <!ELEMENT root EMPTY> <!ELEMENT data EMPTY> ]><test/>";
|
|
2250 |
1
| Builder builder = new Builder();
|
|
2251 |
1
| Document doc = builder.build(data, "http://www.example.com");
|
|
2252 |
1
| serializer.write(doc);
|
|
2253 |
1
| String result = out.toString("UTF-8");
|
|
2254 |
1
| assertTrue(result.endsWith("<test/>\r"));
|
|
2255 |
1
| assertTrue(result.indexOf("<!DOCTYPE root [\r") > 0);
|
|
2256 |
1
| assertTrue(result.indexOf("\r]>\r") > 0);
|
|
2257 |
1
| assertTrue(result.indexOf("<!ELEMENT root EMPTY>\r") > 0);
|
|
2258 |
1
| assertTrue(result.indexOf("<!ELEMENT data EMPTY>\r") > 0);
|
|
2259 |
1
| assertEquals(-1, result.indexOf("<!ELEMENT data EMPTY>\r\n"));
|
|
2260 |
1
| assertEquals(-1, result.indexOf("<!ELEMENT root EMPTY>\r\n"));
|
|
2261 |
1
| assertEquals(-1, result.indexOf("<!ELEMENT data EMPTY>\r\r"));
|
|
2262 |
1
| assertEquals(-1, result.indexOf("<!ELEMENT root EMPTY>\r\r"));
|
|
2263 |
1
| assertEquals(-1, result.indexOf('\n'));
|
|
2264 |
| |
|
2265 |
| } |
|
2266 |
| |
|
2267 |
| |
|
2268 |
1
| public void testSerializeQuoteInAttributeValue()
|
|
2269 |
| throws IOException { |
|
2270 |
| |
|
2271 |
1
| Serializer serializer = new Serializer(out);
|
|
2272 |
1
| root.addAttribute(new Attribute("name", "\""));
|
|
2273 |
1
| serializer.write(doc);
|
|
2274 |
1
| String result = out.toString("UTF-8");
|
|
2275 |
1
| assertTrue(result.endsWith("<root name=\""\"/>\r\n"));
|
|
2276 |
| |
|
2277 |
| } |
|
2278 |
| |
|
2279 |
| |
|
2280 |
1
| public void testSerializeUnavailableCharacterInMarkup()
|
|
2281 |
| throws IOException { |
|
2282 |
| |
|
2283 |
1
| Serializer serializer = new Serializer(out, "ISO-8859-1");
|
|
2284 |
1
| Element root = new Element("\u0419");
|
|
2285 |
1
| Document doc = new Document(root);
|
|
2286 |
1
| try {
|
|
2287 |
1
| serializer.write(doc);
|
|
2288 |
0
| fail("Wrote bad character: " + out.toString("ISO-8859-1"));
|
|
2289 |
| } |
|
2290 |
| catch (UnavailableCharacterException success) { |
|
2291 |
1
| assertNotNull(success.getMessage());
|
|
2292 |
1
| assertEquals('\u0419', success.getCharacter());
|
|
2293 |
1
| assertEquals("ISO-8859-1", success.getEncoding());
|
|
2294 |
| } |
|
2295 |
| |
|
2296 |
| } |
|
2297 |
| |
|
2298 |
| |
|
2299 |
1
| public void testTurnLineFeedInAttributeValueIntoSpaceWhenIndenting()
|
|
2300 |
| throws IOException { |
|
2301 |
| |
|
2302 |
1
| Element root = new Element("a");
|
|
2303 |
1
| root.appendChild("c");
|
|
2304 |
1
| root.addAttribute(new Attribute("name", "value1\nvalue2"));
|
|
2305 |
1
| Document doc = new Document(root);
|
|
2306 |
1
| Serializer serializer = new Serializer(out);
|
|
2307 |
1
| serializer.setMaxLength(245);
|
|
2308 |
1
| serializer.setIndent(4);
|
|
2309 |
1
| serializer.write(doc);
|
|
2310 |
1
| serializer.flush();
|
|
2311 |
1
| out.close();
|
|
2312 |
1
| String result = new String(out.toByteArray(), "UTF-8");
|
|
2313 |
1
| assertEquals(
|
|
2314 |
| "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" |
|
2315 |
| + "<a name=\"value1 value2\">c</a>\r\n", |
|
2316 |
| result |
|
2317 |
| ); |
|
2318 |
| |
|
2319 |
| } |
|
2320 |
| |
|
2321 |
| |
|
2322 |
1
| public void testConflictBetweenMaxLengthAndIndent()
|
|
2323 |
| throws IOException { |
|
2324 |
| |
|
2325 |
1
| Element root = new Element("a");
|
|
2326 |
1
| Element b = new Element("b");
|
|
2327 |
1
| Element c = new Element("c");
|
|
2328 |
1
| Element d = new Element("d");
|
|
2329 |
1
| b.appendChild(c);
|
|
2330 |
1
| root.appendChild(b);
|
|
2331 |
1
| c.appendChild(d);
|
|
2332 |
1
| d.appendChild("data");
|
|
2333 |
1
| Document doc = new Document(root);
|
|
2334 |
1
| Serializer serializer = new Serializer(out);
|
|
2335 |
1
| serializer.setMaxLength(16);
|
|
2336 |
1
| serializer.setIndent(4);
|
|
2337 |
1
| serializer.write(doc);
|
|
2338 |
1
| serializer.flush();
|
|
2339 |
1
| out.close();
|
|
2340 |
1
| String result = new String(out.toByteArray(), "UTF-8");
|
|
2341 |
1
| assertEquals(
|
|
2342 |
| "<?xml version=\"1.0\"\r\nencoding=\"UTF-8\"?>\r\n" |
|
2343 |
| + "<a>\r\n <b>\r\n <c>\r\n <d>data</d>\r\n </c>\r\n </b>\r\n</a>\r\n", |
|
2344 |
| result |
|
2345 |
| ); |
|
2346 |
| |
|
2347 |
| } |
|
2348 |
| |
|
2349 |
| |
|
2350 |
1
| public void testWriteChild() throws IOException {
|
|
2351 |
| |
|
2352 |
1
| ExposingSerializer serializer = new ExposingSerializer(out, "UTF-8");
|
|
2353 |
1
| try {
|
|
2354 |
1
| serializer.writeChild(doc);
|
|
2355 |
0
| fail("writeChild wrote a document");
|
|
2356 |
| } |
|
2357 |
| catch (XMLException success) { |
|
2358 |
1
| assertNotNull(success.getMessage());
|
|
2359 |
| } |
|
2360 |
1
| try {
|
|
2361 |
1
| serializer.writeChild(new Attribute("name", "value"));
|
|
2362 |
0
| fail("writeChild wrote an attribute");
|
|
2363 |
| } |
|
2364 |
| catch (XMLException success) { |
|
2365 |
1
| assertNotNull(success.getMessage());
|
|
2366 |
| } |
|
2367 |
| |
|
2368 |
| } |
|
2369 |
| |
|
2370 |
| |
|
2371 |
| |
|
2372 |
| private static class ExposingSerializer extends Serializer { |
|
2373 |
| |
|
2374 |
4
| ExposingSerializer(OutputStream out, String encoding)
|
|
2375 |
| throws UnsupportedEncodingException { |
|
2376 |
4
| super(out, encoding);
|
|
2377 |
| } |
|
2378 |
| |
|
2379 |
2
| public void writeChild(Node node) throws IOException {
|
|
2380 |
2
| super.writeChild(node);
|
|
2381 |
| } |
|
2382 |
| |
|
2383 |
1
| public void exposedWriteRaw(String text) throws IOException {
|
|
2384 |
1
| writeRaw(text);
|
|
2385 |
| } |
|
2386 |
| |
|
2387 |
1
| public void exposedWriteEscaped(String text) throws IOException {
|
|
2388 |
1
| writeEscaped(text);
|
|
2389 |
| } |
|
2390 |
| |
|
2391 |
1
| public void exposedWriteAttributeValue(String text) throws IOException {
|
|
2392 |
1
| writeAttributeValue(text);
|
|
2393 |
| } |
|
2394 |
| |
|
2395 |
3
| public int exposeGetColumnNumber() {
|
|
2396 |
3
| return super.getColumnNumber();
|
|
2397 |
| } |
|
2398 |
| |
|
2399 |
| } |
|
2400 |
| |
|
2401 |
| |
|
2402 |
| private static class ElementSerializer extends Serializer { |
|
2403 |
| |
|
2404 |
2
| ElementSerializer(OutputStream out, String encoding)
|
|
2405 |
| throws UnsupportedEncodingException { |
|
2406 |
2
| super(out, encoding);
|
|
2407 |
| } |
|
2408 |
| |
|
2409 |
2
| protected void write(Element element) throws IOException {
|
|
2410 |
2
| super.write(element);
|
|
2411 |
| } |
|
2412 |
| |
|
2413 |
| } |
|
2414 |
| |
|
2415 |
| |
|
2416 |
| private static class TextSerializer extends Serializer { |
|
2417 |
| |
|
2418 |
3
| TextSerializer(OutputStream out) {
|
|
2419 |
3
| super(out);
|
|
2420 |
| } |
|
2421 |
| |
|
2422 |
3
| protected void write(Text text) throws IOException {
|
|
2423 |
3
| super.write(text);
|
|
2424 |
| } |
|
2425 |
| |
|
2426 |
| } |
|
2427 |
| |
|
2428 |
| |
|
2429 |
1
| public void testWriteParentlessTextNode() throws IOException {
|
|
2430 |
| |
|
2431 |
1
| Text t = new Text("Hello");
|
|
2432 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
2433 |
1
| TextSerializer serializer = new TextSerializer(out);
|
|
2434 |
1
| serializer.write(t);
|
|
2435 |
1
| serializer.flush();
|
|
2436 |
1
| byte[] result = out.toByteArray();
|
|
2437 |
1
| String s = new String(result, "UTF-8");
|
|
2438 |
1
| assertEquals(s, t.getValue());
|
|
2439 |
| |
|
2440 |
| } |
|
2441 |
| |
|
2442 |
| |
|
2443 |
1
| public void testWriteParentlessTextNodeWhileIndenting() throws IOException {
|
|
2444 |
| |
|
2445 |
1
| Text t = new Text("Hello");
|
|
2446 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
2447 |
1
| TextSerializer serializer = new TextSerializer(out);
|
|
2448 |
1
| serializer.setIndent(2);
|
|
2449 |
1
| serializer.write(t);
|
|
2450 |
1
| serializer.flush();
|
|
2451 |
1
| byte[] result = out.toByteArray();
|
|
2452 |
1
| String s = new String(result, "UTF-8");
|
|
2453 |
1
| assertEquals(s, t.getValue());
|
|
2454 |
| |
|
2455 |
| } |
|
2456 |
| |
|
2457 |
| |
|
2458 |
1
| public void testWriteWhiteSpaceOnlyTextNodeWhileIndenting() throws IOException {
|
|
2459 |
| |
|
2460 |
1
| Text t = new Text(" \r\n ");
|
|
2461 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
2462 |
1
| TextSerializer serializer = new TextSerializer(out);
|
|
2463 |
1
| serializer.setIndent(2);
|
|
2464 |
1
| serializer.write(t);
|
|
2465 |
1
| serializer.flush();
|
|
2466 |
1
| byte[] result = out.toByteArray();
|
|
2467 |
1
| assertEquals(0, result.length);
|
|
2468 |
| |
|
2469 |
| } |
|
2470 |
| |
|
2471 |
| |
|
2472 |
1
| public void testWriteRaw() throws IOException {
|
|
2473 |
| |
|
2474 |
1
| ExposingSerializer serializer = new ExposingSerializer(out, "UTF-8");
|
|
2475 |
1
| serializer.exposedWriteRaw("<>&\"'");
|
|
2476 |
1
| assertEquals(5, serializer.exposeGetColumnNumber());
|
|
2477 |
1
| serializer.flush();
|
|
2478 |
1
| String result = out.toString("UTF-8");
|
|
2479 |
1
| assertEquals("<>&\"'", result);
|
|
2480 |
| |
|
2481 |
| } |
|
2482 |
| |
|
2483 |
| |
|
2484 |
1
| public void testWriteParentlessElementInANamespace() throws IOException {
|
|
2485 |
| |
|
2486 |
1
| ElementSerializer serializer = new ElementSerializer(out, "UTF-8");
|
|
2487 |
1
| Element element = new Element("a", "http://www.example.org");
|
|
2488 |
1
| serializer.write(element);
|
|
2489 |
1
| serializer.flush();
|
|
2490 |
1
| String result = out.toString("UTF-8");
|
|
2491 |
1
| assertEquals("<a xmlns=\"http://www.example.org\"/>", result);
|
|
2492 |
| |
|
2493 |
| } |
|
2494 |
| |
|
2495 |
| |
|
2496 |
1
| public void testWriteParentedElementInANamespace() throws IOException {
|
|
2497 |
| |
|
2498 |
1
| ElementSerializer serializer = new ElementSerializer(out, "UTF-8");
|
|
2499 |
1
| Element a = new Element("a", "http://www.example.org");
|
|
2500 |
1
| Element b = new Element("b", "http://www.example.org");
|
|
2501 |
1
| b.appendChild(a);
|
|
2502 |
1
| serializer.write(a);
|
|
2503 |
1
| serializer.flush();
|
|
2504 |
1
| String result = out.toString("UTF-8");
|
|
2505 |
1
| assertEquals("<a xmlns=\"http://www.example.org\"/>", result);
|
|
2506 |
| |
|
2507 |
| } |
|
2508 |
| |
|
2509 |
| |
|
2510 |
1
| public void testWriteEscaped() throws IOException {
|
|
2511 |
| |
|
2512 |
1
| ExposingSerializer serializer = new ExposingSerializer(out, "UTF-8");
|
|
2513 |
1
| serializer.exposedWriteEscaped("<>&\"'");
|
|
2514 |
1
| assertEquals(15, serializer.exposeGetColumnNumber());
|
|
2515 |
1
| serializer.flush();
|
|
2516 |
1
| byte[] data = out.toByteArray();
|
|
2517 |
1
| String result = new String(data, "UTF-8");
|
|
2518 |
1
| assertEquals("<>&\"'", result);
|
|
2519 |
| |
|
2520 |
| } |
|
2521 |
| |
|
2522 |
| |
|
2523 |
1
| public void testWriteAttributeValue() throws IOException {
|
|
2524 |
| |
|
2525 |
1
| ExposingSerializer serializer = new ExposingSerializer(out, "UTF-8");
|
|
2526 |
1
| serializer.exposedWriteAttributeValue("<>&\"'");
|
|
2527 |
1
| assertEquals(20, serializer.exposeGetColumnNumber());
|
|
2528 |
1
| serializer.flush();
|
|
2529 |
1
| String result = out.toString("UTF-8");
|
|
2530 |
1
| assertEquals("<>&"'", result);
|
|
2531 |
| |
|
2532 |
| } |
|
2533 |
| |
|
2534 |
| |
|
2535 |
| |
|
2536 |
| |
|
2537 |
1
| public void testElementsThatOnlyContainsAnEmptyTextNodeShouldBeOutputWithAnEmptyElementTag()
|
|
2538 |
| throws IOException { |
|
2539 |
| |
|
2540 |
1
| Element child = new Element("b");
|
|
2541 |
1
| child.appendChild("");
|
|
2542 |
1
| root.appendChild(child);
|
|
2543 |
| |
|
2544 |
1
| Serializer serializer = new Serializer(out);
|
|
2545 |
1
| serializer.write(doc);
|
|
2546 |
| |
|
2547 |
1
| serializer.flush();
|
|
2548 |
1
| String result = out.toString("UTF-8");
|
|
2549 |
1
| assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" +
|
|
2550 |
| "<root><b/></root>\r\n", result); |
|
2551 |
| |
|
2552 |
| } |
|
2553 |
| |
|
2554 |
| |
|
2555 |
| |
|
2556 |
| |
|
2557 |
1
| public void testElementsThatOnlyContainEmptyTextNodesShouldBeOutputWithAnEmptyElementTag()
|
|
2558 |
| throws IOException { |
|
2559 |
| |
|
2560 |
1
| Element root = new Element("a");
|
|
2561 |
1
| Element child = new Element("b");
|
|
2562 |
1
| child.appendChild("");
|
|
2563 |
1
| child.appendChild("");
|
|
2564 |
1
| child.appendChild("");
|
|
2565 |
1
| root.appendChild(child);
|
|
2566 |
| |
|
2567 |
1
| Document doc = new Document(root);
|
|
2568 |
| |
|
2569 |
1
| Serializer serializer = new Serializer(out);
|
|
2570 |
1
| serializer.write(doc);
|
|
2571 |
| |
|
2572 |
1
| serializer.flush();
|
|
2573 |
1
| String result = out.toString("UTF-8");
|
|
2574 |
1
| assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" +
|
|
2575 |
| "<a><b/></a>\r\n", result); |
|
2576 |
| |
|
2577 |
| } |
|
2578 |
| |
|
2579 |
| |
|
2580 |
| |
|
2581 |
| |
|
2582 |
1
| public void testElementsThatOnlyContainASingleSpaceShouldNotBeSplitWhileIndenting()
|
|
2583 |
| throws IOException { |
|
2584 |
| |
|
2585 |
1
| Element root = new Element("a");
|
|
2586 |
1
| Element child = new Element("b");
|
|
2587 |
1
| child.appendChild(" ");
|
|
2588 |
1
| root.appendChild(child);
|
|
2589 |
| |
|
2590 |
1
| Document doc = new Document(root);
|
|
2591 |
| |
|
2592 |
1
| Serializer serializer = new Serializer(out);
|
|
2593 |
1
| serializer.setIndent(4);
|
|
2594 |
1
| serializer.write(doc);
|
|
2595 |
| |
|
2596 |
1
| serializer.flush();
|
|
2597 |
1
| String result = out.toString("UTF-8");
|
|
2598 |
1
| assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" +
|
|
2599 |
| "<a>\r\n <b> </b>\r\n</a>\r\n", result); |
|
2600 |
| |
|
2601 |
| } |
|
2602 |
| |
|
2603 |
| |
|
2604 |
1
| public void testElementsThatOnlyContainTextNodesWithBoundaryWhiteSpaceShouldNotBeSplitWhileIndenting()
|
|
2605 |
| throws IOException { |
|
2606 |
| |
|
2607 |
1
| Element root = new Element("a");
|
|
2608 |
1
| Element child = new Element("b");
|
|
2609 |
1
| child.appendChild(" ");
|
|
2610 |
1
| child.appendChild(" ");
|
|
2611 |
1
| root.appendChild(child);
|
|
2612 |
| |
|
2613 |
1
| Document doc = new Document(root);
|
|
2614 |
| |
|
2615 |
1
| Serializer serializer = new Serializer(out);
|
|
2616 |
1
| serializer.setIndent(4);
|
|
2617 |
1
| serializer.write(doc);
|
|
2618 |
| |
|
2619 |
1
| serializer.flush();
|
|
2620 |
1
| String result = out.toString("UTF-8");
|
|
2621 |
1
| assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" +
|
|
2622 |
| "<a>\r\n <b> </b>\r\n</a>\r\n", result); |
|
2623 |
| |
|
2624 |
| } |
|
2625 |
| |
|
2626 |
| |
|
2627 |
1
| public void testElementsThatOnlyContainASingleLinefeedShouldNotBeSplitWhileIndenting()
|
|
2628 |
| throws IOException { |
|
2629 |
| |
|
2630 |
1
| Element root = new Element("a");
|
|
2631 |
1
| Element child = new Element("b");
|
|
2632 |
1
| child.appendChild("\n");
|
|
2633 |
1
| root.appendChild(child);
|
|
2634 |
| |
|
2635 |
1
| Document doc = new Document(root);
|
|
2636 |
| |
|
2637 |
1
| Serializer serializer = new Serializer(out);
|
|
2638 |
1
| serializer.setIndent(4);
|
|
2639 |
1
| serializer.write(doc);
|
|
2640 |
| |
|
2641 |
1
| serializer.flush();
|
|
2642 |
1
| String result = out.toString("UTF-8");
|
|
2643 |
1
| assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" +
|
|
2644 |
| "<a>\r\n <b> </b>\r\n</a>\r\n", result); |
|
2645 |
| |
|
2646 |
| } |
|
2647 |
| |
|
2648 |
| |
|
2649 |
1
| public void testEndTagsOfElementsWithContentGoOnSeparateLine()
|
|
2650 |
| throws ParsingException, IOException { |
|
2651 |
| |
|
2652 |
1
| Serializer serializer = new Serializer(out, "ISO-8859-1");
|
|
2653 |
1
| serializer.setIndent(4);
|
|
2654 |
1
| serializer.setPreserveBaseURI(true);
|
|
2655 |
1
| serializer.flush();
|
|
2656 |
| |
|
2657 |
1
| File f = new File("data");
|
|
2658 |
1
| f = new File(f, "prettyxml.xml");
|
|
2659 |
1
| Builder builder = new Builder();
|
|
2660 |
1
| Document doc = builder.build(f);
|
|
2661 |
1
| serializer.write(doc);
|
|
2662 |
1
| String result = out.toString("UTF-8");
|
|
2663 |
1
| assertTrue(result.endsWith("\r\n</html>\r\n"));
|
|
2664 |
| |
|
2665 |
| } |
|
2666 |
| |
|
2667 |
| |
|
2668 |
1
| public void testDontDoubleBreak()
|
|
2669 |
| throws ParsingException, IOException { |
|
2670 |
| |
|
2671 |
1
| Serializer serializer = new Serializer(out, "ISO-8859-1");
|
|
2672 |
1
| serializer.setIndent(4);
|
|
2673 |
1
| serializer.setMaxLength(64);
|
|
2674 |
| |
|
2675 |
1
| File f = new File("data");
|
|
2676 |
1
| f = new File(f, "prettytest.xml");
|
|
2677 |
1
| Builder builder = new Builder();
|
|
2678 |
1
| Document doc = builder.build(f);
|
|
2679 |
1
| serializer.write(doc);
|
|
2680 |
1
| String result = out.toString("UTF-8");
|
|
2681 |
1
| assertEquals("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\r\n" +
|
|
2682 |
| "<html a=\"AReallyLongNameWithNoOpportunitiesToBreakToPutUsPastTheMaxLineLengthAndForceABreak\">\r\n" + |
|
2683 |
| " <head> </head>\r\n" + |
|
2684 |
| "</html>\r\n", result); |
|
2685 |
| |
|
2686 |
| } |
|
2687 |
| |
|
2688 |
| |
|
2689 |
1
| public void testUnicodeNFCTestSuite()
|
|
2690 |
| throws ParsingException, IOException { |
|
2691 |
| |
|
2692 |
1
| Builder builder = new Builder();
|
|
2693 |
1
| Document doc = builder.build("data/nfctests.xml");
|
|
2694 |
1
| Elements tests = doc.getRootElement().getChildElements("test");
|
|
2695 |
1
| int size = tests.size();
|
|
2696 |
1
| for (int i = 0; i < size; i++) {
|
|
2697 |
| |
|
2698 |
17123
| Element test = tests.get(i);
|
|
2699 |
17123
| test.detach();
|
|
2700 |
17123
| Document testdoc = new Document(test);
|
|
2701 |
17123
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
2702 |
17123
| Serializer serializer = new Serializer(out);
|
|
2703 |
17123
| serializer.setUnicodeNormalizationFormC(true);
|
|
2704 |
17123
| serializer.write(testdoc);
|
|
2705 |
17123
| serializer.flush();
|
|
2706 |
17123
| String result = new String(out.toByteArray(), "UTF-8");
|
|
2707 |
17123
| Document resultDoc = builder.build(result, null);
|
|
2708 |
17123
| Element root = resultDoc.getRootElement();
|
|
2709 |
17123
| String c1 = root.getAttributeValue("c1");
|
|
2710 |
17123
| String c2 = root.getAttributeValue("c2");
|
|
2711 |
17123
| String c3 = root.getAttributeValue("c3");
|
|
2712 |
17123
| String c4 = root.getAttributeValue("c4");
|
|
2713 |
17123
| String c5 = root.getAttributeValue("c5");
|
|
2714 |
| |
|
2715 |
| |
|
2716 |
| |
|
2717 |
| |
|
2718 |
| |
|
2719 |
| |
|
2720 |
17123
| assertEquals(root.getValue(), c1, c2);
|
|
2721 |
17123
| assertEquals(c2, c3);
|
|
2722 |
| |
|
2723 |
| |
|
2724 |
| |
|
2725 |
| |
|
2726 |
| |
|
2727 |
17123
| assertEquals(c4, c5);
|
|
2728 |
| |
|
2729 |
| |
|
2730 |
| |
|
2731 |
| } |
|
2732 |
| |
|
2733 |
| } |
|
2734 |
| |
|
2735 |
| } |