Skip to content

Commit 74ac5ac

Browse files
authored
Feature : Allow setting custom linefeed on DefaultXmlPrettyPrinter (#568)
1 parent 0e59be6 commit 74ac5ac

File tree

2 files changed

+128
-34
lines changed

2 files changed

+128
-34
lines changed

src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,27 @@ public interface Indenter
7777
*/
7878
protected boolean _spacesInObjectEntries = true;
7979

80+
/**
81+
* By default, will try to set as System.getProperty("line.separator").
82+
* Can later set custom new line with withCustomNewLine method.
83+
* @since 2.15
84+
*/
85+
private static final String SYSTEM_DEFAULT_NEW_LINE;
86+
static {
87+
String lf = null;
88+
try {
89+
lf = System.getProperty("line.separator");
90+
} catch (Exception t) { } // access exception?
91+
SYSTEM_DEFAULT_NEW_LINE = lf;
92+
}
93+
protected String _newLine = SYSTEM_DEFAULT_NEW_LINE;
94+
95+
static final int SPACE_COUNT = 64;
96+
static final char[] SPACES = new char[SPACE_COUNT];
97+
static {
98+
Arrays.fill(SPACES, ' ');
99+
}
100+
80101
/*
81102
/**********************************************************
82103
/* State
@@ -112,6 +133,7 @@ protected DefaultXmlPrettyPrinter(DefaultXmlPrettyPrinter base)
112133
_objectIndenter = base._objectIndenter;
113134
_spacesInObjectEntries = base._spacesInObjectEntries;
114135
_nesting = base._nesting;
136+
_newLine = base._newLine;
115137
}
116138

117139
public void indentArraysWith(Indenter i)
@@ -126,6 +148,15 @@ public void indentObjectsWith(Indenter i)
126148

127149
public void spacesInObjectEntries(boolean b) { _spacesInObjectEntries = b; }
128150

151+
/**
152+
* Sets custom new-line.
153+
* @since 2.15
154+
*/
155+
public DefaultXmlPrettyPrinter withCustomNewLine(String newLine) {
156+
_newLine = newLine != null ? newLine : SYSTEM_DEFAULT_NEW_LINE;
157+
return this;
158+
}
159+
129160
/*
130161
/**********************************************************
131162
/* Instantiatable impl
@@ -443,7 +474,7 @@ public void writeLeafXsiNilElement(XMLStreamWriter2 sw,
443474
public void writePrologLinefeed(XMLStreamWriter2 sw) throws XMLStreamException
444475
{
445476
// 06-Dec-2015, tatu: Alternatively could try calling `writeSpace()`...
446-
sw.writeRaw(Lf2SpacesIndenter.SYSTEM_LINE_SEPARATOR);
477+
sw.writeRaw(_newLine);
447478
}
448479

449480
/*
@@ -486,7 +517,7 @@ public void writeIndentation(XMLStreamWriter2 sw, int level)
486517
{
487518
sw.writeRaw(" ");
488519
}
489-
520+
490521
@Override
491522
public void writeIndentation(JsonGenerator g, int level) throws IOException
492523
{
@@ -501,26 +532,11 @@ public void writeIndentation(JsonGenerator g, int level) throws IOException
501532
* Default linefeed-based indenter uses system-specific linefeeds and
502533
* 2 spaces for indentation per level.
503534
*/
504-
protected static class Lf2SpacesIndenter
535+
protected class Lf2SpacesIndenter
505536
implements Indenter, java.io.Serializable
506537
{
507538
private static final long serialVersionUID = 1L;
508539

509-
final static String SYSTEM_LINE_SEPARATOR;
510-
static {
511-
String lf = null;
512-
try {
513-
lf = System.getProperty("line.separator");
514-
} catch (Throwable t) { } // access exception?
515-
SYSTEM_LINE_SEPARATOR = (lf == null) ? "\n" : lf;
516-
}
517-
518-
final static int SPACE_COUNT = 64;
519-
final static char[] SPACES = new char[SPACE_COUNT];
520-
static {
521-
Arrays.fill(SPACES, ' ');
522-
}
523-
524540
public Lf2SpacesIndenter() { }
525541

526542
@Override
@@ -529,7 +545,7 @@ public Lf2SpacesIndenter() { }
529545
@Override
530546
public void writeIndentation(XMLStreamWriter2 sw, int level) throws XMLStreamException
531547
{
532-
sw.writeRaw(SYSTEM_LINE_SEPARATOR);
548+
sw.writeRaw(_newLine);
533549
level += level; // 2 spaces per level
534550
while (level > SPACE_COUNT) { // should never happen but...
535551
sw.writeRaw(SPACES, 0, SPACE_COUNT);
@@ -541,7 +557,7 @@ public void writeIndentation(XMLStreamWriter2 sw, int level) throws XMLStreamExc
541557
@Override
542558
public void writeIndentation(JsonGenerator jg, int level) throws IOException
543559
{
544-
jg.writeRaw(SYSTEM_LINE_SEPARATOR);
560+
jg.writeRaw(_newLine);
545561
level += level; // 2 spaces per level
546562
while (level > SPACE_COUNT) { // should never happen but...
547563
jg.writeRaw(SPACES, 0, SPACE_COUNT);

src/test/java/com/fasterxml/jackson/dataformat/xml/ser/XmlPrettyPrinterTest.java

Lines changed: 92 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,26 @@
44

55
import com.fasterxml.jackson.annotation.JsonInclude;
66
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
7+
import com.fasterxml.jackson.core.PrettyPrinter;
78
import com.fasterxml.jackson.databind.SerializationFeature;
89
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
910
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;
1011
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
1112
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
13+
import com.fasterxml.jackson.dataformat.xml.util.DefaultXmlPrettyPrinter;
1214

1315
public class XmlPrettyPrinterTest extends XmlTestBase
1416
{
1517
static class StringWrapperBean {
1618
public StringWrapper string;
17-
19+
1820
public StringWrapperBean() { }
1921
public StringWrapperBean(String s) { string = new StringWrapper(s); }
2022
}
2123

2224
static class IntWrapperBean {
2325
public IntWrapper wrapped;
24-
26+
2527
public IntWrapperBean() { }
2628
public IntWrapperBean(int i) { wrapped = new IntWrapper(i); }
2729
}
@@ -44,11 +46,11 @@ public class PojoFor123
4446
@JacksonXmlProperty(isAttribute = true)
4547
public String name;
4648

47-
@JsonInclude(JsonInclude.Include.NON_EMPTY)
49+
@JsonInclude(JsonInclude.Include.NON_EMPTY)
4850
public String property;
4951

5052
public PojoFor123(String name) {
51-
this.name = name;
53+
this.name = name;
5254
}
5355
}
5456

@@ -89,6 +91,8 @@ public void setUp() throws Exception {
8991
_xmlMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
9092
}
9193

94+
private static final String SYSTEM_DEFAULT_NEW_LINE = System.getProperty("line.separator");
95+
9296
/*
9397
/**********************************************************
9498
/* Unit tests
@@ -176,16 +180,90 @@ public void testMultiLevel172() throws Exception
176180
.writeValueAsString(root);
177181
// unify possible apostrophes to quotes
178182
xml = a2q(xml);
179-
// with indentation, should get linefeeds in prolog/epilog too
180-
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
181-
+"<Company>\n"
182-
+" <e>\n"
183-
+" <employee>\n"
184-
+" <id>abc</id>\n"
185-
+" <type>FULL_TIME</type>\n"
186-
+" </employee>\n"
187-
+" </e>\n"
188-
+"</Company>\n",
183+
184+
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + SYSTEM_DEFAULT_NEW_LINE
185+
+"<Company>" + SYSTEM_DEFAULT_NEW_LINE
186+
+" <e>" + SYSTEM_DEFAULT_NEW_LINE
187+
+" <employee>" + SYSTEM_DEFAULT_NEW_LINE
188+
+" <id>abc</id>" + SYSTEM_DEFAULT_NEW_LINE
189+
+" <type>FULL_TIME</type>" + SYSTEM_DEFAULT_NEW_LINE
190+
+" </employee>" + SYSTEM_DEFAULT_NEW_LINE
191+
+" </e>" + SYSTEM_DEFAULT_NEW_LINE
192+
+"</Company>" + SYSTEM_DEFAULT_NEW_LINE,
189193
xml);
190194
}
195+
196+
public void testNewLine_withCustomNewLine() throws Exception {
197+
String customNewLine = "\n\rLF\n\r";
198+
PrettyPrinter customXmlPrettyPrinter = new DefaultXmlPrettyPrinter().withCustomNewLine(customNewLine);
199+
200+
Company root = new Company();
201+
root.employee.add(new Employee("abc"));
202+
203+
String xml = _xmlMapper.writer()
204+
.with(customXmlPrettyPrinter)
205+
.with(ToXmlGenerator.Feature.WRITE_XML_DECLARATION)
206+
.writeValueAsString(root);
207+
// unify possible apostrophes to quotes
208+
xml = a2q(xml);
209+
210+
// with indentation, should get newLines in prolog/epilog too
211+
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + customNewLine
212+
+ "<Company>" + customNewLine
213+
+ " <e>" + customNewLine
214+
+ " <employee>" + customNewLine
215+
+ " <id>abc</id>" + customNewLine
216+
+ " <type>FULL_TIME</type>" + customNewLine
217+
+ " </employee>" + customNewLine
218+
+ " </e>" + customNewLine
219+
+ "</Company>" + customNewLine,
220+
xml);
221+
}
222+
223+
public void testNewLine_systemDefault() throws Exception {
224+
Company root = new Company();
225+
root.employee.add(new Employee("abc"));
226+
227+
String xml = _xmlMapper.writer()
228+
.with(new DefaultXmlPrettyPrinter())
229+
.with(ToXmlGenerator.Feature.WRITE_XML_DECLARATION)
230+
.writeValueAsString(root);
231+
// unify possible apostrophes to quotes
232+
xml = a2q(xml);
233+
234+
// with indentation, should get newLines in prolog/epilog too
235+
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + SYSTEM_DEFAULT_NEW_LINE
236+
+ "<Company>" + SYSTEM_DEFAULT_NEW_LINE
237+
+ " <e>" + SYSTEM_DEFAULT_NEW_LINE
238+
+ " <employee>" + SYSTEM_DEFAULT_NEW_LINE
239+
+ " <id>abc</id>" + SYSTEM_DEFAULT_NEW_LINE
240+
+ " <type>FULL_TIME</type>" + SYSTEM_DEFAULT_NEW_LINE
241+
+ " </employee>" + SYSTEM_DEFAULT_NEW_LINE
242+
+ " </e>" + SYSTEM_DEFAULT_NEW_LINE
243+
+ "</Company>" + SYSTEM_DEFAULT_NEW_LINE,
244+
xml);
245+
}
246+
247+
public void testNewLine_UseSystemDefaultLineSeperatorOnNullCustomNewLine() throws Exception {
248+
Company root = new Company();
249+
root.employee.add(new Employee("abc"));
250+
251+
String xml = _xmlMapper.writer()
252+
.with(new DefaultXmlPrettyPrinter().withCustomNewLine(null))
253+
.with(ToXmlGenerator.Feature.WRITE_XML_DECLARATION)
254+
.writeValueAsString(root);
255+
// unify possible apostrophes to quotes
256+
xml = a2q(xml);
257+
258+
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + SYSTEM_DEFAULT_NEW_LINE
259+
+ "<Company>" + SYSTEM_DEFAULT_NEW_LINE
260+
+ " <e>" + SYSTEM_DEFAULT_NEW_LINE
261+
+ " <employee>" + SYSTEM_DEFAULT_NEW_LINE
262+
+ " <id>abc</id>" + SYSTEM_DEFAULT_NEW_LINE
263+
+ " <type>FULL_TIME</type>" + SYSTEM_DEFAULT_NEW_LINE
264+
+ " </employee>" + SYSTEM_DEFAULT_NEW_LINE
265+
+ " </e>" + SYSTEM_DEFAULT_NEW_LINE
266+
+ "</Company>" + SYSTEM_DEFAULT_NEW_LINE,
267+
xml);
268+
}
191269
}

0 commit comments

Comments
 (0)