You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by st...@apache.org on 2009/05/11 16:50:42 UTC

svn commit: r773588 - in /jackrabbit/trunk: jackrabbit-core/src/main/java/org/apache/jackrabbit/core/NodeImpl.java jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/ChildrenCollectorFilter.java

Author: stefan
Date: Mon May 11 14:50:42 2009
New Revision: 773588

URL: http://svn.apache.org/viewvc?rev=773588&view=rev
Log:
JCR-2060: JSR 283: Access Nodes and Properties by Array of "NameGlob"

Modified:
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/NodeImpl.java
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/ChildrenCollectorFilter.java

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/NodeImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/NodeImpl.java?rev=773588&r1=773587&r2=773588&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/NodeImpl.java (original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/NodeImpl.java Mon May 11 14:50:42 2009
@@ -2638,16 +2638,6 @@
         }
     }
 
-    public NodeIterator getNodes(String[] nameGlobs)
-            throws RepositoryException {
-        throw new UnsupportedRepositoryOperationException("JCR-2060");
-    }
-
-    public PropertyIterator getProperty(String[] nameGlobs)
-            throws RepositoryException {
-        throw new UnsupportedRepositoryOperationException("JCR-2060");
-    }
-
     /**
      * {@inheritDoc}
      */
@@ -4596,17 +4586,57 @@
         }
     }
 
+    /**
+     * @see javax.jcr.Node#getWeakReferences()
+     * @since JCR 2.0
+     */
     public PropertyIterator getWeakReferences() throws RepositoryException {
         // TODO
         throw new RuntimeException("Not implemented yet, see JCR-2061");
     }
 
+    /**
+     * @see javax.jcr.Node#getWeakReferences(String)
+     * @since JCR 2.0
+     */
     public PropertyIterator getWeakReferences(String name) throws RepositoryException {
         // TODO
         throw new RuntimeException("Not implemented yet, see JCR-2061");
     }
 
     /**
+     * @see javax.jcr.Node#getNodes(String[])
+     * @since JCR 2.0
+     */
+    public NodeIterator getNodes(String[] nameGlobs)
+            throws RepositoryException {
+        // check state of this instance
+        sanityCheck();
+
+        ArrayList nodes = new ArrayList();
+        // traverse children using a special filtering 'collector'
+        accept(new ChildrenCollectorFilter(nameGlobs, nodes, true, false, 1));
+        return new NodeIteratorAdapter(nodes);
+    }
+
+    /**
+     * @see javax.jcr.Node#getProperties(String[])
+     * @since JCR 2.0
+     */
+    // TODO rename method to 'getProperties' once JCR 2.0 api has been fixed
+    public PropertyIterator getProperty(String[]
+            nameGlobs)
+            throws RepositoryException {
+        // check state of this instance
+        sanityCheck();
+
+        ArrayList props = new ArrayList();
+        // traverse children using a special filtering 'collector'
+        accept(new ChildrenCollectorFilter(nameGlobs, props, true, false, 1));
+        return new PropertyIteratorAdapter(props);
+    }
+
+    /**
      * @see javax.jcr.Node#setPrimaryType(String) 
      * @since JCR 2.0
      */

Modified: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/ChildrenCollectorFilter.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/ChildrenCollectorFilter.java?rev=773588&r1=773587&r2=773588&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/ChildrenCollectorFilter.java (original)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/ChildrenCollectorFilter.java Mon May 11 14:50:42 2009
@@ -36,7 +36,9 @@
     private final Collection children;
     private final boolean collectNodes;
     private final boolean collectProperties;
+    // namePattern and nameGlobs fields are used mutually exclusive
     private final String namePattern;
+    private final String[] nameGlobs;
 
     /**
      * Constructs a <code>ChildrenCollectorFilter</code>
@@ -46,7 +48,7 @@
      * @param children          where the matching children should be added
      * @param collectNodes      true, if child nodes should be collected; otherwise false
      * @param collectProperties true, if child properties should be collected; otherwise false
-     * @param maxLevel          umber of hierarchy levels to traverse
+     * @param maxLevel          number of hierarchy levels to traverse
      *                          (e.g. 1 for direct children only, 2 for children and their children, and so on)
      */
     public ChildrenCollectorFilter(
@@ -54,6 +56,29 @@
             boolean collectNodes, boolean collectProperties, int maxLevel) {
         super(false, maxLevel);
         this.namePattern = namePattern;
+        nameGlobs = null;
+        this.children = children;
+        this.collectNodes = collectNodes;
+        this.collectProperties = collectProperties;
+    }
+
+    /**
+     * Constructs a <code>ChildrenCollectorFilter</code>
+     *
+     * @param nameGlobs         an array of globbing strings which should be
+     *                          applied to the names of the children
+     * @param children          where the matching children should be added
+     * @param collectNodes      true, if child nodes should be collected; otherwise false
+     * @param collectProperties true, if child properties should be collected; otherwise false
+     * @param maxLevel          number of hierarchy levels to traverse
+     *                          (e.g. 1 for direct children only, 2 for children and their children, and so on)
+     */
+    public ChildrenCollectorFilter(
+            String[] nameGlobs, Collection children,
+            boolean collectNodes, boolean collectProperties, int maxLevel) {
+        super(false, maxLevel);
+        this.nameGlobs = nameGlobs;
+        namePattern = null;
         this.children = children;
         this.collectNodes = collectNodes;
         this.collectProperties = collectProperties;
@@ -65,8 +90,14 @@
     protected void entering(Node node, int level)
             throws RepositoryException {
         if (level > 0 && collectNodes) {
-            if (matches(node.getName(), namePattern)) {
-                children.add(node);
+            if (namePattern != null) {
+                if (matches(node.getName(), namePattern)) {
+                    children.add(node);
+                }
+            } else {
+                if (matches(node.getName(), nameGlobs)) {
+                    children.add(node);
+                }
             }
         }
     }
@@ -77,8 +108,14 @@
     protected void entering(Property property, int level)
             throws RepositoryException {
         if (level > 0 && collectProperties) {
-            if (matches(property.getName(), namePattern)) {
-                children.add(property);
+            if (namePattern != null) {
+                if (matches(property.getName(), namePattern)) {
+                    children.add(property);
+                }
+            } else {
+                if (matches(property.getName(), nameGlobs)) {
+                    children.add(property);
+                }
             }
         }
     }
@@ -110,6 +147,7 @@
      *               ''', '"', '|' or any whitespace
      *               character *)
      * </pre>
+     * Note that leading and trailing whitespace around a pattern <i>is</i> ignored.
      *
      * @param name the name to test the pattern with
      * @param pattern the pattern to be matched against the name
@@ -130,6 +168,32 @@
     }
 
     /**
+     * Matches the <code>nameGlob</code> strings in the passed array against
+     * the specified name.
+     * <p>
+     * A glob may be a full name or a partial name with one or more
+     * wildcard characters ("<code>*</code>").
+     * <p>
+     * Note that unlike in the case of the {@link #matches(String, String)}
+     * leading and trailing whitespace around a glob is <i>not</i> ignored.
+     *
+     * @param name the name to test the pattern with
+     * @param nameGlobs an array of globbing strings
+     * @return true if the specified name matches any of the globs
+     * @see javax.jcr.Node#getNodes(String[])
+     */
+    public static boolean matches(String name, String[] nameGlobs) {
+        for (int i = 0; i < nameGlobs.length; i++) {
+            // use globbing string as-is, i.e. don't trim any leading/trailing
+            // whitespace
+            if (internalMatches(name, nameGlobs[i], 0, 0)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    /**
      * Internal helper used to recursively match the pattern
      *
      * @param s       The string to be tested