jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thom...@apache.org
Subject svn commit: r1138557 - in /jackrabbit/sandbox/microkernel/src: main/java/org/apache/jackrabbit/mk/util/PathUtils.java test/java/org/apache/jackrabbit/mk/PathTest.java
Date Wed, 22 Jun 2011 17:21:30 GMT
Author: thomasm
Date: Wed Jun 22 17:21:29 2011
New Revision: 1138557

URL: http://svn.apache.org/viewvc?rev=1138557&view=rev
Log:
Improve path utils (better support weird paths)

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=1138557&r1=1138556&r2=1138557&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 Jun 22 17:21:29 2011
@@ -36,11 +36,17 @@ public class PathUtils {
     }
 
     public static String getParentPath(String path) {
-        int pos = getPreviousSlash(path, path.length() - 1);
+        int end = path.length() - 1;
+        while (end >= 0 && path.charAt(end) == '/') {
+            end--;
+        }
+        int pos = getPreviousSlash(path, end);
         if (pos > 0) {
             return path.substring(0, pos);
+        } else if (pos < 0 && !path.startsWith("/")) {
+            return "";
         }
-        return "/";
+        return path.length() > 0 ? "/" : path;
     }
 
     /**
@@ -50,12 +56,16 @@ public class PathUtils {
      * @return the last element
      */
     public static String getName(String path) {
-        if (path.endsWith("/")) {
-            path = path.substring(0, path.length() - 1);
+        int end = path.length() - 1;
+        while (end >= 0 && path.charAt(end) == '/') {
+            end--;
+            if (end < 0) {
+                return "";
+            }
         }
-        int pos = getPreviousSlash(path, path.length() - 1);
+        int pos = getPreviousSlash(path, end);
         if (pos != -1) {
-            return path.substring(pos + 1);
+            return path.substring(pos + 1, end + 1);
         }
         return path;
     }
@@ -107,12 +117,15 @@ public class PathUtils {
         if (nameOrRelativePath.startsWith("/")) {
             throw new IllegalArgumentException("can't append absolute path");
         }
-        StringBuilder buf = new StringBuilder(parentPath);
-        if (buf.charAt(buf.length() - 1) != '/') {
-            buf.append('/');
+        int parentLen = parentPath.length();
+        StringBuilder buff = new StringBuilder(
+                parentLen + 1 + nameOrRelativePath.length());
+        buff.append(parentPath);
+        if (parentLen > 0 && !parentPath.endsWith("/")) {
+            buff.append('/');
         }
-        buf.append(nameOrRelativePath);
-        return buf.toString();
+        buff.append(nameOrRelativePath);
+        return buff.toString();
     }
 
     /**

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=1138557&r1=1138556&r2=1138557&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
Jun 22 17:21:29 2011
@@ -50,15 +50,27 @@ public class PathTest extends TestCase {
 
     public void testMore() {
         String[] paths = {
+            "",
+            "/",
             "/foo",
             "/foo/",
             "/foo//",
             "/foo///",
             "foo",
+            "/foo",
+            "/foo/bar",
+            "/foo/bar/",
+            "/foo/bar//",
         };
 
         for (String path : paths) {
-            assertEquals(path, PathUtils.concat(PathUtils.getParentPath(path), PathUtils.getName(path)));
+            String parent = PathUtils.getParentPath(path);
+            String name = PathUtils.getName(path);
+            String concat = PathUtils.concat(parent, name);
+            while (path.endsWith("/") && path.length() > 1) {
+                path = path.substring(0, path.length() - 1);
+            }
+            assertEquals(path, concat);
         }
     }
 



Mime
View raw message