Skip to content

Commit 7c5f9aa

Browse files
committed
Fixes #127 - PRESERVE after some other mode.
1 parent 7e327db commit 7c5f9aa

File tree

3 files changed

+79
-11
lines changed

3 files changed

+79
-11
lines changed

core/src/java/org/jdom2/output/support/FormatStack.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -173,15 +173,23 @@ public FormatStack(Format format) {
173173
defaultMode = format.getTextMode();
174174
specifiedAttributesOnly = format.isSpecifiedAttributesOnly();
175175

176-
levelIndent[depth] = format.getIndent() == null
177-
? null : "";
178-
levelEOL[depth] = format.getLineSeparator();
179-
levelEOLIndent[depth] = levelIndent[depth] == null ?
180-
null : levelEOL[depth];
181-
termEOLIndent[depth] = levelEOLIndent[depth];
182-
176+
mode[depth] = format.getTextMode();
177+
if (mode[depth] == TextMode.PRESERVE) {
178+
// undo any special indenting and end-of-line management:
179+
levelIndent[depth] = null;
180+
levelEOL[depth] = null;
181+
levelEOLIndent[depth] = null;
182+
termEOLIndent[depth] = null;
183+
} else {
184+
levelIndent[depth] = format.getIndent() == null
185+
? null : "";
186+
levelEOL[depth] = format.getLineSeparator();
187+
levelEOLIndent[depth] = levelIndent[depth] == null ?
188+
null : levelEOL[depth];
189+
termEOLIndent[depth] = levelEOLIndent[depth];
190+
191+
}
183192
ignoreTrAXEscapingPIs[depth] = format.getIgnoreTrAXEscapingPIs();
184-
mode[depth] = format.getTextMode();
185193
escapeOutput[depth] = true;
186194
}
187195

core/src/java/org/jdom2/output/support/WalkerPRESERVE.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,20 @@ public WalkerPRESERVE(final List<? extends Content> content) {
103103
} else {
104104
iter = content.iterator();
105105
alltext = false;
106+
// final int len = content.size();
107+
// boolean at = true;
108+
// for (int i = 0 ; i < len && at ; i++) {
109+
// switch (content.get(i).getCType()) {
110+
// case Text:
111+
// case CDATA:
112+
// case EntityRef:
113+
// break;
114+
// default :
115+
// at = false;
116+
// break;
117+
// }
118+
// }
119+
// alltext = at;
106120
}
107121

108122
}

test/src/java/org/jdom2/test/cases/output/AbstractTestOutputter.java

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,13 +284,15 @@ protected final String expect(String expect) {
284284
public abstract String outputElementContentString(Format format, Element element);
285285

286286
protected static final Format fraw = Format.getRawFormat();
287+
protected static final Format frawfp = Format.getPrettyFormat().setTextMode(TextMode.PRESERVE);
287288
protected static final Format fcompact = Format.getCompactFormat();
288289
protected static final Format fpretty = Format.getPrettyFormat();
289290
protected static final Format ftso = Format.getPrettyFormat();
290291
protected static final Format ftfw = Format.getPrettyFormat();
291292

292293
static {
293294
fraw.setLineSeparator("\n");
295+
frawfp.setLineSeparator("\n");
294296
fcompact.setLineSeparator("\n");
295297
fpretty.setLineSeparator("\n");
296298
ftso.setLineSeparator("\n");
@@ -304,6 +306,7 @@ protected final String expect(String expect) {
304306
public void testTextEmpty() {
305307
Text content = new Text("");
306308
assertEquals("", outputString(fraw, content));
309+
assertEquals("", outputString(frawfp, content));
307310
assertEquals("", outputString(fcompact, content));
308311
assertEquals("", outputString(fpretty, content));
309312
assertEquals("", outputString(ftso, content));
@@ -315,6 +318,8 @@ public void testTextWhitespace() {
315318
Text content = new Text(" \r \n \t ");
316319
assertEquals(expect(" \r \n \t "),
317320
outputString(fraw, content));
321+
assertEquals(expect(" \r \n \t "),
322+
outputString(frawfp, content));
318323
assertEquals("",
319324
outputString(fcompact, content));
320325
assertEquals("",
@@ -330,6 +335,8 @@ public void testTextWithText() {
330335
Text content = new Text(" \r & \n \t ");
331336
assertEquals(expect(" \r &amp; \n \t "),
332337
outputString(fraw, content));
338+
assertEquals(expect(" \r &amp; \n \t "),
339+
outputString(frawfp, content));
333340
assertEquals(expect("&amp;"),
334341
outputString(fcompact, content));
335342
assertEquals(expect("&amp;"),
@@ -345,6 +352,8 @@ public void testCDATAEmpty() {
345352
CDATA content = new CDATA("");
346353
assertEquals("<![CDATA[]]>",
347354
outputString(fraw, content));
355+
assertEquals("<![CDATA[]]>",
356+
outputString(frawfp, content));
348357
assertEquals("",
349358
outputString(fcompact, content));
350359
assertEquals("",
@@ -360,6 +369,8 @@ public void testCDATAWhitespace() {
360369
CDATA content = new CDATA(" \r \n \t ");
361370
assertEquals("<![CDATA[ \r \n \t ]]>",
362371
outputString(fraw, content));
372+
assertEquals("<![CDATA[ \r \n \t ]]>",
373+
outputString(frawfp, content));
363374
assertEquals("",
364375
outputString(fcompact, content));
365376
assertEquals("",
@@ -375,6 +386,8 @@ public void testCDATAWithText() {
375386
CDATA content = new CDATA(" \r & \n \t ");
376387
assertEquals("<![CDATA[ \r & \n \t ]]>",
377388
outputString(fraw, content));
389+
assertEquals("<![CDATA[ \r & \n \t ]]>",
390+
outputString(frawfp, content));
378391
assertEquals("<![CDATA[&]]>",
379392
outputString(fcompact, content));
380393
assertEquals("<![CDATA[&]]>",
@@ -390,6 +403,8 @@ public void testEntityRef() {
390403
EntityRef content = new EntityRef("ref");
391404
assertEquals("&ref;",
392405
outputString(fraw, content));
406+
assertEquals("&ref;",
407+
outputString(frawfp, content));
393408
assertEquals("&ref;",
394409
outputString(fcompact, content));
395410
assertEquals("&ref;",
@@ -405,6 +420,8 @@ public void testProcessingInstructionTargetOnly() {
405420
ProcessingInstruction content = new ProcessingInstruction("target");
406421
assertEquals(expect("<?target?>"),
407422
outputString(fraw, content));
423+
assertEquals(expect("<?target?>"),
424+
outputString(frawfp, content));
408425
assertEquals(expect("<?target?>"),
409426
outputString(fcompact, content));
410427
assertEquals(expect("<?target?>"),
@@ -419,6 +436,8 @@ public void testProcessingInstructionTargetWithData() {
419436
new ProcessingInstruction("target", "data");
420437
assertEquals("<?target data?>",
421438
outputString(fraw, content));
439+
assertEquals("<?target data?>",
440+
outputString(frawfp, content));
422441
assertEquals("<?target data?>",
423442
outputString(fcompact, content));
424443
assertEquals("<?target data?>",
@@ -434,6 +453,8 @@ public void testComment() {
434453
Comment content = new Comment("comment");
435454
assertEquals("<!--comment-->",
436455
outputString(fraw, content));
456+
assertEquals("<!--comment-->",
457+
outputString(frawfp, content));
437458
assertEquals("<!--comment-->",
438459
outputString(fcompact, content));
439460
assertEquals("<!--comment-->",
@@ -450,6 +471,8 @@ public void testDocTypeSimple() {
450471
DocType content = new DocType("root");
451472
assertEquals("<!DOCTYPE root>",
452473
outputString(fraw, content));
474+
assertEquals("<!DOCTYPE root>",
475+
outputString(frawfp, content));
453476
assertEquals("<!DOCTYPE root>",
454477
outputString(fcompact, content));
455478
assertEquals("<!DOCTYPE root>",
@@ -466,6 +489,8 @@ public void testDocTypeSimpleISS() {
466489
content.setInternalSubset("<!ENTITY name \"value\">");
467490
assertEquals("<!DOCTYPE root [\n<!ENTITY name \"value\">]>",
468491
outputString(fraw, content));
492+
assertEquals("<!DOCTYPE root [\n<!ENTITY name \"value\">]>",
493+
outputString(frawfp, content));
469494
assertEquals("<!DOCTYPE root [\n<!ENTITY name \"value\">]>",
470495
outputString(fcompact, content));
471496
assertEquals("<!DOCTYPE root [\n<!ENTITY name \"value\">]>",
@@ -481,6 +506,8 @@ public void testDocTypeSystemID() {
481506
DocType content = new DocType("root", "sysid");
482507
assertEquals("<!DOCTYPE root SYSTEM \"sysid\">",
483508
outputString(fraw, content));
509+
assertEquals("<!DOCTYPE root SYSTEM \"sysid\">",
510+
outputString(frawfp, content));
484511
assertEquals("<!DOCTYPE root SYSTEM \"sysid\">",
485512
outputString(fcompact, content));
486513
assertEquals("<!DOCTYPE root SYSTEM \"sysid\">",
@@ -497,6 +524,8 @@ public void testDocTypeSystemIDISS() {
497524
content.setInternalSubset("internal");
498525
assertEquals("<!DOCTYPE root SYSTEM \"sysid\" [\ninternal]>",
499526
outputString(fraw, content));
527+
assertEquals("<!DOCTYPE root SYSTEM \"sysid\" [\ninternal]>",
528+
outputString(frawfp, content));
500529
assertEquals("<!DOCTYPE root SYSTEM \"sysid\" [\ninternal]>",
501530
outputString(fcompact, content));
502531
assertEquals("<!DOCTYPE root SYSTEM \"sysid\" [\ninternal]>",
@@ -512,6 +541,8 @@ public void testDocTypePublicSystemID() {
512541
DocType content = new DocType("root", "pubid", "sysid");
513542
assertEquals("<!DOCTYPE root PUBLIC \"pubid\" \"sysid\">",
514543
outputString(fraw, content));
544+
assertEquals("<!DOCTYPE root PUBLIC \"pubid\" \"sysid\">",
545+
outputString(frawfp, content));
515546
assertEquals("<!DOCTYPE root PUBLIC \"pubid\" \"sysid\">",
516547
outputString(fcompact, content));
517548
assertEquals("<!DOCTYPE root PUBLIC \"pubid\" \"sysid\">",
@@ -528,6 +559,8 @@ public void testDocTypePublicSystemIDISS() {
528559
content.setInternalSubset("internal");
529560
assertEquals("<!DOCTYPE root PUBLIC \"pubid\" \"sysid\" [\ninternal]>",
530561
outputString(fraw, content));
562+
assertEquals("<!DOCTYPE root PUBLIC \"pubid\" \"sysid\" [\ninternal]>",
563+
outputString(frawfp, content));
531564
assertEquals("<!DOCTYPE root PUBLIC \"pubid\" \"sysid\" [\ninternal]>",
532565
outputString(fcompact, content));
533566
assertEquals("<!DOCTYPE root PUBLIC \"pubid\" \"sysid\" [\ninternal]>",
@@ -551,6 +584,8 @@ public void testMultiWhiteText() {
551584
root.addContent(new Text(" "));
552585
assertEquals(expect("<root><![CDATA[ ]]> \n \n \t </root>"),
553586
outputString(fraw, root));
587+
assertEquals(expect("<root><![CDATA[ ]]> \n \n \t </root>"),
588+
outputString(frawfp, root));
554589
assertEquals(expect("<root/>"),
555590
outputString(fcompact, root));
556591
assertEquals(expect("<root/>"),
@@ -574,6 +609,8 @@ public void testMultiText() {
574609
root.addContent(new Text(" "));
575610
assertEquals(expect("<root><![CDATA[ ]]> X \n \n \t </root>"),
576611
outputString(fraw, root));
612+
assertEquals(expect("<root><![CDATA[ ]]> X \n \n \t </root>"),
613+
outputString(frawfp, root));
577614
assertEquals(expect("<root>X</root>"),
578615
outputString(fcompact, root));
579616
assertEquals(expect("<root>X</root>"),
@@ -589,6 +626,8 @@ public void testDocumentSimple() {
589626
Document content = new Document();
590627
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
591628
outputString(fraw, content));
629+
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
630+
outputString(frawfp, content));
592631
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
593632
outputString(fcompact, content));
594633
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
@@ -605,6 +644,8 @@ public void testDocumentDocType() {
605644
content.setDocType(new DocType("root"));
606645
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE root>\n",
607646
outputString(fraw, content));
647+
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE root>\n",
648+
outputString(frawfp, content));
608649
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE root>\n",
609650
outputString(fcompact, content));
610651
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE root>\n",
@@ -621,6 +662,8 @@ public void testDocumentComment() {
621662
content.addContent(new Comment("comment"));
622663
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!--comment-->\n",
623664
outputString(fraw, content));
665+
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!--comment-->\n",
666+
outputString(frawfp, content));
624667
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!--comment-->\n",
625668
outputString(fcompact, content));
626669
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!--comment-->\n",
@@ -645,6 +688,8 @@ public void testXXX() {
645688
Text content = new Text("");
646689
assertEquals("",
647690
outputString(fraw, content));
691+
assertEquals("",
692+
outputString(frawfp, content));
648693
assertEquals("",
649694
outputString(fcompact, content));
650695
assertEquals("",
@@ -1337,20 +1382,21 @@ protected void checkOutput(Object content, String methodprefix, Class<?> clazz,
13371382
return;
13381383
}
13391384

1340-
String[] descn = new String[] {"Raw", "Compact", "Pretty", "PrettySpecifiedOnly", "TrimFullWhite"};
1385+
String[] descn = new String[] {"Raw", "PrettyPreserve", "Compact", "Pretty", "PrettySpecifiedOnly", "TrimFullWhite"};
13411386
Format ftrimfw = Format.getPrettyFormat();
13421387
ftrimfw.setTextMode(TextMode.TRIM_FULL_WHITE);
13431388
Format fattspec = Format.getPrettyFormat();
13441389
fattspec.setSpecifiedAttributesOnly(true);
13451390
Format[] formats = new Format[] {
13461391
getFormat(setup, Format.getRawFormat()),
1392+
getFormat(setup, Format.getPrettyFormat().setTextMode(TextMode.PRESERVE)),
13471393
getFormat(setup, Format.getCompactFormat()),
13481394
getFormat(setup, Format.getPrettyFormat()),
13491395
getFormat(setup, fattspec),
13501396
getFormat(setup, ftrimfw)};
1351-
String[] result = new String[] {raw, compact, pretty, tso, trimfw};
1397+
String[] result = new String[] {raw, raw, compact, pretty, tso, trimfw};
13521398

1353-
for (int i = 0; i < 5; i++) {
1399+
for (int i = 0; i < result.length; i++) {
13541400

13551401
String mstring;
13561402
try {

0 commit comments

Comments
 (0)