From do-not-reply at jboss.org Fri Jul 23 05:26:38 2010 Content-Type: multipart/mixed; boundary="===============2408344440344265345==" MIME-Version: 1.0 From: do-not-reply at jboss.org To: exo-jcr-commits at lists.jboss.org Subject: [exo-jcr-commits] exo-jcr SVN: r2810 - jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/core. Date: Fri, 23 Jul 2010 05:26:38 -0400 Message-ID: <201007230926.o6N9QcSd029134@svn01.web.mwc.hst.phx2.redhat.com> --===============2408344440344265345== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Author: areshetnyak Date: 2010-07-23 05:26:38 -0400 (Fri, 23 Jul 2010) New Revision: 2810 Added: jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/= jcr/impl/core/JCRPathExt.java Log: EXOJCR-750 : Patch to JCR path management improvement was applied. Added: jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/servi= ces/jcr/impl/core/JCRPathExt.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services= /jcr/impl/core/JCRPathExt.java (rev 0) +++ jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services= /jcr/impl/core/JCRPathExt.java 2010-07-23 09:26:38 UTC (rev 2810) @@ -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 Gennady Azarenk= ov + * @version $Id$ + */ + +public class JCRPathExt extends JCRPath +{ + + private static PathElement[] EMPTY_PATH =3D new PathElement[0]; + + private static final PathElement THIS_ELEMENT =3D new PathElement(Const= ants.NS_DEFAULT_URI, THIS_RELPATH, Constants.NS_EMPTY_PREFIX, -1); + + private static final PathElement MOVE_UP_ELEMENT =3D new PathElement(Co= nstants.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 =3D new JCRPathExt(); + + private JCRPathExt() + { + this(EMPTY_PATH); + } + + JCRPathExt(NamespaceAccessor namespaces, QPathEntry[] path) throws Repo= sitoryException + { + PathElement[] names =3D new PathElement[path.length]; + int size =3D 0; + for (QPathEntry entry : path) + { + String prefix =3D namespaces.getNamespacePrefixByURI(entry.getNam= espace()); + PathElement element =3D element(entry.getNamespace(), entry.getNa= me(), prefix, entry.getIndex()); + size =3D addEntry(names, size, element); + } + + // + this.names =3D names; + this.size =3D size; + } + + JCRPathExt(JCRPathExt that, PathElement[] addedEntries, int addedSize) + { + PathElement[] names =3D new PathElement[that.size + addedSize]; + int size =3D 0; + for (int i =3D 0;i < that.size;i++) + { + size =3D addEntry(names, size, that.names[i]); + } + for (int i =3D 0;i < addedSize;i++) + { + size =3D addEntry(names, size, addedEntries[i]); + } + + // + this.names =3D names; + this.size =3D 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 + " i= nstead of max: " + names.length); + } + + // + this.names =3D names; + this.size =3D size; + } + + public boolean isAbsolute() + { + if (size > 0) + { + PathElement first =3D 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)p= ath).size); + } + + @Override + JCRPath addEntries(PathElement... entries) + { + return new JCRPathExt(this, entries, entries.length); + } + + JCRPathExt addEntry(String namespace, String name, String prefix, int i= ndex) + { + 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, Strin= g 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, PathElemen= t entry) + { + if (entry =3D=3D THIS_ELEMENT) + { + return size; + } + if (size > 0 && entry =3D=3D MOVE_UP_ELEMENT && !(entries[size - 1] = =3D=3D MOVE_UP_ELEMENT)) + { + if (size <=3D 0) + { + log.warn("Wrong relative path. Can't move up in path hierarhy.= "); + return 0; + } + return size - 1; + } + + // + entries[size] =3D 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 =3D new PathElement[relativeDegree]; + System.arraycopy(names, size - relativeDegree, relPath, 0, relativeD= egree); + return relPath; + } + + public QPath getInternalPath() + { + if (cachedInternalQPath =3D=3D null) + { + QPathEntry[] entries =3D new QPathEntry[size]; + + for (int i =3D 0; i < size; i++) + entries[i] =3D new QPathEntry(names[i].getNamespace(), names[i= ].getName(), names[i].getIndex()); + + cachedInternalQPath =3D new QPath(entries); + } + + return cachedInternalQPath; + } + + public String getAsString(boolean showIndex) + { + if (showIndex) + { + if (cachedToStringShowIndex !=3D null) + { + return cachedToStringShowIndex; + } + } + else + { + if (cachedToString !=3D null) + { + return cachedToString; + } + } + + // [PN] 27.06.07 + String path; + if (isAbsolute()) + { + if (size =3D=3D 1) + { + path =3D "/"; + } + else + { + StringBuilder builder =3D new StringBuilder(); + for (int i =3D 1; i < size; i++) + { + builder.append("/").append(names[i].getAsString(showIndex)); + } + path =3D builder.toString(); + } + } + else + { + // relative + StringBuilder builder =3D new StringBuilder(); + for (int i =3D 0; i < size; i++) + { + if (i > 0) + { + builder.append("/"); + } + else + { + + } + builder.append(names[i].getAsString(showIndex)); + } + path =3D builder.toString(); + } + + + // + if (showIndex) + { + cachedToStringShowIndex =3D path; + } + else + { + cachedToString =3D path; + } + + // + return path; + } + + public int getDepth() + { + return size - 1; + } + + public boolean isDescendantOf(JCRPath ancestorLocation, boolean childOn= ly) + { + return isDescendantOf((JCRPathExt)ancestorLocation, childOnly); + } + + public boolean isDescendantOf(JCRPathExt ancestorLocation, boolean chil= dOnly) + { + int depthDiff =3D getDepth() - ancestorLocation.getDepth(); + if (depthDiff <=3D 0 || (childOnly && depthDiff !=3D 1)) + return false; + + JCRPathExt.PathElement[] anotherNames =3D ancestorLocation.names; + for (int i =3D 0; i < ancestorLocation.size; i++) + { + boolean result =3D anotherNames[i].equals(names[i]); + if (!result) + return false; + } + return true; + } + + public boolean isAncestorOf(JCRPath descendantLocation, boolean childOn= ly) + { + 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 =3D anotherPath.names; + for (int i =3D 0; i < anotherPath.size - 1; i++) + { + boolean result =3D anotherNames[i].equals(names[i]); + if (!result) + return false; + } + return getName().getName().equals(anotherPath.getName().getName()) + && this.getName().getPrefix().equals(anotherPath.getName().getPre= fix()); + } + + public boolean equals(Object obj) + { + if (this =3D=3D obj) + { + return true; + } + if (obj instanceof JCRPathExt) + { + JCRPathExt other =3D (JCRPathExt)obj; + return this.getInternalPath().equals(other.getInternalPath()); + } + return false; + } + + public PathElement[] getEntries() + { + return names.clone(); + } + +} Property changes on: jcr/trunk/exo.jcr.component.core/src/main/java/org/exo= platform/services/jcr/impl/core/JCRPathExt.java ___________________________________________________________________ Name: svn:keywords + Id Name: svn:eol-style + native --===============2408344440344265345==--