jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r494567 - in /jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/name: CachingPathResolver.java ParsingPathResolver.java PathResolver.java
Date Tue, 09 Jan 2007 20:47:50 GMT
Author: jukka
Date: Tue Jan  9 12:47:49 2007
New Revision: 494567

URL: http://svn.apache.org/viewvc?view=rev&rev=494567
Log:
JCR-688: Added PathResolver and two implementation classes

Added:
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/name/CachingPathResolver.java
  (with props)
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/name/ParsingPathResolver.java
  (with props)
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/name/PathResolver.java
  (with props)

Added: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/name/CachingPathResolver.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/name/CachingPathResolver.java?view=auto&rev=494567
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/name/CachingPathResolver.java
(added)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/name/CachingPathResolver.java
Tue Jan  9 12:47:49 2007
@@ -0,0 +1,99 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 javax.jcr.NamespaceException;
+
+/**
+ * Path resolver decorator that uses a generational cache to speed up
+ * parsing and formatting of JCR paths. Uncached paths are resolved using
+ * the underlying decorated path resolver.
+ */
+public class CachingPathResolver implements PathResolver {
+
+    /**
+     * Decorated path resolver.
+     */
+    private final PathResolver resolver;
+
+    /**
+     * Generational cache.
+     */
+    private final GenerationalCache cache;
+
+    /**
+     * Creates a caching decorator for the given path resolver. The given
+     * generational cache is used for caching.
+     *
+     * @param resolver decorated path resolver
+     * @param cache generational cache
+     */
+    public CachingPathResolver(PathResolver resolver, GenerationalCache cache) {
+        this.resolver = resolver;
+        this.cache = cache;
+    }
+
+    /**
+     * Creates a caching decorator for the given path resolver.
+     *
+     * @param resolver name resolver
+     */
+    public CachingPathResolver(PathResolver resolver) {
+        this(resolver, new GenerationalCache());
+    }
+
+    //--------------------------------------------------------< PathResolver >
+
+    /**
+     * Returns the qualified path for the given prefixed JCR path. The path
+     * is first looked up form the generational cache and the call gets
+     * delegated to the decorated path resolver only if the cache misses.
+     *
+     * @param path prefixed JCR path
+     * @return qualified path
+     * @throws NameException if the JCR path format is invalid
+     * @throws NamespaceException if a namespace prefix can not be resolved
+     */
+    public Path getQPath(String path) throws NameException, NamespaceException {
+        Path qpath = (Path) cache.get(path);
+        if (qpath == null) {
+            qpath = resolver.getQPath(path);
+            cache.put(path, qpath);
+        }
+        return qpath;
+    }
+
+
+    /**
+     * Returns the prefixed JCR path for the given qualified path. The path
+     * is first looked up form the generational cache and the call gets
+     * delegated to the decorated path resolver only if the cache misses.
+     *
+     * @param qpath qualified path
+     * @return prefixed JCR path
+     * @throws NamespaceException if a namespace URI can not be resolved
+     */
+    public String getJCRPath(Path qpath) throws NamespaceException {
+        String path = (String) cache.get(qpath);
+        if (path == null) {
+            path = resolver.getJCRPath(qpath);
+            cache.put(qpath, path);
+        }
+        return path;
+    }
+
+}

Propchange: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/name/CachingPathResolver.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/name/ParsingPathResolver.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/name/ParsingPathResolver.java?view=auto&rev=494567
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/name/ParsingPathResolver.java
(added)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/name/ParsingPathResolver.java
Tue Jan  9 12:47:49 2007
@@ -0,0 +1,148 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 javax.jcr.NamespaceException;
+
+/**
+ * Path resolver that parsers and formats prefixed JCR paths.
+ * A {@link NameResolver} is used for resolving the path element names.
+ */
+public class ParsingPathResolver implements PathResolver {
+
+    /**
+     * Name resolver.
+     */
+    private final NameResolver resolver;
+
+    /**
+     * Creates a parsing path resolver.
+     *
+     * @param resolver name resolver
+     */
+    public ParsingPathResolver(NameResolver resolver) {
+        this.resolver = resolver;
+    }
+
+    /**
+     * Parses and returns a single path element.
+     *
+     * @param path JCR path
+     * @param begin begin index of the path element
+     * @param end end index of the path element
+     * @return path element
+     * @throws NameException if the path element format is invalid
+     * @throws NamespaceException if the namespace prefix can not be resolved
+     */
+    private Path.PathElement getElement(String path, int begin, int end)
+            throws NameException, NamespaceException {
+        if (begin == end) {
+            throw new MalformedPathException("Empty path element: " + path);
+        } else if (begin + 1 == end && path.charAt(begin) == '.') {
+            return Path.CURRENT_ELEMENT;
+        } else if (begin + 2 == end && path.charAt(begin) == '.'
+                   && path.charAt(begin + 1) == '.') {
+            return Path.PARENT_ELEMENT;
+        } else if (begin + 3 < end && path.charAt(end - 1) == ']') {
+            int index = 0;
+            int factor = 1;
+            end -= 2;
+            char ch = path.charAt(end);
+            while (begin < end && '0' <= ch && ch <= '9') {
+                index += factor * (ch - '0');
+                factor *= 10;
+                ch = path.charAt(--end);
+            }
+            if (index == 0 || ch != '[') {
+                throw new MalformedPathException("Invalid path index: " + path);
+            }
+            QName qname = resolver.getQName(path.substring(begin, end));
+            return Path.PathElement.create(qname, index);
+        } else {
+            QName qname = resolver.getQName(path.substring(begin, end));
+            return Path.PathElement.create(qname);
+        }
+    }
+
+    /**
+     * Parses the prefixed JCR path and returns the resolved qualified path.
+     *
+     * @param path prefixed JCR path
+     * @return qualified path
+     * @throws NameException if the JCR path format is invalid
+     * @throws NamespaceException if a namespace prefix can not be resolved
+     */
+    public Path getQPath(String path) throws NameException, NamespaceException {
+        Path.PathBuilder builder = new Path.PathBuilder();
+        int length = path.length();
+        int position = 0;
+        int slash = path.indexOf('/');
+
+        if (slash == 0) {
+            builder.addRoot();
+            if (length == 1) {
+                return builder.getPath();
+            }
+            position = 1;
+            slash = path.indexOf('/', 1);
+        }
+        while (slash != -1) {
+            builder.addLast(getElement(path, position, slash));
+            position = slash + 1;
+            slash = path.indexOf('/', position);
+        }
+        builder.addLast(getElement(path, position, length));
+
+        return builder.getPath();
+    }
+
+
+    /**
+     * Returns the prefixed JCR path for the given qualified path.
+     *
+     * @param path qualified path
+     * @return prefixed JCR path
+     * @throws NamespaceException if a namespace URI can not be resolved
+     */
+    public String getJCRPath(Path path) throws NamespaceException {
+        StringBuffer buffer = new StringBuffer();
+
+        Path.PathElement[] elements = path.getElements();
+        for (int i = 0; i < elements.length; i++) {
+            if (i > 0) {
+                buffer.append('/');
+            }
+            if (i == 0 && elements.length == 1 && elements[i].denotesRoot())
{
+                buffer.append('/');
+            } else if (elements[i].denotesCurrent()) {
+                buffer.append('.');
+            } else if (elements[i].denotesParent()) {
+                buffer.append("..");
+            } else {
+                buffer.append(resolver.getJCRName(elements[i].getName()));
+                if (elements[i].getIndex() != 0) {
+                    buffer.append('[');
+                    buffer.append(elements[i].getIndex());
+                    buffer.append(']');
+                }
+            }
+        }
+
+        return buffer.toString();
+    }
+
+}

Propchange: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/name/ParsingPathResolver.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/name/PathResolver.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/name/PathResolver.java?view=auto&rev=494567
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/name/PathResolver.java
(added)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/name/PathResolver.java
Tue Jan  9 12:47:49 2007
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 javax.jcr.NamespaceException;
+
+/**
+ * Resolver for JCR paths.
+ */
+public interface PathResolver {
+
+    /**
+     * Returns the qualified path for the given prefixed JCR path.
+     *
+     * @param path prefixed JCR path
+     * @return qualified path
+     * @throws NameException if the JCR path format is invalid
+     * @throws NamespaceException if a namespace prefix can not be resolved
+     */
+    Path getQPath(String path) throws NameException, NamespaceException;
+
+    /**
+     * Returns the prefixed JCR path for the given qualified path.
+     *
+     * @param path qualified path
+     * @return prefixed JCR path
+     * @throws NamespaceException if a namespace URI can not be resolved
+     */
+    String getJCRPath(Path path) throws NamespaceException;
+
+}

Propchange: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/name/PathResolver.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message