commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject [commons-configuration] branch master updated: [CONFIGURATION-767] NullPointerException in XMLConfiguration#createTransformer() when no FileLocator is set.
Date Sun, 03 Nov 2019 23:13:26 GMT
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 746821e  [CONFIGURATION-767] NullPointerException in XMLConfiguration#createTransformer()
when no FileLocator is set.
746821e is described below

commit 746821e2568246d8d7b09403f68930ab000a9592
Author: Gary Gregory <gardgregory@gmail.com>
AuthorDate: Sun Nov 3 18:13:22 2019 -0500

    [CONFIGURATION-767] NullPointerException in
    XMLConfiguration#createTransformer() when no FileLocator is set.
---
 src/changes/changes.xml                            |  3 ++
 .../commons/configuration2/XMLConfiguration.java   |  8 ++----
 .../configuration2/TestXMLConfiguration.java       | 33 +++++++++++++++++-----
 3 files changed, 32 insertions(+), 12 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 542a735..ad5d6da 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -41,6 +41,9 @@
       <action dev="ggregory" type="add" issue="CONFIGURATION-765" due-to="Gary Gregory">
         Refactor XMLConfiguration.write(Writer) to add XMLConfiguration.write(Writer, Transformer).
       </action>
+      <action dev="ggregory" type="fix" issue="CONFIGURATION-767" due-to="Gary Gregory">
+        NullPointerException in XMLConfiguration#createTransformer() when no FileLocator
is set.
+      </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 d74005a..469b226 100644
--- a/src/main/java/org/apache/commons/configuration2/XMLConfiguration.java
+++ b/src/main/java/org/apache/commons/configuration2/XMLConfiguration.java
@@ -881,19 +881,17 @@ public class XMLConfiguration extends BaseHierarchicalConfiguration
implements
         final Transformer transformer = XMLDocumentHelper.createTransformer();
 
         transformer.setOutputProperty(OutputKeys.INDENT, "yes");
-        if (locator.getEncoding() != null)
+        if (locator != null && locator.getEncoding() != null)
         {
             transformer.setOutputProperty(OutputKeys.ENCODING, locator.getEncoding());
         }
         if (publicID != null)
         {
-            transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,
-                    publicID);
+            transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, publicID);
         }
         if (systemID != null)
         {
-            transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
-                    systemID);
+            transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, systemID);
         }
 
         return transformer;
diff --git a/src/test/java/org/apache/commons/configuration2/TestXMLConfiguration.java b/src/test/java/org/apache/commons/configuration2/TestXMLConfiguration.java
index dd8a3a7..840bc96 100644
--- a/src/test/java/org/apache/commons/configuration2/TestXMLConfiguration.java
+++ b/src/test/java/org/apache/commons/configuration2/TestXMLConfiguration.java
@@ -26,21 +26,23 @@ import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.ParserConfigurationException;
-import javax.xml.transform.TransformerFactoryConfigurationError;
 import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.StringReader;
 import java.io.StringWriter;
+import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.List;
 
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.TransformerFactoryConfigurationError;
+
 import org.apache.commons.configuration2.SynchronizerTestImpl.Methods;
 import org.apache.commons.configuration2.builder.FileBasedBuilderParametersImpl;
 import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
@@ -52,10 +54,12 @@ 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.junit.Assert;
 import org.junit.Before;
-import org.junit.Test;
 import org.junit.Rule;
+import org.junit.Test;
 import org.junit.rules.TemporaryFolder;
+import org.w3c.dom.Document;
 import org.xml.sax.SAXException;
 import org.xml.sax.SAXParseException;
 import org.xml.sax.helpers.DefaultHandler;
@@ -230,6 +234,11 @@ public class TestXMLConfiguration
         return builder;
     }
 
+    private Document parseXml(final String xml) throws SAXException, IOException, ParserConfigurationException
{
+        return DocumentBuilderFactory.newInstance().newDocumentBuilder()
+                .parse(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));
+    }
+
     /**
      * Removes the test output file if it exists.
      */
@@ -965,13 +974,13 @@ public class TestXMLConfiguration
         assertEquals("three", list.get(1));
     }
 
+
     @Test
     public void testGetProperty()
     {
         assertEquals("value", conf.getProperty("element"));
     }
 
-
     @Test
     public void testGetPropertyWithXMLEntity()
     {
@@ -1764,7 +1773,17 @@ public class TestXMLConfiguration
         conf.setValidating(true);
         load(conf, "testValidateInvalid.xml");
     }
-
+    
+    @Test
+    public void testWrite() throws Exception
+    {
+        XMLConfiguration xmlConfig = new XMLConfiguration();
+        xmlConfig.setRootElementName("IAmRoot");
+        StringWriter sw = new StringWriter();
+        xmlConfig.write(sw);
+        // Check that we can parse the XML.
+        Assert.assertNotNull(parseXml(sw.toString()));
+    }
     /**
      * Tests accessing properties when the XPATH expression engine is set.
      */


Mime
View raw message