1
/* Copyright 2002-2005 Elliotte Rusty Harold
3
This library is free software; you can redistribute it and/or modify
4
it under the terms of version 2.1 of the GNU Lesser General Public
5
License as published by the Free Software Foundation.
7
This library is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU Lesser General Public License for more details.
12
You should have received a copy of the GNU Lesser General Public
13
License along with this library; if not, write to the
14
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
15
Boston, MA 02111-1307 USA
17
You can contact Elliotte Rusty Harold by sending e-mail to
18
elharo@metalab.unc.edu. Please include the word "XOM" in the
19
subject line. The XOM home page is located at http://www.xom.nu/
24
import nu.xom.Elements;
25
import nu.xom.Namespace;
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;
37
import nu.xom.UnavailableCharacterException;
38
import nu.xom.ValidityException;
39
import nu.xom.XMLException;
41
import java.io.ByteArrayInputStream;
42
import java.io.ByteArrayOutputStream;
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;
53
* Tests for <code>Serializer</code> functionality.
56
* @author Elliotte Rusty Harold
60
public class SerializerTest extends XOMTestCase {
63
private Builder parser;
64
private final static double version = Double.parseDouble(
65
System.getProperty("java.version").substring(0,3)
67
Element root = new Element("root");
68
Document doc = new Document(root);
69
ByteArrayOutputStream out = new ByteArrayOutputStream();
72
public SerializerTest(String name) {
77
protected void setUp() {
78
parser = new Builder();
82
public void testNFC88592() throws IOException, ParsingException {
84
String data = "Ā ÄĖÅĀ¤Ä½ÅĀ§ĀØÅ ÅŤŹĀŽŻĀ°Ä
ĖÅĀ“ľÅĖĀøÅ”ÅÅ„ÅŗĖžżÅĆĆÄĆĹÄĆÄĆÄĆÄĆĆÄÄÅÅĆĆÅĆĆÅÅ®ĆÅ°ĆĆÅ¢ĆÅĆ”Ć¢ÄƤÄŗÄƧÄĆ©ÄĆ«ÄĆĆ®ÄÄÅÅĆ³Ć“ÅƶƷÅÅÆĆŗÅ±Ć¼Ć½Å£Ė";
85
Element root = new Element("a");
86
root.appendChild(data);
87
Document doc = new Document(root);
89
// try with 8859-2 encoding on serializer
90
Serializer serializer = new Serializer(out);
91
serializer.setUnicodeNormalizationFormC(true);
92
serializer.write(doc);
95
byte[] temp = out.toByteArray();
96
Document roundTrip = parser.build(new ByteArrayInputStream(temp));
98
assertEquals(data, roundTrip.getValue());
103
public void testMultipleCombiningCharactersWithNFC()
104
throws ParsingException, IOException {
106
// LATIN CAPITAL LETTER D WITH DOT BELOW, COMBINING DOT ABOVE
107
String input = "<a>Ḍ̇</a>";
108
// LATIN CAPITAL LETTER D WITH DOT ABOVE, COMBINING DOT BELOW
109
String output = "<?xml version=\"1.0\" encoding=\"US-ASCII\"?>\r\n"
110
+ "<a>Ḍ̇</a>\r\n";
112
Document doc = parser.build(input, null);
113
Serializer serializer = new Serializer(out, "US-ASCII");
114
serializer.setUnicodeNormalizationFormC(true);
115
serializer.write(doc);
117
String result = out.toString("US-ASCII");
118
assertEquals(output, result);
123
public void testMultipleCombiningCharactersWithDifferentCombiningClassesNFC()
124
throws ParsingException, IOException {
126
// LATIN SMALL LETTER A, COMBINING GRAVE ACCENT, HEBREW ACCENT ZINOR,
127
// MUSICAL SYMBOL COMBINING AUGMENTATION DOT, HEBREW ACCENT ZINOR,
128
// LATIN SMALL LETTER B
129
String input = "<a>à𝅭֮֮b</a>";
130
// There was a bug where 1D16D was not listed as a combining character
131
String output = "<?xml version=\"1.0\" encoding=\"US-ASCII\"?>\r\n"
132
+ "<a>à𝅭֮֮b</a>\r\n";
134
Document doc = parser.build(input, null);
135
Serializer serializer = new Serializer(out, "US-ASCII");
136
serializer.setUnicodeNormalizationFormC(true);
137
serializer.write(doc);
139
String result = out.toString("US-ASCII");
140
assertEquals(output, result);
145
public void testEWithCombiningMacron()
146
throws ParsingException, IOException {
148
// LATIN CAPITAL LETTER E WITH COMBINING MACRON
149
String input = "<a>Ē</a>";
150
String output = "<?xml version=\"1.0\" encoding=\"US-ASCII\"?>\r\n<a>Ē</a>\r\n";
152
Document doc = parser.build(input, null);
153
Serializer serializer = new Serializer(out, "US-ASCII");
154
serializer.setUnicodeNormalizationFormC(true);
155
serializer.write(doc);
157
String result = out.toString("US-ASCII");
158
assertEquals(output, result);
163
public void testEWithCombiningMacronAndGrave()
164
throws ParsingException, IOException {
166
// LATIN CAPITAL LETTER E WITH COMBINING MACRON and grave
167
String input = "<a>Ḕ</a>";
168
String output = "<?xml version=\"1.0\" encoding=\"US-ASCII\"?>\r\n<a>Ḕ</a>\r\n";
170
Document doc = parser.build(input, null);
171
Serializer serializer = new Serializer(out, "US-ASCII");
172
serializer.setUnicodeNormalizationFormC(true);
173
serializer.write(doc);
175
String result = out.toString("US-ASCII");
176
assertEquals(output, result);
181
public void testParenthesizedOjeon()
182
throws ParsingException, IOException {
184
// (Initial consonant Medial Vowel Initial consonant Medial Vowel Final Consonant)
185
String input = "<a>(오전)</a>";
186
String output = "<?xml version=\"1.0\" encoding=\"US-ASCII\"?>\r\n<a>(오전)</a>\r\n";
188
Document doc = parser.build(input, null);
189
Serializer serializer = new Serializer(out, "US-ASCII");
190
serializer.setUnicodeNormalizationFormC(true);
191
serializer.write(doc);
193
String result = out.toString("US-ASCII");
194
assertEquals(output, result);
199
public void testNonParenthesizedOjeon()
200
throws ParsingException, IOException {
202
// Initial consonant Medial Vowel Initial consonant Medial Vowel Final Consonant
203
String input = "<a>오전</a>";
204
String output = "<?xml version=\"1.0\" encoding=\"US-ASCII\"?>\r\n<a>오전</a>\r\n";
206
Document doc = parser.build(input, null);
207
Serializer serializer = new Serializer(out, "US-ASCII");
208
serializer.setUnicodeNormalizationFormC(true);
209
serializer.write(doc);
211
String result = out.toString("US-ASCII");
212
assertEquals(output, result);
217
public void testOjeon()
218
throws ParsingException, IOException {
220
// (Initial consonant Medial Vowel Initial consonant Medial Vowel Final Consonant)
221
String input = "<a>전</a>";
222
String output = "<?xml version=\"1.0\" encoding=\"US-ASCII\"?>\r\n<a>전</a>\r\n";
224
Document doc = parser.build(input, null);
225
Serializer serializer = new Serializer(out, "US-ASCII");
226
serializer.setUnicodeNormalizationFormC(true);
227
serializer.write(doc);
229
String result = out.toString("US-ASCII");
230
assertEquals(output, result);
235
public void testKannadaVowelSignOO()
236
throws ParsingException, IOException {
238
String input = "<a>ೋ</a>";
239
String output = "<?xml version=\"1.0\" encoding=\"US-ASCII\"?>\r\n<a>ೋ</a>\r\n";
241
Document doc = parser.build(input, null);
242
Serializer serializer = new Serializer(out, "US-ASCII");
243
serializer.setUnicodeNormalizationFormC(true);
244
serializer.write(doc);
246
String result = out.toString("US-ASCII");
247
assertEquals(output, result);
252
public void testCDATASectionEndDelimiter() throws IOException {
254
root.appendChild("]]>");
255
Serializer serializer = new Serializer(out, "UTF-8");
256
serializer.setMaxLength(20);
257
serializer.write(doc);
258
String result = out.toString("UTF-8");
259
assertTrue(result.indexOf("]]>") > 0);
264
public void testXMLSpacePreserve() throws IOException {
269
"http://www.w3.org/XML/1998/namespace",
272
"This is a long sentence with plenty of opportunities for " +
273
"breaking from beginning to end.";
274
root.appendChild(value);
275
Serializer serializer = new Serializer(out, "UTF-8");
276
serializer.setMaxLength(20);
277
serializer.write(doc);
278
String result = out.toString("UTF-8");
279
assertTrue(result.indexOf(value) > 0);
286
* Check that the UTF-16LE encoding omits the byte-order mark.
289
* @throws IOException
291
public void testUTF16LEBOM() throws IOException {
293
if (version >= 1.3) {
294
// UTF-16LE only supported in 1.3 and later
295
Serializer serializer = new Serializer(out, "UTF-16LE");
296
serializer.write(doc);
300
byte[] data = out.toByteArray();
301
assertEquals('<', (char) data[0]);
302
assertEquals((byte) 0, data[1]);
310
* Check that the UTF-16 encoding outputs a byte-order mark.
313
* @throws IOException
315
public void testUTF16BOM() throws IOException {
317
Serializer serializer = new Serializer(out, "UTF-16");
318
serializer.write(doc);
322
byte[] data = out.toByteArray();
323
assertEquals((byte) 0xFE, data[0]);
324
assertEquals((byte) 0xFF, data[1]);
325
assertEquals((byte) 0, data[2]);
326
assertEquals('<', (char) data[3]);
333
* Check that the UTF-16BE encoding omits the byte-order mark.
336
* @throws IOException
338
public void testUTF16BEBOM() throws IOException {
340
if (version >= 1.3) {
341
// UTF-16LE only supported in 1.3 and later
342
Serializer serializer = new Serializer(out, "UTF-16BE");
343
serializer.write(doc);
347
byte[] data = out.toByteArray();
348
assertEquals((byte) 0, data[0]);
349
assertEquals('<', (char) data[1]);
355
public void testXMLSpaceDefault() throws IOException {
360
"http://www.w3.org/XML/1998/namespace",
362
Element child1 = new Element("preserve");
364
"This is a long sentence with plenty of opportunities for " +
365
"breaking from beginning to end.";
366
child1.appendChild(value);
367
Element child2 = new Element("default");
368
root.appendChild(child1);
369
root.appendChild(child2);
373
"http://www.w3.org/XML/1998/namespace",
376
"This is another very long sentence with plenty" +
377
" of opportunities for breaking from beginning to end.";
378
child2.appendChild(value2);
381
"This is still another very long sentence with plenty of " +
382
"opportunities for breaking from beginning to end.";
383
Element preserveAgain = new Element("test");
384
preserveAgain.appendChild(value3);
385
child2.appendChild(preserveAgain);
386
preserveAgain.addAttribute(
389
"http://www.w3.org/XML/1998/namespace",
393
Serializer serializer = new Serializer(out, "UTF-8");
394
serializer.setMaxLength(20);
395
serializer.write(doc);
396
String result = out.toString("UTF-8");
397
assertTrue(result.indexOf(value) > 0);
398
assertTrue(result.indexOf(value3) > 0);
399
assertEquals(-1, result.indexOf(value2));
404
public void testXMLSpacePreserveWithIndenting()
410
"http://www.w3.org/XML/1998/namespace",
412
root.appendChild(new Element("sameline"));
413
ByteArrayOutputStream out = new ByteArrayOutputStream();
414
Serializer serializer = new Serializer(out, "UTF-8");
415
serializer.setIndent(4);
416
serializer.write(doc);
417
String result = out.toString("UTF-8");
418
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
419
+ "<root xml:space=\"preserve\"><sameline/></root>\r\n",
425
public void testXMLSpaceUnspecifiedValueWithIndenting()
431
"http://www.w3.org/XML/1998/namespace",
433
root.appendChild(new Element("sameline"));
434
Serializer serializer = new Serializer(out, "UTF-8");
435
serializer.setIndent(4);
436
serializer.write(doc);
437
String result = out.toString("UTF-8");
438
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
439
+ "<root xml:space=\"undefined\">\r\n <sameline/>\r\n</root>\r\n",
445
public void testXMLSpaceDefaultWithIndenting() throws IOException {
450
"http://www.w3.org/XML/1998/namespace",
452
Element child = new Element("child");
456
"http://www.w3.org/XML/1998/namespace",
458
root.appendChild(child);
459
Serializer serializer = new Serializer(out, "UTF-8");
460
serializer.setIndent(4);
461
serializer.write(doc);
462
String result = out.toString("UTF-8");
463
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
464
+ "<root xml:space=\"preserve\">" +
465
"<child xml:space=\"default\"/></root>\r\n",
471
public void testXMLSpaceDefaultWithIndentingAndGrandchildren()
477
"http://www.w3.org/XML/1998/namespace",
479
Element child = new Element("child");
483
"http://www.w3.org/XML/1998/namespace",
485
root.appendChild(child);
486
child.appendChild(new Element("differentLine"));
487
Serializer serializer = new Serializer(out, "UTF-8");
488
serializer.setIndent(2);
489
serializer.write(doc);
490
String result = out.toString("UTF-8");
491
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",
500
public void testDontSerializeXMLNamespace() throws IOException {
503
= new Element("html", "http://www.w3.org/1999/xhtml");
506
"xml:lang", "http://www.w3.org/XML/1998/namespace", "en"));
507
Document doc = new Document(root);
508
Serializer serializer = new Serializer(out, "UTF-8");
509
serializer.write(doc);
510
String result = out.toString("UTF-8");
511
assertEquals(-1, result.indexOf("xmlns:xml"));
512
assertTrue(result.indexOf("xml:lang=") > 1);
516
public void testDontSerializeNoNamespace() throws IOException {
518
Serializer serializer = new Serializer(out, "UTF-8");
519
serializer.write(doc);
520
String result = out.toString("UTF-8");
521
assertEquals(-1, result.indexOf("xmlns="));
526
public void testDefaultNamespace() throws IOException {
528
Element root = new Element("root", "http://www.example.com");
529
Document doc = new Document(root);
530
Serializer serializer = new Serializer(out, "UTF-8");
531
serializer.write(doc);
532
String result = out.toString("UTF-8");
533
assertTrue(result.indexOf("xmlns=") > 1);
534
assertTrue(result.indexOf("http://www.example.com") > 1);
539
public void testEmptyElement() throws IOException {
541
Serializer serializer = new Serializer(out, "UTF-8");
542
serializer.write(doc);
543
String result = out.toString("UTF-8");
545
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<root/>\r\n",
552
public void testElementWithText() throws IOException {
554
String data = " test \n\n \n \n hello again";
555
root.appendChild(data);
556
Serializer serializer = new Serializer(out, "UTF-8");
557
serializer.write(doc);
558
String result = out.toString("UTF-8");
561
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<root>"
562
+ data + "</root>\r\n",
568
public void testStaticElementWithText()
571
String data = " test \n\n \n \n hello again";
572
root.appendChild(data);
573
Serializer serializer = new Serializer(out);
574
serializer.write(doc);
575
String result = out.toString("UTF-8");
578
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<root>"
579
+ data + "</root>\r\n",
585
public void testElementWithTextAndCarriageReturns()
588
String data = " test \r\n \n \r hello again";
589
root.appendChild(data);
590
Serializer serializer = new Serializer(out, "UTF-8");
591
serializer.write(doc);
592
String result = out.toString("UTF-8");
595
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<root>"
596
+ " test 
\n \n 
 hello again"
603
private void serializeParseAndCompare(Document doc)
604
throws IOException, ParsingException {
606
ByteArrayOutputStream out = new ByteArrayOutputStream();
607
Serializer serializer = new Serializer(out, "UTF-8");
608
serializer.write(doc);
609
String result = out.toString("UTF-8");
611
Document resultDoc = parser.build(result, null);
612
XOMTestCase.assertEquals(doc, resultDoc);
614
setOutputStreamSerializeParseAndCompare(doc);
619
private void setOutputStreamSerializeParseAndCompare(Document doc)
620
throws IOException, ParsingException {
622
Serializer serializer = new Serializer(out);
623
serializer.write(doc);
624
ByteArrayOutputStream out2 = new ByteArrayOutputStream();
625
serializer.setOutputStream(out2);
626
serializer.write(doc);
627
String result = out2.toString("UTF-8");
629
Document resultDoc = parser.build(result, null);
630
XOMTestCase.assertEquals(doc, resultDoc);
635
public void testComment()
636
throws IOException, ParsingException {
638
String data = " <>&&entity; test \n hello again";
639
root.appendChild(new Comment(data));
640
serializeParseAndCompare(doc);
645
public void testProcessingInstruction()
646
throws IOException, ParsingException {
648
String data = "<>&&entity; test \n hello again";
649
root.appendChild(new ProcessingInstruction("target", data));
650
serializeParseAndCompare(doc);
654
public void testBasicElementWithText()
655
throws IOException, ParsingException {
657
String data = " test \n hello again";
658
root.appendChild(data);
659
serializeParseAndCompare(doc);
664
public void testAttributes()
665
throws IOException, ParsingException {
667
root.addAttribute(new Attribute("test", "sadlkhasdk"));
668
String data = " test \n hello again";
669
root.appendChild(data);
670
serializeParseAndCompare(doc);
672
root.addAttribute(new Attribute("test2", "sadlkhasdk"));
673
serializeParseAndCompare(doc);
675
root.addAttribute(new Attribute("test3", " s adl khasdk "));
676
serializeParseAndCompare(doc);
678
root.addAttribute(new Attribute("xlink:type",
679
"http://www.w3.org/2001/xlink", " s adl khasdk "));
680
serializeParseAndCompare(doc);
685
public void testChildElements()
686
throws IOException, ParsingException {
688
serializeParseAndCompare(doc);
690
Element child1 = new Element("child");
691
Element child2 = new Element("child");
692
Element child3 = new Element("child");
693
serializeParseAndCompare(doc);
694
root.appendChild(child1);
695
serializeParseAndCompare(doc);
696
child1.appendChild(child2);
697
serializeParseAndCompare(doc);
698
root.appendChild(child3);
699
serializeParseAndCompare(doc);
700
child3.appendChild("some data");
701
serializeParseAndCompare(doc);
702
child2.appendChild("\nsome data with \n line breaks\n");
703
serializeParseAndCompare(doc);
704
root.insertChild("now let's have some mixed content", 0);
705
serializeParseAndCompare(doc);
707
root.setNamespaceURI("http://www.example.org/");
708
serializeParseAndCompare(doc);
709
child1.setNamespaceURI("http://www.example.org/");
710
serializeParseAndCompare(doc);
711
child2.setNamespaceURI("http://www.example.org/");
712
serializeParseAndCompare(doc);
713
child3.setNamespaceURI("http://www.example.org/");
714
serializeParseAndCompare(doc);
715
child1.setNamespacePrefix("example");
716
serializeParseAndCompare(doc);
717
child2.setNamespacePrefix("perverse");
718
serializeParseAndCompare(doc);
723
public void testPrologAndEpilog()
724
throws IOException, ParsingException {
726
serializeParseAndCompare(doc);
728
doc.insertChild(new Comment("Hello"), 0);
729
serializeParseAndCompare(doc);
730
doc.insertChild(new DocType("root"), 0);
731
serializeParseAndCompare(doc);
732
doc.insertChild(new ProcessingInstruction("test", "some data"),
734
serializeParseAndCompare(doc);
735
doc.insertChild(new Comment("Goodbye"), 0);
736
serializeParseAndCompare(doc);
738
new ProcessingInstruction("goodbye", "some data"), 0);
739
serializeParseAndCompare(doc);
740
doc.appendChild(new Comment("Hello"));
741
serializeParseAndCompare(doc);
743
new ProcessingInstruction("test", "some data"));
744
serializeParseAndCompare(doc);
749
public void testChangeLineSeparator() throws IOException {
752
"This\nstring\rcontains\r\nseveral\r\rweird line breaks.";
753
root.appendChild(breaks);
754
root.addAttribute(new Attribute("test", breaks));
756
Serializer serializer = new Serializer(out, "UTF-8");
757
serializer.setLineSeparator("\n");
758
serializer.write(doc);
759
String result = out.toString("UTF-8");
760
assertTrue(result.indexOf('\n') > 0);
761
assertTrue(result + "**\n" + result.indexOf('\r'),
762
result.indexOf('\r') == -1);
764
out = new ByteArrayOutputStream();
765
serializer = new Serializer(out, "UTF-8");
766
serializer.setLineSeparator("\r");
767
serializer.write(doc);
768
result = out.toString("UTF-8");
769
assertTrue(result.indexOf('\r') > 0);
770
assertTrue(result.indexOf('\n') == -1);
772
out = new ByteArrayOutputStream();
773
serializer = new Serializer(out, "UTF-8");
774
serializer.setLineSeparator("\r\n");
775
serializer.write(doc);
776
result = out.toString("UTF-8");
777
assertTrue(result.indexOf("\r\n") > 0);
778
for (int i = 0; i < result.length(); i++) {
779
int c = result.charAt(i);
780
if (c == '\r') assertTrue(result.charAt(i+1) == '\n');
786
public void testSettingOutputStreamDoesNotSetLineSeparator()
789
root.appendChild("This\rstring");
790
Serializer serializer = new Serializer(System.out, "UTF-8");
792
serializer.setOutputStream(out);
794
serializer.write(doc);
795
String result = out.toString("UTF-8");
796
assertTrue(result.indexOf("This
string") > 0);
801
public void testSettingOutputStreamDoesNotUnnecessarilyEscapeLineBreaks()
804
root.appendChild("This\rstring");
805
Serializer serializer = new Serializer(System.out, "UTF-8");
807
serializer.setOutputStream(out);
808
serializer.setLineSeparator("\r\n");
809
serializer.write(doc);
810
String result = out.toString("UTF-8");
811
assertTrue(result.indexOf("This\r\nstring") > 0);
816
public void testDontChangeLineSeparator() throws IOException {
819
= "This\nstring\rcontains\r\rseveral\n\nweird line breaks.";
820
String breaksHalfEscaped
821
= "This\nstring
contains

several" +
822
"\n\nweird line breaks.";
824
= "This
string
contains

several" +
825
"

weird line breaks.";
826
root.appendChild(breaks);
828
Serializer serializer = new Serializer(out, "UTF-8");
829
serializer.write(doc);
830
String result = out.toString("UTF-8");
831
assertTrue(result.indexOf(breaksHalfEscaped) > 0);
833
root = new Element("root");
834
doc = new Document(root);
835
root.addAttribute(new Attribute("test", breaks));
837
out = new ByteArrayOutputStream();
838
serializer = new Serializer(out, "UTF-8");
839
serializer.write(doc);
840
result = out.toString("UTF-8");
841
assertTrue(result.indexOf(breaksEscaped) > 0);
846
public void testPreserveBaseURI() throws IOException {
848
Serializer serializer = new Serializer(out, "UTF-8");
849
serializer.setPreserveBaseURI(true);
850
serializer.write(doc);
851
String result = out.toString("UTF-8");
852
assertTrue(result.indexOf("<root") > 1);
853
doc.setBaseURI("http://www.example.com/index.xml");
854
serializer.write(doc);
855
result = out.toString("UTF-8");
856
assertTrue(result.indexOf("<root ") > 1);
857
assertTrue(result.indexOf("xml:base=") > 1);
859
result.indexOf("http://www.example.com/index.xml") > 1
865
public void testPreserveBaseURIWithChildren() throws IOException {
867
String base = "http://www.example.com/index.xml";
868
root.setBaseURI(base);
869
Element child = new Element("child");
870
child.setBaseURI(base);
871
root.appendChild(child);
872
Serializer serializer = new Serializer(out, "UTF-8");
873
serializer.setPreserveBaseURI(true);
874
serializer.write(doc);
875
String result = out.toString("UTF-8");
876
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
877
+ "<root xml:base=\"" + base + "\"><child/></root>\r\n", result);
882
public void testPreserveBaseURIDoesntOverrideXMLBase()
885
root.addAttribute(new Attribute("xml:base",
886
"http://www.w3.org/XML/1998/namespace",
887
"http://www.cafeconleche.org/"));
888
Serializer serializer = new Serializer(out, "UTF-8");
889
serializer.setPreserveBaseURI(true);
890
serializer.write(doc);
891
String result = out.toString("UTF-8");
892
assertTrue(result.indexOf("<root") > 1);
893
doc.setBaseURI("http://www.example.com/index.xml");
894
serializer.write(doc);
895
result = out.toString("UTF-8");
896
assertTrue(result.indexOf("<root ") > 1);
897
assertTrue(result.indexOf("xml:base=") > 1);
898
assertTrue(result.indexOf("http://www.cafeconleche.org/") > 1);
899
assertEquals(-1, result.indexOf("http://www.example.com/index.xml"));
904
public void testSetLineSeparator() {
906
Serializer serializer = new Serializer(System.out);
908
serializer.setLineSeparator("\r");
909
assertEquals("\r", serializer.getLineSeparator());
910
serializer.setLineSeparator("\n");
911
assertEquals("\n", serializer.getLineSeparator());
912
serializer.setLineSeparator("\r\n");
913
assertEquals("\r\n", serializer.getLineSeparator());
916
serializer.setLineSeparator("r");
917
fail("Allowed illegal separator character");
919
catch (IllegalArgumentException success) {
920
assertNotNull(success.getMessage());
924
serializer.setLineSeparator("n");
925
fail("Allowed illegal separator character");
927
catch (IllegalArgumentException success) {
928
assertNotNull(success.getMessage());
932
serializer.setLineSeparator(" ");
933
fail("Allowed illegal separator character");
935
catch (IllegalArgumentException success) {
936
assertNotNull(success.getMessage());
940
serializer.setLineSeparator("rn");
941
fail("Allowed illegal separator character");
943
catch (IllegalArgumentException success) {
944
assertNotNull(success.getMessage());
948
serializer.setLineSeparator("<");
949
fail("Allowed illegal separator character");
951
catch (IllegalArgumentException success) {
952
assertNotNull(success.getMessage());
956
serializer.setLineSeparator("\u0085");
957
fail("Allowed NEL separator character");
959
catch (IllegalArgumentException success) {
960
assertNotNull(success.getMessage());
966
public void testLowerLimitOfUnicodeInCharacterData()
969
root.appendChild("\uD800\uDC00");
970
Serializer serializer = new Serializer(out, "ISO-8859-1");
971
serializer.write(doc);
972
String result = out.toString("ISO-8859-1");
973
assertTrue(result, result.indexOf("𐀀") > 12);
978
public void testUpperLimitOfUnicodeInCharacterData()
981
root.appendChild("\uDBFF\uDFFD");
982
Serializer serializer = new Serializer(out, "ISO-8859-1");
983
serializer.write(doc);
984
String result = out.toString("ISO-8859-1");
985
assertTrue(result, result.indexOf("􏿽") > 12);
990
public void testSerializePlane1CharacterInAttributeValue()
993
root.addAttribute(new Attribute("test", "\uD834\uDD1E"));
994
Serializer serializer = new Serializer(out, "ISO-8859-1");
995
serializer.write(doc);
996
String result = out.toString("ISO-8859-1");
997
assertTrue(result, result.indexOf("𝄞") > 12);
1001
public void testSerializePlane1CharacterInCharacterData()
1002
throws IOException {
1004
root.appendChild("\uD834\uDD1E");
1005
Serializer serializer = new Serializer(out, "ISO-8859-1");
1006
serializer.write(doc);
1007
String result = out.toString("ISO-8859-1");
1008
assertTrue(result, result.indexOf("𝄞") > 12);
1013
public void testSurrogatePairCountsAsOneCharacterForColumnCount()
1014
throws IOException {
1016
Element root = new Element("r");
1017
root.appendChild("\uD834\uDD1E");
1018
Document doc = new Document(root);
1019
Serializer serializer = new ColumnSerializer(out);
1020
serializer.write(doc);
1025
private static class ColumnSerializer extends Serializer {
1028
ColumnSerializer(OutputStream out) {
1033
public void write(Document doc) throws IOException {
1035
for (int i = 0; i < doc.getChildCount(); i++) {
1036
writeChild(doc.getChild(i));
1039
assertEquals(8, super.getColumnNumber());
1047
public void testEscapeAttributeValue() throws IOException {
1049
root.addAttribute(new Attribute("test", "\u0110"));
1050
Serializer serializer = new Serializer(out, "ISO-8859-1");
1051
serializer.write(doc);
1052
String result = out.toString("ISO-8859-1");
1053
assertTrue(result, result.indexOf("Đ") > 5);
1058
public void testLineFeedInAttributeValueWithDefaultOptions()
1059
throws IOException, ParsingException {
1061
root.addAttribute(new Attribute("test", "\n"));
1062
Serializer serializer = new Serializer(out, "ISO-8859-1");
1063
serializer.write(doc);
1065
InputStream in = new ByteArrayInputStream(out.toByteArray());
1066
Document reparsed = parser.build(in);
1067
assertEquals(doc, reparsed);
1072
public void testCarriageReturnInAttributeValueWithDefaultOptions()
1073
throws IOException, ParsingException {
1075
root.addAttribute(new Attribute("test", "\r"));
1076
Serializer serializer = new Serializer(out, "ISO-8859-1");
1077
serializer.write(doc);
1079
InputStream in = new ByteArrayInputStream(out.toByteArray());
1080
Document reparsed = parser.build(in);
1081
assertEquals(doc, reparsed);
1086
public void testCarriageReturnInTextWithDefaultOptions()
1087
throws IOException, ParsingException {
1089
root.appendChild("\r");
1090
Serializer serializer = new Serializer(out, "ISO-8859-1");
1091
serializer.write(doc);
1093
InputStream in = new ByteArrayInputStream(out.toByteArray());
1094
Document reparsed = parser.build(in);
1095
assertEquals(doc, reparsed);
1100
public void testTabInAttributeValueWithDefaultOptions()
1101
throws IOException, ParsingException {
1103
root.addAttribute(new Attribute("test", "\t"));
1104
Serializer serializer = new Serializer(out, "ISO-8859-1");
1105
serializer.write(doc);
1107
InputStream in = new ByteArrayInputStream(out.toByteArray());
1108
Document reparsed = parser.build(in);
1109
assertEquals(doc, reparsed);
1116
* Test that tabs in attribute values are escaped even when
1117
* a line separator is set.
1120
public void testTabInAttributeValueWithLineSeparator()
1121
throws IOException, ParsingException {
1123
root.addAttribute(new Attribute("test", "\t"));
1124
Serializer serializer = new Serializer(out, "ISO-8859-1");
1125
serializer.setLineSeparator("\r");
1126
serializer.write(doc);
1128
InputStream in = new ByteArrayInputStream(out.toByteArray());
1129
Document reparsed = parser.build(in);
1130
assertEquals(doc, reparsed);
1138
* Test that tabs in attribute values are not escaped when
1142
public void testTabInAttributeValueWithIndenting()
1143
throws IOException, ParsingException {
1145
root.addAttribute(new Attribute("test", "\t"));
1146
Serializer serializer = new Serializer(out, "ISO-8859-1");
1147
serializer.setIndent(2);
1148
serializer.write(doc);
1150
InputStream in = new ByteArrayInputStream(out.toByteArray());
1151
Document reparsed = parser.build(in);
1152
String result = reparsed.getRootElement().getAttributeValue("test");
1153
assertEquals("Tab not normalized to space", " ", result);
1158
public void testCRLFInAttributeValueWithLineSeparatorCR()
1159
throws IOException, ParsingException {
1161
root.addAttribute(new Attribute("test", "\r\n"));
1162
Serializer serializer = new Serializer(out, "ISO-8859-1");
1163
serializer.setLineSeparator("\r");
1164
serializer.write(doc);
1166
InputStream in = new ByteArrayInputStream(out.toByteArray());
1167
Document reparsed = parser.build(in);
1168
String result = reparsed.getRootElement().getAttributeValue("test");
1169
assertEquals("\r", result);
1174
public void testCRLFInAttributeValueWithLineSeparatorLF()
1175
throws IOException, ParsingException {
1177
root.addAttribute(new Attribute("test", "\r\n"));
1178
Serializer serializer = new Serializer(out, "ISO-8859-1");
1179
serializer.setLineSeparator("\n");
1180
serializer.write(doc);
1182
InputStream in = new ByteArrayInputStream(out.toByteArray());
1183
Document reparsed = parser.build(in);
1184
String result = reparsed.getRootElement().getAttributeValue("test");
1185
assertEquals("\n", result);
1189
public void testLFInAttributeValueWithLineSeparatorCRLF()
1190
throws IOException, ParsingException {
1192
root.addAttribute(new Attribute("test", "\n"));
1193
Serializer serializer = new Serializer(out, "ISO-8859-1");
1194
serializer.setLineSeparator("\r\n");
1195
serializer.write(doc);
1197
InputStream in = new ByteArrayInputStream(out.toByteArray());
1198
Document reparsed = parser.build(in);
1199
String result = reparsed.getRootElement().getAttributeValue("test");
1200
assertEquals("\r\n", result);
1205
public void testNotEscapeLinefeedInTextContent()
1206
throws IOException {
1208
root.appendChild("\r\n");
1209
Serializer serializer = new Serializer(out, "ISO-8859-1");
1210
serializer.write(doc);
1212
String result = new String(out.toByteArray(), "ISO-8859-1");
1214
"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\r\n<root>
\n</root>\r\n",
1221
public void testCRLFInAttributeValue()
1222
throws IOException, ParsingException {
1224
root.addAttribute(new Attribute("test", "a\r\na"));
1225
Serializer serializer = new Serializer(out, "ISO-8859-1");
1226
serializer.write(doc);
1228
InputStream in = new ByteArrayInputStream(out.toByteArray());
1229
Document reparsed = parser.build(in);
1230
String result = reparsed.getRootElement().getAttributeValue("test");
1231
assertEquals("CRLF not escaped", "a\r\na", result);
1236
public void testPunctuationInAttributeValueNonUnicode()
1237
throws IOException, ParsingException {
1239
root.addAttribute(new Attribute("test", "$()*+,="));
1240
Serializer serializer = new Serializer(out, "ISO-8859-1");
1241
serializer.write(doc);
1243
InputStream in = new ByteArrayInputStream(out.toByteArray());
1244
Document reparsed = parser.build(in);
1245
String result = reparsed.getRootElement().getAttributeValue("test");
1246
assertEquals("$()*+,=", result);
1251
public void testCRLFInAttributeValueWithIndenting()
1252
throws IOException, ParsingException {
1254
root.addAttribute(new Attribute("test", "\r\n"));
1255
Serializer serializer = new Serializer(out, "ISO-8859-1");
1256
serializer.setIndent(2);
1257
serializer.write(doc);
1259
InputStream in = new ByteArrayInputStream(out.toByteArray());
1260
Document reparsed = parser.build(in);
1261
String result = reparsed.getRootElement().getAttributeValue("test");
1262
assertEquals("CRLF unnecessarily escaped", -1, result.indexOf('\r'));
1263
// Need to figure out the serializer should indent this
1264
// and write a unit test for that too.
1269
public void testCRLFInAttributeValueWithMaxLength()
1270
throws IOException, ParsingException {
1272
root.addAttribute(new Attribute("test", "\r\n"));
1274
Serializer serializer = new Serializer(out, "ISO-8859-1");
1275
serializer.setMaxLength(64);
1276
serializer.write(doc);
1278
InputStream in = new ByteArrayInputStream(out.toByteArray());
1279
Document reparsed = parser.build(in);
1280
String result = reparsed.getRootElement().getAttributeValue("test");
1281
assertEquals("CRLF unnecessarily escaped", " ", result);
1286
public void testCRInTextValueWithLineSeparator()
1287
throws IOException, ParsingException {
1289
root.appendChild("\r");
1290
Serializer serializer = new Serializer(out, "ISO-8859-1");
1291
serializer.setLineSeparator("\n");
1292
serializer.write(doc);
1294
InputStream in = new ByteArrayInputStream(out.toByteArray());
1295
Document reparsed = parser.build(in);
1296
String result = reparsed.getValue();
1297
assertEquals("\n", result);
1302
public void testCRLFInTextValueWithLineSeparator()
1303
throws IOException, ParsingException {
1305
root.appendChild("test \r\n test");
1306
Serializer serializer = new Serializer(out, "ISO-8859-1");
1307
serializer.setLineSeparator("\n");
1308
serializer.write(doc);
1310
InputStream in = new ByteArrayInputStream(out.toByteArray());
1311
Document reparsed = parser.build(in);
1312
String result = reparsed.getValue();
1313
assertEquals("test \n test", result);
1318
public void testCRInTextWithIndenting()
1319
throws IOException, ParsingException {
1321
root.appendChild("\r");
1322
Serializer serializer = new Serializer(out, "ISO-8859-1");
1323
serializer.setIndent(2);
1324
serializer.write(doc);
1326
InputStream in = new ByteArrayInputStream(out.toByteArray());
1327
Document reparsed = parser.build(in);
1328
String result = reparsed.getValue();
1329
assertEquals("Carriage return unnecessarily escaped",
1330
-1, result.indexOf('\r'));
1332
// really need to think about what the serializer should output here
1333
// and write a test case for that; this is not ideal output
1338
public void testCRInTextWithMaxLength()
1339
throws IOException, ParsingException {
1341
root.appendChild("\r");
1342
Serializer serializer = new Serializer(out, "ISO-8859-1");
1343
serializer.setMaxLength(64);
1344
serializer.write(doc);
1346
InputStream in = new ByteArrayInputStream(out.toByteArray());
1347
Document reparsed = parser.build(in);
1348
String result = reparsed.getValue();
1349
assertEquals("Carriage return unnecessarily escaped", "\n", result);
1354
public void testTabInAttributeValueWithMaxLength()
1355
throws IOException, ParsingException {
1357
root.addAttribute(new Attribute("test", "\t"));
1358
Serializer serializer = new Serializer(out, "ISO-8859-1");
1359
serializer.setMaxLength(64);
1360
serializer.write(doc);
1362
InputStream in = new ByteArrayInputStream(out.toByteArray());
1363
Document reparsed = parser.build(in);
1364
String result = reparsed.getRootElement().getAttributeValue("test");
1365
assertEquals("Tab not normalized to space", " ", result);
1372
* Test that tabs in attribute values are escaped when
1373
* max length is set to 0
1376
public void testTabInAttributeValueWithZeroMaxLength()
1377
throws IOException, ParsingException {
1379
root.addAttribute(new Attribute("test", "\t"));
1380
Serializer serializer = new Serializer(out, "ISO-8859-1");
1381
serializer.setMaxLength(0);
1382
serializer.write(doc);
1384
InputStream in = new ByteArrayInputStream(out.toByteArray());
1385
Document reparsed = parser.build(in);
1386
String result = reparsed.getRootElement().getAttributeValue("test");
1387
assertEquals("Tab not normalized to space", "\t", result);
1392
public void testSetMaxLength() {
1393
Serializer serializer = new Serializer(System.out);
1395
serializer.setMaxLength(72);
1396
assertEquals(72, serializer.getMaxLength());
1397
serializer.setMaxLength(720);
1398
assertEquals(720, serializer.getMaxLength());
1399
serializer.setMaxLength(1);
1400
assertEquals(1, serializer.getMaxLength());
1401
serializer.setMaxLength(0);
1402
assertEquals(0, serializer.getMaxLength());
1403
serializer.setMaxLength(-1);
1404
assertEquals(0, serializer.getMaxLength());
1409
public void testSetIndent() {
1411
Serializer serializer = new Serializer(System.out);
1412
serializer.setIndent(72);
1413
assertEquals(72, serializer.getIndent());
1414
serializer.setIndent(720);
1415
assertEquals(720, serializer.getIndent());
1416
serializer.setIndent(1);
1417
assertEquals(1, serializer.getIndent());
1418
serializer.setIndent(0);
1419
assertEquals(0, serializer.getIndent());
1421
serializer.setIndent(-1);
1422
fail("Allowed negative indent");
1424
catch (IllegalArgumentException success) {
1425
assertNotNull(success.getMessage());
1431
public void testReallyBigIndent() throws ValidityException, ParsingException, IOException {
1433
ByteArrayOutputStream out = new ByteArrayOutputStream();
1434
Serializer serializer = new Serializer(out);
1435
serializer.setIndent(100);
1436
StringBuffer spaces = new StringBuffer(400);
1437
for (int i = 0; i < 400; i++) spaces.append(' ');
1438
String _400 = spaces.toString();
1439
String _300 = spaces.substring(0, 300);
1440
String _200 = spaces.substring(0, 200);
1441
String _100 = spaces.substring(0, 100);
1443
String original ="<a><b><c><d><e>Hello</e></d></c></b></a>";
1444
Document doc = parser.build(original, null);
1445
serializer.write(doc);
1446
String expected ="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<a>\r\n"
1447
+ _100 + "<b>\r\n" + _200 + "<c>\r\n" + _300 + "<d>\r\n" + _400
1448
+ "<e>Hello</e>\r\n" + _300 + "</d>\r\n" + _200 + "</c>\r\n"
1449
+ _100 + "</b>\r\n</a>\r\n";
1450
String result = new String(out.toByteArray(), "UTF-8");
1451
assertEquals(expected, result);
1456
public void testLineLength() throws IOException {
1459
String data = "This is a really long string that does not "
1460
+ "contain any line breaks. However, there is lots of "
1461
+ "white space so there shouldn't be any trouble wrapping it"
1462
+ " into 40 characters or less per line. ";
1463
root.appendChild(data);
1464
Serializer serializer = new Serializer(out, "UTF-8");
1465
serializer.setMaxLength(length);
1466
serializer.write(doc);
1467
String result = out.toString("UTF-8");
1469
BufferedReader reader
1470
= new BufferedReader(new StringReader(result));
1471
for (String line = reader.readLine();
1473
line = reader.readLine()) {
1474
assertTrue(line.length() + ": " + line,
1475
line.length() <= length);
1481
public void testLineLengthWithSetOutputStream()
1482
throws IOException {
1485
String data = "This is a really long string that does not "
1486
+ "contain any line breaks. However, there is lots of "
1487
+ "white space so there shouldn't be any trouble wrapping it"
1488
+ " into 40 characters or less per line. ";
1489
root.appendChild(data);
1490
Serializer serializer = new Serializer(new ByteArrayOutputStream(), "UTF-8");
1491
serializer.setMaxLength(length);
1492
serializer.write(doc);
1493
serializer.setOutputStream(out);
1494
serializer.write(doc);
1495
String result = out.toString("UTF-8");
1497
BufferedReader reader
1498
= new BufferedReader(new StringReader(result));
1499
for (String line = reader.readLine();
1501
line = reader.readLine()) {
1502
assertTrue(line.length() + ": " + line,
1503
line.length() <= length);
1509
public void testPrettyXML() throws IOException {
1511
Element items = new Element("itemSet");
1512
items.appendChild(new Element("item1"));
1513
items.appendChild(new Element("item2"));
1514
Document doc = new Document(items);
1515
Serializer serializer = new Serializer(out);
1516
serializer.setIndent(4);
1517
serializer.write(doc);
1520
String result = new String(out.toByteArray(), "UTF-8");
1522
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1523
+ "<itemSet>\r\n <item1/>\r\n <item2/>\r\n"
1531
public void testIndentAndBreakBeforeComment() throws IOException {
1533
Element items = new Element("itemSet");
1534
items.appendChild(new Comment("item1"));
1535
Document doc = new Document(items);
1536
Serializer serializer = new Serializer(out);
1537
serializer.setIndent(4);
1538
serializer.write(doc);
1541
String result = new String(out.toByteArray(), "UTF-8");
1543
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1544
+ "<itemSet>\r\n <!--item1-->\r\n"
1552
public void testOnlyXMLSpaceCountsAsBoundaryWhiteSpace()
1553
throws IOException {
1555
String emSpace = "\u2003";
1556
Element items = new Element("itemSet");
1557
items.appendChild(emSpace);
1558
items.appendChild(new Element("a"));
1560
Document doc = new Document(items);
1561
Serializer serializer = new Serializer(out);
1562
serializer.setIndent(4);
1563
serializer.write(doc);
1566
String result = new String(out.toByteArray(), "UTF-8");
1568
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1569
+ "<itemSet>\u2003\r\n <a/>\r\n</itemSet>\r\n",
1576
public void testWhiteSpaceBetweenCommentsIsBoundaryWhiteSpace()
1577
throws IOException {
1579
Element items = new Element("itemSet");
1580
items.appendChild(new Comment("item1"));
1581
items.appendChild(" \r\n ");
1582
items.appendChild(new Comment("item2"));
1583
Document doc = new Document(items);
1584
Serializer serializer = new Serializer(out);
1585
serializer.setIndent(4);
1586
serializer.write(doc);
1589
String result = new String(out.toByteArray(), "UTF-8");
1591
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1592
+ "<itemSet>\r\n <!--item1-->\r\n <!--item2-->\r\n"
1600
public void testWhiteSpaceBeforeCommentIsBoundaryWhiteSpace()
1601
throws IOException {
1603
Element items = new Element("itemSet");
1604
items.appendChild(" \r\n ");
1605
items.appendChild(new Comment("item1"));
1606
items.appendChild(new Comment("item2"));
1607
Document doc = new Document(items);
1608
Serializer serializer = new Serializer(out);
1609
serializer.setIndent(4);
1610
serializer.write(doc);
1613
String result = new String(out.toByteArray(), "UTF-8");
1615
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1616
+ "<itemSet>\r\n <!--item1-->\r\n <!--item2-->\r\n"
1624
public void testWhiteSpaceAfterCommentsIsBoundaryWhiteSpace()
1625
throws IOException {
1627
Element items = new Element("itemSet");
1628
items.appendChild(new Comment("item1"));
1629
items.appendChild(new Comment("item2"));
1630
items.appendChild(" \r\n ");
1631
Document doc = new Document(items);
1632
Serializer serializer = new Serializer(out);
1633
serializer.setIndent(4);
1634
serializer.write(doc);
1637
String result = new String(out.toByteArray(), "UTF-8");
1639
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1640
+ "<itemSet>\r\n <!--item1-->\r\n <!--item2-->\r\n"
1648
public void testIndentAndBreakBeforeProcessingInstruction()
1649
throws IOException {
1651
Element items = new Element("itemSet");
1652
items.appendChild(new ProcessingInstruction("target", "value"));
1653
Document doc = new Document(items);
1654
Serializer serializer = new Serializer(out);
1655
serializer.setIndent(4);
1656
serializer.write(doc);
1659
String result = new String(out.toByteArray(), "UTF-8");
1661
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1662
+ "<itemSet>\r\n <?target value?>\r\n"
1670
public void testDontBreakLineInElementWithSimpleContent()
1671
throws IOException {
1673
Element items = new Element("itemSet");
1674
Element item1 = new Element("item1");
1675
items.appendChild(item1);
1676
item1.appendChild("content");
1677
Document doc = new Document(items);
1678
Serializer serializer = new Serializer(out);
1679
serializer.setIndent(4);
1680
serializer.write(doc);
1683
String result = new String(out.toByteArray(), "UTF-8");
1685
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1686
+ "<itemSet>\r\n <item1>content</item1>\r\n"
1694
public void testPrettyXMLWithSetOutputStream() throws IOException {
1696
Element items = new Element("itemSet");
1697
items.appendChild(new Element("item1"));
1698
items.appendChild(new Element("item2"));
1699
Document doc = new Document(items);
1700
Serializer serializer = new Serializer(new ByteArrayOutputStream());
1701
serializer.setIndent(4);
1702
serializer.setLineSeparator("\n");
1703
serializer.write(doc);
1704
serializer.setOutputStream(out);
1705
serializer.write(doc);
1708
String result = new String(out.toByteArray(), "UTF-8");
1710
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1711
+ "<itemSet>\n <item1/>\n <item2/>\n"
1719
public void testAmpersandAndLessThanInText() throws IOException {
1721
root.appendChild("data<data&data");
1722
Serializer serializer = new Serializer(out);
1723
serializer.write(doc);
1726
String result = new String(out.toByteArray(), "UTF-8");
1728
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1729
+ "<root>data<data&data"
1737
public void testAmpersandAndAngleBracketsInAttributeValue()
1738
throws IOException {
1740
root.addAttribute(new Attribute("b", "data<data>data&"));
1741
Serializer serializer = new Serializer(out);
1742
serializer.write(doc);
1745
String result = new String(out.toByteArray(), "UTF-8");
1747
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1748
+ "<root b=\"data<data>data&\"/>\r\n",
1755
public void testSetNFC() {
1757
Serializer serializer = new Serializer(System.out);
1758
assertFalse(serializer.getUnicodeNormalizationFormC());
1759
serializer.setUnicodeNormalizationFormC(true);
1760
assertTrue(serializer.getUnicodeNormalizationFormC());
1765
public void testNFCInElementContent() throws IOException {
1767
Element root = new Element("a");
1768
root.appendChild("c\u0327"); // c with combining cedilla
1769
Document doc = new Document(root);
1770
Serializer serializer = new Serializer(out);
1771
serializer.setUnicodeNormalizationFormC(true);
1772
serializer.write(doc);
1775
String result = new String(out.toByteArray(), "UTF-8");
1777
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1778
+ "<a>\u00E7</a>\r\n",
1785
public void testNFCInConsecutiveTextNodes() throws IOException {
1787
Element root = new Element("a");
1788
root.appendChild("c");
1789
root.appendChild("\u0327"); // combining cedilla
1790
Document doc = new Document(root);
1791
Serializer serializer = new Serializer(out);
1792
serializer.setUnicodeNormalizationFormC(true);
1793
serializer.write(doc);
1796
String result = new String(out.toByteArray(), "UTF-8");
1798
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1799
+ "<a>\u00E7</a>\r\n",
1806
public void testNoNFCByDefault() throws IOException {
1808
Element root = new Element("c\u0327");
1809
root.appendChild("c\u0327"); // c with combining cedilla
1810
Document doc = new Document(root);
1811
Serializer serializer = new Serializer(out);
1812
serializer.write(doc);
1815
String result = new String(out.toByteArray(), "UTF-8");
1817
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1818
+ "<c\u0327>c\u0327</c\u0327>\r\n",
1825
public void testUnicodeNFCTestSuite()
1826
throws ParsingException, IOException {
1828
Builder builder = new Builder();
1829
Document doc = builder.build("data/nfctests.xml");
1830
Elements tests = doc.getRootElement().getChildElements("test");
1831
int size = tests.size();
1832
for (int i = 0; i < size; i++) {
1833
// System.out.println(i);
1834
Element test = tests.get(i);
1836
Document testdoc = new Document(test);
1837
ByteArrayOutputStream out = new ByteArrayOutputStream();
1838
Serializer serializer = new Serializer(out);
1839
serializer.setUnicodeNormalizationFormC(true);
1840
serializer.write(testdoc);
1842
String result = new String(out.toByteArray(), "UTF-8");
1843
Document resultDoc = builder.build(result, null);
1844
Element root = resultDoc.getRootElement();
1845
String c1 = root.getAttributeValue("c1");
1846
String c2 = root.getAttributeValue("c2");
1847
String c3 = root.getAttributeValue("c3");
1848
String c4 = root.getAttributeValue("c4");
1849
String c5 = root.getAttributeValue("c5");
1850
/* String v1 = root.getAttributeValue("v1");
1851
String v2 = root.getAttributeValue("v2");
1852
String v3 = root.getAttributeValue("v3");
1853
String v4 = root.getAttributeValue("v4");
1854
String v5 = root.getAttributeValue("v5"); */
1856
assertEquals(root.getValue(), c1, c2);
1857
assertEquals(c2, c3);
1858
// I'm not sure the v's are correct past the BMP
1859
//assertEquals(root.getValue(), c1, v1);
1860
// assertEquals(c1, v2);
1861
// assertEquals(c1, v3);
1863
assertEquals(c4, c5);
1864
// assertEquals(c4, v4);
1865
// assertEquals(c4, v5);
1872
public void testNFCInAttribute() throws IOException {
1874
root.addAttribute(new Attribute("c\u0327", "c\u0327")); // c with combining cedilla
1875
Serializer serializer = new Serializer(out);
1876
serializer.setUnicodeNormalizationFormC(true);
1877
serializer.write(doc);
1880
String result = new String(out.toByteArray(), "UTF-8");
1882
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1883
+ "<root \u00E7=\"\u00E7\"/>\r\n",
1890
public void testNFCInElementName() throws IOException {
1892
Element root = new Element("c\u0327"); // c with combining cedilla
1893
Document doc = new Document(root);
1894
Serializer serializer = new Serializer(out);
1895
serializer.setUnicodeNormalizationFormC(true);
1896
serializer.write(doc);
1899
String result = new String(out.toByteArray(), "UTF-8");
1901
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1909
public void testNFCInComment() throws IOException {
1911
Element root = new Element("a");
1912
Document doc = new Document(root);
1913
doc.insertChild(new Comment("c\u0327hat"), 0); // c with combining cedilla
1914
Serializer serializer = new Serializer(out);
1915
serializer.setUnicodeNormalizationFormC(true);
1916
serializer.write(doc);
1919
String result = new String(out.toByteArray(), "UTF-8");
1921
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1922
+ "<!--\u00E7hat-->\r\n"
1930
public void testNFCInProcessingInstruction() throws IOException {
1932
doc.appendChild(new ProcessingInstruction("c\u0327hat", "c\u0327hat"));
1933
Serializer serializer = new Serializer(out);
1934
serializer.setUnicodeNormalizationFormC(true);
1935
serializer.write(doc);
1938
String result = new String(out.toByteArray(), "UTF-8");
1940
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1942
+ "<?\u00E7hat \u00E7hat?>\r\n",
1949
public void testNFCInElementContentWithNonUnicodeEncoding()
1950
throws IOException {
1952
root.appendChild("c\u0327"); // c with combining cedilla
1953
Serializer serializer = new Serializer(out, "ISO-8859-5");
1954
serializer.setUnicodeNormalizationFormC(true);
1955
serializer.write(doc);
1958
String result = new String(out.toByteArray(), "ISO-8859-5");
1960
"<?xml version=\"1.0\" encoding=\"ISO-8859-5\"?>\r\n"
1961
+ "<root>ç</root>\r\n",
1968
public void testNFCFollowingEntityReference()
1969
throws IOException {
1971
root.appendChild("<\u0338"); // < followed by not
1972
Serializer serializer = new Serializer(out);
1973
serializer.setUnicodeNormalizationFormC(true);
1974
serializer.write(doc);
1977
String result = new String(out.toByteArray(), "UTF-8");
1979
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
1980
+ "<root>\u226E</root>\r\n",
1987
public void testNFCWithSetOutputStream()
1988
throws IOException {
1990
root.appendChild("c\u0327"); // c with combining cedilla
1991
Serializer serializer = new Serializer(new ByteArrayOutputStream(), "ISO-8859-5");
1992
serializer.setUnicodeNormalizationFormC(true);
1993
serializer.write(doc);
1994
serializer.setOutputStream(out);
1995
serializer.write(doc);
1998
String result = new String(out.toByteArray(), "ISO-8859-5");
2000
"<?xml version=\"1.0\" encoding=\"ISO-8859-5\"?>\r\n"
2001
+ "<root>ç</root>\r\n",
2008
public void testNFCWithKoreanCharacter()
2009
throws IOException {
2011
root.appendChild("\u1111\u1171\u11B6"); // see p. 88 of Unicode 4.0 book
2012
Serializer serializer = new Serializer(new ByteArrayOutputStream());
2013
serializer.setUnicodeNormalizationFormC(true);
2014
serializer.write(doc);
2015
serializer.setOutputStream(out);
2016
serializer.write(doc);
2019
String result = new String(out.toByteArray(), "UTF-8");
2021
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
2022
+ "<root>\uD4DB</root>\r\n",
2029
public void testNullOutputStream() {
2032
new Serializer(null);
2033
fail("Allowed null output stream");
2035
catch (NullPointerException success) {
2036
assertNotNull(success.getMessage());
2042
public void testNullOutputStreamWithEncoding()
2043
throws UnsupportedEncodingException {
2046
new Serializer(null, "UTF-8");
2047
fail("Allowed null output stream");
2049
catch (NullPointerException success) {
2050
assertNotNull(success.getMessage());
2056
public void testNullEncoding()
2057
throws UnsupportedEncodingException {
2060
new Serializer(System.out, null);
2061
fail("Allowed null encoding");
2063
catch (NullPointerException success) {
2064
assertNotNull(success.getMessage());
2070
// make sure null pointer exception doesn't cause any output
2071
public void testNullDocument() throws IOException {
2073
Serializer serializer = new Serializer(out, "UTF-16");
2075
serializer.write(null);
2076
fail("Wrote null document");
2078
catch (NullPointerException success) {
2081
byte[] result = out.toByteArray();
2082
assertEquals(0, result.length);
2087
public void testGetEncoding()
2088
throws UnsupportedEncodingException {
2090
Serializer serializer = new Serializer(System.out, "ISO-8859-1");
2091
assertEquals("ISO-8859-1", serializer.getEncoding());
2096
public void testGetPreserveBaseURI()
2097
throws UnsupportedEncodingException {
2099
Serializer serializer = new Serializer(System.out, "ISO-8859-1");
2100
assertFalse(serializer.getPreserveBaseURI());
2101
serializer.setPreserveBaseURI(true);
2102
assertTrue(serializer.getPreserveBaseURI());
2103
serializer.setPreserveBaseURI(false);
2104
assertFalse(serializer.getPreserveBaseURI());
2109
public void testSerializeDocTypeWithSystemID()
2110
throws IOException {
2112
Serializer serializer = new Serializer(out);
2113
Document doc = new Document(new Element("a"));
2114
doc.setDocType(new DocType("b", "example.dtd"));
2115
serializer.write(doc);
2116
String result = out.toString("UTF-8");
2117
assertTrue(result.endsWith("<a/>\r\n"));
2118
assertTrue(result.indexOf("<!DOCTYPE b SYSTEM \"example.dtd\">") > 0);
2123
public void testSerializeDocTypeWithPublicAndSystemID()
2124
throws IOException {
2126
Serializer serializer = new Serializer(out);
2127
Document doc = new Document(new Element("a"));
2128
doc.setDocType(new DocType("b",
2129
"-//W3C//DTD XHTML 1.0 Transitional//EN", "example.dtd"));
2130
serializer.write(doc);
2131
String result = out.toString("UTF-8");
2132
assertTrue(result.endsWith("<a/>\r\n"));
2133
assertTrue(result.indexOf(
2134
"<!DOCTYPE b PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"example.dtd\">") > 0);
2139
public void testSerializeXMLNamespaceDeclaration()
2140
throws ParsingException, IOException {
2142
Serializer serializer = new Serializer(out);
2143
Element root = new Element("root");
2144
root.addNamespaceDeclaration("xml", Namespace.XML_NAMESPACE);
2145
Document doc = new Document(root);
2146
serializer.write(doc);
2147
String result = out.toString("UTF-8");
2148
assertTrue(result.endsWith("<root/>\r\n"));
2153
public void testSerializeDocTypeWithInternalDTDSubset()
2154
throws ParsingException, IOException {
2156
Serializer serializer = new Serializer(out);
2157
String data = "<!DOCTYPE root [ <!ELEMENT root EMPTY> ]><test/>";
2158
Builder builder = new Builder();
2159
Document doc = builder.build(data, "http://www.example.com");
2160
serializer.write(doc);
2161
String result = out.toString("UTF-8");
2162
assertTrue(result.endsWith("<test/>\r\n"));
2163
assertTrue(result.indexOf("<!DOCTYPE root [\r\n") > 0);
2164
assertTrue(result.indexOf("\r\n]>\r\n") > 0);
2165
assertTrue(result.indexOf("<!ELEMENT root EMPTY>\r\n") > 0);
2170
public void testSerializeInternalDTDSubsetContainingUnavailableCharacter()
2171
throws ParsingException, IOException {
2173
Serializer serializer = new Serializer(out, "US-ASCII");
2174
String data = "<!DOCTYPE root ["
2175
+ "<!ELEMENT root EMPTY> "
2176
+ "<!ATTLIST root attr CDATA 'cafƩ creme'> "
2178
Builder builder = new Builder();
2179
Document doc = builder.build(data, "http://www.example.com");
2181
serializer.write(doc);
2182
fail("How'd you serialize Ć© in ASCII?");
2184
catch (UnavailableCharacterException success) {
2185
assertTrue(success.getMessage().indexOf("Ć© (é)") > 1);
2191
public void testLineBreaksInInternalDTDSubset()
2192
throws ParsingException, IOException {
2194
Serializer serializer = new Serializer(out);
2195
serializer.setLineSeparator("\r");
2196
String data = "<!DOCTYPE root [ <!ELEMENT root EMPTY> <!ELEMENT data EMPTY> ]><test/>";
2197
Builder builder = new Builder();
2198
Document doc = builder.build(data, "http://www.example.com");
2199
serializer.write(doc);
2200
String result = out.toString("UTF-8");
2201
assertTrue(result.endsWith("<test/>\r"));
2202
assertTrue(result.indexOf("<!DOCTYPE root [\r") > 0);
2203
assertTrue(result.indexOf("\r]>\r") > 0);
2204
assertTrue(result.indexOf("<!ELEMENT root EMPTY>\r") > 0);
2205
assertTrue(result.indexOf("<!ELEMENT data EMPTY>\r") > 0);
2206
assertEquals(-1, result.indexOf("<!ELEMENT data EMPTY>\r\n"));
2207
assertEquals(-1, result.indexOf("<!ELEMENT root EMPTY>\r\n"));
2208
assertEquals(-1, result.indexOf("<!ELEMENT data EMPTY>\r\r"));
2209
assertEquals(-1, result.indexOf("<!ELEMENT root EMPTY>\r\r"));
2210
assertEquals(-1, result.indexOf('\n'));
2215
public void testSerializeQuoteInAttributeValue()
2216
throws IOException {
2218
Serializer serializer = new Serializer(out);
2219
root.addAttribute(new Attribute("name", "\""));
2220
serializer.write(doc);
2221
String result = out.toString("UTF-8");
2222
assertTrue(result.endsWith("<root name=\""\"/>\r\n"));
2227
public void testSerializeUnavailableCharacterInMarkup()
2228
throws IOException {
2230
Serializer serializer = new Serializer(out, "ISO-8859-1");
2231
Element root = new Element("\u0419");
2232
Document doc = new Document(root);
2234
serializer.write(doc);
2235
fail("Wrote bad character: " + out.toString("ISO-8859-1"));
2237
catch (UnavailableCharacterException success) {
2238
assertNotNull(success.getMessage());
2239
assertEquals('\u0419', success.getCharacter());
2240
assertEquals("ISO-8859-1", success.getEncoding());
2246
public void testTurnLineFeedInAttributeValueIntoSpaceWhenIndenting()
2247
throws IOException {
2249
Element root = new Element("a");
2250
root.appendChild("c");
2251
root.addAttribute(new Attribute("name", "value1\nvalue2"));
2252
Document doc = new Document(root);
2253
Serializer serializer = new Serializer(out);
2254
serializer.setMaxLength(245);
2255
serializer.setIndent(4);
2256
serializer.write(doc);
2259
String result = new String(out.toByteArray(), "UTF-8");
2261
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
2262
+ "<a name=\"value1 value2\">c</a>\r\n",
2269
public void testConflictBetweenMaxLengthAndIndent()
2270
throws IOException {
2272
Element root = new Element("a");
2273
Element b = new Element("b");
2274
Element c = new Element("c");
2275
Element d = new Element("d");
2277
root.appendChild(b);
2279
d.appendChild("data");
2280
Document doc = new Document(root);
2281
Serializer serializer = new Serializer(out);
2282
serializer.setMaxLength(16);
2283
serializer.setIndent(4);
2284
serializer.write(doc);
2287
String result = new String(out.toByteArray(), "UTF-8");
2289
"<?xml version=\"1.0\"\r\nencoding=\"UTF-8\"?>\r\n"
2290
+ "<a>\r\n <b>\r\n <c>\r\n <d>data</d>\r\n </c>\r\n </b>\r\n</a>\r\n",
2297
public void testWriteChild() throws IOException {
2299
ExposingSerializer serializer = new ExposingSerializer(out, "UTF-8");
2301
serializer.writeChild(doc);
2302
fail("writeChild wrote a document");
2304
catch (XMLException success) {
2305
assertNotNull(success.getMessage());
2308
serializer.writeChild(new Attribute("name", "value"));
2309
fail("writeChild wrote an attribute");
2311
catch (XMLException success) {
2312
assertNotNull(success.getMessage());
2318
// just so we can test protected methods
2319
private static class ExposingSerializer extends Serializer {
2321
ExposingSerializer(OutputStream out, String encoding)
2322
throws UnsupportedEncodingException {
2323
super(out, encoding);
2326
public void writeChild(Node node) throws IOException {
2327
super.writeChild(node);
2330
public void exposedWriteRaw(String text) throws IOException {
2334
public void exposedWriteEscaped(String text) throws IOException {
2338
public void exposedWriteAttributeValue(String text) throws IOException {
2339
writeAttributeValue(text);
2342
public int exposeGetColumnNumber() {
2343
return super.getColumnNumber();
2349
private static class ElementSerializer extends Serializer {
2351
ElementSerializer(OutputStream out, String encoding)
2352
throws UnsupportedEncodingException {
2353
super(out, encoding);
2356
protected void write(Element element) throws IOException {
2357
super.write(element);
2363
private static class TextSerializer extends Serializer {
2365
TextSerializer(OutputStream out) {
2369
protected void write(Text text) throws IOException {
2376
public void testWriteParentlessTextNode() throws IOException {
2378
Text t = new Text("Hello");
2379
ByteArrayOutputStream out = new ByteArrayOutputStream();
2380
TextSerializer serializer = new TextSerializer(out);
2381
serializer.write(t);
2383
byte[] result = out.toByteArray();
2384
String s = new String(result, "UTF-8");
2385
assertEquals(s, t.getValue());
2390
public void testWriteParentlessTextNodeWhileIndenting() throws IOException {
2392
Text t = new Text("Hello");
2393
ByteArrayOutputStream out = new ByteArrayOutputStream();
2394
TextSerializer serializer = new TextSerializer(out);
2395
serializer.setIndent(2);
2396
serializer.write(t);
2398
byte[] result = out.toByteArray();
2399
String s = new String(result, "UTF-8");
2400
assertEquals(s, t.getValue());
2405
public void testWriteWhiteSpaceOnlyTextNodeWhileIndenting() throws IOException {
2407
Text t = new Text(" \r\n ");
2408
ByteArrayOutputStream out = new ByteArrayOutputStream();
2409
TextSerializer serializer = new TextSerializer(out);
2410
serializer.setIndent(2);
2411
serializer.write(t);
2413
byte[] result = out.toByteArray();
2414
assertEquals(0, result.length);
2419
public void testWriteRaw() throws IOException {
2421
ExposingSerializer serializer = new ExposingSerializer(out, "UTF-8");
2422
serializer.exposedWriteRaw("<>&\"'");
2423
assertEquals(5, serializer.exposeGetColumnNumber());
2425
String result = out.toString("UTF-8");
2426
assertEquals("<>&\"'", result);
2431
public void testWriteParentlessElementInANamespace() throws IOException {
2433
ElementSerializer serializer = new ElementSerializer(out, "UTF-8");
2434
Element element = new Element("a", "http://www.example.org");
2435
serializer.write(element);
2437
String result = out.toString("UTF-8");
2438
assertEquals("<a xmlns=\"http://www.example.org\"/>", result);
2443
public void testWriteEscaped() throws IOException {
2445
ExposingSerializer serializer = new ExposingSerializer(out, "UTF-8");
2446
serializer.exposedWriteEscaped("<>&\"'");
2447
assertEquals(15, serializer.exposeGetColumnNumber());
2449
byte[] data = out.toByteArray();
2450
String result = new String(data, "UTF-8");
2451
assertEquals("<>&\"'", result);
2456
public void testWriteAttributeValue() throws IOException {
2458
ExposingSerializer serializer = new ExposingSerializer(out, "UTF-8");
2459
serializer.exposedWriteAttributeValue("<>&\"'");
2460
assertEquals(20, serializer.exposeGetColumnNumber());
2462
String result = out.toString("UTF-8");
2463
assertEquals("<>&"'", result);
2468
// This test case reproduces a bug that
2469
// showed up while working on SAX conformance testing.
2470
public void testElementsThatOnlyContainsAnEmptyTextNodeShouldBeOutputWithAnEmptyElementTag()
2471
throws IOException {
2473
Element child = new Element("b");
2474
child.appendChild("");
2475
root.appendChild(child);
2477
Serializer serializer = new Serializer(out);
2478
serializer.write(doc);
2481
String result = out.toString("UTF-8");
2482
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" +
2483
"<root><b/></root>\r\n", result);
2488
// This test case reproduces a bug that
2489
// showed up while working on SAX conformance testing.
2490
public void testElementsThatOnlyContainEmptyTextNodesShouldBeOutputWithAnEmptyElementTag()
2491
throws IOException {
2493
Element root = new Element("a");
2494
Element child = new Element("b");
2495
child.appendChild("");
2496
child.appendChild("");
2497
child.appendChild("");
2498
root.appendChild(child);
2500
Document doc = new Document(root);
2502
Serializer serializer = new Serializer(out);
2503
serializer.write(doc);
2506
String result = out.toString("UTF-8");
2507
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" +
2508
"<a><b/></a>\r\n", result);
2513
// This test case reproduces a bug that
2514
// showed up while working on SAX conformance testing.
2515
public void testElementsThatOnlyContainASingleSpaceShouldNotBeSplitWhileIndenting()
2516
throws IOException {
2518
Element root = new Element("a");
2519
Element child = new Element("b");
2520
child.appendChild(" ");
2521
root.appendChild(child);
2523
Document doc = new Document(root);
2525
Serializer serializer = new Serializer(out);
2526
serializer.setIndent(4);
2527
serializer.write(doc);
2530
String result = out.toString("UTF-8");
2531
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" +
2532
"<a>\r\n <b> </b>\r\n</a>\r\n", result);
2537
public void testElementsThatOnlyContainTextNodesWithBoundaryWhiteSpaceShouldNotBeSplitWhileIndenting()
2538
throws IOException {
2540
Element root = new Element("a");
2541
Element child = new Element("b");
2542
child.appendChild(" ");
2543
child.appendChild(" ");
2544
root.appendChild(child);
2546
Document doc = new Document(root);
2548
Serializer serializer = new Serializer(out);
2549
serializer.setIndent(4);
2550
serializer.write(doc);
2553
String result = out.toString("UTF-8");
2554
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" +
2555
"<a>\r\n <b> </b>\r\n</a>\r\n", result);
2560
public void testElementsThatOnlyContainASingleLinefeedShouldNotBeSplitWhileIndenting()
2561
throws IOException {
2563
Element root = new Element("a");
2564
Element child = new Element("b");
2565
child.appendChild("\n");
2566
root.appendChild(child);
2568
Document doc = new Document(root);
2570
Serializer serializer = new Serializer(out);
2571
serializer.setIndent(4);
2572
serializer.write(doc);
2575
String result = out.toString("UTF-8");
2576
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" +
2577
"<a>\r\n <b> </b>\r\n</a>\r\n", result);
2582
public void testEndTagsOfElementsWithContentGoOnSeparateLine()
2583
throws ParsingException, IOException {
2585
Serializer serializer = new Serializer(out, "ISO-8859-1");
2586
serializer.setIndent(4);
2587
serializer.setPreserveBaseURI(true);
2590
File f = new File("data");
2591
f = new File(f, "prettyxml.xml");
2592
Builder builder = new Builder();
2593
Document doc = builder.build(f);
2594
serializer.write(doc);
2595
String result = out.toString("UTF-8");
2596
assertTrue(result.endsWith("\r\n</html>\r\n"));
2601
public void testDontDoubleBreak()
2602
throws ParsingException, IOException {
2604
Serializer serializer = new Serializer(out, "ISO-8859-1");
2605
serializer.setIndent(4);
2606
serializer.setMaxLength(64);
2608
File f = new File("data");
2609
f = new File(f, "prettytest.xml");
2610
Builder builder = new Builder();
2611
Document doc = builder.build(f);
2612
serializer.write(doc);
2613
String result = out.toString("UTF-8");
2614
assertEquals("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\r\n" +
2615
"<html a=\"AReallyLongNameWithNoOpportunitiesToBreakToPutUsPastTheMaxLineLengthAndForceABreak\">\r\n" +
2616
" <head> </head>\r\n" +
2617
"</html>\r\n", result);