jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdue...@apache.org
Subject svn commit: r1324820 - in /jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr: ItemStateProvider.java NodeImpl.java Paths.java PropertyImpl.java SessionImpl.java
Date Wed, 11 Apr 2012 15:31:09 GMT
Author: mduerig
Date: Wed Apr 11 15:31:09 2012
New Revision: 1324820

URL: http://svn.apache.org/viewvc?rev=1324820&view=rev
Log:
OAK-61: Implement JCR path handling
- copy required methods from PathUtils to get started

Added:
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/Paths.java
Modified:
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/ItemStateProvider.java
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeImpl.java
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/PropertyImpl.java
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionImpl.java

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/ItemStateProvider.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/ItemStateProvider.java?rev=1324820&r1=1324819&r2=1324820&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/ItemStateProvider.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/ItemStateProvider.java
Wed Apr 11 15:31:09 2012
@@ -1,6 +1,5 @@
 package org.apache.jackrabbit.oak.jcr;
 
-import org.apache.jackrabbit.mk.util.PathUtils;
 import org.apache.jackrabbit.oak.api.PropertyState;
 import org.apache.jackrabbit.oak.api.TransientNodeState;
 
@@ -14,7 +13,7 @@ public class ItemStateProvider {
     TransientNodeState getNodeState(String path){
         TransientNodeState state = root;
 
-        for (String name : PathUtils.elements(path)) {
+        for (String name : Paths.elements(path)) {
             state = state.getChildNode(name);
             if (state == null) {
                 return null;
@@ -25,13 +24,13 @@ public class ItemStateProvider {
     }
 
     PropertyState getPropertyState(String path) {
-        TransientNodeState parentState = getNodeState(PathUtils.getParentPath(path));
+        TransientNodeState parentState = getNodeState(Paths.getParentPath(path));
 
         if (parentState == null) {
             return null;
         }
         else {
-            return parentState.getProperty(PathUtils.getName(path));
+            return parentState.getProperty(Paths.getName(path));
         }
     }
 }

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeImpl.java?rev=1324820&r1=1324819&r2=1324820&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/NodeImpl.java
Wed Apr 11 15:31:09 2012
@@ -19,17 +19,16 @@ package org.apache.jackrabbit.oak.jcr;
 import org.apache.jackrabbit.JcrConstants;
 import org.apache.jackrabbit.commons.iterator.NodeIteratorAdapter;
 import org.apache.jackrabbit.commons.iterator.PropertyIteratorAdapter;
-import org.apache.jackrabbit.mk.util.PathUtils;
 import org.apache.jackrabbit.oak.api.NodeStateEditor;
 import org.apache.jackrabbit.oak.api.PropertyState;
 import org.apache.jackrabbit.oak.api.TransientNodeState;
-import org.apache.jackrabbit.oak.util.Function1;
 import org.apache.jackrabbit.oak.jcr.util.ItemNameMatcher;
-import org.apache.jackrabbit.oak.util.Iterators;
 import org.apache.jackrabbit.oak.jcr.util.LogUtil;
-import org.apache.jackrabbit.oak.util.Predicate;
 import org.apache.jackrabbit.oak.jcr.util.ValueConverter;
 import org.apache.jackrabbit.oak.kernel.ScalarImpl;
+import org.apache.jackrabbit.oak.util.Function1;
+import org.apache.jackrabbit.oak.util.Iterators;
+import org.apache.jackrabbit.oak.util.Predicate;
 import org.apache.jackrabbit.value.ValueHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -129,7 +128,7 @@ public class NodeImpl extends ItemImpl i
      */
     @Override
     public int getDepth() throws RepositoryException {
-        return PathUtils.getDepth(path());
+        return Paths.getDepth(path());
     }
 
     /**
@@ -175,13 +174,13 @@ public class NodeImpl extends ItemImpl i
     public Node addNode(String relPath) throws RepositoryException {
         checkStatus();
 
-        String parentPath = PathUtils.concat(path(), PathUtils.getParentPath(relPath));
+        String parentPath = Paths.concat(path(), Paths.getParentPath(relPath));
         TransientNodeState parentState = getItemStateProvider().getNodeState(parentPath);
         if (parentState == null) {
             throw new PathNotFoundException(relPath);
         }
 
-        String name = PathUtils.getName(relPath);
+        String name = Paths.getName(relPath);
         parentState.getEditor().addNode(name);
         return new NodeImpl(sessionContext, parentState.getChildNode(name));
     }
@@ -874,7 +873,7 @@ public class NodeImpl extends ItemImpl i
     }
 
     private NodeImpl getNodeOrNull(String relPath) {
-        String absPath = PathUtils.concat(path(), relPath);
+        String absPath = Paths.concat(path(), relPath);
         TransientNodeState nodeState = getItemStateProvider().getNodeState(absPath);
         return nodeState == null
             ? null
@@ -882,13 +881,13 @@ public class NodeImpl extends ItemImpl i
     }
     
     private PropertyImpl getPropertyOrNull(String relPath) {
-        String absPath = PathUtils.concat(path(), PathUtils.getParentPath(relPath));
+        String absPath = Paths.concat(path(), Paths.getParentPath(relPath));
         TransientNodeState parentState = getItemStateProvider().getNodeState(absPath);
         if (parentState == null) {
             return null;
         }
 
-        String name = PathUtils.getName(relPath);
+        String name = Paths.getName(relPath);
         PropertyState propertyState = parentState.getProperty(name);
         return propertyState == null
             ? null

Added: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/Paths.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/Paths.java?rev=1324820&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/Paths.java (added)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/Paths.java Wed
Apr 11 15:31:09 2012
@@ -0,0 +1,248 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.jackrabbit.oak.jcr;
+
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+
+/**
+ *
+ */
+public class Paths {
+
+    private Paths() {
+    }
+
+    /**
+     * Whether the path is absolute (starts with a slash) or not.
+     *
+     * @param path the path
+     * @return true if it starts with a slash
+     */
+    public static boolean isAbsolute(String path) {
+        return !path.isEmpty() && path.charAt(0) == '/';
+    }
+
+    /**
+     * Whether the path is the root path ("/").
+     *
+     * @param path the path
+     * @return whether this is the root
+     */
+    public static boolean denotesRoot(String path) {
+        return "/".equals(path);
+    }
+
+    /**
+     * Get the parent of a path. The parent of the root path ("/") is the root
+     * path.
+     *
+     * @param path the path
+     * @return the parent path
+     */
+    public static String getParentPath(String path) {
+        return getAncestorPath(path, 1);
+    }
+
+    /**
+     * Get the last element of the (absolute or relative) path. The name of the
+     * root node ("/") and the name of the empty path ("") is the empty path.
+     *
+     * @param path the complete path
+     * @return the last element
+     */
+    public static String getName(String path) {
+        if (path.isEmpty() || denotesRoot(path)) {
+            return "";
+        }
+        int end = path.length() - 1;
+        int pos = path.lastIndexOf('/', end);
+        if (pos != -1) {
+            return path.substring(pos + 1, end + 1);
+        }
+        return path;
+    }
+
+    /**
+     * Get the nth ancestor of a path. The 1st ancestor is the parent path,
+     * 2nd ancestor the grandparent path, and so on...
+     * <p/>
+     * If nth <= 0, the path argument is returned as is.
+     *
+     * @param path the path
+     * @return the ancestor path
+     */
+    public static String getAncestorPath(String path, int nth) {
+        if (path.isEmpty() || denotesRoot(path)
+                || nth <= 0) {
+            return path;
+        }
+
+        int end = path.length() - 1;
+        int pos = -1;
+        while (nth-- > 0) {
+            pos = path.lastIndexOf('/', end);
+            if (pos > 0) {
+                end = pos - 1;
+            } else if (pos == 0) {
+                return "/";
+            } else {
+                return "";
+            }
+        }
+
+        return path.substring(0, pos);
+    }
+
+    /**
+     * Concatenate path elements.
+     *
+     * @param parentPath the parent path
+     * @param subPath the subPath path to add
+     * @return the concatenated path
+     */
+    public static String concat(String parentPath, String subPath) {
+        // special cases
+        if (parentPath.isEmpty()) {
+            return subPath;
+        } else if (subPath.isEmpty()) {
+            return parentPath;
+        } else if (isAbsolute(subPath)) {
+            throw new IllegalArgumentException("Cannot append absolute path " + subPath);
+        }
+        StringBuilder buff = new StringBuilder(parentPath);
+        if (!denotesRoot(parentPath)) {
+            buff.append('/');
+        }
+        buff.append(subPath);
+        return buff.toString();
+    }
+
+    /**
+     * Relativize a path wrt. a parent path such that
+     * {@code relativize(parentPath, concat(parentPath, path)) == paths}
+     * holds.
+     *
+     * @param parentPath parent pth
+     * @param path path to relativize
+     * @return relativized path
+     */
+    public static String relativize(String parentPath, String path) {
+        if (parentPath.equals(path)) {
+            return "";
+        }
+
+        String prefix = denotesRoot(parentPath)
+                ? parentPath
+                : parentPath + '/';
+
+        if (path.startsWith(prefix)) {
+            return path.substring(prefix.length());
+        }
+        throw new IllegalArgumentException("Cannot relativize " + path + " wrt. " + parentPath);
+    }
+
+    /**
+     * Calculate the number of elements in the path. The root path has zero
+     * elements.
+     *
+     * @param path the path
+     * @return the number of elements
+     */
+    public static int getDepth(String path) {
+        int count = 1, i = 0;
+        if (isAbsolute(path)) {
+            if (denotesRoot(path)) {
+                return 0;
+            }
+            i++;
+        }
+        while (true) {
+            i = path.indexOf('/', i) + 1;
+            if (i == 0) {
+                return count;
+            }
+            count++;
+        }
+    }
+
+    /**
+     * Split a path into elements. The root path ("/") and the empty path ("")
+     * is zero elements.
+     *
+     * @param path the path
+     * @return an Iterable for the path elements
+     */
+    public static Iterable<String> elements(final String path) {
+        final Iterator<String> it = new Iterator<String>() {
+            int pos = isAbsolute(path) ? 1 : 0;
+            String next;
+
+            @Override
+            public boolean hasNext() {
+                if (next == null) {
+                    if (pos >= path.length()) {
+                        return false;
+                    }
+                    else {
+                        int i = path.indexOf('/', pos);
+                        if (i < 0) {
+                            next = path.substring(pos);
+                            pos = path.length();
+                        }
+                        else {
+                            next = path.substring(pos, i);
+                            pos = i + 1;
+                        }
+                        return true;
+                    }
+                }
+                else {
+                    return true;
+                }
+            }
+
+            @Override
+            public String next() {
+                if (hasNext()) {
+                    String next = this.next;
+                    this.next = null;
+                    return next;
+                }
+                else {
+                    throw new NoSuchElementException();
+                }
+            }
+
+            @Override
+            public void remove() {
+                throw new UnsupportedOperationException("remove");
+            }
+        };
+
+        return new Iterable<String>() {
+            @Override
+            public Iterator<String> iterator() {
+                return it;
+            }
+        };
+    }
+
+
+}

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/PropertyImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/PropertyImpl.java?rev=1324820&r1=1324819&r2=1324820&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/PropertyImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/PropertyImpl.java
Wed Apr 11 15:31:09 2012
@@ -16,7 +16,6 @@
  */
 package org.apache.jackrabbit.oak.jcr;
 
-import org.apache.jackrabbit.mk.util.PathUtils;
 import org.apache.jackrabbit.oak.api.NodeStateEditor;
 import org.apache.jackrabbit.oak.api.PropertyState;
 import org.apache.jackrabbit.oak.api.TransientNodeState;
@@ -113,7 +112,7 @@ public class PropertyImpl extends ItemIm
      */
     @Override
     public int getDepth() throws RepositoryException {
-        return PathUtils.getDepth(getPath());
+        return Paths.getDepth(getPath());
     }
 
     /**
@@ -632,7 +631,7 @@ public class PropertyImpl extends ItemIm
     private void resolve() {
         parentState = getItemStateProvider().getNodeState(parentState.getPath());
         propertyState = getItemStateProvider().getPropertyState(
-                PathUtils.concat(parentState.getPath(), propertyState.getName()));
+                Paths.concat(parentState.getPath(), propertyState.getName()));
     }
 
 }
\ No newline at end of file

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionImpl.java?rev=1324820&r1=1324819&r2=1324820&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/SessionImpl.java
Wed Apr 11 15:31:09 2012
@@ -17,7 +17,6 @@
 package org.apache.jackrabbit.oak.jcr;
 
 import org.apache.jackrabbit.commons.AbstractSession;
-import org.apache.jackrabbit.mk.util.PathUtils;
 import org.apache.jackrabbit.oak.api.CommitFailedException;
 import org.apache.jackrabbit.oak.api.Connection;
 import org.apache.jackrabbit.oak.api.NodeState;
@@ -149,8 +148,8 @@ public class SessionImpl extends Abstrac
     public void move(String srcAbsPath, String destAbsPath) throws RepositoryException {
         checkIsAlive();
 
-        String srcPath = PathUtils.relativize("/", srcAbsPath);
-        String destPath = PathUtils.relativize("/", destAbsPath);
+        String srcPath = Paths.relativize("/", srcAbsPath);
+        String destPath = Paths.relativize("/", destAbsPath);
         editor.move(srcPath, destPath);
     }
 



Mime
View raw message