jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ste...@apache.org
Subject svn commit: r156060 - in incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core: ItemImpl.java NodeImpl.java Path.java xml/DocViewSAXEventGenerator.java xml/SessionImporter.java xml/SysViewSAXEventGenerator.java
Date Thu, 03 Mar 2005 18:05:39 GMT
Author: stefan
Date: Thu Mar  3 10:05:37 2005
New Revision: 156060

URL: http://svn.apache.org/viewcvs?view=rev&rev=156060
Log:
- added Path.computeRelativePath method
- removed NodeImpl.isRepositoryRoot and replaced calls with with n.getDepth()==0

Modified:
    incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/ItemImpl.java
    incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/NodeImpl.java
    incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/Path.java
    incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/xml/DocViewSAXEventGenerator.java
    incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/xml/SessionImporter.java
    incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/xml/SysViewSAXEventGenerator.java

Modified: incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/ItemImpl.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/ItemImpl.java?view=diff&r1=156059&r2=156060
==============================================================================
--- incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/ItemImpl.java (original)
+++ incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/ItemImpl.java Thu Mar 
3 10:05:37 2005
@@ -1053,8 +1053,8 @@
         // check if protected
         if (isNode()) {
             NodeImpl node = (NodeImpl) this;
-            // check if this is the repository root node
-            if (node.isRepositoryRoot()) {
+            // check if this is the root node
+            if (node.getDepth() == 0) {
                 String msg = safeGetJCRPath() + ": cannot remove root node";
                 log.debug(msg);
                 throw new RepositoryException(msg);
@@ -1272,8 +1272,8 @@
         }
 
         if (isNode()) {
-            // check if this is the repository root node
-            if (((NodeImpl) this).isRepositoryRoot()) {
+            // check if this is the root node
+            if (((NodeImpl) this).getDepth() == 0) {
                 // optimization
                 stateMgr.disposeAllTransientItemStates();
                 return;

Modified: incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/NodeImpl.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/NodeImpl.java?view=diff&r1=156059&r2=156060
==============================================================================
--- incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/NodeImpl.java (original)
+++ incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/NodeImpl.java Thu Mar 
3 10:05:37 2005
@@ -62,7 +62,6 @@
 import javax.jcr.PropertyType;
 import javax.jcr.ReferenceValue;
 import javax.jcr.RepositoryException;
-import javax.jcr.Session;
 import javax.jcr.UnsupportedRepositoryOperationException;
 import javax.jcr.Value;
 import javax.jcr.ValueFormatException;
@@ -875,16 +874,6 @@
     }
 
     /**
-     * Checks if this node is the root node.
-     * todo: is this the root node of this workspace?
-     *
-     * @return
-     */
-    public boolean isRepositoryRoot() {
-        return ((NodeState) state).getUUID().equals(rep.getRootNodeUUID());
-    }
-
-    /**
      * Same as {@link Node#addMixin(String)}, but takes a <code>QName</code>
      * instad of a <code>String</code>.
      *
@@ -2525,13 +2514,18 @@
 
         // search nearest ancestor that is referenceable
         NodeImpl m1 = this;
-        while (!m1.isRepositoryRoot() && !m1.isNodeType(MIX_REFERENCEABLE)) {
+        while (m1.getDepth() != 0 && !m1.isNodeType(MIX_REFERENCEABLE)) {
             m1 = (NodeImpl) m1.getParent();
         }
 
         // if root is common ancestor, corresponding path is same as ours
-        if (m1.isRepositoryRoot()) {
-            return getPath();
+        if (m1.getDepth() == 0) {
+            // check existence
+            if (!srcSession.getItemManager().itemExists(getPrimaryPath())) {
+                throw new ItemNotFoundException(safeGetJCRPath());
+            } else {
+                return getPath();
+            }
         }
 
         // get corresponding ancestor
@@ -2542,19 +2536,23 @@
             return m2.getPath();
         }
 
-        // calculate relative path from the referenceable ancestor to this node.
-        // please note, that this cannot be done
-        // iteratively in the 'while' loop above, since getName() does not
-        // return the relative path, but just the name (without path indices)
-        // n1.getPath() = /foo/bar/something[1]
-        // m1.getPath() = /foo
-        //      relpath = bar/something[1]
-
-        // @todo: replace as soon as implemented
-        // Path relPath = m1.getPrimaryPath().getRelativePath(getPrimaryPath());
+        String relPath;
+        try {
+            Path p = m1.getPrimaryPath().computeRelativePath(getPrimaryPath());
+            // use prefix mappings of srcSession
+            relPath = p.toJCRPath(srcSession.getNamespaceResolver());
+        } catch (BaseException be) {
+            // should never get here...
+            String msg = "internal error: failed to determine relative path";
+            log.error(msg, be);
+            throw new RepositoryException(msg, be);
+        }
 
-        String relPath = getPath().substring(m1.getPath().length() + 1);
-        return m2.getNode(relPath).getPath();
+        if (!m2.hasNode(relPath)) {
+            throw new ItemNotFoundException();
+        } else {
+            return m2.getNode(relPath).getPath();
+        }
     }
 
     /**
@@ -3083,42 +3081,42 @@
      * @param srcSession
      * @return the corresponding node or <code>null</code> if no corresponding
      *         node exists.
-     * @throws NoSuchWorkspaceException If <code>srcWorkspace</code> does not
exist.
-     * @throws AccessDeniedException    If the current session does not have sufficient rights
to perform the operation.
-     * @throws RepositoryException      If another error occurs.
+     * @throws RepositoryException If another error occurs.
      */
-    private NodeImpl getCorrespondingNode(Session srcSession)
+    private NodeImpl getCorrespondingNode(SessionImpl srcSession)
             throws AccessDeniedException, RepositoryException {
 
         // search nearest ancestor that is referenceable
         NodeImpl m1 = this;
-        while (!m1.isRepositoryRoot() && !m1.isNodeType(MIX_REFERENCEABLE)) {
+        while (m1.getDepth() != 0 && !m1.isNodeType(MIX_REFERENCEABLE)) {
             m1 = (NodeImpl) m1.getParent();
         }
 
         try {
-            // get corresponding ancestor
+            // get corresponding ancestor (might throw ItemNotFoundException)
             NodeImpl m2 = (NodeImpl) srcSession.getNodeByUUID(m1.getUUID());
 
-            // return path of m2, if m1 == n1
+            // return m2, if m1 == n1
             if (m1 == this) {
                 return m2;
             }
 
-            // calculate relative path from the referenceable ancestor to this node.
-            // please note, that this cannot be done
-            // iteratively in the 'while' loop above, since getName() does not
-            // return the relative path, but just the name (without path indices)
-            // n1.getPath() = /foo/bar/something[1]
-            // m1.getPath() = /foo
-            //      relpath = bar/something[1]
-
-            // @todo: replace as soon as implemented
-            // Path relPath = m1.getPrimaryPath().getRelativePath(getPrimaryPath());
-
-            String relPath = getPath().substring(m1.getPath().length() + 1);
-            return (NodeImpl) m2.getNode(relPath);
-
+            String relPath;
+            try {
+                Path p = m1.getPrimaryPath().computeRelativePath(getPrimaryPath());
+                // use prefix mappings of srcSession
+                relPath = p.toJCRPath(srcSession.getNamespaceResolver());
+            } catch (BaseException be) {
+                // should never get here...
+                String msg = "internal error: failed to determine relative path";
+                log.error(msg, be);
+                throw new RepositoryException(msg, be);
+            }
+            if (!m2.hasNode(relPath)) {
+                return null;
+            } else {
+                return (NodeImpl) m2.getNode(relPath);
+            }
         } catch (ItemNotFoundException e) {
             return null;
         }
@@ -3137,7 +3135,7 @@
      * @throws RepositoryException
      * @throws AccessDeniedException
      */
-    private NodeImpl doMergeTest(Session srcSession, boolean bestEffort)
+    private NodeImpl doMergeTest(SessionImpl srcSession, boolean bestEffort)
             throws RepositoryException, AccessDeniedException {
 
         // If N does not have a corresponding node then the merge result for N is leave.
@@ -3219,7 +3217,7 @@
          */
         NodeImpl node = this;
         while (!node.hasProperty(JCR_ISCHECKEDOUT)) {
-            if (node.isRepositoryRoot()) {
+            if (node.getDepth() == 0) {
                 return true;
             }
             node = (NodeImpl) node.getParent();

Modified: incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/Path.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/Path.java?view=diff&r1=156059&r2=156060
==============================================================================
--- incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/Path.java (original)
+++ incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/Path.java Thu Mar  3 10:05:37
2005
@@ -481,7 +481,7 @@
      * absolute path is normalized that would result in a 'negative' path
      * (eg: /a/../../) a MalformedPathException is thrown.
      *
-     * @return a normailzed path representation of this path
+     * @return a normalized path representation of this path
      * @throws MalformedPathException if the path cannot be normalized.
      * @see #isNormalized()
      */
@@ -530,6 +530,66 @@
             throw new MalformedPathException("only an absolute path can be canonicalized.");
         }
         return getNormalizedPath();
+    }
+
+    /**
+     * Computes the relative path from <code>this</code> absolute path to
+     * <code>other</code>.
+     *
+     * @param other an absolute path
+     * @return the relative path from <code>this</code> path to
+     *         <code>other</code> path
+     * @throws MalformedPathException if either <code>this</code> or
+     *                                <code>other</code> path is not absolute
+     */
+    public Path computeRelativePath(Path other) throws MalformedPathException {
+        if (other == null) {
+            throw new IllegalArgumentException("null argument");
+        }
+
+        // make sure both paths are absolute
+        if (!isAbsolute() || !other.isAbsolute()) {
+            throw new MalformedPathException("not an absolute path");
+        }
+
+        // make sure we're comparing canonical paths
+        Path p0 = getCanonicalPath();
+        Path p1 = other.getCanonicalPath();
+
+        if (p0.equals(p1)) {
+            // both paths are equal, the relative path is therefore '.'
+            PathBuilder pb = new PathBuilder();
+            pb.addLast(CURRENT_ELEMENT);
+            return pb.getPath();
+        }
+
+        // determine length of common path fragment
+        int lengthCommon = 0;
+        for (int i = 0; i < p0.elements.length && i < p1.elements.length; i++)
{
+            if (!p0.elements[i].equals(p1.elements[i])) {
+                break;
+            }
+            lengthCommon++;
+        }
+
+        PathBuilder pb = new PathBuilder();
+        if (lengthCommon < p0.elements.length) {
+            /**
+             * the common path fragment is an ancestor of this path;
+             * this has to be accounted for by prepending '..' elements
+             * to the relative path
+             */
+            int tmp = p0.elements.length - lengthCommon;
+            while (tmp-- > 0) {
+                pb.addFirst(PARENT_ELEMENT);
+            }
+        }
+        // add remainder of other path
+        for (int i = lengthCommon; i < p1.elements.length; i++) {
+            pb.addLast(p1.elements[i]);
+        }
+        // we're done
+        return pb.getPath();
     }
 
     /**

Modified: incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/xml/DocViewSAXEventGenerator.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/xml/DocViewSAXEventGenerator.java?view=diff&r1=156059&r2=156060
==============================================================================
--- incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/xml/DocViewSAXEventGenerator.java
(original)
+++ incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/xml/DocViewSAXEventGenerator.java
Thu Mar  3 10:05:37 2005
@@ -111,7 +111,7 @@
             // element name
             String elemName;
             try {
-                if (node.isRepositoryRoot()) {
+                if (node.getDepth() == 0) {
                     // root node needs a name
                     elemName = JCR_ROOT.toJCRName(session.getNamespaceResolver());
                 } else {
@@ -214,7 +214,7 @@
         // element name
         String elemName;
         try {
-            if (node.isRepositoryRoot()) {
+            if (node.getDepth() == 0) {
                 // root node needs a name
                 elemName = JCR_ROOT.toJCRName(session.getNamespaceResolver());
             } else {

Modified: incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/xml/SessionImporter.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/xml/SessionImporter.java?view=diff&r1=156059&r2=156060
==============================================================================
--- incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/xml/SessionImporter.java
(original)
+++ incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/xml/SessionImporter.java
Thu Mar  3 10:05:37 2005
@@ -124,7 +124,7 @@
                     nodeInfo.getNodeTypeName(), nodeInfo.getMixinNames(),
                     nodeInfo.getUUID());
         } else if (uuidBehavior == IMPORT_UUID_COLLISION_REPLACE_EXISTING) {
-            if (conflicting.isRepositoryRoot()) {
+            if (conflicting.getDepth() == 0) {
                 String msg = "root node cannot be replaced";
                 log.debug(msg);
                 throw new RepositoryException(msg);

Modified: incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/xml/SysViewSAXEventGenerator.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/xml/SysViewSAXEventGenerator.java?view=diff&r1=156059&r2=156060
==============================================================================
--- incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/xml/SysViewSAXEventGenerator.java
(original)
+++ incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/xml/SysViewSAXEventGenerator.java
Thu Mar  3 10:05:37 2005
@@ -79,7 +79,7 @@
         // name attribute
         String nodeName;
         try {
-            if (node.isRepositoryRoot()) {
+            if (node.getDepth() == 0) {
                 // root node needs a name
                 nodeName = JCR_ROOT.toJCRName(session.getNamespaceResolver());
             } else {



Mime
View raw message