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/02/02 14:27:51 UTC

svn commit: r1822968 - in /jackrabbit/branches/2.16: ./ jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/ChildrenCollectorFilter.java

Author: reschke
Date: Fri Feb  2 14:27:50 2018
New Revision: 1822968

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

Modified:
    jackrabbit/branches/2.16/   (props changed)
    jackrabbit/branches/2.16/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/ChildrenCollectorFilter.java

Propchange: jackrabbit/branches/2.16/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Fri Feb  2 14:27:50 2018
@@ -1,3 +1,3 @@
 /jackrabbit/branches/JCR-2272:1173165-1176545
 /jackrabbit/sandbox/JCR-2415-lucene-3.0:1060860-1064038
-/jackrabbit/trunk:1816227,1817094,1817097-1817098,1817100,1817113,1817201,1817213,1817341,1817373,1817377,1818586,1820119,1820133
+/jackrabbit/trunk:1816227,1817094,1817097-1817098,1817100,1817113,1817201,1817213,1817341,1817373,1817377,1818586,1820119,1820133,1821597

Modified: jackrabbit/branches/2.16/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/ChildrenCollectorFilter.java
URL: http://svn.apache.org/viewvc/jackrabbit/branches/2.16/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/ChildrenCollectorFilter.java?rev=1822968&r1=1822967&r2=1822968&view=diff
==============================================================================
--- jackrabbit/branches/2.16/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/ChildrenCollectorFilter.java (original)
+++ jackrabbit/branches/2.16/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/ChildrenCollectorFilter.java Fri Feb  2 14:27:50 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;
+    }
 }