You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by oh...@apache.org on 2014/04/05 21:47:14 UTC
svn commit: r1585187 - in
/commons/proper/configuration/branches/immutableNodes/src:
main/java/org/apache/commons/configuration/
test/java/org/apache/commons/configuration/
Author: oheger
Date: Sat Apr 5 19:47:14 2014
New Revision: 1585187
URL: http://svn.apache.org/r1585187
Log:
Fixed handling of public and system IDs.
The IDs are now correctly obtained from the source XML document and passed to
the transformer before the target document is written.
Modified:
commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/XMLConfiguration.java
commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/XMLDocumentHelper.java
commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestXMLConfiguration.java
commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestXMLDocumentHelper.java
Modified: commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/XMLConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/XMLConfiguration.java?rev=1585187&r1=1585186&r2=1585187&view=diff
==============================================================================
--- commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/XMLConfiguration.java (original)
+++ commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/XMLConfiguration.java Sat Apr 5 19:47:14 2014
@@ -20,6 +20,7 @@ package org.apache.commons.configuration
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
@@ -548,11 +549,8 @@ public class XMLConfiguration extends Ba
private void initProperties(XMLDocumentHelper docHelper, boolean elemRefs)
{
Document document = docHelper.getDocument();
- if (document.getDoctype() != null)
- {
- setPublicID(document.getDoctype().getPublicId());
- setSystemID(document.getDoctype().getSystemId());
- }
+ setPublicID(docHelper.getSourcePublicID());
+ setSystemID(docHelper.getSourceSystemID());
ImmutableNode.Builder rootBuilder = new ImmutableNode.Builder();
MutableObject<String> rootValue = new MutableObject<String>();
@@ -828,6 +826,39 @@ public class XMLConfiguration extends Ba
}
/**
+ * Creates and initializes the transformer used for save operations. This
+ * base implementation initializes all of the default settings like
+ * indention mode and the DOCTYPE. Derived classes may overload this method
+ * if they have specific needs.
+ *
+ * @return the transformer to use for a save operation
+ * @throws ConfigurationException if an error occurs
+ * @since 1.3
+ */
+ protected Transformer createTransformer() throws ConfigurationException
+ {
+ Transformer transformer = XMLDocumentHelper.createTransformer();
+
+ transformer.setOutputProperty(OutputKeys.INDENT, "yes");
+ if (locator.getEncoding() != null)
+ {
+ transformer.setOutputProperty(OutputKeys.ENCODING, locator.getEncoding());
+ }
+ if (publicID != null)
+ {
+ transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,
+ publicID);
+ }
+ if (systemID != null)
+ {
+ transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
+ systemID);
+ }
+
+ return transformer;
+ }
+
+ /**
* Creates a DOM document from the internal tree of configuration nodes.
*
* @return the new document
@@ -964,7 +995,7 @@ public class XMLConfiguration extends Ba
@Override
public void write(Writer writer) throws ConfigurationException, IOException
{
- Transformer transformer = XMLDocumentHelper.createTransformer();
+ Transformer transformer = createTransformer();
Source source = new DOMSource(createDocument());
Result result = new StreamResult(writer);
XMLDocumentHelper.transform(transformer, source, result);
Modified: commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/XMLDocumentHelper.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/XMLDocumentHelper.java?rev=1585187&r1=1585186&r2=1585187&view=diff
==============================================================================
--- commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/XMLDocumentHelper.java (original)
+++ commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/XMLDocumentHelper.java Sat Apr 5 19:47:14 2014
@@ -71,6 +71,12 @@ class XMLDocumentHelper
/** The element mapping to the source document. */
private final Map<Node, Node> elementMapping;
+ /** Stores the public ID of the source document. */
+ private final String sourcePublicID;
+
+ /** Stores the system ID of the source document. */
+ private final String sourceSystemID;
+
/**
* Creates a new instance of {@code XMLDocumentHelper} and initializes it
* with the given XML document. Note: This constructor is package private
@@ -79,11 +85,16 @@ class XMLDocumentHelper
*
* @param doc the {@code Document}
* @param elemMap the element mapping
+ * @param pubID the public ID of the source document
+ * @param sysID the system ID of the source document
*/
- XMLDocumentHelper(Document doc, Map<Node, Node> elemMap)
+ XMLDocumentHelper(Document doc, Map<Node, Node> elemMap, String pubID,
+ String sysID)
{
document = doc;
elementMapping = elemMap;
+ sourcePublicID = pubID;
+ sourceSystemID = sysID;
}
/**
@@ -105,7 +116,7 @@ class XMLDocumentHelper
.newDocument();
Element rootElem = doc.createElement(rootElementName);
doc.appendChild(rootElem);
- return new XMLDocumentHelper(doc, emptyElementMapping());
+ return new XMLDocumentHelper(doc, emptyElementMapping(), null, null);
}
/**
@@ -129,8 +140,20 @@ class XMLDocumentHelper
public static XMLDocumentHelper forSourceDocument(Document srcDoc)
throws ConfigurationException
{
+ String pubID;
+ String sysID;
+ if (srcDoc.getDoctype() != null)
+ {
+ pubID = srcDoc.getDoctype().getPublicId();
+ sysID = srcDoc.getDoctype().getSystemId();
+ }
+ else
+ {
+ pubID = sysID = null;
+ }
+
return new XMLDocumentHelper(copyDocument(srcDoc),
- emptyElementMapping());
+ emptyElementMapping(), pubID, sysID);
}
/**
@@ -157,6 +180,26 @@ class XMLDocumentHelper
}
/**
+ * Returns the public ID of the source document.
+ *
+ * @return the public ID of the source document
+ */
+ public String getSourcePublicID()
+ {
+ return sourcePublicID;
+ }
+
+ /**
+ * Returns the system ID of the source document.
+ *
+ * @return the system ID of the source document
+ */
+ public String getSourceSystemID()
+ {
+ return sourceSystemID;
+ }
+
+ /**
* Creates a new {@code Transformer} object. No initializations are
* performed on the new instance.
*
@@ -204,7 +247,8 @@ class XMLDocumentHelper
{
Document docCopy = copyDocument(getDocument());
return new XMLDocumentHelper(docCopy, createElementMapping(
- getDocument(), docCopy));
+ getDocument(), docCopy), getSourcePublicID(),
+ getSourceSystemID());
}
/**
Modified: commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestXMLConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestXMLConfiguration.java?rev=1585187&r1=1585186&r2=1585187&view=diff
==============================================================================
--- commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestXMLConfiguration.java (original)
+++ commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestXMLConfiguration.java Sat Apr 5 19:47:14 2014
@@ -17,10 +17,12 @@
package org.apache.commons.configuration;
+import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
@@ -971,8 +973,8 @@ public class TestXMLConfiguration
conf.setSystemID(SYSTEM_ID);
StringWriter out = new StringWriter();
new FileHandler(conf).save(out);
- assertTrue("Did not find DOCTYPE", out.toString().indexOf(
- DOCTYPE + "testconfig" + DOCTYPE_DECL) >= 0);
+ assertThat("Did not find DOCTYPE", out.toString(), containsString(
+ DOCTYPE + "testconfig" + DOCTYPE_DECL));
}
/**
Modified: commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestXMLDocumentHelper.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestXMLDocumentHelper.java?rev=1585187&r1=1585186&r2=1585187&view=diff
==============================================================================
--- commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestXMLDocumentHelper.java (original)
+++ commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestXMLDocumentHelper.java Sat Apr 5 19:47:14 2014
@@ -20,6 +20,7 @@ import static org.junit.Assert.assertEqu
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
+import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
@@ -216,6 +217,8 @@ public class TestXMLDocumentHelper
rootElement.getNodeName());
NodeList childNodes = rootElement.getChildNodes();
assertEquals("Got child nodes", 0, childNodes.getLength());
+ assertNull("Got a public ID", helper.getSourcePublicID());
+ assertNull("Got a system ID", helper.getSourceSystemID());
}
/**