jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thom...@apache.org
Subject svn commit: r1151486 - in /jackrabbit/sandbox/microkernel/src: main/java/org/apache/jackrabbit/mk/util/PathUtils.java test/java/org/apache/jackrabbit/mk/PathTest.java
Date Wed, 27 Jul 2011 14:49:40 GMT
Author: thomasm
Date: Wed Jul 27 14:49:39 2011
New Revision: 1151486

URL: http://svn.apache.org/viewvc?rev=1151486&view=rev
Log:
PathUtils simplification

Modified:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/PathUtils.java
    jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/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=1151486&r1=1151485&r2=1151486&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 Jul 27 14:49:39 2011
@@ -71,12 +71,9 @@ public class PathUtils {
     public static String getParentPath(String path) {
         assertValid(path);
 
-        if (path.equals("/") || path.length() == 0) {
+        if (path.length() == 0 || path.equals("/")) {
             return path;
         }
-        if (path.endsWith("/")) {
-            throw new IllegalArgumentException(path + " (ends with '/')");
-        }
         int end = path.length() - 1;
         int pos = getPreviousSlash(path, end);
         if (pos > 0) {
@@ -97,7 +94,7 @@ public class PathUtils {
     public static String getName(String path) {
         assertValid(path);
 
-        if (path.equals("/") || path.length() == 0) {
+        if (path.length() == 0 || path.equals("/")) {
             return "";
         }
         int end = path.length() - 1;
@@ -120,7 +117,7 @@ public class PathUtils {
 
         int count = 1, i = 0;
         if (path.startsWith("/")) {
-            if (PathUtils.denotesRoot(path)) {
+            if (path.equals("/")) {
                 return 0;
             }
             i++;
@@ -134,16 +131,25 @@ public class PathUtils {
         }
     }
 
+    /**
+     * Split a path into elements. The root path ("/") and the empty path ("")
+     * is zero elements.
+     *
+     * @param path the path
+     * @return the path elements
+     */
     public static String[] split(String path) {
         assertValid(path);
 
-        ArrayList<String> list = new ArrayList<String>();
-        if (path.startsWith("/")) {
+        if (path.length() == 0) {
+            return new String[0];
+        } else if (path.startsWith("/")) {
             if (path.length() == 1) {
                 return new String[0];
             }
             path = path.substring(1);
         }
+        ArrayList<String> list = new ArrayList<String>();
         while(true) {
             int index = getNextSlash(path, 0);
             if (index < 0) {
@@ -202,7 +208,7 @@ public class PathUtils {
             return "";
         }
 
-        String prefix = denotesRoot(parentPath)
+        String prefix = parentPath.equals("/")
             ? parentPath
             : parentPath + '/';
 
@@ -224,21 +230,7 @@ public class PathUtils {
     public static int getNextSlash(String path, int index) {
         assertValid(path);
 
-        for (int len = path.length(); index < len; index++) {
-            char c = path.charAt(index);
-            if (c == '/') {
-                return index;
-            }
-            if (c == '{') {
-                do {
-                    index++;
-                    if (index >= len) {
-                        throw new IllegalArgumentException(path);
-                    }
-                } while (path.charAt(index) != '}');
-            }
-        }
-        return -1;
+        return path.indexOf('/', index);
     }
 
     /**
@@ -253,36 +245,20 @@ public class PathUtils {
     public static int getPreviousSlash(String path, int index) {
         assertValid(path);
 
-        for (; index >= 0; index--) {
-            char c = path.charAt(index);
-            if (c == '/') {
-                break;
-            }
-            if (c == '}') {
-                do {
-                    index--;
-                    if (index < 0) {
-                        throw new IllegalArgumentException(path);
-                    }
-                } while (path.charAt(index) != '{');
-            }
-        }
-        return index;
+        return path.lastIndexOf('/', index);
     }
 
     /**
      * Check if the path is valid, and throw an IllegalArgumentException if not.
      * A valid path is absolute (starts with a '/') or relative (doesn't start
-     * with '/'), and contains none or more elements. A path may not end with '/',
-     * except for the root path. Elements itself must be at least one character
-     * long. Within an element, there may be one or more section starting with
-     * '{' and ending with '}' that can contain any character (including '/',
-     * but not '{' or '}').
+     * with '/'), and contains none or more elements. A path may not end with
+     * '/', except for the root path. Elements itself must be at least one
+     * character long.
      *
      * @param path the path
      */
     public static void validate(String path) {
-        if ("/".equals(path) || path.length() == 0) {
+        if (path.length() == 0 || "/".equals(path)) {
             return;
         } else if (path.endsWith("/")) {
             throw new IllegalArgumentException(path + " (ends with '/')");
@@ -295,22 +271,6 @@ public class PathUtils {
                     throw new IllegalArgumentException(path + " (contains '//')");
                 }
             }
-            if (c == '{') {
-                while (true) {
-                    index++;
-                    if (index >= len) {
-                        throw new IllegalArgumentException(path + " (missing '{')");
-                    }
-                    char x = path.charAt(index);
-                    if (x == '{') {
-                        throw new IllegalArgumentException(path +" (unmatched '{')");
-                    } if (x == '}') {
-                        break;
-                    }
-                }
-            } else if (c == '}') {
-                throw new IllegalArgumentException(path + " (missing '{')");
-            }
             last = c;
         }
     }

Modified: jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/PathTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/PathTest.java?rev=1151486&r1=1151485&r2=1151486&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/PathTest.java (original)
+++ jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/PathTest.java Wed
Jul 27 14:49:39 2011
@@ -24,20 +24,6 @@ public class PathTest extends TestCase {
     public void test() {
 
         try {
-            PathUtils.getNextSlash("/{error", 1);
-            fail();
-        } catch (IllegalArgumentException e) {
-            // expected
-        }
-
-        try {
-            PathUtils.getPreviousSlash("error}", "error}".length() - 1);
-            fail();
-        } catch (IllegalArgumentException e) {
-            // expected
-        }
-
-        try {
             PathUtils.getParentPath("invalid/path/");
             fail();
         } catch (IllegalArgumentException e) {
@@ -52,14 +38,7 @@ public class PathTest extends TestCase {
         }
 
         test("parent", "child");
-
-        test("{/}parent", "child");
-        test("{/}parent", "{/}child");
-        test("parent", "{/}child");
-
-        test("{x/y}parent", "child");
-        test("{x/y}parent", "{x/y}child");
-        test("parent", "{x/y}child");
+        test("x", "y");
     }
 
     public void testValidate() {
@@ -70,10 +49,11 @@ public class PathTest extends TestCase {
                 "test/",
                 "/test//",
                 "/test//test",
-                "{{}",
-                "{}}",
-                "{test",
-                "}test"
+                "//x",
+                "/x/",
+                "x/",
+                "/x//",
+                "/x//x",
         }) {
             try {
                 PathUtils.validate(invalid);
@@ -89,11 +69,10 @@ public class PathTest extends TestCase {
                 "test/test",
                 "/test",
                 "/test/test",
-                "{}",
-                "{}/{}",
-                "/{}",
-                "/{}/{}",
-                "/{/}",
+                "x",
+                "x/x",
+                "/x",
+                "/x/x",
         }) {
             PathUtils.validate(valid);
         }
@@ -109,6 +88,12 @@ public class PathTest extends TestCase {
             "/foo/bar",
             "foo/bar/baz",
             "/foo/bar/baz",
+            "x",
+            "/x",
+            "x/y",
+            "/x/y",
+            "x/y/z",
+            "/x/y/z",
         };
 
         for (String path : paths) {
@@ -125,9 +110,10 @@ public class PathTest extends TestCase {
     private void test(String parent, String child) {
 
         // split
-        assertEquals("/".split("/").length, PathUtils.split("/").length);
-        assertEquals("x".split("/").length, PathUtils.split("x").length);
-        assertEquals("x/x".split("/").length, PathUtils.split("x/x").length);
+        assertEquals(0, PathUtils.split("").length);
+        assertEquals(0, PathUtils.split("/").length);
+        assertEquals(1, PathUtils.split(parent).length);
+        assertEquals(2, PathUtils.split(parent + "/" + child).length);
         assertEquals(1, PathUtils.split("/" + parent).length);
         assertEquals(2, PathUtils.split("/" + parent + "/" + child).length);
 



Mime
View raw message