You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by he...@apache.org on 2013/10/21 23:07:13 UTC

svn commit: r1534371 - in /commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE: ./ src/changes/ src/main/java/org/apache/commons/configuration/ src/test/java/org/apache/commons/configuration/ src/test/resources/

Author: henning
Date: Mon Oct 21 21:07:12 2013
New Revision: 1534371

URL: http://svn.apache.org/r1534371
Log:
Backport CONFIGURATION-555 from r1527396.


Modified:
    commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/RELEASE-NOTES.txt
    commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/changes/changes.xml
    commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/main/java/org/apache/commons/configuration/XMLConfiguration.java
    commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/test/java/org/apache/commons/configuration/TestXMLConfiguration.java
    commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/test/resources/test.xml

Modified: commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/RELEASE-NOTES.txt
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/RELEASE-NOTES.txt?rev=1534371&r1=1534370&r2=1534371&view=diff
==============================================================================
--- commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/RELEASE-NOTES.txt (original)
+++ commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/RELEASE-NOTES.txt Mon Oct 21 21:07:12 2013
@@ -36,6 +36,12 @@ BUG FIXES IN 1.10
   XMLConfiguration now adds attributes of elements defining a list to
   all list nodes.
 
+* [CONFIGURATION-555] XMLConfiguration doesn't seem to be preserving whitespace 
+                      for the current node where xml:space="preserve" is set.
+
+  Fixed a bug in the handling of the xml:space attribute in XMLConfiguration. 
+  The attribute is now also applied to the current element, not only to sub elements.
+
 * [CONFIGURATION-556] Regression with SystemProperties in 1.8 and 1.9
 
   In 1.7 and before, any change to the system properties was immediately reflected in a 

Modified: commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/changes/changes.xml?rev=1534371&r1=1534370&r2=1534371&view=diff
==============================================================================
--- commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/changes/changes.xml (original)
+++ commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/changes/changes.xml Mon Oct 21 21:07:12 2013
@@ -31,6 +31,11 @@
         XMLConfiguration now adds attributes of elements defining a list to
         all list nodes.
       </action>
+       <action dev="oheger" type="update" issue="CONFIGURATION-555">
+         Fixed a bug in the handling of the xml:space attribute in
+         XMLConfiguration. The attribute is now also applied to the current
+         element, not only to sub elements.
+       </action>
       <action dev="henning" type="fix" issue="CONFIGURATION-556">
         In 1.7 and before, any change to the system properties was
         immediately reflected in a SystemConfiguration object. This

Modified: commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/main/java/org/apache/commons/configuration/XMLConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/main/java/org/apache/commons/configuration/XMLConfiguration.java?rev=1534371&r1=1534370&r2=1534371&view=diff
==============================================================================
--- commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/main/java/org/apache/commons/configuration/XMLConfiguration.java (original)
+++ commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/main/java/org/apache/commons/configuration/XMLConfiguration.java Mon Oct 21 21:07:12 2013
@@ -46,9 +46,11 @@ import javax.xml.transform.TransformerFa
 import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.stream.StreamResult;
 
+import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.configuration.resolver.DefaultEntityResolver;
 import org.apache.commons.configuration.resolver.EntityRegistry;
 import org.apache.commons.configuration.tree.ConfigurationNode;
+import org.apache.commons.lang.StringUtils;
 import org.apache.commons.logging.LogFactory;
 import org.w3c.dom.Attr;
 import org.w3c.dom.CDATASection;
@@ -605,10 +607,13 @@ public class XMLConfiguration extends Ab
      *
      * @param node the actual node
      * @param element the actual XML element
-     * @param elemRefs a flag whether references to the XML elements should be set
-     * @param trim a flag whether the text content of elements should be trimmed;
-     * this controls the whitespace handling
-     * @return a map with all attribute values extracted for the current node
+     * @param elemRefs a flag whether references to the XML elements should be
+     *        set
+     * @param trim a flag whether the text content of elements should be
+     *        trimmed; this controls the whitespace handling
+     * @return a map with all attribute values extracted for the current node;
+     *         this map also contains the value of the trim flag for this node
+     *         under the key {@value #ATTR_SPACE}
      */
     private Map<String, Collection<String>> constructHierarchy(Node node,
             Element element, boolean elemRefs, boolean trim)
@@ -616,6 +621,7 @@ public class XMLConfiguration extends Ab
         boolean trimFlag = shouldTrim(element, trim);
         Map<String, Collection<String>> attributes =
                 processAttributes(node, element, elemRefs);
+        attributes.put(ATTR_SPACE, Collections.singleton(String.valueOf(trimFlag)));
         StringBuilder buffer = new StringBuilder();
         NodeList list = element.getChildNodes();
         for (int i = 0; i < list.getLength(); i++)
@@ -629,7 +635,9 @@ public class XMLConfiguration extends Ab
                 Map<String, Collection<String>> attrmap =
                         constructHierarchy(childNode, child, elemRefs, trimFlag);
                 node.addChild(childNode);
-                handleDelimiters(node, childNode, trimFlag, attrmap);
+                Collection<String> attrSpace = attrmap.remove(ATTR_SPACE);
+                Boolean childTrim = CollectionUtils.isEmpty(attrSpace) ? Boolean.FALSE : Boolean.valueOf(attrSpace.iterator().next());
+                handleDelimiters(node, childNode, childTrim.booleanValue(), attrmap);
             }
             else if (w3cNode instanceof Text)
             {
@@ -638,11 +646,7 @@ public class XMLConfiguration extends Ab
             }
         }
 
-        String text = buffer.toString();
-        if (trimFlag)
-        {
-            text = text.trim();
-        }
+        String text = determineValue(node, buffer.toString(), trimFlag);
         if (text.length() > 0 || (!node.hasChildren() && node != getRoot()))
         {
             node.setValue(text);
@@ -651,6 +655,28 @@ public class XMLConfiguration extends Ab
     }
 
     /**
+     * Determines the value of a configuration node. This method mainly checks
+     * whether the text value is to be trimmed or not. This is normally defined
+     * by the trim flag. However, if the node has children and its content is
+     * only whitespace, then it makes no sense to store any value; this would
+     * only scramble layout when the configuration is saved again.
+     *
+     * @param node the current {@code ConfigurationNode}
+     * @param content the text content of this node
+     * @param trimFlag the trim flag
+     * @return the value to be stored for this node
+     */
+    private static String determineValue(ConfigurationNode node,
+            String content, boolean trimFlag)
+    {
+        boolean shouldTrim =
+                trimFlag
+                        || (StringUtils.isBlank(content) && node
+                                .getChildrenCount() > 0);
+        return shouldTrim ? content.trim() : content;
+    }
+
+    /**
      * Helper method for constructing node objects for the attributes of the
      * given XML element.
      *
@@ -663,15 +689,7 @@ public class XMLConfiguration extends Ab
             Element element, boolean elemRefs)
     {
         NamedNodeMap attributes = element.getAttributes();
-        Map<String, Collection<String>> attrmap;
-        if (attributes.getLength() > 0)
-        {
-            attrmap = new HashMap<String, Collection<String>>();
-        }
-        else
-        {
-            attrmap = Collections.emptyMap();
-        }
+        Map<String, Collection<String>> attrmap = new HashMap<String, Collection<String>>();
 
         for (int i = 0; i < attributes.getLength(); ++i)
         {

Modified: commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/test/java/org/apache/commons/configuration/TestXMLConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/test/java/org/apache/commons/configuration/TestXMLConfiguration.java?rev=1534371&r1=1534370&r2=1534371&view=diff
==============================================================================
--- commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/test/java/org/apache/commons/configuration/TestXMLConfiguration.java (original)
+++ commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/test/java/org/apache/commons/configuration/TestXMLConfiguration.java Mon Oct 21 21:07:12 2013
@@ -1648,6 +1648,16 @@ public class TestXMLConfiguration
     }
 
     /**
+     * Tests whether the xml:space attribute works directly on the current
+     * element. This test is related to CONFIGURATION-555.
+     */
+    @Test
+    public void testPreserveSpaceOnElement()
+    {
+        assertEquals("Wrong value", " preserved ", conf.getString("spaceElement"));
+    }
+
+    /**
      * Tests whether the xml:space attribute can be overridden in nested
      * elements.
      */

Modified: commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/test/resources/test.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/test/resources/test.xml?rev=1534371&r1=1534370&r2=1534371&view=diff
==============================================================================
--- commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/test/resources/test.xml (original)
+++ commons/proper/configuration/branches/CONFIGURATION_1_X_MAINTENANCE/src/test/resources/test.xml Mon Oct 21 21:07:12 2013
@@ -115,4 +115,5 @@ And even longer.
       <description xml:space="default">     Some text      </description>
       <testInvalid xml:space="invalid">     Some other text </testInvalid>
     </space>
+    <spaceElement xml:space="preserve"> preserved </spaceElement>
 </testconfig>