jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdue...@apache.org
Subject svn commit: r1384266 - /jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/namepath/NamePathMapperImpl.java
Date Thu, 13 Sep 2012 10:23:18 GMT
Author: mduerig
Date: Thu Sep 13 10:23:17 2012
New Revision: 1384266

URL: http://svn.apache.org/viewvc?rev=1384266&view=rev
Log:
OAK-108: Shortcut path conversions
validate path in the case of a short cut conversion

Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/namepath/NamePathMapperImpl.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/namepath/NamePathMapperImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/namepath/NamePathMapperImpl.java?rev=1384266&r1=1384265&r2=1384266&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/namepath/NamePathMapperImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/namepath/NamePathMapperImpl.java
Thu Sep 13 10:23:17 2012
@@ -201,7 +201,7 @@ public class NamePathMapperImpl implemen
         // try a shortcut
         if (!hasNameStartingWithDot && !hasClarkBrackets && !hasIndexBrackets)
{
             if (!hasColon || !hasSessionLocalMappings()) {
-                return removeTrailingSlash(jcrPath);
+                return validateJcrPath(jcrPath);
             }
         }
 
@@ -286,13 +286,65 @@ public class NamePathMapperImpl implemen
         return oakPath.toString();
     }
 
-    private static String removeTrailingSlash(String path) {
-        if ("/".equals(path) || path.isEmpty()) {
-            return path;
-        } else if (path.endsWith("/")) {
-            return path.substring(0, path.length() - 1);
-        } else {
-            return path;
+    /**
+     * Validate a jcrPath assuming it doesn't contain any of the following
+     * characters: {@code {, }, [, ], ., :}.
+     * @param jcrPath  path to validate
+     * @return  {@code jcrPath} i.e. the same string instance if valid.
+     *      {@code null} otherwise.
+     */
+    private String validateJcrPath(String jcrPath) {
+        final StringBuilder parseErrors = new StringBuilder();
+        JcrPathParser.Listener listener = new JcrPathParser.Listener() {
+            boolean hasRoot;
+
+            @Override
+            public boolean root() {
+                if (hasRoot) {
+                    parseErrors.append("/ on non-empty path");
+                    return false;
+                }
+                else {
+                    hasRoot = true;
+                    return true;
+                }
+            }
+
+            @Override
+            public boolean current() {
+                return true;
+            }
+
+            @Override
+            public boolean parent() {
+                return true;
+            }
+
+            @Override
+            public void error(String message) {
+                parseErrors.append(message);
+            }
+
+            @Override
+            public boolean name(String name, int index) {
+                String p = nameMapper.getOakName(name);
+                if (p == null) {
+                    parseErrors.append("Invalid name: ").append(name);
+                    return false;
+                }
+                else {
+                    return true;
+                }
+            }
+        };
+
+        JcrPathParser.parse(jcrPath, listener);
+        if (parseErrors.length() != 0) {
+            log.debug("Could not parse path " + jcrPath + ": " + parseErrors.toString());
+            return null;
+        }
+        else {
+            return jcrPath;
         }
     }
 }



Mime
View raw message