jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdue...@apache.org
Subject svn commit: r1221725 - in /jackrabbit/sandbox/microkernel/src: main/java/org/apache/jackrabbit/mk/util/PathUtils.java test/java/org/apache/jackrabbit/mk/util/PathTest.java
Date Wed, 21 Dec 2011 13:46:07 GMT
Author: mduerig
Date: Wed Dec 21 13:46:06 2011
New Revision: 1221725

URL: http://svn.apache.org/viewvc?rev=1221725&view=rev
Log:
- lazy path splitting

Modified:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/PathUtils.java
    jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/util/PathTest.java

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/PathUtils.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/PathUtils.java?rev=1221725&r1=1221724&r2=1221725&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/PathUtils.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/PathUtils.java
Wed Dec 21 13:46:06 2011
@@ -17,6 +17,8 @@
 package org.apache.jackrabbit.mk.util;
 
 import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
 
 /**
  * Utility methods to parse a JCR path.
@@ -196,6 +198,66 @@ public class PathUtils {
     }
 
     /**
+     * Split a path into elements. The root path ("/") and the empty path ("")
+     * is zero elements.
+     *
+     * @param path the path
+     * @return an Iterable for the path elements
+     */
+    public static Iterable<String> elements(final String path) {
+        assertValid(path);
+
+        final Iterator<String> it = new Iterator<String>() {
+            int pos = PathUtils.isAbsolute(path) ? 1 : 0;
+            String next;
+
+            public boolean hasNext() {
+                if (next == null) {
+                    if (pos >= path.length()) {
+                        return false;
+                    }
+                    else {
+                        int i = path.indexOf('/', pos);
+                        if (i < 0) {
+                            next = path.substring(pos);
+                            pos = path.length();
+                        }
+                        else {
+                            next = path.substring(pos, i);
+                            pos = i + 1;
+                        }
+                        return true;
+                    }
+                }
+                else {
+                    return true;
+                }
+            }
+
+            public String next() {
+                if (hasNext()) {
+                    String next = this.next;
+                    this.next = null;
+                    return next;
+                }
+                else {
+                    throw new NoSuchElementException();
+                }
+            }
+
+            public void remove() {
+                throw new UnsupportedOperationException("remove");
+            }
+        };
+
+        return new Iterable<String>() {
+            public Iterator<String> iterator() {
+                return it;
+            }
+        };
+    }
+
+    /**
      * Concatenate path elements.
      *
      * @param parentPath the parent path

Modified: jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/util/PathTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/util/PathTest.java?rev=1221725&r1=1221724&r2=1221725&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/util/PathTest.java
(original)
+++ jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/util/PathTest.java
Wed Dec 21 13:46:06 2011
@@ -18,6 +18,8 @@ package org.apache.jackrabbit.mk.util;
 
 import junit.framework.TestCase;
 
+import java.util.Iterator;
+
 public class PathTest extends TestCase {
 
     public void test() {
@@ -326,4 +328,28 @@ public class PathTest extends TestCase {
         }
     }
 
+    public void testPathElements() {
+        String[] paths = new String[]{"", "/", "/a", "a", "/abc/def/ghj", "abc/def/ghj"};
+        for (String path : paths) {
+            String[] elements = PathUtils.split(path);
+            Iterator<String> it = PathUtils.elements(path).iterator();
+            for (String element : elements) {
+                assertTrue(it.hasNext());
+                assertEquals(element, it.next());
+            }
+            assertFalse(it.hasNext());
+        }
+        
+        String[] invalidPaths = new String[]{"//", "/a/", "a/", "/a//", "a//b"};
+        for (String path: invalidPaths) {
+            try {
+                PathUtils.elements(path);
+                fail();
+            }
+            catch (IllegalArgumentException e) {
+                // expected
+            }
+        }
+    }
+
 }



Mime
View raw message