From 374e12585170809d437d481e97063c9883e524c5 Mon Sep 17 00:00:00 2001 From: joohyukkim Date: Mon, 6 Feb 2023 06:29:28 +0900 Subject: [PATCH 01/22] feature allow custom line feed --- .../jackson/dataformat/xml/XmlFactory.java | 2 +- .../xml/util/DefaultXmlPrettyPrinter.java | 46 +++++++++--- .../xml/ser/XmlPrettyPrinterTest.java | 73 +++++++++++++++++++ 3 files changed, 108 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/fasterxml/jackson/dataformat/xml/XmlFactory.java b/src/main/java/com/fasterxml/jackson/dataformat/xml/XmlFactory.java index ad97266e9..6feb0917a 100644 --- a/src/main/java/com/fasterxml/jackson/dataformat/xml/XmlFactory.java +++ b/src/main/java/com/fasterxml/jackson/dataformat/xml/XmlFactory.java @@ -197,7 +197,7 @@ public XmlFactory copy() { @Override public Version version() { - return PackageVersion.VERSION; + return null; } /* diff --git a/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java b/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java index a0c0bbb61..7d3658047 100644 --- a/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java +++ b/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java @@ -77,6 +77,13 @@ public interface Indenter */ protected boolean _spacesInObjectEntries = true; + /** + * By default, will try to set as System.getProperty("line.separator") + * can later set custom lineFeed with withCustomlineFeed method + * @since 2.15 + */ + protected static String _lineFeed; + /* /********************************************************** /* State @@ -126,6 +133,30 @@ public void indentObjectsWith(Indenter i) public void spacesInObjectEntries(boolean b) { _spacesInObjectEntries = b; } + /** + * Initialize lineFeed with systemDefault. + * @since 2.15 + */ + static { + String lf = null; + try { + lf = System.getProperty("line.separator"); + } catch (Throwable t) { } // access exception? + _lineFeed = lf; + } + + /** + * Sets custom linefeed + * @since 2.15 + */ + public DefaultXmlPrettyPrinter withCustomlineFeed(String lineFeed) { + // 06-Feb-2023, joohyukkim: when JacksonException extends RuntimeExceptions, throw it? + if (lineFeed != null) { + _lineFeed = lineFeed; + } + return this; + } + /* /********************************************************** /* Instantiatable impl @@ -443,7 +474,7 @@ public void writeLeafXsiNilElement(XMLStreamWriter2 sw, public void writePrologLinefeed(XMLStreamWriter2 sw) throws XMLStreamException { // 06-Dec-2015, tatu: Alternatively could try calling `writeSpace()`... - sw.writeRaw(Lf2SpacesIndenter.SYSTEM_LINE_SEPARATOR); + sw.writeRaw(_lineFeed); } /* @@ -506,15 +537,6 @@ protected static class Lf2SpacesIndenter { private static final long serialVersionUID = 1L; - final static String SYSTEM_LINE_SEPARATOR; - static { - String lf = null; - try { - lf = System.getProperty("line.separator"); - } catch (Throwable t) { } // access exception? - SYSTEM_LINE_SEPARATOR = (lf == null) ? "\n" : lf; - } - final static int SPACE_COUNT = 64; final static char[] SPACES = new char[SPACE_COUNT]; static { @@ -529,7 +551,7 @@ public Lf2SpacesIndenter() { } @Override public void writeIndentation(XMLStreamWriter2 sw, int level) throws XMLStreamException { - sw.writeRaw(SYSTEM_LINE_SEPARATOR); + sw.writeRaw(_lineFeed); level += level; // 2 spaces per level while (level > SPACE_COUNT) { // should never happen but... sw.writeRaw(SPACES, 0, SPACE_COUNT); @@ -541,7 +563,7 @@ public void writeIndentation(XMLStreamWriter2 sw, int level) throws XMLStreamExc @Override public void writeIndentation(JsonGenerator jg, int level) throws IOException { - jg.writeRaw(SYSTEM_LINE_SEPARATOR); + jg.writeRaw(_lineFeed); level += level; // 2 spaces per level while (level > SPACE_COUNT) { // should never happen but... jg.writeRaw(SPACES, 0, SPACE_COUNT); diff --git a/src/test/java/com/fasterxml/jackson/dataformat/xml/ser/XmlPrettyPrinterTest.java b/src/test/java/com/fasterxml/jackson/dataformat/xml/ser/XmlPrettyPrinterTest.java index bc0aa59a5..8e2bee811 100644 --- a/src/test/java/com/fasterxml/jackson/dataformat/xml/ser/XmlPrettyPrinterTest.java +++ b/src/test/java/com/fasterxml/jackson/dataformat/xml/ser/XmlPrettyPrinterTest.java @@ -9,6 +9,7 @@ import com.fasterxml.jackson.dataformat.xml.XmlTestBase; import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; +import com.fasterxml.jackson.dataformat.xml.util.DefaultXmlPrettyPrinter; public class XmlPrettyPrinterTest extends XmlTestBase { @@ -188,4 +189,76 @@ public void testMultiLevel172() throws Exception +"\n", xml); } + + public void testLineFeed_withCustomLineFeed() throws Exception { + Company root = new Company(); + root.employee.add(new Employee("abc")); + + String xml = _xmlMapper.writer() + .with(new DefaultXmlPrettyPrinter().withCustomlineFeed("\n\rLF\n\r")) + .with(ToXmlGenerator.Feature.WRITE_XML_DECLARATION) + .writeValueAsString(root); + // unify possible apostrophes to quotes + xml = a2q(xml); + + // with indentation, should get linefeeds in prolog/epilog too + assertEquals("\n\rLF\n\r" + + "\n\rLF\n\r" + + " \n\rLF\n\r" + + " \n\rLF\n\r" + + " abc\n\rLF\n\r" + + " FULL_TIME\n\rLF\n\r" + + " \n\rLF\n\r" + + " \n\rLF\n\r" + + "\n\rLF\n\r", + xml); + } + + public void testLineFeed_systemDefault() throws Exception { + Company root = new Company(); + root.employee.add(new Employee("abc")); + + String xml = _xmlMapper.writer() + .with(new DefaultXmlPrettyPrinter()) + .with(ToXmlGenerator.Feature.WRITE_XML_DECLARATION) + .writeValueAsString(root); + // unify possible apostrophes to quotes + xml = a2q(xml); + + // with indentation, should get linefeeds in prolog/epilog too + assertEquals("\n" + + "\n" + + " \n" + + " \n" + + " abc\n" + + " FULL_TIME\n" + + " \n" + + " \n" + + "\n", + xml); + } + + public void testLineFeed_UseSystemDefaultLineSeperatorOnNullCustomLineFeed() throws Exception { + Company root = new Company(); + root.employee.add(new Employee("abc")); + + String xml = _xmlMapper.writer() + .with(new DefaultXmlPrettyPrinter().withCustomlineFeed(null)) + .with(ToXmlGenerator.Feature.WRITE_XML_DECLARATION) + .writeValueAsString(root); + // unify possible apostrophes to quotes + xml = a2q(xml); + + // with indentation, should get linefeeds in prolog/epilog too + assertEquals("\n" + + "\n" + + " \n" + + " \n" + + " abc\n" + + " FULL_TIME\n" + + " \n" + + " \n" + + "\n", + xml); + } } From eb23bbda773ad0e6e8ea714c2018bc80884be1c1 Mon Sep 17 00:00:00 2001 From: joohyukkim Date: Mon, 6 Feb 2023 06:39:35 +0900 Subject: [PATCH 02/22] Update XmlFactory.java --- .../java/com/fasterxml/jackson/dataformat/xml/XmlFactory.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/fasterxml/jackson/dataformat/xml/XmlFactory.java b/src/main/java/com/fasterxml/jackson/dataformat/xml/XmlFactory.java index 6feb0917a..ad97266e9 100644 --- a/src/main/java/com/fasterxml/jackson/dataformat/xml/XmlFactory.java +++ b/src/main/java/com/fasterxml/jackson/dataformat/xml/XmlFactory.java @@ -197,7 +197,7 @@ public XmlFactory copy() { @Override public Version version() { - return null; + return PackageVersion.VERSION; } /* From c0b3e41ffc5574a014cf82298ce2a58efd52ab91 Mon Sep 17 00:00:00 2001 From: joohyukkim Date: Mon, 6 Feb 2023 06:55:34 +0900 Subject: [PATCH 03/22] Correct method naming --- .../jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java | 2 +- .../jackson/dataformat/xml/ser/XmlPrettyPrinterTest.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java b/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java index 7d3658047..a89e61f81 100644 --- a/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java +++ b/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java @@ -149,7 +149,7 @@ public void indentObjectsWith(Indenter i) * Sets custom linefeed * @since 2.15 */ - public DefaultXmlPrettyPrinter withCustomlineFeed(String lineFeed) { + public DefaultXmlPrettyPrinter withCustomLineFeed(String lineFeed) { // 06-Feb-2023, joohyukkim: when JacksonException extends RuntimeExceptions, throw it? if (lineFeed != null) { _lineFeed = lineFeed; diff --git a/src/test/java/com/fasterxml/jackson/dataformat/xml/ser/XmlPrettyPrinterTest.java b/src/test/java/com/fasterxml/jackson/dataformat/xml/ser/XmlPrettyPrinterTest.java index 8e2bee811..6bda595b5 100644 --- a/src/test/java/com/fasterxml/jackson/dataformat/xml/ser/XmlPrettyPrinterTest.java +++ b/src/test/java/com/fasterxml/jackson/dataformat/xml/ser/XmlPrettyPrinterTest.java @@ -195,7 +195,7 @@ public void testLineFeed_withCustomLineFeed() throws Exception { root.employee.add(new Employee("abc")); String xml = _xmlMapper.writer() - .with(new DefaultXmlPrettyPrinter().withCustomlineFeed("\n\rLF\n\r")) + .with(new DefaultXmlPrettyPrinter().withCustomLineFeed("\n\rLF\n\r")) .with(ToXmlGenerator.Feature.WRITE_XML_DECLARATION) .writeValueAsString(root); // unify possible apostrophes to quotes @@ -243,7 +243,7 @@ public void testLineFeed_UseSystemDefaultLineSeperatorOnNullCustomLineFeed() thr root.employee.add(new Employee("abc")); String xml = _xmlMapper.writer() - .with(new DefaultXmlPrettyPrinter().withCustomlineFeed(null)) + .with(new DefaultXmlPrettyPrinter().withCustomLineFeed(null)) .with(ToXmlGenerator.Feature.WRITE_XML_DECLARATION) .writeValueAsString(root); // unify possible apostrophes to quotes From 3b066cbceb9bbcaeefa1398f8bdd5a8cf9b069db Mon Sep 17 00:00:00 2001 From: joohyukkim Date: Mon, 6 Feb 2023 07:01:40 +0900 Subject: [PATCH 04/22] Set default and static default separately --- .../xml/util/DefaultXmlPrettyPrinter.java | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java b/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java index a89e61f81..d9329b935 100644 --- a/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java +++ b/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java @@ -79,10 +79,24 @@ public interface Indenter /** * By default, will try to set as System.getProperty("line.separator") - * can later set custom lineFeed with withCustomlineFeed method + * Can later set custom lineFeed with withCustomlineFeed method * @since 2.15 */ - protected static String _lineFeed; + private final static String DEFAULT_LINE_FEED; + static { + String lf = null; + try { + lf = System.getProperty("line.separator"); + } catch (Throwable t) { } // access exception? + + if (lf != null) { + DEFAULT_LINE_FEED = lf; + } else { + DEFAULT_LINE_FEED = "\n"; // incase system has changed property name? line.separator + } + } + + protected static String _lineFeed = DEFAULT_LINE_FEED; /* /********************************************************** @@ -133,18 +147,6 @@ public void indentObjectsWith(Indenter i) public void spacesInObjectEntries(boolean b) { _spacesInObjectEntries = b; } - /** - * Initialize lineFeed with systemDefault. - * @since 2.15 - */ - static { - String lf = null; - try { - lf = System.getProperty("line.separator"); - } catch (Throwable t) { } // access exception? - _lineFeed = lf; - } - /** * Sets custom linefeed * @since 2.15 From 85953287ed22f373375fd4ac2f8294b1a7f87f5d Mon Sep 17 00:00:00 2001 From: joohyukkim Date: Mon, 6 Feb 2023 07:12:57 +0900 Subject: [PATCH 05/22] Make _lineFeed non static ic --- .../jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java b/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java index d9329b935..8e0ac1228 100644 --- a/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java +++ b/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java @@ -96,7 +96,7 @@ public interface Indenter } } - protected static String _lineFeed = DEFAULT_LINE_FEED; + protected String _lineFeed = DEFAULT_LINE_FEED; /* /********************************************************** From 79564a47c458825abcd1d833515f3c27e143aa76 Mon Sep 17 00:00:00 2001 From: joohyukkim Date: Mon, 6 Feb 2023 07:13:31 +0900 Subject: [PATCH 06/22] Add dot to end documentation sentences. --- .../jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java b/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java index 8e0ac1228..816c84a99 100644 --- a/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java +++ b/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java @@ -78,8 +78,8 @@ public interface Indenter protected boolean _spacesInObjectEntries = true; /** - * By default, will try to set as System.getProperty("line.separator") - * Can later set custom lineFeed with withCustomlineFeed method + * By default, will try to set as System.getProperty("line.separator"). + * Can later set custom lineFeed with withCustomlineFeed method. * @since 2.15 */ private final static String DEFAULT_LINE_FEED; From f515eba685c159ab726721f7f6fb3312ed37f1e9 Mon Sep 17 00:00:00 2001 From: joohyukkim Date: Mon, 6 Feb 2023 07:25:56 +0900 Subject: [PATCH 07/22] Implment linefeed in XmlPrettyPrinter --- .../dataformat/xml/XmlPrettyPrinter.java | 2 ++ .../xml/util/DefaultXmlPrettyPrinter.java | 17 +++++++++++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/fasterxml/jackson/dataformat/xml/XmlPrettyPrinter.java b/src/main/java/com/fasterxml/jackson/dataformat/xml/XmlPrettyPrinter.java index 3b26e66cb..0599065c9 100644 --- a/src/main/java/com/fasterxml/jackson/dataformat/xml/XmlPrettyPrinter.java +++ b/src/main/java/com/fasterxml/jackson/dataformat/xml/XmlPrettyPrinter.java @@ -98,4 +98,6 @@ public void writeLeafNullElement(XMLStreamWriter2 sw, // public void writeLeafXsiNilElement(XMLStreamWriter2 sw, // String nsURI, String localName) // throws XMLStreamException; + + public String lineFeed(); } diff --git a/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java b/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java index 816c84a99..d0aef1123 100644 --- a/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java +++ b/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java @@ -66,7 +66,7 @@ public interface Indenter * system-specific linefeeds, and 2 spaces per level (as opposed to, * say, single tabs) */ - protected Indenter _objectIndenter = new Lf2SpacesIndenter(); + protected Indenter _objectIndenter = new Lf2SpacesIndenter(this); // // // Config, other white space configuration @@ -459,6 +459,11 @@ public void writeLeafNullElement(XMLStreamWriter2 sw, _justHadStartElement = false; } + @Override + public String lineFeed() { + return _lineFeed; + } + // @since 2.12 public void writeLeafXsiNilElement(XMLStreamWriter2 sw, String nsURI, String localName) @@ -545,7 +550,11 @@ protected static class Lf2SpacesIndenter Arrays.fill(SPACES, ' '); } - public Lf2SpacesIndenter() { } + private final XmlPrettyPrinter xmlPrettyPrinter; + + public Lf2SpacesIndenter(XmlPrettyPrinter xmlPrettyPrinter) { + this.xmlPrettyPrinter = xmlPrettyPrinter; + } @Override public boolean isInline() { return false; } @@ -553,7 +562,7 @@ public Lf2SpacesIndenter() { } @Override public void writeIndentation(XMLStreamWriter2 sw, int level) throws XMLStreamException { - sw.writeRaw(_lineFeed); + sw.writeRaw(xmlPrettyPrinter.lineFeed()); level += level; // 2 spaces per level while (level > SPACE_COUNT) { // should never happen but... sw.writeRaw(SPACES, 0, SPACE_COUNT); @@ -565,7 +574,7 @@ public void writeIndentation(XMLStreamWriter2 sw, int level) throws XMLStreamExc @Override public void writeIndentation(JsonGenerator jg, int level) throws IOException { - jg.writeRaw(_lineFeed); + jg.writeRaw(xmlPrettyPrinter.lineFeed()); level += level; // 2 spaces per level while (level > SPACE_COUNT) { // should never happen but... jg.writeRaw(SPACES, 0, SPACE_COUNT); From 4cc439457b6b64a1a71a15bf90140e74bf7481e9 Mon Sep 17 00:00:00 2001 From: joohyukkim Date: Mon, 6 Feb 2023 07:27:17 +0900 Subject: [PATCH 08/22] Implement linefeed only in DefaultXmlPrettyPrinter --- .../jackson/dataformat/xml/XmlPrettyPrinter.java | 2 -- .../dataformat/xml/util/DefaultXmlPrettyPrinter.java | 10 +++++----- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/fasterxml/jackson/dataformat/xml/XmlPrettyPrinter.java b/src/main/java/com/fasterxml/jackson/dataformat/xml/XmlPrettyPrinter.java index 0599065c9..3b26e66cb 100644 --- a/src/main/java/com/fasterxml/jackson/dataformat/xml/XmlPrettyPrinter.java +++ b/src/main/java/com/fasterxml/jackson/dataformat/xml/XmlPrettyPrinter.java @@ -98,6 +98,4 @@ public void writeLeafNullElement(XMLStreamWriter2 sw, // public void writeLeafXsiNilElement(XMLStreamWriter2 sw, // String nsURI, String localName) // throws XMLStreamException; - - public String lineFeed(); } diff --git a/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java b/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java index d0aef1123..0f61aaa86 100644 --- a/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java +++ b/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java @@ -550,10 +550,10 @@ protected static class Lf2SpacesIndenter Arrays.fill(SPACES, ' '); } - private final XmlPrettyPrinter xmlPrettyPrinter; + private final DefaultXmlPrettyPrinter defaultXmlPrettyPrinter; - public Lf2SpacesIndenter(XmlPrettyPrinter xmlPrettyPrinter) { - this.xmlPrettyPrinter = xmlPrettyPrinter; + public Lf2SpacesIndenter(DefaultXmlPrettyPrinter defaultXmlPrettyPrinter) { + this.defaultXmlPrettyPrinter = defaultXmlPrettyPrinter; } @Override @@ -562,7 +562,7 @@ public Lf2SpacesIndenter(XmlPrettyPrinter xmlPrettyPrinter) { @Override public void writeIndentation(XMLStreamWriter2 sw, int level) throws XMLStreamException { - sw.writeRaw(xmlPrettyPrinter.lineFeed()); + sw.writeRaw(defaultXmlPrettyPrinter.lineFeed()); level += level; // 2 spaces per level while (level > SPACE_COUNT) { // should never happen but... sw.writeRaw(SPACES, 0, SPACE_COUNT); @@ -574,7 +574,7 @@ public void writeIndentation(XMLStreamWriter2 sw, int level) throws XMLStreamExc @Override public void writeIndentation(JsonGenerator jg, int level) throws IOException { - jg.writeRaw(xmlPrettyPrinter.lineFeed()); + jg.writeRaw(defaultXmlPrettyPrinter.lineFeed()); level += level; // 2 spaces per level while (level > SPACE_COUNT) { // should never happen but... jg.writeRaw(SPACES, 0, SPACE_COUNT); From 6974014a9c5db9db273df4f3c0fc664985adeb34 Mon Sep 17 00:00:00 2001 From: joohyukkim Date: Mon, 6 Feb 2023 07:33:47 +0900 Subject: [PATCH 09/22] Update DefaultXmlPrettyPrinter.java --- .../xml/util/DefaultXmlPrettyPrinter.java | 35 ++++++------------- 1 file changed, 10 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java b/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java index 0f61aaa86..1594ff7ff 100644 --- a/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java +++ b/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java @@ -66,7 +66,7 @@ public interface Indenter * system-specific linefeeds, and 2 spaces per level (as opposed to, * say, single tabs) */ - protected Indenter _objectIndenter = new Lf2SpacesIndenter(this); + protected Indenter _objectIndenter = new Lf2SpacesIndenter(); // // // Config, other white space configuration @@ -88,12 +88,7 @@ public interface Indenter try { lf = System.getProperty("line.separator"); } catch (Throwable t) { } // access exception? - - if (lf != null) { - DEFAULT_LINE_FEED = lf; - } else { - DEFAULT_LINE_FEED = "\n"; // incase system has changed property name? line.separator - } + DEFAULT_LINE_FEED = lf; } protected String _lineFeed = DEFAULT_LINE_FEED; @@ -459,11 +454,6 @@ public void writeLeafNullElement(XMLStreamWriter2 sw, _justHadStartElement = false; } - @Override - public String lineFeed() { - return _lineFeed; - } - // @since 2.12 public void writeLeafXsiNilElement(XMLStreamWriter2 sw, String nsURI, String localName) @@ -495,7 +485,7 @@ public void writePrologLinefeed(XMLStreamWriter2 sw) throws XMLStreamException /** * Dummy implementation that adds no indentation whatsoever */ - protected static class NopIndenter + protected class NopIndenter implements Indenter, java.io.Serializable { private static final long serialVersionUID = 1L; @@ -511,7 +501,7 @@ public NopIndenter() { } * single space for indentation. It is used as the default * indenter for array values. */ - protected static class FixedSpaceIndenter + protected class FixedSpaceIndenter implements Indenter, java.io.Serializable { private static final long serialVersionUID = 1L; @@ -539,21 +529,16 @@ public void writeIndentation(JsonGenerator g, int level) throws IOException * Default linefeed-based indenter uses system-specific linefeeds and * 2 spaces for indentation per level. */ - protected static class Lf2SpacesIndenter + protected class Lf2SpacesIndenter implements Indenter, java.io.Serializable { private static final long serialVersionUID = 1L; final static int SPACE_COUNT = 64; - final static char[] SPACES = new char[SPACE_COUNT]; - static { - Arrays.fill(SPACES, ' '); - } + final char[] SPACES = new char[SPACE_COUNT]; - private final DefaultXmlPrettyPrinter defaultXmlPrettyPrinter; - - public Lf2SpacesIndenter(DefaultXmlPrettyPrinter defaultXmlPrettyPrinter) { - this.defaultXmlPrettyPrinter = defaultXmlPrettyPrinter; + public Lf2SpacesIndenter() { + Arrays.fill(SPACES, ' '); } @Override @@ -562,7 +547,7 @@ public Lf2SpacesIndenter(DefaultXmlPrettyPrinter defaultXmlPrettyPrinter) { @Override public void writeIndentation(XMLStreamWriter2 sw, int level) throws XMLStreamException { - sw.writeRaw(defaultXmlPrettyPrinter.lineFeed()); + sw.writeRaw(_lineFeed); level += level; // 2 spaces per level while (level > SPACE_COUNT) { // should never happen but... sw.writeRaw(SPACES, 0, SPACE_COUNT); @@ -574,7 +559,7 @@ public void writeIndentation(XMLStreamWriter2 sw, int level) throws XMLStreamExc @Override public void writeIndentation(JsonGenerator jg, int level) throws IOException { - jg.writeRaw(defaultXmlPrettyPrinter.lineFeed()); + jg.writeRaw(_lineFeed); level += level; // 2 spaces per level while (level > SPACE_COUNT) { // should never happen but... jg.writeRaw(SPACES, 0, SPACE_COUNT); From b75bfddce532d877cc89562de55893f0bd21743a Mon Sep 17 00:00:00 2001 From: joohyukkim Date: Mon, 6 Feb 2023 07:49:26 +0900 Subject: [PATCH 10/22] XML declaration not part of linefeed? --- .../jackson/dataformat/xml/ser/XmlPrettyPrinterTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/fasterxml/jackson/dataformat/xml/ser/XmlPrettyPrinterTest.java b/src/test/java/com/fasterxml/jackson/dataformat/xml/ser/XmlPrettyPrinterTest.java index 6bda595b5..91fcad841 100644 --- a/src/test/java/com/fasterxml/jackson/dataformat/xml/ser/XmlPrettyPrinterTest.java +++ b/src/test/java/com/fasterxml/jackson/dataformat/xml/ser/XmlPrettyPrinterTest.java @@ -202,7 +202,7 @@ public void testLineFeed_withCustomLineFeed() throws Exception { xml = a2q(xml); // with indentation, should get linefeeds in prolog/epilog too - assertEquals("\n\rLF\n\r" + assertEquals("\n" + "\n\rLF\n\r" + " \n\rLF\n\r" + " \n\rLF\n\r" @@ -210,7 +210,7 @@ public void testLineFeed_withCustomLineFeed() throws Exception { + " FULL_TIME\n\rLF\n\r" + " \n\rLF\n\r" + " \n\rLF\n\r" - + "\n\rLF\n\r", + + "\n", xml); } From 18abcc2fc66e66af53a066203fd42863f8c4f3e3 Mon Sep 17 00:00:00 2001 From: joohyukkim Date: Mon, 6 Feb 2023 21:43:16 +0900 Subject: [PATCH 11/22] Refactor : change names to newLine --- .../xml/util/DefaultXmlPrettyPrinter.java | 18 +++++++++--------- .../xml/ser/XmlPrettyPrinterTest.java | 4 ++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java b/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java index 1594ff7ff..35bb8f8dc 100644 --- a/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java +++ b/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java @@ -82,16 +82,16 @@ public interface Indenter * Can later set custom lineFeed with withCustomlineFeed method. * @since 2.15 */ - private final static String DEFAULT_LINE_FEED; + private final static String SYSTEM_DEFAULT_NEW_LINE; static { String lf = null; try { lf = System.getProperty("line.separator"); } catch (Throwable t) { } // access exception? - DEFAULT_LINE_FEED = lf; + SYSTEM_DEFAULT_NEW_LINE = lf; } - protected String _lineFeed = DEFAULT_LINE_FEED; + protected String _newLine = SYSTEM_DEFAULT_NEW_LINE; /* /********************************************************** @@ -146,10 +146,10 @@ public void indentObjectsWith(Indenter i) * Sets custom linefeed * @since 2.15 */ - public DefaultXmlPrettyPrinter withCustomLineFeed(String lineFeed) { + public DefaultXmlPrettyPrinter withCustomNewLine(String newLine) { // 06-Feb-2023, joohyukkim: when JacksonException extends RuntimeExceptions, throw it? - if (lineFeed != null) { - _lineFeed = lineFeed; + if (newLine != null) { + _newLine = newLine; } return this; } @@ -471,7 +471,7 @@ public void writeLeafXsiNilElement(XMLStreamWriter2 sw, public void writePrologLinefeed(XMLStreamWriter2 sw) throws XMLStreamException { // 06-Dec-2015, tatu: Alternatively could try calling `writeSpace()`... - sw.writeRaw(_lineFeed); + sw.writeRaw(_newLine); } /* @@ -547,7 +547,7 @@ public Lf2SpacesIndenter() { @Override public void writeIndentation(XMLStreamWriter2 sw, int level) throws XMLStreamException { - sw.writeRaw(_lineFeed); + sw.writeRaw(_newLine); level += level; // 2 spaces per level while (level > SPACE_COUNT) { // should never happen but... sw.writeRaw(SPACES, 0, SPACE_COUNT); @@ -559,7 +559,7 @@ public void writeIndentation(XMLStreamWriter2 sw, int level) throws XMLStreamExc @Override public void writeIndentation(JsonGenerator jg, int level) throws IOException { - jg.writeRaw(_lineFeed); + jg.writeRaw(_newLine); level += level; // 2 spaces per level while (level > SPACE_COUNT) { // should never happen but... jg.writeRaw(SPACES, 0, SPACE_COUNT); diff --git a/src/test/java/com/fasterxml/jackson/dataformat/xml/ser/XmlPrettyPrinterTest.java b/src/test/java/com/fasterxml/jackson/dataformat/xml/ser/XmlPrettyPrinterTest.java index 91fcad841..7af64760e 100644 --- a/src/test/java/com/fasterxml/jackson/dataformat/xml/ser/XmlPrettyPrinterTest.java +++ b/src/test/java/com/fasterxml/jackson/dataformat/xml/ser/XmlPrettyPrinterTest.java @@ -195,7 +195,7 @@ public void testLineFeed_withCustomLineFeed() throws Exception { root.employee.add(new Employee("abc")); String xml = _xmlMapper.writer() - .with(new DefaultXmlPrettyPrinter().withCustomLineFeed("\n\rLF\n\r")) + .with(new DefaultXmlPrettyPrinter().withCustomNewLine("\n\rLF\n\r")) .with(ToXmlGenerator.Feature.WRITE_XML_DECLARATION) .writeValueAsString(root); // unify possible apostrophes to quotes @@ -243,7 +243,7 @@ public void testLineFeed_UseSystemDefaultLineSeperatorOnNullCustomLineFeed() thr root.employee.add(new Employee("abc")); String xml = _xmlMapper.writer() - .with(new DefaultXmlPrettyPrinter().withCustomLineFeed(null)) + .with(new DefaultXmlPrettyPrinter().withCustomNewLine(null)) .with(ToXmlGenerator.Feature.WRITE_XML_DECLARATION) .writeValueAsString(root); // unify possible apostrophes to quotes From c7641894775545ffdb0a52c75b0082dd37b17fca Mon Sep 17 00:00:00 2001 From: joohyukkim Date: Fri, 10 Feb 2023 20:24:03 +0900 Subject: [PATCH 12/22] withCustomNewLine() method... --- .../jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java b/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java index 35bb8f8dc..e4b7f4a3e 100644 --- a/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java +++ b/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java @@ -79,7 +79,7 @@ public interface Indenter /** * By default, will try to set as System.getProperty("line.separator"). - * Can later set custom lineFeed with withCustomlineFeed method. + * Can later set custom new line with withCustomNewLine method. * @since 2.15 */ private final static String SYSTEM_DEFAULT_NEW_LINE; From af70ece206e80f21b2730c311567379f8bd631c4 Mon Sep 17 00:00:00 2001 From: joohyukkim Date: Fri, 10 Feb 2023 20:26:46 +0900 Subject: [PATCH 13/22] Apply review --- .../jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java b/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java index e4b7f4a3e..ed187b9b8 100644 --- a/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java +++ b/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java @@ -87,7 +87,7 @@ public interface Indenter String lf = null; try { lf = System.getProperty("line.separator"); - } catch (Throwable t) { } // access exception? + } catch (Exception t) { } // access exception? SYSTEM_DEFAULT_NEW_LINE = lf; } @@ -147,7 +147,6 @@ public void indentObjectsWith(Indenter i) * @since 2.15 */ public DefaultXmlPrettyPrinter withCustomNewLine(String newLine) { - // 06-Feb-2023, joohyukkim: when JacksonException extends RuntimeExceptions, throw it? if (newLine != null) { _newLine = newLine; } @@ -485,7 +484,7 @@ public void writePrologLinefeed(XMLStreamWriter2 sw) throws XMLStreamException /** * Dummy implementation that adds no indentation whatsoever */ - protected class NopIndenter + protected static class NopIndenter implements Indenter, java.io.Serializable { private static final long serialVersionUID = 1L; From 7deeb8447cc8032ee40f8803a8c4a9d0ae7314b2 Mon Sep 17 00:00:00 2001 From: joohyukkim Date: Fri, 10 Feb 2023 20:28:40 +0900 Subject: [PATCH 14/22] Fix : make FixedSpaceIndenter static --- .../jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java b/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java index ed187b9b8..1edc6592b 100644 --- a/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java +++ b/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java @@ -500,7 +500,7 @@ public NopIndenter() { } * single space for indentation. It is used as the default * indenter for array values. */ - protected class FixedSpaceIndenter + protected static class FixedSpaceIndenter implements Indenter, java.io.Serializable { private static final long serialVersionUID = 1L; From 83602765c64da8cee0482c603d46da374aa24f08 Mon Sep 17 00:00:00 2001 From: joohyukkim Date: Wed, 15 Feb 2023 08:33:05 +0900 Subject: [PATCH 15/22] Refactor : rename from linefeed to newline --- .../dataformat/xml/ser/XmlPrettyPrinterTest.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/test/java/com/fasterxml/jackson/dataformat/xml/ser/XmlPrettyPrinterTest.java b/src/test/java/com/fasterxml/jackson/dataformat/xml/ser/XmlPrettyPrinterTest.java index 7af64760e..ea762beba 100644 --- a/src/test/java/com/fasterxml/jackson/dataformat/xml/ser/XmlPrettyPrinterTest.java +++ b/src/test/java/com/fasterxml/jackson/dataformat/xml/ser/XmlPrettyPrinterTest.java @@ -177,7 +177,7 @@ public void testMultiLevel172() throws Exception .writeValueAsString(root); // unify possible apostrophes to quotes xml = a2q(xml); - // with indentation, should get linefeeds in prolog/epilog too + assertEquals("\n" +"\n" +" \n" @@ -190,7 +190,7 @@ public void testMultiLevel172() throws Exception xml); } - public void testLineFeed_withCustomLineFeed() throws Exception { + public void testNewLine_withCustomNewLine() throws Exception { Company root = new Company(); root.employee.add(new Employee("abc")); @@ -201,7 +201,7 @@ public void testLineFeed_withCustomLineFeed() throws Exception { // unify possible apostrophes to quotes xml = a2q(xml); - // with indentation, should get linefeeds in prolog/epilog too + // with indentation, should get newLines in prolog/epilog too assertEquals("\n" + "\n\rLF\n\r" + " \n\rLF\n\r" @@ -214,7 +214,7 @@ public void testLineFeed_withCustomLineFeed() throws Exception { xml); } - public void testLineFeed_systemDefault() throws Exception { + public void testNewLine_systemDefault() throws Exception { Company root = new Company(); root.employee.add(new Employee("abc")); @@ -225,7 +225,7 @@ public void testLineFeed_systemDefault() throws Exception { // unify possible apostrophes to quotes xml = a2q(xml); - // with indentation, should get linefeeds in prolog/epilog too + // with indentation, should get newLines in prolog/epilog too assertEquals("\n" + "\n" + " \n" @@ -238,7 +238,7 @@ public void testLineFeed_systemDefault() throws Exception { xml); } - public void testLineFeed_UseSystemDefaultLineSeperatorOnNullCustomLineFeed() throws Exception { + public void testNewLine_UseSystemDefaultLineSeperatorOnNullCustomNewLine() throws Exception { Company root = new Company(); root.employee.add(new Employee("abc")); @@ -249,7 +249,6 @@ public void testLineFeed_UseSystemDefaultLineSeperatorOnNullCustomLineFeed() thr // unify possible apostrophes to quotes xml = a2q(xml); - // with indentation, should get linefeeds in prolog/epilog too assertEquals("\n" + "\n" + " \n" From 6187f3d7fc5b842958fa5ac0fecabd6e7a0cc651 Mon Sep 17 00:00:00 2001 From: joohyukkim Date: Wed, 15 Feb 2023 08:35:27 +0900 Subject: [PATCH 16/22] move SPACES char[] to outer DefaultXmlPrettyPrinter --- .../xml/util/DefaultXmlPrettyPrinter.java | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java b/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java index 1edc6592b..ae2a1b23d 100644 --- a/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java +++ b/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java @@ -82,7 +82,7 @@ public interface Indenter * Can later set custom new line with withCustomNewLine method. * @since 2.15 */ - private final static String SYSTEM_DEFAULT_NEW_LINE; + private static final String SYSTEM_DEFAULT_NEW_LINE; static { String lf = null; try { @@ -90,9 +90,14 @@ public interface Indenter } catch (Exception t) { } // access exception? SYSTEM_DEFAULT_NEW_LINE = lf; } - protected String _newLine = SYSTEM_DEFAULT_NEW_LINE; + static final int SPACE_COUNT = 64; + static final char[] SPACES = new char[SPACE_COUNT]; + static { + Arrays.fill(SPACES, ' '); + } + /* /********************************************************** /* State @@ -143,7 +148,7 @@ public void indentObjectsWith(Indenter i) public void spacesInObjectEntries(boolean b) { _spacesInObjectEntries = b; } /** - * Sets custom linefeed + * Sets custom new-line * @since 2.15 */ public DefaultXmlPrettyPrinter withCustomNewLine(String newLine) { @@ -513,7 +518,7 @@ public void writeIndentation(XMLStreamWriter2 sw, int level) { sw.writeRaw(" "); } - + @Override public void writeIndentation(JsonGenerator g, int level) throws IOException { @@ -533,13 +538,6 @@ protected class Lf2SpacesIndenter { private static final long serialVersionUID = 1L; - final static int SPACE_COUNT = 64; - final char[] SPACES = new char[SPACE_COUNT]; - - public Lf2SpacesIndenter() { - Arrays.fill(SPACES, ' '); - } - @Override public boolean isInline() { return false; } From d0aaf640485341e66a0198131858b8ec2ebc1116 Mon Sep 17 00:00:00 2001 From: joohyukkim Date: Wed, 15 Feb 2023 08:38:16 +0900 Subject: [PATCH 17/22] Add default empty constructor --- .../jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java b/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java index ae2a1b23d..770339ae2 100644 --- a/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java +++ b/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java @@ -538,6 +538,8 @@ protected class Lf2SpacesIndenter { private static final long serialVersionUID = 1L; + public Lf2SpacesIndenter() { } + @Override public boolean isInline() { return false; } From 29a2bbf211e7bc3a35bd0f022e42049088588cec Mon Sep 17 00:00:00 2001 From: joohyukkim Date: Fri, 17 Feb 2023 00:13:08 +0900 Subject: [PATCH 18/22] withCustomNewLine revert back to SYSTEM_DEFAULT_NEW_LINE if null is passed --- .../dataformat/xml/util/DefaultXmlPrettyPrinter.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java b/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java index 770339ae2..1eeceb997 100644 --- a/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java +++ b/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java @@ -148,13 +148,11 @@ public void indentObjectsWith(Indenter i) public void spacesInObjectEntries(boolean b) { _spacesInObjectEntries = b; } /** - * Sets custom new-line + * Sets custom new-line. * @since 2.15 */ public DefaultXmlPrettyPrinter withCustomNewLine(String newLine) { - if (newLine != null) { - _newLine = newLine; - } + _newLine = newLine != null ? newLine : SYSTEM_DEFAULT_NEW_LINE; return this; } From fc4fb17222f9626ab32320bef0f9b1e636e98044 Mon Sep 17 00:00:00 2001 From: joohyukkim Date: Fri, 17 Feb 2023 00:28:54 +0900 Subject: [PATCH 19/22] replace \n with system linefeed to make platform independent test --- .../xml/ser/XmlPrettyPrinterTest.java | 98 ++++++++++--------- 1 file changed, 53 insertions(+), 45 deletions(-) diff --git a/src/test/java/com/fasterxml/jackson/dataformat/xml/ser/XmlPrettyPrinterTest.java b/src/test/java/com/fasterxml/jackson/dataformat/xml/ser/XmlPrettyPrinterTest.java index ea762beba..7c8634c84 100644 --- a/src/test/java/com/fasterxml/jackson/dataformat/xml/ser/XmlPrettyPrinterTest.java +++ b/src/test/java/com/fasterxml/jackson/dataformat/xml/ser/XmlPrettyPrinterTest.java @@ -11,51 +11,57 @@ import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; import com.fasterxml.jackson.dataformat.xml.util.DefaultXmlPrettyPrinter; -public class XmlPrettyPrinterTest extends XmlTestBase -{ +public class XmlPrettyPrinterTest extends XmlTestBase { static class StringWrapperBean { public StringWrapper string; - - public StringWrapperBean() { } - public StringWrapperBean(String s) { string = new StringWrapper(s); } + + public StringWrapperBean() { + } + + public StringWrapperBean(String s) { + string = new StringWrapper(s); + } } static class IntWrapperBean { public IntWrapper wrapped; - - public IntWrapperBean() { } - public IntWrapperBean(int i) { wrapped = new IntWrapper(i); } + + public IntWrapperBean() { + } + + public IntWrapperBean(int i) { + wrapped = new IntWrapper(i); + } } // [dataformat-xml#45] static class AttrBean { - @JacksonXmlProperty(isAttribute=true) + @JacksonXmlProperty(isAttribute = true) public int count = 3; } static class AttrBean2 { - @JacksonXmlProperty(isAttribute=true) + @JacksonXmlProperty(isAttribute = true) public int count = 3; public int value = 14; } - public class PojoFor123 - { + public class PojoFor123 { @JacksonXmlProperty(isAttribute = true) public String name; - @JsonInclude(JsonInclude.Include.NON_EMPTY) + @JsonInclude(JsonInclude.Include.NON_EMPTY) public String property; - + public PojoFor123(String name) { - this.name = name; + this.name = name; } } // for [dataformat-xml#172] static class Company { - @JacksonXmlElementWrapper(localName="e") + @JacksonXmlElementWrapper(localName = "e") public List employee = new ArrayList(); } @@ -90,6 +96,8 @@ public void setUp() throws Exception { _xmlMapper.configure(SerializationFeature.INDENT_OUTPUT, true); } + private static final String SYSTEM_DEFAULT_NEW_LINE = System.getProperty("line.separator"); + /* /********************************************************** /* Unit tests @@ -178,15 +186,15 @@ public void testMultiLevel172() throws Exception // unify possible apostrophes to quotes xml = a2q(xml); - assertEquals("\n" - +"\n" - +" \n" - +" \n" - +" abc\n" - +" FULL_TIME\n" - +" \n" - +" \n" - +"\n", + assertEquals("" + SYSTEM_DEFAULT_NEW_LINE + +"" + SYSTEM_DEFAULT_NEW_LINE + +" " + SYSTEM_DEFAULT_NEW_LINE + +" " + SYSTEM_DEFAULT_NEW_LINE + +" abc" + SYSTEM_DEFAULT_NEW_LINE + +" FULL_TIME" + SYSTEM_DEFAULT_NEW_LINE + +" " + SYSTEM_DEFAULT_NEW_LINE + +" " + SYSTEM_DEFAULT_NEW_LINE + +"" + SYSTEM_DEFAULT_NEW_LINE, xml); } @@ -202,7 +210,7 @@ public void testNewLine_withCustomNewLine() throws Exception { xml = a2q(xml); // with indentation, should get newLines in prolog/epilog too - assertEquals("\n" + assertEquals("" + SYSTEM_DEFAULT_NEW_LINE + "\n\rLF\n\r" + " \n\rLF\n\r" + " \n\rLF\n\r" @@ -210,7 +218,7 @@ public void testNewLine_withCustomNewLine() throws Exception { + " FULL_TIME\n\rLF\n\r" + " \n\rLF\n\r" + " \n\rLF\n\r" - + "\n", + + "" + SYSTEM_DEFAULT_NEW_LINE, xml); } @@ -226,15 +234,15 @@ public void testNewLine_systemDefault() throws Exception { xml = a2q(xml); // with indentation, should get newLines in prolog/epilog too - assertEquals("\n" - + "\n" - + " \n" - + " \n" - + " abc\n" - + " FULL_TIME\n" - + " \n" - + " \n" - + "\n", + assertEquals("" + SYSTEM_DEFAULT_NEW_LINE + + "" + SYSTEM_DEFAULT_NEW_LINE + + " " + SYSTEM_DEFAULT_NEW_LINE + + " " + SYSTEM_DEFAULT_NEW_LINE + + " abc" + SYSTEM_DEFAULT_NEW_LINE + + " FULL_TIME" + SYSTEM_DEFAULT_NEW_LINE + + " " + SYSTEM_DEFAULT_NEW_LINE + + " " + SYSTEM_DEFAULT_NEW_LINE + + "" + SYSTEM_DEFAULT_NEW_LINE, xml); } @@ -249,15 +257,15 @@ public void testNewLine_UseSystemDefaultLineSeperatorOnNullCustomNewLine() throw // unify possible apostrophes to quotes xml = a2q(xml); - assertEquals("\n" - + "\n" - + " \n" - + " \n" - + " abc\n" - + " FULL_TIME\n" - + " \n" - + " \n" - + "\n", + assertEquals("" + SYSTEM_DEFAULT_NEW_LINE + + "" + SYSTEM_DEFAULT_NEW_LINE + + " " + SYSTEM_DEFAULT_NEW_LINE + + " " + SYSTEM_DEFAULT_NEW_LINE + + " abc" + SYSTEM_DEFAULT_NEW_LINE + + " FULL_TIME" + SYSTEM_DEFAULT_NEW_LINE + + " " + SYSTEM_DEFAULT_NEW_LINE + + " " + SYSTEM_DEFAULT_NEW_LINE + + "" + SYSTEM_DEFAULT_NEW_LINE, xml); } } From dd198f209991046cc4fcf0383ee778f0f17b9618 Mon Sep 17 00:00:00 2001 From: joohyukkim Date: Fri, 17 Feb 2023 00:38:02 +0900 Subject: [PATCH 20/22] Update DefaultXmlPrettyPrinter copy constructor --- .../xml/util/DefaultXmlPrettyPrinter.java | 1 + .../xml/ser/XmlPrettyPrinterTest.java | 24 +++++++++++-------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java b/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java index 1eeceb997..aca070536 100644 --- a/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java +++ b/src/main/java/com/fasterxml/jackson/dataformat/xml/util/DefaultXmlPrettyPrinter.java @@ -133,6 +133,7 @@ protected DefaultXmlPrettyPrinter(DefaultXmlPrettyPrinter base) _objectIndenter = base._objectIndenter; _spacesInObjectEntries = base._spacesInObjectEntries; _nesting = base._nesting; + _newLine = base._newLine; } public void indentArraysWith(Indenter i) diff --git a/src/test/java/com/fasterxml/jackson/dataformat/xml/ser/XmlPrettyPrinterTest.java b/src/test/java/com/fasterxml/jackson/dataformat/xml/ser/XmlPrettyPrinterTest.java index 7c8634c84..2f361cab1 100644 --- a/src/test/java/com/fasterxml/jackson/dataformat/xml/ser/XmlPrettyPrinterTest.java +++ b/src/test/java/com/fasterxml/jackson/dataformat/xml/ser/XmlPrettyPrinterTest.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonPropertyOrder; +import com.fasterxml.jackson.core.PrettyPrinter; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.dataformat.xml.XmlMapper; import com.fasterxml.jackson.dataformat.xml.XmlTestBase; @@ -199,26 +200,29 @@ public void testMultiLevel172() throws Exception } public void testNewLine_withCustomNewLine() throws Exception { + String customNewLine = "\n\rLF\n\r"; + PrettyPrinter customXmlPrettyPrinter = new DefaultXmlPrettyPrinter().withCustomNewLine(customNewLine); + Company root = new Company(); root.employee.add(new Employee("abc")); String xml = _xmlMapper.writer() - .with(new DefaultXmlPrettyPrinter().withCustomNewLine("\n\rLF\n\r")) + .with(customXmlPrettyPrinter) .with(ToXmlGenerator.Feature.WRITE_XML_DECLARATION) .writeValueAsString(root); // unify possible apostrophes to quotes xml = a2q(xml); // with indentation, should get newLines in prolog/epilog too - assertEquals("" + SYSTEM_DEFAULT_NEW_LINE - + "\n\rLF\n\r" - + " \n\rLF\n\r" - + " \n\rLF\n\r" - + " abc\n\rLF\n\r" - + " FULL_TIME\n\rLF\n\r" - + " \n\rLF\n\r" - + " \n\rLF\n\r" - + "" + SYSTEM_DEFAULT_NEW_LINE, + assertEquals("" + customNewLine + + "" + customNewLine + + " " + customNewLine + + " " + customNewLine + + " abc" + customNewLine + + " FULL_TIME" + customNewLine + + " " + customNewLine + + " " + customNewLine + + "" + customNewLine, xml); } From b43811c5e7e2fd68b0d56d312467bf6920399508 Mon Sep 17 00:00:00 2001 From: joohyukkim Date: Fri, 17 Feb 2023 00:50:44 +0900 Subject: [PATCH 21/22] Remove unrelated changes --- .../xml/ser/XmlPrettyPrinterTest.java | 28 ++++++++----------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/src/test/java/com/fasterxml/jackson/dataformat/xml/ser/XmlPrettyPrinterTest.java b/src/test/java/com/fasterxml/jackson/dataformat/xml/ser/XmlPrettyPrinterTest.java index 2f361cab1..52bae5657 100644 --- a/src/test/java/com/fasterxml/jackson/dataformat/xml/ser/XmlPrettyPrinterTest.java +++ b/src/test/java/com/fasterxml/jackson/dataformat/xml/ser/XmlPrettyPrinterTest.java @@ -12,43 +12,37 @@ import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; import com.fasterxml.jackson.dataformat.xml.util.DefaultXmlPrettyPrinter; -public class XmlPrettyPrinterTest extends XmlTestBase { +public class XmlPrettyPrinterTest extends XmlTestBase +{ static class StringWrapperBean { public StringWrapper string; - public StringWrapperBean() { - } - - public StringWrapperBean(String s) { - string = new StringWrapper(s); - } + public StringWrapperBean() { } + public StringWrapperBean(String s) { string = new StringWrapper(s); } } static class IntWrapperBean { public IntWrapper wrapped; - public IntWrapperBean() { - } - - public IntWrapperBean(int i) { - wrapped = new IntWrapper(i); - } + public IntWrapperBean() { } + public IntWrapperBean(int i) { wrapped = new IntWrapper(i); } } // [dataformat-xml#45] static class AttrBean { - @JacksonXmlProperty(isAttribute = true) + @JacksonXmlProperty(isAttribute=true) public int count = 3; } static class AttrBean2 { - @JacksonXmlProperty(isAttribute = true) + @JacksonXmlProperty(isAttribute=true) public int count = 3; public int value = 14; } - public class PojoFor123 { + public class PojoFor123 + { @JacksonXmlProperty(isAttribute = true) public String name; @@ -62,7 +56,7 @@ public PojoFor123(String name) { // for [dataformat-xml#172] static class Company { - @JacksonXmlElementWrapper(localName = "e") + @JacksonXmlElementWrapper(localName="e") public List employee = new ArrayList(); } From 05b1f048bd6c293e4917901c23c1205baf5c694c Mon Sep 17 00:00:00 2001 From: joohyukkim Date: Fri, 17 Feb 2023 00:53:59 +0900 Subject: [PATCH 22/22] Remove unrelated formatting changes --- .../jackson/dataformat/xml/ser/XmlPrettyPrinterTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/fasterxml/jackson/dataformat/xml/ser/XmlPrettyPrinterTest.java b/src/test/java/com/fasterxml/jackson/dataformat/xml/ser/XmlPrettyPrinterTest.java index 52bae5657..d7f76dce3 100644 --- a/src/test/java/com/fasterxml/jackson/dataformat/xml/ser/XmlPrettyPrinterTest.java +++ b/src/test/java/com/fasterxml/jackson/dataformat/xml/ser/XmlPrettyPrinterTest.java @@ -48,7 +48,7 @@ public class PojoFor123 @JsonInclude(JsonInclude.Include.NON_EMPTY) public String property; - + public PojoFor123(String name) { this.name = name; }