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/11/30 22:08:42 UTC

svn commit: r721895 - in /commons/proper/configuration/trunk: conf/test.xml src/java/org/apache/commons/configuration/XMLConfiguration.java src/test/org/apache/commons/configuration/TestXMLConfiguration.java xdocs/changes.xml

Author: oheger
Date: Sun Nov 30 13:08:42 2008
New Revision: 721895

URL: http://svn.apache.org/viewvc?rev=721895&view=rev
Log:
CONFIGURATION-335: XMLConfiguration now allows disabling the attribute splitting mechanism. For this purpose the new disableAttributeSplitting property was added.

Modified:
    commons/proper/configuration/trunk/conf/test.xml
    commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/XMLConfiguration.java
    commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestXMLConfiguration.java
    commons/proper/configuration/trunk/xdocs/changes.xml

Modified: commons/proper/configuration/trunk/conf/test.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/conf/test.xml?rev=721895&r1=721894&r2=721895&view=diff
==============================================================================
--- commons/proper/configuration/trunk/conf/test.xml (original)
+++ commons/proper/configuration/trunk/conf/test.xml Sun Nov 30 13:08:42 2008
@@ -89,7 +89,8 @@
     <!-- An attribute with multiple values and escape characters for testing
          splitting when delimiter parsing is disabled.
     -->
-    <expressions value="a \|\| (b &amp;&amp; c)|!d"/>
+    <expressions value="a \|\| (b &amp;&amp; c)|!d"
+      value2="a,b|c"/>
 
     <!-- Tests for handling of spaces -->
     <space xml:space="preserve">

Modified: commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/XMLConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/XMLConfiguration.java?rev=721895&r1=721894&r2=721895&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/XMLConfiguration.java (original)
+++ commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/XMLConfiguration.java Sun Nov 30 13:08:42 2008
@@ -26,6 +26,7 @@
 import java.net.URLConnection;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
@@ -202,6 +203,9 @@
     /** Stores a flag whether DTD validation should be performed.*/
     private boolean validating;
 
+    /** A flag whether attribute splitting is disabled.*/
+    private boolean attributeSplittingDisabled;
+
     /**
      * Creates a new instance of <code>XMLConfiguration</code>.
      */
@@ -411,6 +415,71 @@
     }
 
     /**
+     * Returns the flag whether attribute splitting is disabled.
+     *
+     * @return the flag whether attribute splitting is disabled
+     * @see #setAttributeSplittingDisabled(boolean)
+     * @since 1.6
+     */
+    public boolean isAttributeSplittingDisabled()
+    {
+        return attributeSplittingDisabled;
+    }
+
+    /**
+     * <p>
+     * Sets a flag whether attribute splitting is disabled.
+     * </p>
+     * <p>
+     * The Configuration API allows adding multiple values to an attribute. This
+     * is problematic when storing the configuration because in XML an attribute
+     * can appear only once with a single value. To solve this problem, per
+     * default multiple attribute values are concatenated using a special
+     * separator character and split again when the configuration is loaded. The
+     * separator character is either the list delimiter character (see
+     * {@link #setListDelimiter(char)}) or the pipe symbol (&quot;|&quot;) if
+     * list delimiter parsing is disabled.
+     * </p>
+     * <p>
+     * In some constellations the splitting of attribute values can have
+     * undesired effects, especially if list delimiter parsing is disabled and
+     * attributes may contain the &quot;|&quot; character. In these cases it is
+     * possible to disable the attribute splitting mechanism by calling this
+     * method with a boolean value set to <b>false</b>. If attribute splitting
+     * is disabled, the values of attributes will not be processed, but stored
+     * as configuration properties exactly as they are returned by the XML
+     * parser.
+     * </p>
+     * <p>
+     * Note that in this mode multiple attribute values cannot be handled
+     * correctly. It is possible to create a <code>XMLConfiguration</code>
+     * object, add multiple values to an attribute and save it. When the
+     * configuration is loaded again and attribute splitting is disabled, the
+     * attribute will only have a single value, which is the concatenation of
+     * all values set before. So it lies in the responsibility of the
+     * application to carefully set the values of attributes.
+     * </p>
+     * <p>
+     * As is true for the {@link #setDelimiterParsingDisabled(boolean)} method,
+     * this method must be called before the configuration is loaded. So it
+     * can't be used together with one of the constructors expecting the
+     * specification of the file to load. Instead the default constructor has to
+     * be used, then <code>setAttributeSplittingDisabled(false)</code> has to be
+     * called, and finally the configuration can be loaded using one of its
+     * <code>load()</code> methods.
+     * </p>
+     *
+     * @param attributeSplittingDisabled <b>true</b> for disabling attribute
+     *        splitting, <b>false</b> for enabling it
+     * @see #setDelimiterParsingDisabled(boolean)
+     * @since 1.6
+     */
+    public void setAttributeSplittingDisabled(boolean attributeSplittingDisabled)
+    {
+        this.attributeSplittingDisabled = attributeSplittingDisabled;
+    }
+
+    /**
      * Returns the XML document this configuration was loaded from. The return
      * value is <b>null</b> if this configuration was not loaded from a XML
      * document.
@@ -517,14 +586,22 @@
             if (w3cNode instanceof Attr)
             {
                 Attr attr = (Attr) w3cNode;
-                Iterator it = PropertyConverter.split(
-                        attr.getValue(),
-                        isDelimiterParsingDisabled() ? ATTR_VALUE_DELIMITER
-                                : getListDelimiter()).iterator();
-                while (it.hasNext())
+                List values;
+                if (isAttributeSplittingDisabled())
+                {
+                    values = Collections.singletonList(attr.getValue());
+                }
+                else
+                {
+                    values = PropertyConverter.split(attr.getValue(),
+                            isDelimiterParsingDisabled() ? ATTR_VALUE_DELIMITER
+                                    : getListDelimiter());
+                }
+
+                for (Iterator it = values.iterator(); it.hasNext();)
                 {
-                    Node child = new XMLNode(attr.getName(),
-                            elemRefs ? element : null);
+                    Node child = new XMLNode(attr.getName(), elemRefs ? element
+                            : null);
                     child.setValue(it.next());
                     node.addAttribute(child);
                 }

Modified: commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestXMLConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestXMLConfiguration.java?rev=721895&r1=721894&r2=721895&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestXMLConfiguration.java (original)
+++ commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestXMLConfiguration.java Sun Nov 30 13:08:42 2008
@@ -1437,6 +1437,45 @@
     }
 
     /**
+     * Tests whether attribute splitting can be disabled.
+     */
+    public void testAttributeSplittingDisabled() throws ConfigurationException
+    {
+        List values = conf.getList("expressions[@value2]");
+        assertEquals("Wrong number of attribute values", 2, values.size());
+        assertEquals("Wrong value 1", "a", values.get(0));
+        assertEquals("Wrong value 2", "b|c", values.get(1));
+        XMLConfiguration conf2 = new XMLConfiguration();
+        conf2.setAttributeSplittingDisabled(true);
+        conf2.setFile(conf.getFile());
+        conf2.load();
+        assertEquals("Attribute was split", "a,b|c", conf2
+                .getString("expressions[@value2]"));
+    }
+
+    /**
+     * Tests disabling both delimiter parsing and attribute splitting.
+     */
+    public void testAttributeSplittingAndDelimiterParsingDisabled()
+            throws ConfigurationException
+    {
+        conf.clear();
+        conf.setDelimiterParsingDisabled(true);
+        conf.load();
+        List values = conf.getList("expressions[@value2]");
+        assertEquals("Wrong number of attribute values", 2, values.size());
+        assertEquals("Wrong value 1", "a,b", values.get(0));
+        assertEquals("Wrong value 2", "c", values.get(1));
+        XMLConfiguration conf2 = new XMLConfiguration();
+        conf2.setAttributeSplittingDisabled(true);
+        conf2.setDelimiterParsingDisabled(true);
+        conf2.setFile(conf.getFile());
+        conf2.load();
+        assertEquals("Attribute was split", "a,b|c", conf2
+                .getString("expressions[@value2]"));
+    }
+
+    /**
      * Prepares a configuration object for testing a reload operation.
      *
      * @return the initialized configuration

Modified: commons/proper/configuration/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/xdocs/changes.xml?rev=721895&r1=721894&r2=721895&view=diff
==============================================================================
--- commons/proper/configuration/trunk/xdocs/changes.xml (original)
+++ commons/proper/configuration/trunk/xdocs/changes.xml Sun Nov 30 13:08:42 2008
@@ -97,6 +97,14 @@
         method. The expression engine passed to this method will be used when
         converting flat child configurations to hierarchical ones.
       </action>
+      <action dev="oheger" type="add" issue="CONFIGURATION-335">
+        XMLConfiguration now allows disabling the attribute splitting mechanism
+        introduced in the 1.5 release (as part of the fix for CONFIGURATION-268).
+        This may be necessary for correctly processing attributes containing
+        both the list delimiter and the attribute delimiter character. The new
+        property &quot;disableAttributeSplitting&quot; was added for this
+        purpose.
+      </action>
       <action dev="oheger" type="fix" issue="CONFIGURATION-334">
         Made handling of parent nodes more consistent when setRoot() or
         setRootNode() of HierarchicalConfiguration are involved.