You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by re...@apache.org on 2018/01/19 08:32:27 UTC

svn commit: r1821597 - /jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/ChildrenCollectorFilter.java

Author: reschke
Date: Fri Jan 19 08:32:27 2018
New Revision: 1821597

URL: http://svn.apache.org/viewvc?rev=1821597&view=rev
Log:
JCR-4001: When using Node.getProperties(String namePattern) also child nodes are processed

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

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=1821597&r1=1821596&r2=1821597&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 Fri Jan 19 08:32:27 2018
@@ -30,6 +30,7 @@ import org.apache.jackrabbit.commons.ite
 
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 
 /**
  * <code>ChildrenCollectorFilter</code> is a utility class
@@ -64,7 +65,7 @@ public class ChildrenCollectorFilter ext
             boolean collectNodes, boolean collectProperties, int maxLevel) {
         super(false, maxLevel);
         this.namePattern = namePattern;
-        nameGlobs = null;
+        this.nameGlobs = null;
         this.children = children;
         this.collectNodes = collectNodes;
         this.collectProperties = collectProperties;
@@ -86,7 +87,7 @@ public class ChildrenCollectorFilter ext
             boolean collectNodes, boolean collectProperties, int maxLevel) {
         super(false, maxLevel);
         this.nameGlobs = nameGlobs;
-        namePattern = null;
+        this.namePattern = null;
         this.children = children;
         this.collectNodes = collectNodes;
         this.collectProperties = collectProperties;
@@ -110,17 +111,26 @@ public class ChildrenCollectorFilter ext
 
     public static PropertyIterator collectProperties(
             Node node, String namePattern) throws RepositoryException {
-        Collection<Item> properties = new ArrayList<Item>();
-        node.accept(new ChildrenCollectorFilter(
-                namePattern, properties, false, true, 1));
+        Collection<Item> properties = Collections.emptySet();
+        PropertyIterator pit = node.getProperties();
+        while (pit.hasNext()) {
+            Property p = pit.nextProperty();
+            if (matches(p.getName(), namePattern)) {
+                properties = addToCollection(properties, p);
+            }
+        }
         return new PropertyIteratorAdapter(properties);
     }
 
-    public static PropertyIterator collectProperties(
-            Node node, String[] nameGlobs) throws RepositoryException {
-        Collection<Item> properties = new ArrayList<Item>();
-        node.accept(new ChildrenCollectorFilter(
-                nameGlobs, properties, false, true, 1));
+    public static PropertyIterator collectProperties(Node node, String[] nameGlobs) throws RepositoryException {
+        Collection<Item> properties = Collections.emptySet();
+        PropertyIterator pit = node.getProperties();
+        while (pit.hasNext()) {
+            Property p = pit.nextProperty();
+            if (matches(p.getName(), nameGlobs)) {
+                properties = addToCollection(properties, p);
+            }
+        }
         return new PropertyIteratorAdapter(properties);
     }
 
@@ -179,4 +189,18 @@ public class ChildrenCollectorFilter ext
     public static boolean matches(String name, String[] nameGlobs) {
         return ItemNameMatcher.matches(name, nameGlobs);
     }
+    
+    private static Collection<Item> addToCollection(Collection<Item> c, Item p) {
+        Collection<Item> nc = c;
+        if (c.isEmpty()) {
+            nc = Collections.singleton(p);
+        } else if (c.size() == 1) {
+            nc = new ArrayList<Item>(c);
+            nc.add(p);
+        } else {
+            nc.add(p);
+        }
+
+        return nc;
+    }
 }