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 2008/03/23 20:31:50 UTC

svn commit: r640241 - in /commons/proper/configuration/branches/configuration2_experimental: src/main/java/org/apache/commons/configuration2/XMLConfiguration.java src/test/java/org/apache/commons/configuration2/TestXMLConfiguration.java xdocs/changes.xml

Author: oheger
Date: Sun Mar 23 12:31:49 2008
New Revision: 640241

URL: http://svn.apache.org/viewvc?rev=640241&view=rev
Log:
CONFIGURATION-316: Applying changes to configuration2 branch

Modified:
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/XMLConfiguration.java
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestXMLConfiguration.java
    commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml

Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/XMLConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/XMLConfiguration.java?rev=640241&r1=640240&r2=640241&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/XMLConfiguration.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/XMLConfiguration.java Sun Mar 23 12:31:49 2008
@@ -624,6 +624,7 @@
             XMLBuilderVisitor builder = new XMLBuilderVisitor(document,
                     isDelimiterParsingDisabled() ? (char) 0 : getListDelimiter());
             visit(getRootNode(), builder);
+            initRootElementText(document, getRootNode().getValue());
             return document;
         }
         catch (DOMException domEx)
@@ -633,6 +634,34 @@
         catch (ParserConfigurationException pex)
         {
             throw new ConfigurationException(pex);
+        }
+    }
+
+    /**
+     * Sets the text of the root element of a newly created XML Document.
+     *
+     * @param doc the document
+     * @param value the new text to be set
+     */
+    private void initRootElementText(Document doc, Object value)
+    {
+        Element elem = doc.getDocumentElement();
+        NodeList children = elem.getChildNodes();
+
+        // Remove all existing text nodes
+        for (int i = 0; i < children.getLength(); i++)
+        {
+            org.w3c.dom.Node nd = children.item(i);
+            if (nd.getNodeType() == org.w3c.dom.Node.TEXT_NODE)
+            {
+                elem.removeChild(nd);
+            }
+        }
+
+        if (value != null)
+        {
+            // Add a new text node
+            elem.appendChild(doc.createTextNode(String.valueOf(value)));
         }
     }
 

Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestXMLConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestXMLConfiguration.java?rev=640241&r1=640240&r2=640241&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestXMLConfiguration.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestXMLConfiguration.java Sun Mar 23 12:31:49 2008
@@ -1006,6 +1006,36 @@
     }
 
     /**
+     * Tests setting text of the root element.
+     */
+    public void testSetTextRootElement() throws ConfigurationException
+    {
+        conf.setProperty("", "Root text");
+        conf.save(testSaveConf);
+        XMLConfiguration copy = new XMLConfiguration();
+        copy.setFile(testSaveConf);
+        checkSavedConfig(copy);
+    }
+
+    /**
+     * Tests removing the text of the root element.
+     */
+    public void testClearTextRootElement() throws ConfigurationException
+    {
+        final String xml = "<e a=\"v\">text</e>";
+        conf.clear();
+        StringReader in = new StringReader(xml);
+        conf.load(in);
+        assertEquals("Wrong text of root", "text", conf.getString(""));
+
+        conf.clearProperty("");
+        conf.save(testSaveConf);
+        XMLConfiguration copy = new XMLConfiguration();
+        copy.setFile(testSaveConf);
+        checkSavedConfig(copy);
+    }
+
+    /**
      * Tests list nodes with multiple values and attributes.
      */
     public void testListWithAttributes()

Modified: commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml?rev=640241&r1=640240&r2=640241&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml (original)
+++ commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml Sun Mar 23 12:31:49 2008
@@ -66,6 +66,10 @@
     </release>
 
     <release version="1.6" date="in SVN" description="">
+      <action dev="oheger" type="fix" issue="CONFIGURATION-316">
+        Changing the text of the root element of an XMLConfiguration had no
+        effect when the configuration was saved. This has been fixed.
+      </action>
       <action dev="oheger" type="fix" issue="CONFIGURATION-315">
         CombinedConfiguration used to send two EVENT_COMBINED_INVALIDATE events
         for each modified child configuration. Now this event is sent only