You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2019/11/03 23:31:07 UTC

[commons-configuration] branch master updated: [CONFIGURATION-768] XMLConfiguration#write does not indent XML elements.

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-configuration.git


The following commit(s) were added to refs/heads/master by this push:
     new 3385643  [CONFIGURATION-768] XMLConfiguration#write does not indent XML elements.
3385643 is described below

commit 3385643cdc97b180fc6bc8cf26a38b56dc98b69e
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Nov 3 18:31:03 2019 -0500

    [CONFIGURATION-768] XMLConfiguration#write does not indent XML elements.
---
 src/changes/changes.xml                            |  3 ++
 .../commons/configuration2/XMLConfiguration.java   |  7 +++++
 .../configuration2/TestXMLConfiguration.java       | 34 ++++++++++++++++++++++
 3 files changed, 44 insertions(+)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index ad5d6da..3228da4 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -44,6 +44,9 @@
       <action dev="ggregory" type="fix" issue="CONFIGURATION-767" due-to="Gary Gregory">
         NullPointerException in XMLConfiguration#createTransformer() when no FileLocator is set.
       </action>
+      <action dev="ggregory" type="fix" issue="CONFIGURATION-768" due-to="Gary Gregory">
+        XMLConfiguration#write does not indent XML elements.
+      </action>
     </release>
     <release version="2.6" date="2019-09-13"
              description="Minor release with new features and updated dependencies.">
diff --git a/src/main/java/org/apache/commons/configuration2/XMLConfiguration.java b/src/main/java/org/apache/commons/configuration2/XMLConfiguration.java
index 469b226..540486d 100644
--- a/src/main/java/org/apache/commons/configuration2/XMLConfiguration.java
+++ b/src/main/java/org/apache/commons/configuration2/XMLConfiguration.java
@@ -192,9 +192,15 @@ import org.xml.sax.helpers.DefaultHandler;
 public class XMLConfiguration extends BaseHierarchicalConfiguration implements
         FileBasedConfiguration, FileLocatorAware, InputStreamSupport
 {
+    /** Constant for output property name used on a transformer to specify the indent amount. */
+    static final String INDENT_AMOUNT_PROPERTY = "{http://xml.apache.org/xslt}indent-amount";
+
     /** Constant for the default root element name. */
     private static final String DEFAULT_ROOT_NAME = "configuration";
 
+    /** Constant for the default indent size. */
+    static final int DEFAULT_INDENT_SIZE = 2;
+
     /** Constant for the name of the space attribute.*/
     private static final String ATTR_SPACE = "xml:space";
 
@@ -881,6 +887,7 @@ public class XMLConfiguration extends BaseHierarchicalConfiguration implements
         final Transformer transformer = XMLDocumentHelper.createTransformer();
 
         transformer.setOutputProperty(OutputKeys.INDENT, "yes");
+        transformer.setOutputProperty(INDENT_AMOUNT_PROPERTY, Integer.toString(DEFAULT_INDENT_SIZE));
         if (locator != null && locator.getEncoding() != null)
         {
             transformer.setOutputProperty(OutputKeys.ENCODING, locator.getEncoding());
diff --git a/src/test/java/org/apache/commons/configuration2/TestXMLConfiguration.java b/src/test/java/org/apache/commons/configuration2/TestXMLConfiguration.java
index a180a30..aabb8b4 100644
--- a/src/test/java/org/apache/commons/configuration2/TestXMLConfiguration.java
+++ b/src/test/java/org/apache/commons/configuration2/TestXMLConfiguration.java
@@ -41,6 +41,7 @@ import java.util.List;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.Transformer;
 import javax.xml.transform.TransformerFactoryConfigurationError;
 
 import org.apache.commons.configuration2.SynchronizerTestImpl.Methods;
@@ -54,6 +55,7 @@ import org.apache.commons.configuration2.resolver.CatalogResolver;
 import org.apache.commons.configuration2.tree.ImmutableNode;
 import org.apache.commons.configuration2.tree.NodeStructureHelper;
 import org.apache.commons.configuration2.tree.xpath.XPathExpressionEngine;
+import org.apache.commons.lang3.StringUtils;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Rule;
@@ -1785,6 +1787,38 @@ public class TestXMLConfiguration
         Assert.assertNotNull(parseXml(sw.toString()));
     }
 
+    @Test
+    public void testWriteIndentSize() throws Exception
+    {
+        XMLConfiguration xmlConfig = new XMLConfiguration();
+        xmlConfig.setRootElementName("IAmRoot");
+        StringWriter sw = new StringWriter();
+        xmlConfig.setProperty("Child", "Alexander");
+        xmlConfig.write(sw);
+        // Check that we can parse the XML.
+        final String xml = sw.toString();
+        Assert.assertNotNull(parseXml(xml));
+        final String indent = StringUtils.repeat(' ', XMLConfiguration.DEFAULT_INDENT_SIZE);
+        Assert.assertTrue(xml.contains(System.lineSeparator() + indent + "<Child>"));
+    }
+
+    @Test
+    public void testWriteWithTransformer() throws Exception
+    {
+        XMLConfiguration xmlConfig = new XMLConfiguration();
+        xmlConfig.setRootElementName("IAmRoot");
+        xmlConfig.setProperty("Child", "Alexander");
+        StringWriter sw = new StringWriter();
+        final Transformer transformer = xmlConfig.createTransformer();
+        final int indentSize = 8;
+        transformer.setOutputProperty(XMLConfiguration.INDENT_AMOUNT_PROPERTY, Integer.toString(indentSize));
+        xmlConfig.write(sw, transformer);
+        final String xml = sw.toString();
+        Assert.assertNotNull(parseXml(xml));
+        final String indent = StringUtils.repeat(' ', indentSize);
+        Assert.assertTrue(xml.contains(System.lineSeparator() + indent + "<Child>"));
+    }
+
     /**
      * Tests accessing properties when the XPATH expression engine is set.
      */