[exo-jcr-commits] exo-jcr SVN: r3262 - in jcr/branches/1.12.x/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr: impl/core and 1 other directory.

do-not-reply at jboss.org do-not-reply at jboss.org
Fri Oct 8 08:04:25 EDT 2010


Author: sergiykarpenko
Date: 2010-10-08 08:04:24 -0400 (Fri, 08 Oct 2010)
New Revision: 3262

Added:
   jcr/branches/1.12.x/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/JCRPathExt.java
Modified:
   jcr/branches/1.12.x/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/datamodel/InternalQName.java
   jcr/branches/1.12.x/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/datamodel/QPathEntry.java
   jcr/branches/1.12.x/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/JCRName.java
   jcr/branches/1.12.x/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/JCRPath.java
   jcr/branches/1.12.x/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/LocationFactory.java
Log:
JCR-1473: merge performance improvements

Modified: jcr/branches/1.12.x/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/datamodel/InternalQName.java
===================================================================
--- jcr/branches/1.12.x/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/datamodel/InternalQName.java	2010-10-08 11:45:06 UTC (rev 3261)
+++ jcr/branches/1.12.x/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/datamodel/InternalQName.java	2010-10-08 12:04:24 UTC (rev 3262)
@@ -32,7 +32,7 @@
 
    /**
     * InternalQName constructor.
-    * 
+    *
     * @param namespace
     *          - namespace URI
     * @param name
@@ -79,9 +79,18 @@
       if (o == null)
          return false;
 
-      if (!(o instanceof InternalQName))
-         return false;
+      if (o instanceof InternalQName)
+      {
+         InternalQName that = (InternalQName)o;
+         if (hashCode == that.hashCode)
+         {
+            String s1 = getAsString();
+            String s2 = that.getAsString();
+            return s1.equals(s2);
+         }
+      }
 
-      return hashCode == o.hashCode() && getAsString().equals(((QName)o).getAsString());
+      return false;
+
    }
 }

Modified: jcr/branches/1.12.x/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/datamodel/QPathEntry.java
===================================================================
--- jcr/branches/1.12.x/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/datamodel/QPathEntry.java	2010-10-08 11:45:06 UTC (rev 3261)
+++ jcr/branches/1.12.x/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/datamodel/QPathEntry.java	2010-10-08 12:04:24 UTC (rev 3262)
@@ -32,6 +32,10 @@
     */
    private final int index;
 
+   private String cachedToString;
+
+   private String cachedToStringShowIndex;
+
    /**
     * QPathEntry constructor.
     * 
@@ -128,7 +132,44 @@
     */
    public String getAsString(boolean showIndex)
    {
-      return super.getAsString() + (showIndex ? QPath.PREFIX_DELIMITER + this.index : "");
+      if (showIndex)
+      {
+         if (cachedToStringShowIndex != null)
+         {
+            return cachedToStringShowIndex;
+         }
+      }
+      else
+      {
+         if (cachedToString != null)
+         {
+            return cachedToString;
+         }
+      }
+
+      //
+      String res;
+      if (showIndex)
+      {
+         res = super.getAsString() + QPath.PREFIX_DELIMITER + this.index;
+      }
+      else
+      {
+         res = super.getAsString();
+      }
+
+      //
+      if (showIndex)
+      {
+         cachedToStringShowIndex = res;
+      }
+      else
+      {
+         cachedToString = res;
+      }
+
+      //
+      return res;
    }
 
    /**

Modified: jcr/branches/1.12.x/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/JCRName.java
===================================================================
--- jcr/branches/1.12.x/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/JCRName.java	2010-10-08 11:45:06 UTC (rev 3261)
+++ jcr/branches/1.12.x/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/JCRName.java	2010-10-08 12:04:24 UTC (rev 3262)
@@ -40,17 +40,43 @@
 
    protected final int hashCode;
 
+   protected JCRName(InternalQName qname, String prefix)
+   {
+      this(qname.getNamespace(), qname.getName(), prefix);
+   }
+
+   JCRName(JCRPath.PathElement that)
+   {
+      this.prefix = that.prefix;
+      this.name = that.name;
+      this.namespace = that.namespace;
+      this.stringName = that.stringName;
+      this.hashCode = that.hashCode;
+   }
+
    JCRName(String namespace, String name, String prefix)
    {
-      this.name = name.intern();
-      this.namespace = namespace.intern();
-      this.prefix = prefix.intern();
+      int hk = 31 + namespace.hashCode();
+      hk = hk * 31 + name.hashCode();
+      int hashCode = hk * 31 + prefix.hashCode();
 
-      this.stringName = ((this.prefix.length() == 0 ? "" : this.prefix + ":") + this.name);
+      //
+      String stringName;
+      if (prefix.length() == 0)
+      {
+         stringName = name;
+      }
+      else
+      {
+         stringName = prefix + ":" + name;
+      }
 
-      int hk = 31 + this.namespace.hashCode();
-      hk = hk * 31 + this.name.hashCode();
-      this.hashCode = hk * 31 + this.prefix.hashCode();
+      //
+      this.name = name;
+      this.namespace = namespace;
+      this.prefix = prefix;
+      this.stringName = stringName;
+      this.hashCode = hashCode;
    }
 
    /**

Modified: jcr/branches/1.12.x/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/JCRPath.java
===================================================================
--- jcr/branches/1.12.x/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/JCRPath.java	2010-10-08 11:45:06 UTC (rev 3261)
+++ jcr/branches/1.12.x/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/JCRPath.java	2010-10-08 12:04:24 UTC (rev 3262)
@@ -18,16 +18,15 @@
  */
 package org.exoplatform.services.jcr.impl.core;
 
+import org.exoplatform.services.jcr.core.NamespaceAccessor;
+import org.exoplatform.services.jcr.datamodel.InternalQName;
 import org.exoplatform.services.jcr.datamodel.QPath;
 import org.exoplatform.services.jcr.datamodel.QPathEntry;
-import org.exoplatform.services.jcr.impl.Constants;
 import org.exoplatform.services.log.ExoLogger;
 import org.exoplatform.services.log.Log;
 
-import java.util.ArrayList;
+import javax.jcr.RepositoryException;
 
-import javax.jcr.PathNotFoundException;
-
 /**
  * Created by The eXo Platform SAS.
  * 
@@ -35,7 +34,7 @@
  * @version $Id: JCRPath.java 11907 2008-03-13 15:36:21Z ksm $
  */
 
-public class JCRPath
+public abstract class JCRPath
 {
 
    public final static String ROOT_PATH = "/";
@@ -48,250 +47,117 @@
 
    protected static Log log = ExoLogger.getLogger("exo.jcr.component.core.JCRPath");
 
-   protected PathElement[] names;
-
-   JCRPath()
+   public static JCRPath createJCRPath()
    {
-      this.names = new PathElement[0];
+      return JCRPathExt.ROOT;
    }
 
-   public boolean isAbsolute()
+   public static JCRPath createJCRPath(NamespaceAccessor namespaces, QPath qpath) throws RepositoryException
    {
-      if (names.length > 0)
-      {
-         PathElement first = names[0];
-         if (first.getName().equals(ROOT_NAME))
-         {
-            return true;
-         }
-      }
-      return false;
+      return new JCRPathExt(namespaces, qpath.getEntries());
    }
 
-   JCRPath addEntry(String namespace, String name, String prefix, int index)
+   public static JCRPath createJCRPath(NamespaceAccessor namespaces, QPathEntry[] relPath) throws RepositoryException
    {
-      if (name.equals(THIS_RELPATH))
-         return this;
-
-      if (name.equals(PARENT_RELPATH))
-      {
-         return addEntry(new MoveUpElement());
-      }
-
-      return addEntry(new PathElement(namespace, name, prefix, index));
+      return new JCRPathExt(namespaces, relPath);
    }
 
-   JCRPath addEntry(PathElement entry)
-   {
-      if (names.length > 0 && entry instanceof MoveUpElement && !(names[names.length - 1] instanceof MoveUpElement))
-      {
-         return removeLastEntry();
-      }
+   public abstract boolean isAbsolute();
 
-      PathElement[] newNames = new PathElement[names.length + 1];
-      for (int i = 0; i < names.length; i++)
-         newNames[i] = names[i];
-      newNames[names.length] = entry;
-      names = newNames;
-      return this;
-   }
+   abstract JCRPath addEntry(String namespace, String name, String prefix, int index);
 
-   JCRPath removeLastEntry()
-   {
+   abstract JCRPath addEntry(PathElement entry);
 
-      if (names.length <= 0)
-      {
-         log.warn("Wrong relative path. Can't move up in path hierarhy. " + getAsString(true));
-         return this;
-      }
+   abstract JCRPath add(JCRPath path);
 
-      PathElement[] newNames = new PathElement[names.length - 1];
-      for (int i = 0; i < newNames.length; i++)
-         newNames[i] = names[i];
-      names = newNames;
-      return this;
-   }
+   abstract JCRPath addEntries(PathElement... entries);
 
-   public JCRPath makeParentPath()
-   {
-      return makeAncestorPath(1);
-   }
+   public abstract JCRPath makeParentPath();
 
-   /**
-    * Makes ancestor path by relative degree (For ex relativeDegree == 1 means parent path etc)
-    * 
-    * @param relativeDegree
-    * @return
-    * @throws PathNotFoundException
-    */
-   public JCRPath makeAncestorPath(int relativeDegree)
-   {
+   public abstract JCRPath makeAncestorPath(int relativeDegree);
 
-      JCRPath path = new JCRPath();
-      for (int i = 0; i < names.length - relativeDegree; i++)
-         path.addEntry(names[i]);
-      return path;
-   }
+   public abstract PathElement[] getRelPath(int relativeDegree);
 
-   public PathElement[] getRelPath(int relativeDegree)
-   {
-      ArrayList<PathElement> entries = new ArrayList<PathElement>();
-      for (int i = names.length - relativeDegree; i < names.length; i++)
-         entries.add(names[i]);
-      PathElement[] relPath = new PathElement[entries.size()];
-      for (int i = 0; i < relPath.length; i++)
-         relPath[i] = entries.get(i);
-      return relPath;
-   }
+   public abstract QPath getInternalPath();
 
-   public QPath getInternalPath()
-   {
+   public abstract String getAsString(boolean showIndex);
 
-      QPathEntry[] entries = new QPathEntry[names.length];
+   public abstract int getDepth();
 
-      for (int i = 0; i < names.length; i++)
-         entries[i] = new QPathEntry(names[i].getNamespace(), names[i].getName(), names[i].getIndex());
+   public abstract int getLength();
 
-      QPath qpath = new QPath(entries);
-      return qpath;
-   }
+   public abstract PathElement getEntry(int index);
 
-   public String getAsString(boolean showIndex)
-   {
+   public abstract PathElement[] getEntries();
 
-      // [PN] 27.06.07
-      String path = "";
-      if (isAbsolute())
-      {
-         if (size() == 1)
-            return "/";
+   public abstract boolean isDescendantOf(JCRPath ancestorLocation, boolean childOnly);
 
-         for (int i = 1; i < names.length; i++)
-         {
-            path += "/" + names[i].getAsString(showIndex);
-         }
-      }
-      else
-      {
-         // relative
-         for (int i = 0; i < names.length; i++)
-         {
-            path += i > 0 ? "/" + names[i].getAsString(showIndex) : names[i].getAsString(showIndex);
-         }
-      }
+   public abstract boolean isAncestorOf(JCRPath descendantLocation, boolean childOnly);
 
-      return path;
-   }
+   public abstract PathElement getName();
 
-   public int getDepth()
-   {
-      return size() - 1;
-   }
+   public abstract int getIndex();
 
-   public boolean isDescendantOf(JCRPath ancestorLocation, boolean childOnly)
-   {
-      int depthDiff = getDepth() - ancestorLocation.getDepth();
-      if (depthDiff <= 0 || (childOnly && depthDiff != 1))
-         return false;
+   public abstract boolean isIndexSetExplicitly();
 
-      JCRPath.PathElement[] anotherNames = ancestorLocation.getEntries();
-      for (int i = 0; i < anotherNames.length; i++)
-      {
-         boolean result = anotherNames[i].equals(names[i]);
-         if (!result)
-            return false;
-      }
-      return true;
-   }
+   public abstract boolean isSameNameSibling(JCRPath anotherPath);
 
-   public boolean isAncestorOf(JCRPath descendantLocation, boolean childOnly)
-   {
-      return descendantLocation.isDescendantOf(this, childOnly);
-   }
+   public abstract boolean equals(Object obj);
 
-   private int size()
+   protected JCRPath()
    {
-      return names.length;
    }
 
-   public JCRName getName()
+   public static class PathElement extends JCRName
    {
-      if (size() > 0)
-         return names[size() - 1];
 
-      return new ThisElement();
-   }
+      private final int index;
 
-   public int getIndex()
-   {
-      return names[size() - 1].getIndex();
-   }
+      private final boolean indexSetExplicitly;
 
-   public boolean isIndexSetExplicitly()
-   {
-      return names[size() - 1].isIndexSetExplicitly();
-   }
+      private String cachedToString;
 
-   public boolean isSameNameSibling(JCRPath anotherPath)
-   {
-      JCRName[] anotherNames = anotherPath.getEntries();
-      for (int i = 0; i < anotherNames.length - 1; i++)
-      {
-         boolean result = anotherNames[i].equals(names[i]);
-         if (!result)
-            return false;
-      }
-      return getName().getName().equals(anotherPath.getName().getName())
-         && this.getName().getPrefix().equals(anotherPath.getName().getPrefix());
-   }
+      private String cachedToStringShowIndex;
 
-   public boolean equals(Object obj)
-   {
-      if (this == obj)
+      public PathElement(String namespace, String name, String prefix, int index)
       {
-         return true;
-      }
-      if (obj instanceof JCRPath)
-      {
-         JCRPath other = (JCRPath)obj;
-         return this.getInternalPath().equals(other.getInternalPath());
-      }
-      return false;
-   }
+         super(namespace, name, prefix);
 
-   PathElement[] getEntries()
-   {
-      return names;
-   }
-
-   public class ThisElement extends PathElement
-   {
-
-      ThisElement()
-      {
-         super(Constants.NS_DEFAULT_URI, THIS_RELPATH, Constants.NS_EMPTY_PREFIX, -1);
+         //
+         if (index == -1)
+         {
+            this.index = 1;
+            this.indexSetExplicitly = false;
+         }
+         else
+         {
+            this.index = index;
+            this.indexSetExplicitly = true;
+         }
       }
-   }
 
-   public class MoveUpElement extends PathElement
-   {
-
-      MoveUpElement()
+      public PathElement(InternalQName qname, String prefix, int index)
       {
-         super(Constants.NS_DEFAULT_URI, PARENT_RELPATH, Constants.NS_EMPTY_PREFIX, -1);
+         super(qname, prefix);
+
+         //
+         if (index == -1)
+         {
+            this.index = 1;
+            this.indexSetExplicitly = false;
+         }
+         else
+         {
+            this.index = index;
+            this.indexSetExplicitly = true;
+         }
       }
-   }
 
-   public class PathElement extends JCRName
-   {
-
-      private final int index;
-
-      private final boolean indexSetExplicitly;
-
-      public PathElement(String namespace, String name, String prefix, int index)
+      public PathElement(PathElement that, int index)
       {
-         super(namespace, name, prefix);
+         super(that);
+
+         //
          if (index == -1)
          {
             this.index = 1;
@@ -306,7 +172,7 @@
 
       public PathElement clone(int newIndex)
       {
-         return new PathElement(this.namespace, this.name, this.prefix, newIndex);
+         return new PathElement(this, newIndex);
       }
 
       public int getIndex()
@@ -324,12 +190,38 @@
 
       public String getAsString(boolean showIndex)
       {
-         String indexStr;
+         if (showIndex)
+         {
+            if (cachedToStringShowIndex != null)
+            {
+               return cachedToStringShowIndex;
+            }
+         }
+         else
+         {
+            if (cachedToString != null)
+            {
+               return cachedToString;
+            }
+         }
+
+         StringBuffer sb = new StringBuffer(super.getAsString());
          if (showIndex || getIndex() > 1)
-            indexStr = "[" + getIndex() + "]";
+            sb.append("[").append(index).append("]");
+         String res = sb.toString();
+
+         //
+         if (showIndex)
+         {
+            cachedToStringShowIndex = res;
+         }
          else
-            indexStr = "";
-         return (super.getAsString() + indexStr);
+         {
+            cachedToString = res;
+         }
+
+         //
+         return res;
       }
 
       public boolean isIndexSetExplicitly()
@@ -337,5 +229,4 @@
          return indexSetExplicitly;
       }
    }
-
 }

Added: jcr/branches/1.12.x/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/JCRPathExt.java
===================================================================
--- jcr/branches/1.12.x/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/JCRPathExt.java	                        (rev 0)
+++ jcr/branches/1.12.x/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/JCRPathExt.java	2010-10-08 12:04:24 UTC (rev 3262)
@@ -0,0 +1,394 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.exoplatform.services.jcr.impl.core;
+
+import org.exoplatform.services.jcr.core.NamespaceAccessor;
+import org.exoplatform.services.jcr.datamodel.InternalQName;
+import org.exoplatform.services.jcr.datamodel.QPath;
+import org.exoplatform.services.jcr.datamodel.QPathEntry;
+import org.exoplatform.services.jcr.impl.Constants;
+
+import javax.jcr.RepositoryException;
+
+/**
+ * Created by The eXo Platform SAS.
+ *
+ * @author <a href="mailto:geaz at users.sourceforge.net">Gennady Azarenkov </a>
+ * @version $Id: JCRPath.java 11907 2008-03-13 15:36:21Z ksm $
+ */
+
+public class JCRPathExt extends JCRPath
+{
+
+   private static PathElement[] EMPTY_PATH = new PathElement[0];
+
+   private static final PathElement THIS_ELEMENT = new PathElement(Constants.NS_DEFAULT_URI, THIS_RELPATH, Constants.NS_EMPTY_PREFIX, -1);
+
+   private static final PathElement MOVE_UP_ELEMENT = new PathElement(Constants.NS_DEFAULT_URI, PARENT_RELPATH, Constants.NS_EMPTY_PREFIX, -1);
+
+   protected final PathElement[] names;
+
+   protected final int size;
+
+   protected String cachedToString;
+
+   protected String cachedToStringShowIndex;
+
+   protected QPath cachedInternalQPath;
+
+   public final static JCRPathExt ROOT = new JCRPathExt();
+
+   private JCRPathExt()
+   {
+      this(EMPTY_PATH);
+   }
+
+   JCRPathExt(NamespaceAccessor namespaces, QPathEntry[] path) throws RepositoryException
+   {
+      PathElement[] names = new PathElement[path.length];
+      int size = 0;
+      for (QPathEntry entry : path)
+      {
+         String prefix = namespaces.getNamespacePrefixByURI(entry.getNamespace());
+         PathElement element = element(entry.getNamespace(), entry.getName(), prefix, entry.getIndex());
+         size = addEntry(names, size, element);
+      }
+
+      //
+      this.names = names;
+      this.size = size;
+   }
+
+   JCRPathExt(JCRPathExt that, PathElement[] addedEntries, int addedSize)
+   {
+      PathElement[] names = new PathElement[that.size + addedSize];
+      int size = 0;
+      for (int i = 0;i < that.size;i++)
+      {
+         size = addEntry(names, size, that.names[i]);
+      }
+      for (int i = 0;i < addedSize;i++)
+      {
+         size = addEntry(names, size, addedEntries[i]);
+      }
+
+      //
+      this.names = names;
+      this.size = size;
+   }
+
+   JCRPathExt(PathElement[] names)
+   {
+      this(names, names.length);
+   }
+
+   JCRPathExt(PathElement[] names, int size)
+   {
+      if (size < 0)
+      {
+         throw new AssertionError("Size value is negative: " + size);
+      }
+      if (size > names.length)
+      {
+         throw new AssertionError("Size value is too large: " + size + " instead of max: " + names.length);
+      }
+
+      //
+      this.names = names;
+      this.size = size;
+   }
+
+   public boolean isAbsolute()
+   {
+      if (size > 0)
+      {
+         PathElement first = names[0];
+         if (first.getName().equals(ROOT_NAME))
+         {
+            return true;
+         }
+      }
+      return false;
+   }
+
+   @Override
+   JCRPath add(JCRPath path)
+   {
+      return new JCRPathExt(this, ((JCRPathExt)path).names, ((JCRPathExt)path).size);
+   }
+
+   @Override
+   JCRPath addEntries(PathElement... entries)
+   {
+      return new JCRPathExt(this, entries, entries.length);
+   }
+
+   JCRPathExt addEntry(String namespace, String name, String prefix, int index)
+   {
+      return addEntry(element(namespace, name, prefix, index));
+   }
+
+   public JCRPathExt makeAncestorPath(int relativeDegree)
+   {
+      return new JCRPathExt(names, size - relativeDegree);
+   }
+
+   JCRPathExt addEntry(PathElement entry)
+   {
+      return new JCRPathExt(this, new PathElement[]{entry}, 1);
+   }
+
+   private static PathElement element(String namespace, String name, String prefix, int index)
+   {
+      if (name.equals(THIS_RELPATH))
+      {
+         return THIS_ELEMENT;
+      }
+      else if (name.equals(PARENT_RELPATH))
+      {
+         return MOVE_UP_ELEMENT;
+      }
+      else
+      {
+         return new PathElement(namespace, name, prefix, index);
+      }
+   }
+
+   private static int addEntry(PathElement[] entries, int size, PathElement entry)
+   {
+      if (entry == THIS_ELEMENT)
+      {
+         return size;
+      }
+      if (size > 0 && entry == MOVE_UP_ELEMENT && !(entries[size - 1] == MOVE_UP_ELEMENT))
+      {
+         if (size <= 0)
+         {
+            log.warn("Wrong relative path. Can't move up in path hierarhy.");
+            return 0;
+         }
+         return size - 1;
+      }
+
+      //
+      entries[size] = entry;
+
+      //
+      return size + 1;
+   }
+
+   public JCRPathExt makeParentPath()
+   {
+      return makeAncestorPath(1);
+   }
+
+   @Override
+   public int getLength()
+   {
+      return size;
+   }
+
+   @Override
+   public PathElement getEntry(int index)
+   {
+      if (index > size)
+      {
+         throw new ArrayIndexOutOfBoundsException();
+      }
+      return names[index];
+   }
+
+   public PathElement[] getRelPath(int relativeDegree)
+   {
+      PathElement[] relPath = new PathElement[relativeDegree];
+      System.arraycopy(names, size - relativeDegree, relPath, 0, relativeDegree);
+      return relPath;
+   }
+
+   public QPath getInternalPath()
+   {
+      if (cachedInternalQPath == null)
+      {
+         QPathEntry[] entries = new QPathEntry[size];
+
+         for (int i = 0; i < size; i++)
+            entries[i] = new QPathEntry(names[i].getNamespace(), names[i].getName(), names[i].getIndex());
+
+         cachedInternalQPath = new QPath(entries);
+      }
+
+      return cachedInternalQPath;
+   }
+
+   public String getAsString(boolean showIndex)
+   {
+      if (showIndex)
+      {
+         if (cachedToStringShowIndex != null)
+         {
+            return cachedToStringShowIndex;
+         }
+      }
+      else
+      {
+         if (cachedToString != null)
+         {
+            return cachedToString;
+         }
+      }
+
+      // [PN] 27.06.07
+      String path;
+      if (isAbsolute())
+      {
+         if (size == 1)
+         {
+            path = "/";
+         }
+         else
+         {
+            StringBuilder builder = new StringBuilder();
+            for (int i = 1; i < size; i++)
+            {
+               builder.append("/").append(names[i].getAsString(showIndex));
+            }
+            path = builder.toString();
+         }
+      }
+      else
+      {
+         // relative
+         StringBuilder builder = new StringBuilder();
+         for (int i = 0; i < size; i++)
+         {
+            if (i > 0)
+            {
+               builder.append("/");
+            }
+            else
+            {
+
+            }
+            builder.append(names[i].getAsString(showIndex));
+         }
+         path = builder.toString();
+      }
+
+
+      //
+      if (showIndex)
+      {
+         cachedToStringShowIndex = path;
+      }
+      else
+      {
+         cachedToString = path;
+      }
+
+      //
+      return path;
+   }
+
+   public int getDepth()
+   {
+      return size - 1;
+   }
+
+   public boolean isDescendantOf(JCRPath ancestorLocation, boolean childOnly)
+   {
+      return isDescendantOf((JCRPathExt)ancestorLocation, childOnly);
+   }
+
+   public boolean isDescendantOf(JCRPathExt ancestorLocation, boolean childOnly)
+   {
+      int depthDiff = getDepth() - ancestorLocation.getDepth();
+      if (depthDiff <= 0 || (childOnly && depthDiff != 1))
+         return false;
+
+      JCRPathExt.PathElement[] anotherNames = ancestorLocation.names;
+      for (int i = 0; i < ancestorLocation.size; i++)
+      {
+         boolean result = anotherNames[i].equals(names[i]);
+         if (!result)
+            return false;
+      }
+      return true;
+   }
+
+   public boolean isAncestorOf(JCRPath descendantLocation, boolean childOnly)
+   {
+      return descendantLocation.isDescendantOf(this, childOnly);
+   }
+
+   public PathElement getName()
+   {
+      if (size > 0)
+         return names[size - 1];
+
+      return THIS_ELEMENT;
+   }
+
+   public int getIndex()
+   {
+      return names[size - 1].getIndex();
+   }
+
+   public boolean isIndexSetExplicitly()
+   {
+      return names[size - 1].isIndexSetExplicitly();
+   }
+
+   public boolean isSameNameSibling(JCRPath anotherPath)
+   {
+      return isSameNameSibling((JCRPathExt)anotherPath);
+   }
+
+   public boolean isSameNameSibling(JCRPathExt anotherPath)
+   {
+      JCRName[] anotherNames = anotherPath.names;
+      for (int i = 0; i < anotherPath.size - 1; i++)
+      {
+         boolean result = anotherNames[i].equals(names[i]);
+         if (!result)
+            return false;
+      }
+      return getName().getName().equals(anotherPath.getName().getName())
+         && this.getName().getPrefix().equals(anotherPath.getName().getPrefix());
+   }
+
+   public boolean equals(Object obj)
+   {
+      if (this == obj)
+      {
+         return true;
+      }
+      if (obj instanceof JCRPathExt)
+      {
+         JCRPathExt other = (JCRPathExt)obj;
+         return this.getInternalPath().equals(other.getInternalPath());
+      }
+      return false;
+   }
+
+   public PathElement[] getEntries()
+   {
+      return names.clone();
+   }
+
+}

Modified: jcr/branches/1.12.x/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/LocationFactory.java
===================================================================
--- jcr/branches/1.12.x/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/LocationFactory.java	2010-10-08 11:45:06 UTC (rev 3261)
+++ jcr/branches/1.12.x/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core/LocationFactory.java	2010-10-08 12:04:24 UTC (rev 3262)
@@ -65,19 +65,8 @@
     */
    public JCRPath createJCRPath(JCRPath parentLoc, String relPath) throws RepositoryException
    {
-
-      JCRPath path = new JCRPath();
-      for (int i = 0; i < parentLoc.getEntries().length; i++)
-      {
-         path.addEntry(parentLoc.getEntries()[i]);
-      }
-
       JCRPath addPath = parseNames(relPath, false);
-      for (int i = 0; i < addPath.getEntries().length; i++)
-      {
-         path.addEntry(addPath.getEntries()[i]);
-      }
-      return path;
+      return parentLoc.add(addPath);
    }
 
    /**
@@ -125,30 +114,21 @@
     */
    public JCRPath createJCRPath(QPath qPath) throws RepositoryException
    {
-
-      JCRPath path = new JCRPath();
-      for (int i = 0; i < qPath.getEntries().length; i++)
-      {
-         QPathEntry entry = qPath.getEntries()[i];
-         String prefix = namespaces.getNamespacePrefixByURI(entry.getNamespace());
-         path.addEntry(entry.getNamespace(), entry.getName(), prefix, entry.getIndex());
-      }
-
-      return path;
+      return JCRPath.createJCRPath(namespaces, qPath);
    }
 
    public JCRName createJCRName(InternalQName qname) throws RepositoryException
    {
       String prefix = namespaces.getNamespacePrefixByURI(qname.getNamespace());
-      return new JCRName(qname.getNamespace(), qname.getName(), prefix);
+      return new JCRName(qname, prefix);
    }
 
    public String formatPathElement(QPathEntry qe) throws RepositoryException
    {
       String prefix = namespaces.getNamespacePrefixByURI(qe.getNamespace());
-      JCRPath p = new JCRPath();
-      p.addEntry(qe.getNamespace(), qe.getName(), prefix, qe.getIndex());
-      return p.getEntries()[0].getAsString(false);
+      JCRPath p = JCRPath.createJCRPath();
+      p = p.addEntry(qe.getNamespace(), qe.getName(), prefix, qe.getIndex());
+      return p.getEntry(0).getAsString(false);
    }
 
    /**
@@ -160,25 +140,17 @@
     */
    public JCRName parseJCRName(String name) throws RepositoryException
    {
-      JCRPath.PathElement entry = parsePathEntry(new JCRPath(), name);
-
-      return new JCRName(entry.getNamespace(), entry.getName(), entry.getPrefix());
+      JCRPath path = parsePathEntry(JCRPath.createJCRPath(), name);
+      JCRPath.PathElement entry = path.getName();
+      return new JCRName(entry);
    }
 
    public JCRPath.PathElement[] createRelPath(QPathEntry[] relPath) throws RepositoryException
    {
-      JCRPath path = new JCRPath();
-      // JCRPath.PathElement[] entries = new JCRPath.PathElement[relPath.length];
-      for (QPathEntry element : relPath)
-      {
-         String uri = namespaces.getNamespaceURIByPrefix(element.getNamespace());
-         String prefix = namespaces.getNamespacePrefixByURI(uri);
-         path.addEntry(uri, element.getName(), prefix, element.getIndex());
-      }
-      return path.getEntries();
+      return JCRPath.createJCRPath(namespaces, relPath).getEntries();
    }
 
-   private JCRPath.PathElement parsePathEntry(JCRPath path, String name) throws RepositoryException
+   private JCRPath parsePathEntry(JCRPath path, String name) throws RepositoryException
    {
 
       // should be reset here (if there is explicit index) or
@@ -232,8 +204,8 @@
             throw new RepositoryException("Illegal path entry: \"" + name + "\"");
          }
 
-         path.addEntry(namespaces.getNamespaceURIByPrefix(prefix), someName, prefix, index);
-         return (JCRPath.PathElement)path.getName();
+         path = path.addEntry(namespaces.getNamespaceURIByPrefix(prefix), someName, prefix, index);
+         return path;
 
       }
       catch (Exception e)
@@ -250,7 +222,7 @@
          throw new RepositoryException("Illegal relPath: \"" + path + "\"");
       }
 
-      JCRPath jcrPath = new JCRPath();
+      JCRPath jcrPath = JCRPath.createJCRPath();
       int start = 0;
       if (!absolute)
       {
@@ -262,7 +234,7 @@
          {
             throw new RepositoryException("Illegal relPath: \"" + path + "\"");
          }
-         jcrPath.addEntry(namespaces.getNamespaceURIByPrefix(""), "", "", -1);
+         jcrPath = jcrPath.addEntry(namespaces.getNamespaceURIByPrefix(""), "", "", -1);
       }
       else
       {
@@ -280,7 +252,7 @@
 
          if (start + 1 != path.length())
          {
-            parsePathEntry(jcrPath, qname);
+            jcrPath = parsePathEntry(jcrPath, qname);
          }
          else
          {



More information about the exo-jcr-commits mailing list