jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdue...@apache.org
Subject svn commit: r1384559 - in /jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak: namepath/ plugins/type/
Date Thu, 13 Sep 2012 22:01:45 GMT
Author: mduerig
Date: Thu Sep 13 22:01:45 2012
New Revision: 1384559

URL: http://svn.apache.org/viewvc?rev=1384559&view=rev
Log:
OAK-66: JCR Node Type Management
Simplify path and name validation in node type validation

Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/namepath/JcrNameParser.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/namepath/JcrPathParser.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/namepath/NamePathMapperImpl.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/AbstractNodeTypeManager.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/NodeTypeImpl.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/NodeTypeManagerImpl.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/namepath/JcrNameParser.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/namepath/JcrNameParser.java?rev=1384559&r1=1384558&r2=1384559&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/namepath/JcrNameParser.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/namepath/JcrNameParser.java
Thu Sep 13 22:01:45 2012
@@ -209,4 +209,17 @@ public class JcrNameParser {
         return listener.name(jcrName, index);
     }
 
+    public static boolean validate(String jcrName) {
+        Listener listener = new Listener() {
+            @Override
+            public void error(String message) {
+            }
+
+            @Override
+            public boolean name(String name, int index) {
+                return true;
+            }
+        };
+        return parse(jcrName, listener, 0);
+    }
 }

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/namepath/JcrPathParser.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/namepath/JcrPathParser.java?rev=1384559&r1=1384558&r2=1384559&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/namepath/JcrPathParser.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/namepath/JcrPathParser.java
Thu Sep 13 22:01:45 2012
@@ -41,26 +41,26 @@ public class JcrPathParser {
         boolean parent();
     }
 
-    public static void parse(String jcrPath, Listener listener) {
+    public static boolean parse(String jcrPath, Listener listener) {
         // check for length
         int len = jcrPath == null ? 0 : jcrPath.length();
 
         // shortcut for root path
         if (len == 1 && jcrPath.charAt(0) == '/') {
             listener.root();
-            return;
+            return true;
         }
 
         // short cut for empty path
         if (len == 0) {
-            return;
+            return true;
         }
 
         // check if absolute path
         int pos = 0;
         if (jcrPath.charAt(0) == '/') {
             if (!listener.root()) {
-                return;
+                return false;
             }
             pos++;
         }
@@ -88,7 +88,7 @@ public class JcrPathParser {
                     if (state == STATE_PREFIX_START && c != EOF) {
                         listener.error('\'' + jcrPath + "' is not a valid path. " +
                                 "double slash '//' not allowed.");
-                        return;
+                        return false;
                     }
                     if (state == STATE_PREFIX
                             || state == STATE_NAME
@@ -100,13 +100,13 @@ public class JcrPathParser {
                             if (wasSlash) {
                                 listener.error('\'' + jcrPath + "' is not a valid path: "
+
                                         "Trailing slashes not allowed in prefixes and names.");
-                                return;
+                                return false;
                             }
                             name = jcrPath.substring(lastPos, pos - 1);
                         }
 
                         if (!JcrNameParser.parse(name, listener, index)) {
-                            return;
+                            return false;
                         }
                         state = STATE_PREFIX_START;
                         lastPos = pos;
@@ -114,13 +114,13 @@ public class JcrPathParser {
                         index = 0;
                     } else if (state == STATE_DOT) {
                         if (!listener.current()) {
-                            return;
+                            return false;
                         }
                         lastPos = pos;
                         state = STATE_PREFIX_START;
                     } else if (state == STATE_DOTDOT) {
                         if (!listener.parent()) {
-                            return;
+                            return false;
                         }
                         lastPos = pos;
                         state = STATE_PREFIX_START;
@@ -128,7 +128,7 @@ public class JcrPathParser {
                             && !(state == STATE_PREFIX_START && c == EOF))
{ // ignore trailing slash
                         listener.error('\'' + jcrPath + "' is not a valid path. '" + c +
                                 "' not a valid name character.");
-                        return;
+                        return false;
                     }
                     break;
 
@@ -142,7 +142,7 @@ public class JcrPathParser {
                     } else if (state == STATE_INDEX_END) {
                         listener.error('\'' + jcrPath + "' is not a valid path. '" + c +
                                 "' not valid after index. '/' expected.");
-                        return;
+                        return false;
                     }
                     break;
 
@@ -150,19 +150,19 @@ public class JcrPathParser {
                     if (state == STATE_PREFIX_START) {
                         listener.error('\'' + jcrPath + "' is not a valid path. Prefix "
+
                                 "must not be empty");
-                        return;
+                        return false;
                     } else if (state == STATE_PREFIX) {
                         if (wasSlash) {
                             listener.error('\'' + jcrPath + "' is not a valid path: " +
                                     "Trailing slashes not allowed in prefixes and names.");
-                            return;
+                            return false;
                         }
                         state = STATE_NAME_START;
                         // don't reset the lastPos/pos since prefix+name are passed together
to the NameResolver
                     } else if (state != STATE_URI) {
                         listener.error('\'' + jcrPath + "' is not a valid path. '" + c +
                                 "' not valid name character");
-                        return;
+                        return false;
                     }
                     break;
 
@@ -171,7 +171,7 @@ public class JcrPathParser {
                         if (wasSlash) {
                             listener.error('\'' + jcrPath + "' is not a valid path: " +
                                     "Trailing slashes not allowed in prefixes and names.");
-                            return;
+                            return false;
                         }
                         state = STATE_INDEX;
                         name = jcrPath.substring(lastPos, pos - 1);
@@ -187,18 +187,18 @@ public class JcrPathParser {
                             listener.error('\'' + jcrPath + "' is not a valid path. " +
                                     "NumberFormatException in index: " +
                                     jcrPath.substring(lastPos, pos - 1));
-                            return;
+                            return false;
                         }
                         if (index < 0) {
                             listener.error('\'' + jcrPath + "' is not a valid path. " +
                                     "Index number invalid: " + index);
-                            return;
+                            return false;
                         }
                         state = STATE_INDEX_END;
                     } else {
                         listener.error('\'' + jcrPath + "' is not a valid path. '" + c +
                                 "' not a valid name character.");
-                        return;
+                        return false;
                     }
                     break;
 
@@ -206,11 +206,11 @@ public class JcrPathParser {
                     if (state == STATE_PREFIX_START || state == STATE_NAME_START) {
                         listener.error('\'' + jcrPath + "' is not a valid path. '" + c +
                                 "' not valid name start");
-                        return;
+                        return false;
                     } else if (state == STATE_INDEX_END) {
                         listener.error('\'' + jcrPath + "' is not a valid path. '" + c +
                                 "' not valid after index. '/' expected.");
-                        return;
+                        return false;
                     } else if (state == STATE_DOT || state == STATE_DOTDOT) {
                         state = STATE_PREFIX;
                     }
@@ -219,12 +219,12 @@ public class JcrPathParser {
                 case '\t':
                     listener.error('\'' + jcrPath + "' is not a valid path. " +
                             "Whitespace not a allowed in name.");
-                    return;
+                    return false;
                 case '*':
                 case '|':
                     listener.error('\'' + jcrPath + "' is not a valid path. '" + c +
                             "' not a valid name character.");
-                    return;
+                    return false;
                 case '{':
                     if (state == STATE_PREFIX_START && lastPos == pos-1) {
                         // '{' marks the start of a uri enclosed in an expanded name
@@ -251,11 +251,48 @@ public class JcrPathParser {
                     } else if (state == STATE_INDEX_END) {
                         listener.error('\'' + jcrPath + "' is not a valid path. '" + c +
                                 "' not valid after index. '/' expected.");
-                        return;
+                        return false;
                     }
             }
             wasSlash = c == ' ';
         }
+        return true;
     }
 
+    public static boolean validate(String jcrPath) {
+        Listener listener = new Listener() {
+            boolean hasRoot;
+            @Override
+            public boolean root() {
+                if (hasRoot) {
+                    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) {
+            }
+
+            @Override
+            public boolean name(String name, int index) {
+                return true;
+            }
+
+        };
+        return parse(jcrPath, listener);
+    }
 }

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=1384559&r1=1384558&r2=1384559&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 22:01:45 2012
@@ -201,7 +201,13 @@ public class NamePathMapperImpl implemen
         // try a shortcut
         if (!hasNameStartingWithDot && !hasClarkBrackets && !hasIndexBrackets)
{
             if (!hasColon || !hasSessionLocalMappings()) {
-                return validateJcrPath(jcrPath);
+                if (JcrPathParser.validate(jcrPath)) {
+                    return jcrPath;
+                }
+                else {
+                    log.debug("Invalid path: {}", jcrPath);
+                    return null;
+                }
             }
         }
 
@@ -286,65 +292,4 @@ public class NamePathMapperImpl implemen
         return oakPath.toString();
     }
 
-    /**
-     * 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;
-        }
-    }
 }

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/AbstractNodeTypeManager.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/AbstractNodeTypeManager.java?rev=1384559&r1=1384558&r2=1384559&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/AbstractNodeTypeManager.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/AbstractNodeTypeManager.java
Thu Sep 13 22:01:45 2012
@@ -37,7 +37,6 @@ import org.apache.jackrabbit.commons.ite
 import org.apache.jackrabbit.oak.api.CoreValueFactory;
 import org.apache.jackrabbit.oak.api.Tree;
 import org.apache.jackrabbit.oak.namepath.NameMapper;
-import org.apache.jackrabbit.oak.namepath.NamePathMapper;
 import org.apache.jackrabbit.oak.namepath.NamePathMapperImpl;
 import org.apache.jackrabbit.oak.plugins.memory.MemoryValueFactory;
 import org.apache.jackrabbit.oak.util.NodeUtil;
@@ -90,11 +89,6 @@ public abstract class AbstractNodeTypeMa
     }
 
     @Nonnull
-    protected NamePathMapper getNamePathMapper() {
-        return NamePathMapper.DEFAULT;
-    }
-
-    @Nonnull
     protected CoreValueFactory getCoreValueFactory() {
         return MemoryValueFactory.INSTANCE;
     }
@@ -118,7 +112,7 @@ public abstract class AbstractNodeTypeMa
         if (types != null) {
             Tree type = types.getChild(getOakName(name));
             if (type != null) {
-                return new NodeTypeImpl(this, getValueFactory(), getNamePathMapper(),
+                return new NodeTypeImpl(this, getValueFactory(),
                         new NodeUtil(type, getCoreValueFactory(), getNameMapper()));
             }
         }
@@ -131,7 +125,7 @@ public abstract class AbstractNodeTypeMa
         Tree types = getTypes();
         if (types != null) {
             for (Tree type : types.getChildren()) {
-                list.add(new NodeTypeImpl(this, getValueFactory(), getNamePathMapper(),
+                list.add(new NodeTypeImpl(this, getValueFactory(),
                         new NodeUtil(type, getCoreValueFactory(), getNameMapper())));
 
             }

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/NodeTypeImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/NodeTypeImpl.java?rev=1384559&r1=1384558&r2=1384559&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/NodeTypeImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/NodeTypeImpl.java
Thu Sep 13 22:01:45 2012
@@ -42,7 +42,8 @@ import javax.jcr.nodetype.PropertyDefini
 import javax.security.auth.Subject;
 
 import org.apache.jackrabbit.commons.iterator.NodeTypeIteratorAdapter;
-import org.apache.jackrabbit.oak.namepath.NamePathMapper;
+import org.apache.jackrabbit.oak.namepath.JcrNameParser;
+import org.apache.jackrabbit.oak.namepath.JcrPathParser;
 import org.apache.jackrabbit.oak.plugins.identifier.IdentifierManager;
 import org.apache.jackrabbit.oak.plugins.type.constraint.Constraints;
 import org.apache.jackrabbit.oak.spi.security.principal.AdminPrincipal;
@@ -81,16 +82,13 @@ class NodeTypeImpl implements NodeType {
 
     private final NodeTypeManager manager;
 
-    private final NamePathMapper mapper;
-
     private final ValueFactory factory;
 
     private final NodeUtil node;
 
     public NodeTypeImpl(
-            NodeTypeManager manager, ValueFactory factory, NamePathMapper mapper, NodeUtil
node) {
+            NodeTypeManager manager, ValueFactory factory, NodeUtil node) {
         this.manager = manager;
-        this.mapper = mapper;
         this.factory = factory;
         this.node = node;
     }
@@ -310,7 +308,7 @@ class NodeTypeImpl implements NodeType {
         return false;
     }
 
-    private boolean meetsTypeConstraints(Value value, int requiredType) {
+    private static boolean meetsTypeConstraints(Value value, int requiredType) {
         try {
             switch (requiredType) {
                 case PropertyType.STRING:
@@ -332,13 +330,13 @@ class NodeTypeImpl implements NodeType {
                     value.getBoolean();
                     return true;
                 case PropertyType.NAME:
-                    return mapper.getOakName(value.getString()) != null;
+                    return JcrNameParser.validate(value.getString());
                 case PropertyType.PATH:
                     int type = value.getType();
                     return type != PropertyType.DOUBLE &&
                            type != PropertyType.LONG &&
                            type != PropertyType.BOOLEAN &&
-                           mapper.getOakPath(value.getString()) != null;
+                            JcrPathParser.validate(value.getString());
                 case PropertyType.REFERENCE:
                 case PropertyType.WEAKREFERENCE:
                     return IdentifierManager.isValidUUID(value.getString());

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/NodeTypeManagerImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/NodeTypeManagerImpl.java?rev=1384559&r1=1384558&r2=1384559&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/NodeTypeManagerImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/NodeTypeManagerImpl.java
Thu Sep 13 22:01:45 2012
@@ -22,7 +22,6 @@ import java.security.PrivilegedAction;
 import java.util.List;
 import java.util.Map;
 
-import javax.annotation.Nonnull;
 import javax.jcr.PropertyType;
 import javax.jcr.RepositoryException;
 import javax.jcr.Value;
@@ -196,12 +195,6 @@ public class NodeTypeManagerImpl extends
         return factory;
     }
 
-    @Nonnull
-    @Override
-    protected NamePathMapper getNamePathMapper() {
-        return mapper;
-    }
-
     @Override
     protected CoreValueFactory getCoreValueFactory() {
         return session.getCoreValueFactory();
@@ -293,7 +286,7 @@ public class NodeTypeManagerImpl extends
             internalRegisterNodeDefinition(def, nd);
         }
 
-        return new NodeTypeImpl(this, this.factory, this.mapper, node);
+        return new NodeTypeImpl(this, this.factory, node);
     }
 
     private static void internalRegisterItemDefinition(



Mime
View raw message