commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r662281 - in /commons/proper/configuration/branches/configuration2_experimental: src/main/java/org/apache/commons/configuration2/ src/test/java/org/apache/commons/configuration2/ src/test/resources/ xdocs/
Date Sun, 01 Jun 2008 20:20:26 GMT
Author: oheger
Date: Sun Jun  1 13:20:26 2008
New Revision: 662281

URL: http://svn.apache.org/viewvc?rev=662281&view=rev
Log:
CONFIGURATION-307: XMLConfiguration now supports the xml:space attribute. This allows preserving
whitespace in the content of XML elements. (Porting changes from trunk to this 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/src/test/resources/test.xml
    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=662281&r1=662280&r2=662281&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 Jun  1 13:20:26 2008
@@ -154,6 +154,12 @@
     /** Constant for the default root element name. */
     private static final String DEFAULT_ROOT_NAME = "configuration";
 
+    /** Constant for the name of the space attribute.*/
+    private static final String ATTR_SPACE = "xml:space";
+
+    /** Constant for the xml:space value for preserving whitespace.*/
+    private static final String VALUE_PRESERVE = "preserve";
+
     /** Constant for the delimiter for multiple attribute values.*/
     private static final char ATTR_VALUE_DELIMITER = '|';
 
@@ -422,7 +428,7 @@
             setSystemID(document.getDoctype().getSystemId());
         }
 
-        constructHierarchy(getRootNode(), document.getDocumentElement(), elemRefs);
+        constructHierarchy(getRootNode(), document.getDocumentElement(), elemRefs, true);
         getRootNode().setName(document.getDocumentElement().getNodeName());
         if (elemRefs)
         {
@@ -437,9 +443,12 @@
      * @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
      */
-    private void constructHierarchy(ConfigurationNode node, Element element, boolean elemRefs)
+    private void constructHierarchy(ConfigurationNode node, Element element, boolean elemRefs,
boolean trim)
     {
+        boolean trimFlag = shouldTrim(element, trim);
         processAttributes(node, element, elemRefs);
         StringBuilder buffer = new StringBuilder();
         NodeList list = element.getChildNodes();
@@ -450,9 +459,9 @@
             {
                 Element child = (Element) w3cNode;
                 ConfigurationNode childNode = new XMLNode(child.getTagName(), elemRefs ?
child : null);
-                constructHierarchy(childNode, child, elemRefs);
+                constructHierarchy(childNode, child, elemRefs, trimFlag);
                 node.addChild(childNode);
-                handleDelimiters(node, childNode);
+                handleDelimiters(node, childNode, trimFlag);
             }
             else if (w3cNode instanceof Text)
             {
@@ -460,7 +469,12 @@
                 buffer.append(data.getData());
             }
         }
-        String text = buffer.toString().trim();
+
+        String text = buffer.toString();
+        if (trimFlag)
+        {
+            text = text.trim();
+        }
         if (text.length() > 0 || !hasChildren(node))
         {
             node.setValue(text);
@@ -514,8 +528,9 @@
      *
      * @param parent the parent element
      * @param child the child element
+     * @param trim flag whether texts of elements should be trimmed
      */
-    private void handleDelimiters(ConfigurationNode parent, ConfigurationNode child)
+    private void handleDelimiters(ConfigurationNode parent, ConfigurationNode child, boolean
trim)
     {
         if (child.getValue() != null)
         {
@@ -527,7 +542,7 @@
             }
             else
             {
-                values = PropertyConverter.split(child.getValue().toString(), getListDelimiter());
+                values = PropertyConverter.split(child.getValue().toString(), getListDelimiter(),
trim);
             }
 
             if (values.size() > 1)
@@ -563,6 +578,31 @@
     }
 
     /**
+     * Checks whether the content of the current XML element should be trimmed.
+     * This method checks whether a <code>xml:space</code> attribute is
+     * present and evaluates its value. See <a
+     * href="http://www.w3.org/TR/REC-xml/#sec-white-space">
+     * http://www.w3.org/TR/REC-xml/#sec-white-space</a> for more details.
+     *
+     * @param element the current XML element
+     * @param currentTrim the current trim flag
+     * @return a flag whether the content of this element should be trimmed
+     */
+    private boolean shouldTrim(Element element, boolean currentTrim)
+    {
+        Attr attr = element.getAttributeNode(ATTR_SPACE);
+
+        if (attr == null)
+        {
+            return currentTrim;
+        }
+        else
+        {
+            return !VALUE_PRESERVE.equals(attr.getValue());
+        }
+    }
+
+    /**
      * Creates the <code>DocumentBuilder</code> to be used for loading files.
      * This implementation checks whether a specific
      * <code>DocumentBuilder</code> has been set. If this is the case, this

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=662281&r1=662280&r2=662281&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 Jun  1 13:20:26 2008
@@ -1386,6 +1386,36 @@
     }
 
     /**
+     * Tests whether spaces are preserved when the xml:space attribute is set.
+     */
+    public void testPreserveSpace()
+    {
+        assertEquals("Wrong value of blanc", " ", conf.getString("space.blanc"));
+        assertEquals("Wrong value of stars", " * * ", conf
+                .getString("space.stars"));
+    }
+
+    /**
+     * Tests whether the xml:space attribute can be overridden in nested
+     * elements.
+     */
+    public void testPreserveSpaceOverride()
+    {
+        assertEquals("Not trimmed", "Some text", conf
+                .getString("space.description"));
+    }
+
+    /**
+     * Tests an xml:space attribute with an invalid value. This will be
+     * interpreted as default.
+     */
+    public void testPreserveSpaceInvalid()
+    {
+        assertEquals("Invalid not trimmed", "Some other text", conf
+                .getString("space.testInvalid"));
+    }
+
+    /**
      * Prepares a configuration object for testing a reload operation.
      *
      * @return the initialized configuration

Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/resources/test.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/resources/test.xml?rev=662281&r1=662280&r2=662281&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/resources/test.xml
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/resources/test.xml
Sun Jun  1 13:20:26 2008
@@ -90,4 +90,12 @@
          splitting when delimiter parsing is disabled.
     -->
     <expressions value="a \|\| (b &amp;&amp; c)|!d"/>
+
+    <!-- Tests for handling of spaces -->
+    <space xml:space="preserve">
+      <blanc> </blanc>
+      <stars> * * </stars>
+      <description xml:space="default">     Some text      </description>
+      <testInvalid xml:space="invalid">     Some other text </testInvalid>
+    </space>
 </testconfig>

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=662281&r1=662280&r2=662281&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml (original)
+++ commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml Sun
Jun  1 13:20:26 2008
@@ -79,6 +79,10 @@
     </release>
 
     <release version="1.6" date="in SVN" description="">
+      <action dev="oheger" type="fix" issue="CONFIGURATION-307">
+        XMLConfiguration now supports the xml:space attribute. This attribute
+        can be used to preserve whitespace in the content of XML elements.
+      </action>
       <action dev="oheger" type="fix" issue="CONFIGURATION-328">
         A bug in XMLConfiguration.addNodes() made it impossible to add
         attribute nodes using this method. This has been fixed.



Mime
View raw message