Return-Path: Delivered-To: apmail-incubator-jackrabbit-commits-archive@www.apache.org Received: (qmail 65941 invoked from network); 8 May 2005 21:16:49 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 8 May 2005 21:16:49 -0000 Received: (qmail 21477 invoked by uid 500); 8 May 2005 21:19:50 -0000 Mailing-List: contact jackrabbit-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: jackrabbit-dev@incubator.apache.org Delivered-To: mailing list jackrabbit-commits@incubator.apache.org Received: (qmail 21464 invoked by uid 500); 8 May 2005 21:19:50 -0000 Delivered-To: apmail-incubator-jackrabbit-cvs@incubator.apache.org Received: (qmail 21457 invoked by uid 99); 8 May 2005 21:19:50 -0000 X-ASF-Spam-Status: No, hits=0.2 required=10.0 tests=NO_REAL_NAME X-Spam-Check-By: apache.org Received: from minotaur.apache.org (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.28) with SMTP; Sun, 08 May 2005 14:19:49 -0700 Received: (qmail 65938 invoked by uid 65534); 8 May 2005 21:16:46 -0000 Message-ID: <20050508211646.65937.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: svn commit: r169166 - in /incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/name: IndexedElement.java Name.java NamedElement.java ParentElement.java Path.java PathBuilder.java PathElement.java PathParser.java RootElement.java ThisElement.java Date: Sun, 08 May 2005 21:16:45 -0000 To: jackrabbit-cvs@incubator.apache.org From: jukka@apache.org X-Mailer: svnmailer-1.0.0-dev X-Virus-Checked: Checked X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Author: jukka Date: Sun May 8 14:16:44 2005 New Revision: 169166 URL: http://svn.apache.org/viewcvs?rev=3D169166&view=3Drev Log: JCR-EXT: Cleaned up, documented and extended the name and path implementati= on classes. Added: incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabb= it/name/PathBuilder.java incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabb= it/name/PathParser.java Modified: incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabb= it/name/IndexedElement.java incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabb= it/name/Name.java incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabb= it/name/NamedElement.java incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabb= it/name/ParentElement.java incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabb= it/name/Path.java incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabb= it/name/PathElement.java incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabb= it/name/RootElement.java incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabb= it/name/ThisElement.java Modified: incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/ja= ckrabbit/name/IndexedElement.java URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/jcr-e= xt/src/java/org/apache/jackrabbit/name/IndexedElement.java?rev=3D169166&r1= =3D169165&r2=3D169166&view=3Ddiff =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=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabb= it/name/IndexedElement.java (original) +++ incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabb= it/name/IndexedElement.java Sun May 8 14:16:44 2005 @@ -16,39 +16,70 @@ */ package org.apache.jackrabbit.name; =20 +import java.util.NoSuchElementException; + import javax.jcr.Item; -import javax.jcr.ItemNotFoundException; import javax.jcr.Node; import javax.jcr.NodeIterator; +import javax.jcr.PathNotFoundException; import javax.jcr.RepositoryException; =20 /** - * TODO + * Indexed path element. */ -class IndexedElement implements PathElement { +final class IndexedElement implements PathElement { =20 + /** Path element name */ private final Name name; =20 + /** Path element index */ private final int index; =20 + /** + * Creates an indexed path element instance. + * + * @param name path element name + * @param index path element index + */ public IndexedElement(Name name, int index) { this.name =3D name; this.index =3D index; } =20 - /** {@inheritDoc} */ - public Item step(Item item) throws ItemNotFoundException, - RepositoryException { + /** + * Resolves the given item to the named child node with the + * specified index. + * + * @param item context item + * @return indexed child node + * @throws PathNotFoundException if the path resolution fails + * @throws RepositoryException if another error occurs + * @see PathElement#resolve(Item) + */ + public Item resolve(Item item) + throws PathNotFoundException, RepositoryException { if (item.isNode()) { String pattern =3D name.toJCRName(item.getSession()); NodeIterator nodes =3D ((Node) item).getNodes(pattern); - if (index <=3D nodes.getSize()) { + try { nodes.skip(index - 1); return nodes.nextNode(); + } catch (NoSuchElementException e) { + // fall through } } - - throw new ItemNotFoundException("Invalid item path " + name); + throw new PathNotFoundException( + "Path name or index not found: " + this); } =20 + /** + * Returns the string representation of this path element. + * + * @return string representation of the path element name and index + * @see Object#toString() + * @see Name#toString() + */ + public String toString() { + return name + "[" + index + "]"; + } } Modified: incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/ja= ckrabbit/name/Name.java URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/jcr-e= xt/src/java/org/apache/jackrabbit/name/Name.java?rev=3D169166&r1=3D169165&r= 2=3D169166&view=3Ddiff =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=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabb= it/name/Name.java (original) +++ incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabb= it/name/Name.java Sun May 8 14:16:44 2005 @@ -21,52 +21,107 @@ import javax.jcr.Session; =20 /** - * TODO + * Qualified name. Instance of this immutable class are used to + * represent qualified names. A qualified name consists of a + * namespace URI and a local part. */ -public class Name { +public final class Name { =20 - public static Name parseJCRName(Session session, String name) - throws NamespaceException, RepositoryException { - int p =3D name.indexOf(':'); - if (p !=3D -1) { - String prefix =3D name.substring(0, p); - name =3D name.substring(p + 1); - return new Name(session.getNamespaceURI(prefix), name); - } else { - return new Name(session.getNamespaceURI(""), name); - } - } - - private final String namespace; + /** Namespace URI of the qualified name. */ + private final String namespaceURI; =20 - private final String name; + /** Local part of the qualified name. */ + private final String localPart; =20 + /** + * Creates a qualified name instance. + * + * @param namespace namespace URI + * @param name local part + */ public Name(String namespace, String name) { - this.namespace =3D namespace; - this.name =3D name; + this.namespaceURI =3D namespace; + this.localPart =3D name; } =20 + /** + * Returns the namespace URI of the qualified name. + * + * @return namespace URI + */ public String getNamespaceURI() { - return namespace; + return namespaceURI; } =20 + /** + * Returns the local part of the qualified name. + * + * @return local part + */ public String getLocalPart() { - return name; - } - - public String toJCRName(Session session) - throws NamespaceException, RepositoryException { - String prefix =3D session.getNamespacePrefix(namespace); - return prefix + ":" + name; + return localPart; } =20 + /** + * Compares for equality. Two qualified names are equal if they have + * the same namespace URI and the same local part. + * + * @param object the object to compare to + * @return true if the given object is equal to this one, + * false otherwise + * @see Object#equals(Object) + */ public boolean equals(Object object) { - if (object instanceof Name) { - Name qname =3D (Name) object; - return namespace.equals(qname.namespace) && name.equals(qname.= name); + if (this =3D=3D object) { + return true; + } else if (object instanceof Name) { + Name that =3D (Name) object; + return namespaceURI.equals(that.namespaceURI) + && localPart.equals(that.localPart); } else { return false; } + } + + /** + * Calculates the hash code of the qualified name. + * + * @return hash code + * @see Object#hashCode() + */ + public int hashCode() { + int code =3D 17; + code =3D 37 * code + namespaceURI.hashCode(); + code =3D 37 * code + localPart.hashCode(); + return code; + } + + /** + * Returns a string representation of the qualified name. + * + * @return string representation + * @see Object#toString() + */ + public String toString() { + return "{" + namespaceURI + "}" + localPart; + } + + public static Name parseJCRName(Session session, String name) + throws NamespaceException, RepositoryException { + int p =3D name.indexOf(':'); + if (p !=3D -1) { + String prefix =3D name.substring(0, p); + name =3D name.substring(p + 1); + return new Name(session.getNamespaceURI(prefix), name); + } else { + return new Name(session.getNamespaceURI(""), name); + } + } + + public String toJCRName(Session session) + throws NamespaceException, RepositoryException { + String prefix =3D session.getNamespacePrefix(namespaceURI); + return prefix + ":" + localPart; } =20 } Modified: incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/ja= ckrabbit/name/NamedElement.java URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/jcr-e= xt/src/java/org/apache/jackrabbit/name/NamedElement.java?rev=3D169166&r1=3D= 169165&r2=3D169166&view=3Ddiff =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=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabb= it/name/NamedElement.java (original) +++ incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabb= it/name/NamedElement.java Sun May 8 14:16:44 2005 @@ -6,7 +6,7 @@ * 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 + * 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, @@ -17,41 +17,68 @@ package org.apache.jackrabbit.name; =20 import javax.jcr.Item; -import javax.jcr.ItemNotFoundException; import javax.jcr.Node; import javax.jcr.NodeIterator; +import javax.jcr.PathNotFoundException; import javax.jcr.PropertyIterator; import javax.jcr.RepositoryException; =20 /** - * TODO + * Named path element. */ -class NamedElement implements PathElement { +final class NamedElement implements PathElement { =20 + /** Path element name */ private final Name name; =20 + /** + * Creates a named path element instance. + * + * @param name path element name + */ public NamedElement(Name name) { this.name =3D name; } =20 - /** {@inheritDoc} */ - public Item step(Item item) throws ItemNotFoundException, - RepositoryException { + /** + * Resolves the given item to the named property or child node. + * + * @param item context item + * @return named property or child node + * @throws PathNotFoundException if the path resolution fails + * @throws RepositoryException if another error occurs + * @see PathElement#resolve(Item) + */ + public Item resolve(Item item) + throws PathNotFoundException, RepositoryException { if (item.isNode()) { + Node node =3D (Node) item; + + /* Note: JCR names can not contain special pattern characters = */ String pattern =3D name.toJCRName(item.getSession()); =20 - PropertyIterator properties =3D ((Node) item).getProperties(pa= ttern); + PropertyIterator properties =3D node.getProperties(pattern); if (properties.hasNext()) { return properties.nextProperty(); } =20 - NodeIterator nodes =3D ((Node) item).getNodes(pattern); + NodeIterator nodes =3D node.getNodes(pattern); if (nodes.hasNext()) { return nodes.nextNode(); } } + throw new PathNotFoundException("Path name not found: " + this); + } =20 - throw new ItemNotFoundException("Invalid item path " + name); + /** + * Returns the string representation of this path element. + * + * @return string representation of the path element name + * @see Object#toString() + * @see Name#toString() + */ + public String toString() { + return name.toString(); } =20 } Modified: incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/ja= ckrabbit/name/ParentElement.java URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/jcr-e= xt/src/java/org/apache/jackrabbit/name/ParentElement.java?rev=3D169166&r1= =3D169165&r2=3D169166&view=3Ddiff =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=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabb= it/name/ParentElement.java (original) +++ incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabb= it/name/ParentElement.java Sun May 8 14:16:44 2005 @@ -17,32 +17,34 @@ package org.apache.jackrabbit.name; =20 import javax.jcr.Item; -import javax.jcr.ItemNotFoundException; import javax.jcr.RepositoryException; =20 /** - * TODO + * The ".." path element. */ class ParentElement implements PathElement { =20 - /** Singleton instance. */ - private static final PathElement instance =3D new ParentElement(); - - public static PathElement getInstance() { - return instance; - } - - private ParentElement() { - } - - /** {@inheritDoc} */ - public Item step(Item item) throws ItemNotFoundException, - RepositoryException { + /** + * Resolves the given item to its parent. + * + * @param item context item + * @return parent node + * @throws RepositoryException if the parent node can not be retrieved + * @see PathElement#resolve(Item) + * @see Item#getParent() + */ + public Item resolve(Item item) throws RepositoryException { return item.getParent(); } =20 - public boolean equals(Object other) { - return other =3D=3D instance; + /** + * Returns the string representation of this path element. + * + * @return the string ".." + * @see Object#toString() + */ + public String toString() { + return ".."; } =20 } Modified: incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/ja= ckrabbit/name/Path.java URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/jcr-e= xt/src/java/org/apache/jackrabbit/name/Path.java?rev=3D169166&r1=3D169165&r= 2=3D169166&view=3Ddiff =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=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabb= it/name/Path.java (original) +++ incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabb= it/name/Path.java Sun May 8 14:16:44 2005 @@ -16,102 +16,36 @@ */ package org.apache.jackrabbit.name; =20 -import java.util.Iterator; -import java.util.List; -import java.util.Vector; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - import javax.jcr.Item; -import javax.jcr.ItemNotFoundException; -import javax.jcr.NamespaceException; +import javax.jcr.PathNotFoundException; import javax.jcr.RepositoryException; import javax.jcr.Session; =20 -/** - * TODO - */ -public class Path { - - /** - * Pattern used to validate and parse path elements:

- *

    - *
  • group 1 is . - *
  • group 2 is .. - *
  • group 3 is namespace prefix incl. delimiter (colon) - *
  • group 4 is namespace prefix excl. delimiter (colon) - *
  • group 5 is localName - *
  • group 6 is index incl. brackets - *
  • group 7 is index excl. brackets - *
- */ - private static final Pattern PATH_ELEMENT_PATTERN =3D Pattern.compile( - "(\\.?)" + "|" + "(\\.\\.)" + "|" + "(([^ /:\\[\\]*'\"|](?:[^/= :\\[\\]*'\"|]*[^ /:\\[\\]*'\"|])?):)?([^ /:\\[\\]*'\"|](?:[^/:\\[\\]*'\"|]*= [^ /:\\[\\]*'\"|])?)(\\[([1-9]\\d*)\\])?"); - - public static Path parseJCRPath(Session session, String path) - throws IllegalArgumentException, RepositoryException { - Vector elements =3D new Vector(); - - int p =3D path.indexOf('/'); - if (p =3D=3D 0) { - elements.add(RootElement.getInstance()); - path =3D path.substring(1); - p =3D path.indexOf('/'); - } - - while (p !=3D -1) { - elements.add(parseJCRPathElement(session, path.substring(0, p)= )); - path =3D path.substring(p + 1); - p =3D path.indexOf('/'); - } +public final class Path { =20 - elements.add(parseJCRPathElement(session, path)); + private final PathElement[] elements; =20 - return new Path(elements);=20 + Path(PathElement[] elements) { + this.elements =3D elements; } - =20 - private static PathElement parseJCRPathElement( - Session session, String element) - throws IllegalArgumentException, RepositoryException { - Matcher matcher =3D PATH_ELEMENT_PATTERN.matcher(element); - if (matcher.matches()) { - try { - if (matcher.group(1) !=3D null) { - return ThisElement.getInstance(); - } else if (matcher.group(2) !=3D null) { - return ParentElement.getInstance(); - } else if (matcher.group(3) !=3D null) { - return new NamedElement( - Name.parseJCRName(session, element)); - } else { - return new IndexedElement( - Name.parseJCRName(session, matcher.group(5)), - Integer.parseInt(matcher.group(6))); - } - } catch (NamespaceException e) { - throw new IllegalArgumentException( - "Invalid path element " + element); - } - } else { - throw new IllegalArgumentException( - "Invalid path element " + element); + + public Item resolve(Item item) + throws PathNotFoundException, RepositoryException { + for (int i =3D 0; i < elements.length; i++) { + item =3D elements[i].resolve(item); } + return item; } =20 - private final List elements; - - private Path(List elements) { - this.elements =3D elements; + public static Path parse(Session session, String path) + throws IllegalArgumentException, RepositoryException { + return new PathParser(session).parsePath(path); } =20 - public Item walk(Item item) - throws ItemNotFoundException, RepositoryException { - Iterator iterator =3D elements.iterator(); - while (iterator.hasNext()) { - PathElement element =3D (PathElement) iterator.next(); - item =3D element.step(item); - } - return item; + public static Item resolve(Item item, String path) + throws IllegalArgumentException, PathNotFoundException, + RepositoryException { + return parse(item.getSession(), path).resolve(item); } =20 } Added: incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackr= abbit/name/PathBuilder.java URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/jcr-e= xt/src/java/org/apache/jackrabbit/name/PathBuilder.java?rev=3D169166&view= =3Dauto =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=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabb= it/name/PathBuilder.java (added) +++ incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabb= it/name/PathBuilder.java Sun May 8 14:16:44 2005 @@ -0,0 +1,39 @@ +/* + * Copyright 2004-2005 The Apache Software Foundation or its licensors, + * as applicable. + * + * Licensed 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.name; + +import java.util.List; +import java.util.Vector; + +final class PathBuilder { + + private final List elements; + + public PathBuilder() { + elements =3D new Vector(); + } + + public void addElement(PathElement element) { + elements.add(element); + } + + public Path getPath() { + return new Path((PathElement[]) + elements.toArray(new PathElement[elements.size()])); + } + +} Modified: incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/ja= ckrabbit/name/PathElement.java URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/jcr-e= xt/src/java/org/apache/jackrabbit/name/PathElement.java?rev=3D169166&r1=3D1= 69165&r2=3D169166&view=3Ddiff =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=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabb= it/name/PathElement.java (original) +++ incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabb= it/name/PathElement.java Sun May 8 14:16:44 2005 @@ -17,14 +17,28 @@ package org.apache.jackrabbit.name; =20 import javax.jcr.Item; -import javax.jcr.ItemNotFoundException; +import javax.jcr.PathNotFoundException; import javax.jcr.RepositoryException; =20 /** - * TODO + * Path element. This interface is used by the {@link Path Path} class + * to manage the various types of JCR path elements. + *

+ * Each path element knows how to resolve itself in the context of a given + * content item. The {@link #resolve(Item) resolve(Item)} method is used + * to access this logic. */ interface PathElement { =20 - Item step(Item item) throws ItemNotFoundException, RepositoryException; + /** + * Resolves this path element within the context of the given content + * item. Retuns the result of the path element resolution. + * + * @param item the context from which to resolve this path element + * @return the resolved content item + * @throws PathNotFoundException if the path element could not be reso= lved + * @throws RepositoryException if another error occurred + */ + Item resolve(Item item) throws PathNotFoundException, RepositoryExcept= ion; =20 } Added: incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackr= abbit/name/PathParser.java URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/jcr-e= xt/src/java/org/apache/jackrabbit/name/PathParser.java?rev=3D169166&view=3D= auto =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=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabb= it/name/PathParser.java (added) +++ incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabb= it/name/PathParser.java Sun May 8 14:16:44 2005 @@ -0,0 +1,110 @@ +/* + * Copyright 2004-2005 The Apache Software Foundation or its licensors, + * as applicable. + * + * Licensed 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.name; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import javax.jcr.NamespaceException; +import javax.jcr.RepositoryException; +import javax.jcr.Session; + +final class PathParser { + + /** + * Pattern used to validate and parse path elements:

+ *

    + *
  • group 1 is . + *
  • group 2 is .. + *
  • group 3 is namespace prefix excl. delimiter (colon) + *
  • group 4 is localName + *
  • group 5 is index excl. brackets + *
+ */ + private static final Pattern PATH_ELEMENT_PATTERN =3D Pattern.compile( + "(\\.)|(\\.\\.)|" + + "(?:([^ /:\\[\\]*'\"|](?:[^/:\\[\\]*'\"|]*[^ /:\\[\\]*'\"|])= ?):)?" + + "([^ /:\\[\\]*'\"|](?:[^/:\\[\\]*'\"|]*[^ /:\\[\\]*'\"|])?)" + + "(?:\\[([1-9]\\d*)\\])?"); + + private final Session session; + + public PathParser(Session session) { + this.session =3D session; + } + + public Path parsePath(String path) + throws IllegalArgumentException, RepositoryException { + PathBuilder builder =3D new PathBuilder(); + + int p =3D path.indexOf('/'); + if (p =3D=3D 0) { + builder.addElement(new RootElement()); + path =3D path.substring(1); + p =3D path.indexOf('/'); + } + + while (p !=3D -1) { + if (p > 0) { + builder.addElement(parsePathElement(path.substring(0, p)))= ; + } + path =3D path.substring(p + 1); + p =3D path.indexOf('/'); + } + + if (path.length() > 0) { + builder.addElement(parsePathElement(path)); + } + + return builder.getPath(); + } + + private PathElement parsePathElement(String element) + throws IllegalArgumentException, RepositoryException { + Matcher matcher =3D PATH_ELEMENT_PATTERN.matcher(element); + if (matcher.matches()) { + try { + if (matcher.group(1) !=3D null) { // . + return new ThisElement(); + } else if (matcher.group(2) !=3D null) { // .. + return new ParentElement(); + } else { + String prefix =3D matcher.group(3); + String localPart =3D matcher.group(4); + String index =3D matcher.group(5); + + String namespaceURI =3D + session.getNamespaceURI(prefix !=3D null ? prefix = : ""); + Name name =3D new Name(namespaceURI, localPart); + if (index =3D=3D null) { + return new NamedElement(name); + } else { + return new IndexedElement(name, Integer.parseInt(i= ndex)); + } + } + } catch (NamespaceException e) { + throw new IllegalArgumentException( + "Invalid path name prefix: " + element); + } + } else { + throw new IllegalArgumentException( + "Invalid path name syntax: " + element); + } + } + + +} Modified: incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/ja= ckrabbit/name/RootElement.java URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/jcr-e= xt/src/java/org/apache/jackrabbit/name/RootElement.java?rev=3D169166&r1=3D1= 69165&r2=3D169166&view=3Ddiff =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=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabb= it/name/RootElement.java (original) +++ incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabb= it/name/RootElement.java Sun May 8 14:16:44 2005 @@ -17,32 +17,34 @@ package org.apache.jackrabbit.name; =20 import javax.jcr.Item; -import javax.jcr.ItemNotFoundException; import javax.jcr.RepositoryException; =20 /** - * TODO + * The leading "/" path element. */ class RootElement implements PathElement { =20 - /** Singleton instance. */ - private static final PathElement instance =3D new RootElement(); - - public static PathElement getInstance() { - return instance; - } - - private RootElement() { - } - - /** {@inheritDoc} */ - public Item step(Item item) - throws ItemNotFoundException, RepositoryException { + /** + * Resolves the given item to the root node. + * + * @param item context item + * @return root node + * @throws RepositoryException if the root node can not be retrieved + * @see PathElement#resolve(Item) + * @see Item#getAncestor(int) + */ + public Item resolve(Item item) throws RepositoryException { return item.getAncestor(0); } =20 - public boolean equals(Object other) { - return other =3D=3D instance; + /** + * Returns the string representation of this path element. + * + * @return the empty string + * @see Object#toString() + */ + public String toString() { + return ""; } =20 } Modified: incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/ja= ckrabbit/name/ThisElement.java URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/jcr-e= xt/src/java/org/apache/jackrabbit/name/ThisElement.java?rev=3D169166&r1=3D1= 69165&r2=3D169166&view=3Ddiff =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=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabb= it/name/ThisElement.java (original) +++ incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabb= it/name/ThisElement.java Sun May 8 14:16:44 2005 @@ -19,27 +19,29 @@ import javax.jcr.Item; =20 /** - * TODO + * The "." path element. */ -class ThisElement implements PathElement { +final class ThisElement implements PathElement { =20 - /** Singleton instance. */ - private static final PathElement instance =3D new ThisElement(); - - public static PathElement getInstance() { - return instance; - } - - private ThisElement() { - } - - /** {@inheritDoc} */ - public Item step(Item item) { + /** + * Resolves the given item to itself. + * + * @param item context item + * @return the same item + * @see PathElement#resolve(Item) + */ + public Item resolve(Item item) { return item; } =20 - public boolean equals(Object other) { - return other =3D=3D instance; + /** + * Returns the string representation of this path element. + * + * @return the string "." + * @see Object#toString() + */ + public String toString() { + return "."; } =20 }