jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdue...@apache.org
Subject svn commit: r1418299 - /jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/namepath/NamePathMapperImpl.java
Date Fri, 07 Dec 2012 13:18:19 GMT
Author: mduerig
Date: Fri Dec  7 13:18:18 2012
New Revision: 1418299

URL: http://svn.apache.org/viewvc?rev=1418299&view=rev
Log:
OAK-498: NamePathMapper should fail on absolute paths escaping root
- factor out duplicate code

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=1418299&r1=1418298&r2=1418299&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
Fri Dec  7 13:18:18 2012
@@ -22,6 +22,7 @@ import java.util.List;
 import javax.annotation.Nonnull;
 
 import org.apache.jackrabbit.oak.commons.PathUtils;
+import org.apache.jackrabbit.oak.namepath.JcrPathParser.Listener;
 import org.apache.jackrabbit.oak.plugins.identifier.IdentifierManager;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -76,26 +77,16 @@ public class NamePathMapperImpl implemen
         return getOakPath(jcrPath, true);
     }
 
+
     @Override
     @Nonnull
     public String getJcrPath(final String oakPath) {
-        final List<String> elements = new ArrayList<String>();
-
         if ("/".equals(oakPath)) {
             // avoid the need to special case the root path later on
             return "/";
         }
 
-        JcrPathParser.Listener listener = new JcrPathParser.Listener() {
-            @Override
-            public boolean root() {
-                if (!elements.isEmpty()) {
-                    throw new IllegalArgumentException("/ on non-empty path");
-                }
-                elements.add("");
-                return true;
-            }
-
+        PathListener listener = new PathListener() {
             @Override
             public boolean current() {
                 // nothing to do here
@@ -103,23 +94,6 @@ public class NamePathMapperImpl implemen
             }
 
             @Override
-            public boolean parent() {
-                int prevIdx = elements.size() - 1;
-                String prevElem = prevIdx >= 0 ? elements.get(prevIdx) : null;
-
-                if (prevElem == null || PathUtils.denotesParent(prevElem)) {
-                    elements.add("..");
-                    return true;
-                }
-                if (prevElem.isEmpty()) {
-                    throw new IllegalArgumentException("Absolute path escapes root: " + oakPath);
-                }
-
-                elements.remove(prevElem);
-                return true;
-            }
-
-            @Override
             public void error(String message) {
                 throw new IllegalArgumentException(message);
             }
@@ -127,7 +101,7 @@ public class NamePathMapperImpl implemen
             @Override
             public boolean name(String name, int index) {
                 if (index > 1) {
-                    throw new IllegalArgumentException("index > 1");
+                    error("index > 1");
                 }
                 String p = nameMapper.getJcrName(name);
                 elements.add(p);
@@ -138,12 +112,12 @@ public class NamePathMapperImpl implemen
         JcrPathParser.parse(oakPath, listener);
 
         // empty path: map to "."
-        if (elements.isEmpty()) {
+        if (listener.elements.isEmpty()) {
             return ".";
         }
 
         StringBuilder jcrPath = new StringBuilder();
-        for (String element : elements) {
+        for (String element : listener.elements) {
             if (element.isEmpty()) {
                 // root
                 jcrPath.append('/');
@@ -222,45 +196,9 @@ public class NamePathMapperImpl implemen
             }
         }
 
-        final List<String> elements = new ArrayList<String>();
         final StringBuilder parseErrors = new StringBuilder();
 
-        JcrPathParser.Listener listener = new JcrPathParser.Listener() {
-
-            @Override
-            public boolean root() {
-                if (!elements.isEmpty()) {
-                    parseErrors.append("/ on non-empty path");
-                    return false;
-                }
-                elements.add("");
-                return true;
-            }
-
-            @Override
-            public boolean current() {
-                // nothing to do here
-                return true;
-            }
-
-            @Override
-            public boolean parent() {
-                int prevIdx = elements.size() - 1;
-                String prevElem = prevIdx >= 0 ? elements.get(prevIdx) : null;
-
-                if (prevElem == null || PathUtils.denotesParent(prevElem)) {
-                    elements.add("..");
-                    return true;
-                }
-                if (prevElem.isEmpty()) {
-                    parseErrors.append("Absolute path escapes root: ").append(jcrPath);
-                    return false;
-                }
-
-                elements.remove(prevElem);
-                return true;
-            }
-
+        PathListener listener = new PathListener() {
             @Override
             public void error(String message) {
                 parseErrors.append(message);
@@ -269,12 +207,12 @@ public class NamePathMapperImpl implemen
             @Override
             public boolean name(String name, int index) {
                 if (!keepIndex && index > 1) {
-                    parseErrors.append("index > 1");
+                    error("index > 1");
                     return false;
                 }
                 String p = nameMapper.getOakName(name);
                 if (p == null) {
-                    parseErrors.append("Invalid name: ").append(name);
+                    error("Invalid name: " + name);
                     return false;
                 }
                 if (keepIndex && index > 0) {
@@ -292,12 +230,12 @@ public class NamePathMapperImpl implemen
         }
 
         // Empty path maps to ""
-        if (elements.isEmpty()) {
+        if (listener.elements.isEmpty()) {
             return "";
         }
 
         StringBuilder oakPath = new StringBuilder();
-        for (String element : elements) {
+        for (String element : listener.elements) {
             if (element.isEmpty()) {
                 // root
                 oakPath.append('/');
@@ -314,4 +252,44 @@ public class NamePathMapperImpl implemen
         return oakPath.toString();
     }
 
+    //------------------------------------------------------------< PathListener >---
+
+    private abstract static class PathListener implements Listener {
+        final List<String> elements = new ArrayList<String>();
+
+        @Override
+        public boolean root() {
+            if (!elements.isEmpty()) {
+                error("/ on non-empty path");
+                return false;
+            }
+            elements.add("");
+            return true;
+        }
+
+        @Override
+        public boolean current() {
+            // nothing to do here
+            return true;
+        }
+
+        @Override
+        public boolean parent() {
+            int prevIdx = elements.size() - 1;
+            String prevElem = prevIdx >= 0 ? elements.get(prevIdx) : null;
+
+            if (prevElem == null || PathUtils.denotesParent(prevElem)) {
+                elements.add("..");
+                return true;
+            }
+            if (prevElem.isEmpty()) {
+                error("Absolute path escapes root");
+                return false;
+            }
+
+            elements.remove(prevElem);
+            return true;
+        }
+    }
+
 }



Mime
View raw message