From c6ec2f0e4cadf6c9efbcfa1245f3182ef50be292 Mon Sep 17 00:00:00 2001 From: rudrajyoti biswas Date: Wed, 25 Oct 2023 23:23:00 +0530 Subject: [PATCH 1/3] #748 - close XML tag explicitly for empty tags with configuration. --- src/main/java/org/json/XML.java | 25 ++++++++++++++----- .../java/org/json/XMLParserConfiguration.java | 22 ++++++++++++++-- .../org/json/junit/XMLConfigurationTest.java | 11 ++++++++ src/test/java/org/json/junit/XMLTest.java | 20 +++++++++++++++ 4 files changed, 70 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/json/XML.java b/src/main/java/org/json/XML.java index 5331923..efdfd9f 100644 --- a/src/main/java/org/json/XML.java +++ b/src/main/java/org/json/XML.java @@ -877,12 +877,25 @@ public class XML { } } } else if ("".equals(value)) { - sb.append(indent(indent)); - sb.append('<'); - sb.append(key); - sb.append("/>"); - if(indentFactor > 0){ - sb.append("\n"); + if (config.isCloseEmptyTag()){ + sb.append(indent(indent)); + sb.append('<'); + sb.append(key); + sb.append(">"); + sb.append(""); + if (indentFactor > 0) { + sb.append("\n"); + } + }else { + sb.append(indent(indent)); + sb.append('<'); + sb.append(key); + sb.append("/>"); + if (indentFactor > 0) { + sb.append("\n"); + } } // Emit a new tag diff --git a/src/main/java/org/json/XMLParserConfiguration.java b/src/main/java/org/json/XMLParserConfiguration.java index 566146d..5d7ecfa 100644 --- a/src/main/java/org/json/XMLParserConfiguration.java +++ b/src/main/java/org/json/XMLParserConfiguration.java @@ -43,6 +43,13 @@ public class XMLParserConfiguration extends ParserConfiguration { */ private boolean convertNilAttributeToNull; + /** + * When creating an XML from JSON Object, an empty tag by default will self-close. + * If it has to be closed explicitly, with empty content between start and end tag, + * this flag is to be turned on. + */ + private boolean closeEmptyTag; + /** * This will allow type conversion for values in XML if xsi:type attribute is defined */ @@ -145,12 +152,13 @@ public class XMLParserConfiguration extends ParserConfiguration { */ private XMLParserConfiguration (final boolean keepStrings, final String cDataTagName, final boolean convertNilAttributeToNull, final Map> xsiTypeMap, final Set forceList, - final int maxNestingDepth) { + final int maxNestingDepth, final boolean closeEmptyTag) { super(keepStrings, maxNestingDepth); this.cDataTagName = cDataTagName; this.convertNilAttributeToNull = convertNilAttributeToNull; this.xsiTypeMap = Collections.unmodifiableMap(xsiTypeMap); this.forceList = Collections.unmodifiableSet(forceList); + this.closeEmptyTag = closeEmptyTag; } /** @@ -169,7 +177,8 @@ public class XMLParserConfiguration extends ParserConfiguration { this.convertNilAttributeToNull, this.xsiTypeMap, this.forceList, - this.maxNestingDepth + this.maxNestingDepth, + this.closeEmptyTag ); } @@ -303,4 +312,13 @@ public class XMLParserConfiguration extends ParserConfiguration { public XMLParserConfiguration withMaxNestingDepth(int maxNestingDepth) { return super.withMaxNestingDepth(maxNestingDepth); } + + public XMLParserConfiguration withCloseEmptyTag(boolean closeEmptyTag){ + this.closeEmptyTag = closeEmptyTag; + return this; + } + + public boolean isCloseEmptyTag() { + return this.closeEmptyTag; + } } diff --git a/src/test/java/org/json/junit/XMLConfigurationTest.java b/src/test/java/org/json/junit/XMLConfigurationTest.java index 2eaaf99..153d4ed 100755 --- a/src/test/java/org/json/junit/XMLConfigurationTest.java +++ b/src/test/java/org/json/junit/XMLConfigurationTest.java @@ -557,6 +557,17 @@ public class XMLConfigurationTest { assertEquals(actualXML, resultXML); } + @Test + public void shouldHandleEmptyNodeValue() + { + JSONObject inputJSON = new JSONObject(); + inputJSON.put("Emptyness", ""); + String expectedXmlWithoutExplicitEndTag = ""; + String expectedXmlWithExplicitEndTag = ""; + assertEquals(expectedXmlWithoutExplicitEndTag, XML.toString(inputJSON, null, new XMLParserConfiguration().withCloseEmptyTag(false))); + assertEquals(expectedXmlWithExplicitEndTag, XML.toString(inputJSON, null, new XMLParserConfiguration().withCloseEmptyTag(true))); + } + /** * Investigate exactly how the "content" keyword works */ diff --git a/src/test/java/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java index 63135eb..d8aedb3 100644 --- a/src/test/java/org/json/junit/XMLTest.java +++ b/src/test/java/org/json/junit/XMLTest.java @@ -1177,6 +1177,26 @@ public class XMLTest { } + + @Test + public void shouldCreateExplicitEndTagWithEmptyValueWhenConfigured(){ + String jsonString = "{outer:{innerOne:\"\", innerTwo:\"two\"}}"; + JSONObject jsonObject = new JSONObject(jsonString); + String expectedXmlString = "two"; + String xmlForm = XML.toString(jsonObject,"encloser", new XMLParserConfiguration().withCloseEmptyTag(true)); + assertEquals(expectedXmlString, xmlForm); + } + + @Test + public void shouldNotCreateExplicitEndTagWithEmptyValueWhenNotConfigured(){ + String jsonString = "{outer:{innerOne:\"\", innerTwo:\"two\"}}"; + JSONObject jsonObject = new JSONObject(jsonString); + String expectedXmlString = "two"; + String xmlForm = XML.toString(jsonObject,"encloser", new XMLParserConfiguration().withCloseEmptyTag(false)); + assertEquals(expectedXmlString, xmlForm); + } + + @Test public void testIndentSimpleJsonObject(){ String str = "{ \"employee\": { \n" + From c05d7058ff7bfd0a1520cd8bcf94eebd2b9a11be Mon Sep 17 00:00:00 2001 From: rudrajyoti biswas Date: Fri, 27 Oct 2023 17:17:20 +0530 Subject: [PATCH 2/3] #748 - javadoc updated for methods. --- src/main/java/org/json/XMLParserConfiguration.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/java/org/json/XMLParserConfiguration.java b/src/main/java/org/json/XMLParserConfiguration.java index 5d7ecfa..e85ce53 100644 --- a/src/main/java/org/json/XMLParserConfiguration.java +++ b/src/main/java/org/json/XMLParserConfiguration.java @@ -149,6 +149,7 @@ public class XMLParserConfiguration extends ParserConfiguration { * xsi:type="integer" as integer, xsi:type="string" as string * @param forceList new HashSet() to parse the provided tags' values as arrays * @param maxNestingDepth int to limit the nesting depth + * @param closeEmptyTag boolean to turn on explicit end tag for tag with empty value */ private XMLParserConfiguration (final boolean keepStrings, final String cDataTagName, final boolean convertNilAttributeToNull, final Map> xsiTypeMap, final Set forceList, @@ -313,6 +314,11 @@ public class XMLParserConfiguration extends ParserConfiguration { return super.withMaxNestingDepth(maxNestingDepth); } + /** + * To enable explicit end tag with empty value. + * @param closeEmptyTag + * @return same instance of configuration with empty tag config updated + */ public XMLParserConfiguration withCloseEmptyTag(boolean closeEmptyTag){ this.closeEmptyTag = closeEmptyTag; return this; From 8ec822c5756acbf78ae83ba2c4800ebf25b5336e Mon Sep 17 00:00:00 2001 From: rudrajyoti biswas Date: Sat, 28 Oct 2023 07:36:31 +0530 Subject: [PATCH 3/3] #748 - PR comments - follow convention of configuration builder. --- .../java/org/json/XMLParserConfiguration.java | 5 +-- .../org/json/junit/XMLConfigurationTest.java | 31 ++++++++++++++----- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/json/XMLParserConfiguration.java b/src/main/java/org/json/XMLParserConfiguration.java index e85ce53..2e4907f 100644 --- a/src/main/java/org/json/XMLParserConfiguration.java +++ b/src/main/java/org/json/XMLParserConfiguration.java @@ -320,8 +320,9 @@ public class XMLParserConfiguration extends ParserConfiguration { * @return same instance of configuration with empty tag config updated */ public XMLParserConfiguration withCloseEmptyTag(boolean closeEmptyTag){ - this.closeEmptyTag = closeEmptyTag; - return this; + XMLParserConfiguration clonedConfiguration = this.clone(); + clonedConfiguration.closeEmptyTag = closeEmptyTag; + return clonedConfiguration; } public boolean isCloseEmptyTag() { diff --git a/src/test/java/org/json/junit/XMLConfigurationTest.java b/src/test/java/org/json/junit/XMLConfigurationTest.java index 153d4ed..ffdc20c 100755 --- a/src/test/java/org/json/junit/XMLConfigurationTest.java +++ b/src/test/java/org/json/junit/XMLConfigurationTest.java @@ -4,11 +4,6 @@ package org.json.junit; Public Domain. */ -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - import java.io.File; import java.io.FileReader; import java.io.FileWriter; @@ -27,6 +22,8 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; +import static org.junit.Assert.*; + /** * Tests for JSON-Java XML.java with XMLParserConfiguration.java @@ -564,8 +561,28 @@ public class XMLConfigurationTest { inputJSON.put("Emptyness", ""); String expectedXmlWithoutExplicitEndTag = ""; String expectedXmlWithExplicitEndTag = ""; - assertEquals(expectedXmlWithoutExplicitEndTag, XML.toString(inputJSON, null, new XMLParserConfiguration().withCloseEmptyTag(false))); - assertEquals(expectedXmlWithExplicitEndTag, XML.toString(inputJSON, null, new XMLParserConfiguration().withCloseEmptyTag(true))); + assertEquals(expectedXmlWithoutExplicitEndTag, XML.toString(inputJSON, null, + new XMLParserConfiguration().withCloseEmptyTag(false))); + assertEquals(expectedXmlWithExplicitEndTag, XML.toString(inputJSON, null, + new XMLParserConfiguration().withCloseEmptyTag(true))); + } + + @Test + public void shouldKeepConfigurationIntactAndUpdateCloseEmptyTagChoice() + { + XMLParserConfiguration keepStrings = XMLParserConfiguration.KEEP_STRINGS; + XMLParserConfiguration keepStringsAndCloseEmptyTag = keepStrings.withCloseEmptyTag(true); + XMLParserConfiguration keepDigits = keepStringsAndCloseEmptyTag.withKeepStrings(false); + XMLParserConfiguration keepDigitsAndNoCloseEmptyTag = keepDigits.withCloseEmptyTag(false); + assertTrue(keepStrings.isKeepStrings()); + assertFalse(keepStrings.isCloseEmptyTag()); + assertTrue(keepStringsAndCloseEmptyTag.isKeepStrings()); + assertTrue(keepStringsAndCloseEmptyTag.isCloseEmptyTag()); + assertFalse(keepDigits.isKeepStrings()); + assertTrue(keepDigits.isCloseEmptyTag()); + assertFalse(keepDigitsAndNoCloseEmptyTag.isKeepStrings()); + assertFalse(keepDigitsAndNoCloseEmptyTag.isCloseEmptyTag()); + } /**