jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
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 GMT
Author: jukka
Date: Sun May  8 14:16:44 2005
New Revision: 169166

URL: http://svn.apache.org/viewcvs?rev=169166&view=rev
Log:
JCR-EXT: Cleaned up, documented and extended the name and path implementation classes.

Added:
    incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/name/PathBuilder.java
    incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/name/PathParser.java
Modified:
    incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/name/IndexedElement.java
    incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/name/Name.java
    incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/name/NamedElement.java
    incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/name/ParentElement.java
    incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/name/Path.java
    incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/name/PathElement.java
    incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/name/RootElement.java
    incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/name/ThisElement.java

Modified: incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/name/IndexedElement.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/name/IndexedElement.java?rev=169166&r1=169165&r2=169166&view=diff
==============================================================================
--- incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/name/IndexedElement.java
(original)
+++ incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/name/IndexedElement.java
Sun May  8 14:16:44 2005
@@ -16,39 +16,70 @@
  */
 package org.apache.jackrabbit.name;
 
+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;
 
 /**
- * TODO
+ * Indexed path element.
  */
-class IndexedElement implements PathElement {
+final class IndexedElement implements PathElement {
 
+    /** Path element name */
     private final Name name;
 
+    /** Path element index */
     private final int index;
 
+    /**
+     * Creates an indexed path element instance.
+     *
+     * @param name  path element name
+     * @param index path element index
+     */
     public IndexedElement(Name name, int index) {
         this.name = name;
         this.index = index;
     }
 
-    /** {@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 = name.toJCRName(item.getSession());
             NodeIterator nodes = ((Node) item).getNodes(pattern);
-            if (index <= 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);
     }
 
+    /**
+     * 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/jackrabbit/name/Name.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/name/Name.java?rev=169166&r1=169165&r2=169166&view=diff
==============================================================================
--- incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/name/Name.java
(original)
+++ incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/name/Name.java
Sun May  8 14:16:44 2005
@@ -21,52 +21,107 @@
 import javax.jcr.Session;
 
 /**
- * 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 {
 
-    public static Name parseJCRName(Session session, String name)
-            throws NamespaceException, RepositoryException {
-        int p = name.indexOf(':');
-        if (p != -1) {
-            String prefix = name.substring(0, p);
-            name = 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;
 
-    private final String name;
+    /** Local part of the qualified name. */
+    private final String localPart;
 
+    /**
+     * Creates a qualified name instance.
+     *
+     * @param namespace namespace URI
+     * @param name      local part
+     */
     public Name(String namespace, String name) {
-        this.namespace = namespace;
-        this.name = name;
+        this.namespaceURI = namespace;
+        this.localPart = name;
     }
 
+    /**
+     * Returns the namespace URI of the qualified name.
+     *
+     * @return namespace URI
+     */
     public String getNamespaceURI() {
-        return namespace;
+        return namespaceURI;
     }
 
+    /**
+     * 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 = session.getNamespacePrefix(namespace);
-        return prefix + ":" + name;
+        return localPart;
     }
 
+    /**
+     * 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 <code>true</code> if the given object is equal to this one,
+     *         <code>false</code> otherwise
+     * @see Object#equals(Object)
+     */
     public boolean equals(Object object) {
-        if (object instanceof Name) {
-            Name qname = (Name) object;
-            return namespace.equals(qname.namespace) && name.equals(qname.name);
+        if (this == object) {
+            return true;
+        } else if (object instanceof Name) {
+            Name that = (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 = 17;
+        code = 37 * code + namespaceURI.hashCode();
+        code = 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 = name.indexOf(':');
+        if (p != -1) {
+            String prefix = name.substring(0, p);
+            name = 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 = session.getNamespacePrefix(namespaceURI);
+        return prefix + ":" + localPart;
     }
 
 }

Modified: incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/name/NamedElement.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/name/NamedElement.java?rev=169166&r1=169165&r2=169166&view=diff
==============================================================================
--- incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/name/NamedElement.java
(original)
+++ incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/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;
 
 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;
 
 /**
- * TODO
+ * Named path element.
  */
-class NamedElement implements PathElement {
+final class NamedElement implements PathElement {
 
+    /** Path element name */
     private final Name name;
 
+    /**
+     * Creates a named path element instance.
+     *
+     * @param name path element name
+     */
     public NamedElement(Name name) {
         this.name = name;
     }
 
-    /** {@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 = (Node) item;
+
+            /* Note: JCR names can not contain special pattern characters */
             String pattern = name.toJCRName(item.getSession());
 
-            PropertyIterator properties = ((Node) item).getProperties(pattern);
+            PropertyIterator properties = node.getProperties(pattern);
             if (properties.hasNext()) {
                 return properties.nextProperty();
             }
 
-            NodeIterator nodes = ((Node) item).getNodes(pattern);
+            NodeIterator nodes = node.getNodes(pattern);
             if (nodes.hasNext()) {
                 return nodes.nextNode();
             }
         }
+        throw new PathNotFoundException("Path name not found: " + this);
+    }
 
-        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();
     }
 
 }

Modified: incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/name/ParentElement.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/name/ParentElement.java?rev=169166&r1=169165&r2=169166&view=diff
==============================================================================
--- incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/name/ParentElement.java
(original)
+++ incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/name/ParentElement.java
Sun May  8 14:16:44 2005
@@ -17,32 +17,34 @@
 package org.apache.jackrabbit.name;
 
 import javax.jcr.Item;
-import javax.jcr.ItemNotFoundException;
 import javax.jcr.RepositoryException;
 
 /**
- * TODO
+ * The ".." path element.
  */
 class ParentElement implements PathElement {
 
-    /** Singleton instance. */
-    private static final PathElement instance = 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();
     }
 
-    public boolean equals(Object other) {
-        return other == instance;
+    /**
+     * Returns the string representation of this path element.
+     *
+     * @return the string ".."
+     * @see Object#toString()
+     */
+    public String toString() {
+        return "..";
     }
 
 }

Modified: incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/name/Path.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/name/Path.java?rev=169166&r1=169165&r2=169166&view=diff
==============================================================================
--- incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/name/Path.java
(original)
+++ incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/name/Path.java
Sun May  8 14:16:44 2005
@@ -16,102 +16,36 @@
  */
 package org.apache.jackrabbit.name;
 
-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;
 
-/**
- * TODO
- */
-public class Path {
-
-    /**
-     * Pattern used to validate and parse path elements:<p>
-     * <ul>
-     * <li>group 1 is .
-     * <li>group 2 is ..
-     * <li>group 3 is namespace prefix incl. delimiter (colon)
-     * <li>group 4 is namespace prefix excl. delimiter (colon)
-     * <li>group 5 is localName
-     * <li>group 6 is index incl. brackets
-     * <li>group 7 is index excl. brackets
-     * </ul>
-     */
-    private static final Pattern PATH_ELEMENT_PATTERN = Pattern.compile(
-            "(\\.?)" + "|" + "(\\.\\.)" + "|" + "(([^ /:\\[\\]*'\"|](?:[^/:\\[\\]*'\"|]*[^
/:\\[\\]*'\"|])?):)?([^ /:\\[\\]*'\"|](?:[^/:\\[\\]*'\"|]*[^ /:\\[\\]*'\"|])?)(\\[([1-9]\\d*)\\])?");
-
-    public static Path parseJCRPath(Session session, String path)
-            throws IllegalArgumentException, RepositoryException {
-        Vector elements = new Vector();
-
-        int p = path.indexOf('/');
-        if (p == 0) {
-            elements.add(RootElement.getInstance());
-            path = path.substring(1);
-            p = path.indexOf('/');
-        }
-
-        while (p != -1) {
-            elements.add(parseJCRPathElement(session, path.substring(0, p)));
-            path = path.substring(p + 1);
-            p = path.indexOf('/');
-        }
+public final class Path {
 
-        elements.add(parseJCRPathElement(session, path));
+    private final PathElement[] elements;
 
-        return new Path(elements); 
+    Path(PathElement[] elements) {
+        this.elements = elements;
     }
-    
-    private static PathElement parseJCRPathElement(
-            Session session, String element)
-            throws IllegalArgumentException, RepositoryException {
-        Matcher matcher = PATH_ELEMENT_PATTERN.matcher(element);
-        if (matcher.matches()) {
-            try {
-                if (matcher.group(1) != null) {
-                    return ThisElement.getInstance();
-                } else if (matcher.group(2) != null) {
-                    return ParentElement.getInstance();
-                } else if (matcher.group(3) != 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 = 0; i < elements.length; i++) {
+            item = elements[i].resolve(item);
         }
+        return item;
     }
 
-    private final List elements;
-
-    private Path(List elements) {
-        this.elements = elements;
+    public static Path parse(Session session, String path)
+            throws IllegalArgumentException, RepositoryException {
+        return new PathParser(session).parsePath(path);
     }
 
-    public Item walk(Item item)
-            throws ItemNotFoundException, RepositoryException {
-        Iterator iterator = elements.iterator();
-        while (iterator.hasNext()) {
-            PathElement element = (PathElement) iterator.next();
-            item = element.step(item);
-        }
-        return item;
+    public static Item resolve(Item item, String path)
+            throws IllegalArgumentException, PathNotFoundException,
+            RepositoryException {
+        return parse(item.getSession(), path).resolve(item);
     }
 
 }

Added: incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/name/PathBuilder.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/name/PathBuilder.java?rev=169166&view=auto
==============================================================================
--- incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/name/PathBuilder.java
(added)
+++ incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/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 = 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/jackrabbit/name/PathElement.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/name/PathElement.java?rev=169166&r1=169165&r2=169166&view=diff
==============================================================================
--- incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/name/PathElement.java
(original)
+++ incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/name/PathElement.java
Sun May  8 14:16:44 2005
@@ -17,14 +17,28 @@
 package org.apache.jackrabbit.name;
 
 import javax.jcr.Item;
-import javax.jcr.ItemNotFoundException;
+import javax.jcr.PathNotFoundException;
 import javax.jcr.RepositoryException;
 
 /**
- * TODO
+ * Path element. This interface is used by the {@link Path Path} class
+ * to manage the various types of JCR path elements.
+ * <p>
+ * 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 {
 
-    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 resolved
+     * @throws RepositoryException   if another error occurred
+     */
+    Item resolve(Item item) throws PathNotFoundException, RepositoryException;
 
 }

Added: incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/name/PathParser.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/name/PathParser.java?rev=169166&view=auto
==============================================================================
--- incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/name/PathParser.java
(added)
+++ incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/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:<p>
+     * <ul>
+     * <li>group 1 is .
+     * <li>group 2 is ..
+     * <li>group 3 is namespace prefix excl. delimiter (colon)
+     * <li>group 4 is localName
+     * <li>group 5 is index excl. brackets
+     * </ul>
+     */
+    private static final Pattern PATH_ELEMENT_PATTERN = Pattern.compile(
+            "(\\.)|(\\.\\.)|"
+            + "(?:([^ /:\\[\\]*'\"|](?:[^/:\\[\\]*'\"|]*[^ /:\\[\\]*'\"|])?):)?"
+            + "([^ /:\\[\\]*'\"|](?:[^/:\\[\\]*'\"|]*[^ /:\\[\\]*'\"|])?)"
+            + "(?:\\[([1-9]\\d*)\\])?");
+
+    private final Session session;
+
+    public PathParser(Session session) {
+        this.session = session;
+    }
+
+    public Path parsePath(String path)
+            throws IllegalArgumentException, RepositoryException {
+        PathBuilder builder = new PathBuilder();
+
+        int p = path.indexOf('/');
+        if (p == 0) {
+            builder.addElement(new RootElement());
+            path = path.substring(1);
+            p = path.indexOf('/');
+        }
+
+        while (p != -1) {
+            if (p > 0) {
+                builder.addElement(parsePathElement(path.substring(0, p)));
+            }
+            path = path.substring(p + 1);
+            p = path.indexOf('/');
+        }
+
+        if (path.length() > 0) {
+            builder.addElement(parsePathElement(path));
+        }
+
+        return builder.getPath();
+    }
+
+    private PathElement parsePathElement(String element)
+            throws IllegalArgumentException, RepositoryException {
+        Matcher matcher = PATH_ELEMENT_PATTERN.matcher(element);
+        if (matcher.matches()) {
+            try {
+                if (matcher.group(1) != null) {          // .
+                    return new ThisElement();
+                } else if (matcher.group(2) != null) {   // ..
+                    return new ParentElement();
+                } else {
+                    String prefix = matcher.group(3);
+                    String localPart = matcher.group(4);
+                    String index = matcher.group(5);
+
+                    String namespaceURI =
+                        session.getNamespaceURI(prefix != null ? prefix : "");
+                    Name name = new Name(namespaceURI, localPart);
+                    if (index == null) {
+                        return new NamedElement(name);
+                    } else {
+                        return new IndexedElement(name, Integer.parseInt(index));
+                    }
+                }
+            } 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/jackrabbit/name/RootElement.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/name/RootElement.java?rev=169166&r1=169165&r2=169166&view=diff
==============================================================================
--- incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/name/RootElement.java
(original)
+++ incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/name/RootElement.java
Sun May  8 14:16:44 2005
@@ -17,32 +17,34 @@
 package org.apache.jackrabbit.name;
 
 import javax.jcr.Item;
-import javax.jcr.ItemNotFoundException;
 import javax.jcr.RepositoryException;
 
 /**
- * TODO
+ * The leading "/" path element.
  */
 class RootElement implements PathElement {
 
-    /** Singleton instance. */
-    private static final PathElement instance = 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);
     }
 
-    public boolean equals(Object other) {
-        return other == instance;
+    /**
+     * Returns the string representation of this path element.
+     *
+     * @return the empty string
+     * @see Object#toString()
+     */
+    public String toString() {
+        return "";
     }
 
 }

Modified: incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/name/ThisElement.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/name/ThisElement.java?rev=169166&r1=169165&r2=169166&view=diff
==============================================================================
--- incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/name/ThisElement.java
(original)
+++ incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/name/ThisElement.java
Sun May  8 14:16:44 2005
@@ -19,27 +19,29 @@
 import javax.jcr.Item;
 
 /**
- * TODO
+ * The "." path element.
  */
-class ThisElement implements PathElement {
+final class ThisElement implements PathElement {
 
-    /** Singleton instance. */
-    private static final PathElement instance = 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;
     }
 
-    public boolean equals(Object other) {
-        return other == instance;
+    /**
+     * Returns the string representation of this path element.
+     *
+     * @return the string "."
+     * @see Object#toString()
+     */
+    public String toString() {
+        return ".";
     }
 
 }



Mime
View raw message