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/19 20:02:38 UTC

svn commit: r1588683 - in /commons/proper/configuration/branches/immutableNodes/src: main/java/org/apache/commons/configuration/plist/PropertyListConfiguration.java test/java/org/apache/commons/configuration/plist/TestPropertyListConfiguration.java

Author: oheger
Date: Sat Apr 19 18:02:38 2014
New Revision: 1588683

URL: http://svn.apache.org/r1588683
Log:
Reworked PropertyListConfiguration to operate on immutable nodes.

Modified:
    commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/plist/PropertyListConfiguration.java
    commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/plist/TestPropertyListConfiguration.java

Modified: commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/plist/PropertyListConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/plist/PropertyListConfiguration.java?rev=1588683&r1=1588682&r2=1588683&view=diff
==============================================================================
--- commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/plist/PropertyListConfiguration.java (original)
+++ commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/plist/PropertyListConfiguration.java Sat Apr 19 18:02:38 2014
@@ -36,8 +36,9 @@ import org.apache.commons.configuration.
 import org.apache.commons.configuration.HierarchicalConfiguration;
 import org.apache.commons.configuration.MapConfiguration;
 import org.apache.commons.configuration.ex.ConfigurationException;
-import org.apache.commons.configuration.tree.ConfigurationNode;
-import org.apache.commons.configuration.tree.DefaultConfigurationNode;
+import org.apache.commons.configuration.tree.ImmutableNode;
+import org.apache.commons.configuration.tree.InMemoryNodeModel;
+import org.apache.commons.configuration.tree.NodeHandler;
 import org.apache.commons.lang3.StringUtils;
 
 /**
@@ -146,11 +147,22 @@ public class PropertyListConfiguration e
      * @param c the configuration to copy
      * @since 1.4
      */
-    public PropertyListConfiguration(HierarchicalConfiguration c)
+    public PropertyListConfiguration(HierarchicalConfiguration<ImmutableNode> c)
     {
         super(c);
     }
 
+    /**
+     * Creates a new instance of {@code PropertyListConfiguration} with the
+     * given root node.
+     *
+     * @param root the root node
+     */
+    PropertyListConfiguration(ImmutableNode root)
+    {
+        super(new InMemoryNodeModel(root));
+    }
+
     @Override
     protected void setPropertyInternal(String key, Object value)
     {
@@ -193,7 +205,7 @@ public class PropertyListConfiguration e
         PropertyListParser parser = new PropertyListParser(in);
         try
         {
-            HierarchicalConfiguration config = parser.parse();
+            PropertyListConfiguration config = parser.parse();
             setRootNode(config.getRootNode());
         }
         catch (ParseException e)
@@ -206,23 +218,25 @@ public class PropertyListConfiguration e
     public void write(Writer out) throws ConfigurationException
     {
         PrintWriter writer = new PrintWriter(out);
-        printNode(writer, 0, getRootNode());
+        NodeHandler<ImmutableNode> handler = getModel().getNodeHandler();
+        printNode(writer, 0, handler.getRootNode(), handler);
         writer.flush();
     }
 
     /**
      * Append a node to the writer, indented according to a specific level.
      */
-    private void printNode(PrintWriter out, int indentLevel, ConfigurationNode node)
+    private void printNode(PrintWriter out, int indentLevel,
+            ImmutableNode node, NodeHandler<ImmutableNode> handler)
     {
         String padding = StringUtils.repeat(" ", indentLevel * INDENT_SIZE);
 
-        if (node.getName() != null)
+        if (node.getNodeName() != null)
         {
-            out.print(padding + quoteString(node.getName()) + " = ");
+            out.print(padding + quoteString(node.getNodeName()) + " = ");
         }
 
-        List<ConfigurationNode> children = new ArrayList<ConfigurationNode>(node.getChildren());
+        List<ImmutableNode> children = new ArrayList<ImmutableNode>(node.getChildren());
         if (!children.isEmpty())
         {
             // skip a line, except for the root dictionary
@@ -234,12 +248,12 @@ public class PropertyListConfiguration e
             out.println(padding + "{");
 
             // display the children
-            Iterator<ConfigurationNode> it = children.iterator();
+            Iterator<ImmutableNode> it = children.iterator();
             while (it.hasNext())
             {
-                ConfigurationNode child = it.next();
+                ImmutableNode child = it.next();
 
-                printNode(out, indentLevel + 1, child);
+                printNode(out, indentLevel + 1, child, handler);
 
                 // add a semi colon for elements that are not dictionaries
                 Object value = child.getValue();
@@ -258,7 +272,7 @@ public class PropertyListConfiguration e
             out.print(padding + "}");
 
             // line feed if the dictionary is not in an array
-            if (node.getParentNode() != null)
+            if (handler.getParent(node) != null)
             {
                 out.println();
             }
@@ -269,7 +283,7 @@ public class PropertyListConfiguration e
             out.print(padding + "{ };");
 
             // line feed if the dictionary is not in an array
-            if (node.getParentNode() != null)
+            if (handler.getParent(node) != null)
             {
                 out.println();
             }
@@ -303,9 +317,12 @@ public class PropertyListConfiguration e
             }
             out.print(" )");
         }
-        else if (value instanceof HierarchicalConfiguration)
+        else if (value instanceof PropertyListConfiguration)
         {
-            printNode(out, indentLevel, ((HierarchicalConfiguration) value).getRootNode());
+            NodeHandler<ImmutableNode> handler =
+                    ((PropertyListConfiguration) value).getModel()
+                            .getNodeHandler();
+            printNode(out, indentLevel, handler.getRootNode(), handler);
         }
         else if (value instanceof Configuration)
         {
@@ -318,10 +335,11 @@ public class PropertyListConfiguration e
             while (it.hasNext())
             {
                 String key = it.next();
-                ConfigurationNode node = new DefaultConfigurationNode(key);
-                node.setValue(config.getProperty(key));
-
-                printNode(out, indentLevel + 1, node);
+                ImmutableNode node =
+                        new ImmutableNode.Builder().name(key)
+                                .value(config.getProperty(key)).create();
+                InMemoryNodeModel tempModel = new InMemoryNodeModel(node);
+                printNode(out, indentLevel + 1, node, tempModel.getNodeHandler());
                 out.println(";");
             }
             out.println(padding + "}");

Modified: commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/plist/TestPropertyListConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/plist/TestPropertyListConfiguration.java?rev=1588683&r1=1588682&r2=1588683&view=diff
==============================================================================
--- commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/plist/TestPropertyListConfiguration.java (original)
+++ commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/plist/TestPropertyListConfiguration.java Sat Apr 19 18:02:38 2014
@@ -38,9 +38,12 @@ import junitx.framework.ObjectAssert;
 import org.apache.commons.configuration.Configuration;
 import org.apache.commons.configuration.ConfigurationAssert;
 import org.apache.commons.configuration.ConfigurationComparator;
+import org.apache.commons.configuration.HierarchicalConfiguration;
 import org.apache.commons.configuration.StrictConfigurationComparator;
 import org.apache.commons.configuration.ex.ConfigurationException;
 import org.apache.commons.configuration.io.FileHandler;
+import org.apache.commons.configuration.tree.ImmutableNode;
+import org.apache.commons.configuration.tree.NodeHandler;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
@@ -222,7 +225,7 @@ public class TestPropertyListConfigurati
     {
         Calendar cal = Calendar.getInstance();
         cal.clear();
-        cal.set(2002, 2, 22, 11, 30, 0);
+        cal.set(2002, Calendar.MARCH, 22, 11, 30, 0);
         cal.setTimeZone(TimeZone.getTimeZone("GMT+0100"));
         Date date = cal.getTime();
 
@@ -311,8 +314,24 @@ public class TestPropertyListConfigurati
         PropertyListConfiguration checkConfig = new PropertyListConfiguration();
         load(checkConfig, savedFile);
 
-        assertFalse(config.getRootNode().getChildren("empty-dictionary").isEmpty());
-        assertFalse(checkConfig.getRootNode().getChildren("empty-dictionary").isEmpty());
+        assertFalse(getNamedChildren(config, "empty-dictionary").isEmpty());
+        assertFalse(getNamedChildren(checkConfig, "empty-dictionary").isEmpty());
+    }
+
+    /**
+     * Returns a list with the children of the given configuration's root note
+     * with the specified name.
+     *
+     * @param config the configuration
+     * @param name the name of the desired children
+     * @return the list with the corresponding child nodes
+     */
+    private static List<ImmutableNode> getNamedChildren(
+            HierarchicalConfiguration<ImmutableNode> config, String name)
+    {
+        NodeHandler<ImmutableNode> handler =
+                config.getNodeModel().getNodeHandler();
+        return handler.getChildren(handler.getRootNode(), name);
     }
 
     @Test
@@ -421,12 +440,12 @@ public class TestPropertyListConfigurati
     {
         Calendar cal = Calendar.getInstance();
         cal.clear();
-        cal.set(2007, 9, 29, 23, 4, 30);
+        cal.set(2007, Calendar.OCTOBER, 29, 23, 4, 30);
         cal.setTimeZone(TimeZone.getTimeZone("GMT-0230"));
         assertEquals("Wrong date literal (1)", "<*D2007-10-29 23:04:30 -0230>",
                 PropertyListConfiguration.formatDate(cal));
         cal.clear();
-        cal.set(2007, 9, 30, 22, 2, 15);
+        cal.set(2007, Calendar.OCTOBER, 30, 22, 2, 15);
         cal.setTimeZone(TimeZone.getTimeZone("GMT+1111"));
         assertEquals("Wrong date literal (2)", "<*D2007-10-30 22:02:15 +1111>",
                 PropertyListConfiguration.formatDate(cal));