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/11/01 21:50:23 UTC

svn commit: r1636038 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration2/tree/DefaultExpressionEngine.java test/java/org/apache/commons/configuration2/tree/TestDefaultExpressionEngine.java

Author: oheger
Date: Sat Nov  1 20:50:22 2014
New Revision: 1636038

URL: http://svn.apache.org/r1636038
Log:
DefaultExpressionEngine now supports a node name matcher.

By providing a custom matcher it is possible to influence the search operations
performed by the expression engine.

Modified:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/tree/DefaultExpressionEngine.java
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/tree/TestDefaultExpressionEngine.java

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/tree/DefaultExpressionEngine.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/tree/DefaultExpressionEngine.java?rev=1636038&r1=1636037&r2=1636038&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/tree/DefaultExpressionEngine.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/tree/DefaultExpressionEngine.java Sat Nov  1 20:50:22 2014
@@ -129,6 +129,9 @@ public class DefaultExpressionEngine imp
     /** The symbols used by this instance. */
     private final DefaultExpressionEngineSymbols symbols;
 
+    /** The matcher for node names. */
+    private final NodeMatcher<String> nameMatcher;
+
     /**
      * Creates a new instance of {@code DefaultExpressionEngine} and initializes
      * its symbols.
@@ -138,11 +141,32 @@ public class DefaultExpressionEngine imp
      */
     public DefaultExpressionEngine(DefaultExpressionEngineSymbols syms)
     {
+        this(syms, null);
+    }
+
+    /**
+     * Creates a new instance of {@code DefaultExpressionEngine} and initializes
+     * its symbols and the matcher for comparing node names. The passed in
+     * matcher is always used when the names of nodes have to be matched against
+     * parts of configuration keys.
+     *
+     * @param syms the object with the symbols (must not be <b>null</b>)
+     * @param nodeNameMatcher the matcher for node names; can be <b>null</b>,
+     *        then a default matcher is used
+     * @throws IllegalArgumentException if the symbols are <b>null</b>
+     */
+    public DefaultExpressionEngine(DefaultExpressionEngineSymbols syms,
+            NodeMatcher<String> nodeNameMatcher)
+    {
         if (syms == null)
         {
             throw new IllegalArgumentException("Symbols must not be null!");
         }
+
         symbols = syms;
+        nameMatcher =
+                (nodeNameMatcher != null) ? nodeNameMatcher
+                        : NodeNameMatchers.EQUALS;
     }
 
     /**
@@ -161,6 +185,7 @@ public class DefaultExpressionEngine imp
      * {@inheritDoc} This method supports the syntax as described in the class
      * comment.
      */
+    @Override
     public <T> List<QueryResult<T>> query(T root, String key,
             NodeHandler<T> handler)
     {
@@ -177,6 +202,7 @@ public class DefaultExpressionEngine imp
      * The name of the root node is a blank string. Note that no indices are
      * returned.
      */
+    @Override
     public <T> String nodeKey(T node, String parentKey, NodeHandler<T> handler)
     {
         if (parentKey == null)
@@ -194,6 +220,7 @@ public class DefaultExpressionEngine imp
         }
     }
 
+    @Override
     public String attributeKey(String parentKey, String attributeName)
     {
         DefaultConfigurationKey key =
@@ -209,6 +236,7 @@ public class DefaultExpressionEngine imp
      * in any case and without further checks. If it is <b>null</b>, only the
      * name of the current node with its index is returned.
      */
+    @Override
     public <T> String canonicalKey(T node, String parentKey,
             NodeHandler<T> handler)
     {
@@ -315,6 +343,7 @@ public class DefaultExpressionEngine imp
      * @param handler the node handler
      * @return a data object with information needed for the add operation
      */
+    @Override
     public <T> NodeAddData<T> prepareAdd(T root, String key, NodeHandler<T> handler)
     {
         DefaultConfigurationKey.KeyIterator it = new DefaultConfigurationKey(
@@ -369,7 +398,7 @@ public class DefaultExpressionEngine imp
             String key = keyPart.nextKey(false);
             if (keyPart.isPropertyKey())
             {
-                processSubNodes(keyPart, handler.getChildren(node, key),
+                processSubNodes(keyPart, findChildNodesByName(handler, node, key),
                         results, handler);
             }
             if (keyPart.isAttribute() && !keyPart.hasNext())
@@ -409,15 +438,19 @@ public class DefaultExpressionEngine imp
             }
             int idx =
                     keyIt.hasIndex() ? keyIt.getIndex() : handler
-                            .getChildrenCount(node, keyPart) - 1;
-            if (idx < 0 || idx >= handler.getChildrenCount(node, keyPart))
+                            .getMatchingChildrenCount(node, nameMatcher,
+                                    keyPart) - 1;
+            if (idx < 0
+                    || idx >= handler.getMatchingChildrenCount(node,
+                            nameMatcher, keyPart))
             {
                 return node;
             }
             else
             {
                 return findLastPathNode(keyIt,
-                        handler.getChildren(node, keyPart).get(idx), handler);
+                        findChildNodesByName(handler, node, keyPart).get(idx),
+                        handler);
             }
         }
 
@@ -469,9 +502,26 @@ public class DefaultExpressionEngine imp
      * @param <T> the type of the nodes to be dealt with
      * @return the index of this node
      */
-    private static <T> int determineIndex(T node, T parent, String nodeName,
+    private <T> int determineIndex(T node, T parent, String nodeName,
                                           NodeHandler<T> handler)
     {
-        return handler.getChildren(parent, nodeName).indexOf(node);
+        return findChildNodesByName(handler, parent, nodeName).indexOf(node);
+    }
+
+    /**
+     * Returns a list with all child nodes of the given parent node which match
+     * the specified node name. The match is done using the current node name
+     * matcher.
+     *
+     * @param handler the {@code NodeHandler}
+     * @param parent the parent node
+     * @param nodeName the name of the current node
+     * @param <T> the type of the nodes to be dealt with
+     * @return a list with all matching child nodes
+     */
+    private <T> List<T> findChildNodesByName(NodeHandler<T> handler, T parent,
+            String nodeName)
+    {
+        return handler.getMatchingChildren(parent, nameMatcher, nodeName);
     }
 }

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/tree/TestDefaultExpressionEngine.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/tree/TestDefaultExpressionEngine.java?rev=1636038&r1=1636037&r2=1636038&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/tree/TestDefaultExpressionEngine.java (original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/tree/TestDefaultExpressionEngine.java Sat Nov  1 20:50:22 2014
@@ -24,6 +24,7 @@ import static org.junit.Assert.assertTru
 import java.util.Iterator;
 import java.util.List;
 
+import org.apache.commons.lang3.StringUtils;
 import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;
@@ -582,6 +583,30 @@ public class TestDefaultExpressionEngine
     }
 
     /**
+     * Tests whether the node matcher is used when querying keys.
+     */
+    @Test
+    public void testQueryKeyWithAlternativeMatcher()
+    {
+        setUpAlternativeMatcher();
+        checkKey("tables_._table_.name_", "name", 2);
+    }
+
+    /**
+     * Tests whether the node matcher is used when adding keys.
+     */
+    @Test
+    public void testPrepareAddWithAlternativeMatcher()
+    {
+        setUpAlternativeMatcher();
+        NodeAddData<ImmutableNode> data =
+                engine.prepareAdd(root, "tables_.table._fields__._field.name",
+                        handler);
+        assertEquals("Wrong name of new node", "name", data.getNewNodeName());
+        assertTrue("Path nodes available", data.getPathNodes().isEmpty());
+    }
+
+    /**
      * Creates a node hierarchy for testing that consists of tables, their
      * fields, and some additional data:
      *
@@ -649,6 +674,24 @@ public class TestDefaultExpressionEngine
     }
 
     /**
+     * Configures the test expression engine to use a special matcher. This
+     * matcher ignores underscore characters in node names.
+     */
+    private void setUpAlternativeMatcher()
+    {
+        NodeMatcher<String> matcher = new NodeMatcher<String>()
+        {
+            @Override
+            public <T> boolean matches(T node, NodeHandler<T> handler,
+                    String criterion)
+            {
+                return handler.nodeName(node).equals(StringUtils.remove(criterion, '_'));
+            }
+        };
+        engine = new DefaultExpressionEngine(engine.getSymbols(), matcher);
+    }
+
+    /**
      * Helper method for checking the evaluation of a key. Queries the
      * expression engine and tests if the expected results are returned.
      *