From commits-return-7664-apmail-jackrabbit-commits-archive=jackrabbit.apache.org@jackrabbit.apache.org Tue May 05 12:10:28 2009 Return-Path: Delivered-To: apmail-jackrabbit-commits-archive@www.apache.org Received: (qmail 99365 invoked from network); 5 May 2009 12:10:28 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 5 May 2009 12:10:28 -0000 Received: (qmail 36821 invoked by uid 500); 5 May 2009 12:10:28 -0000 Delivered-To: apmail-jackrabbit-commits-archive@jackrabbit.apache.org Received: (qmail 36737 invoked by uid 500); 5 May 2009 12:10:28 -0000 Mailing-List: contact commits-help@jackrabbit.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@jackrabbit.apache.org Delivered-To: mailing list commits@jackrabbit.apache.org Received: (qmail 36724 invoked by uid 99); 5 May 2009 12:10:27 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 05 May 2009 12:10:27 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 05 May 2009 12:10:25 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id DF1C423888F1; Tue, 5 May 2009 12:10:03 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r771687 - /jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/AbstractSession.java Date: Tue, 05 May 2009 12:10:01 -0000 To: commits@jackrabbit.apache.org From: jukka@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090505121003.DF1C423888F1@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: jukka Date: Tue May 5 12:09:58 2009 New Revision: 771687 URL: http://svn.apache.org/viewvc?rev=771687&view=rev Log: JCR-1104: JSR 283 support Add default implementations of some new Session methods in JCR 2.0 Modified: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/AbstractSession.java Modified: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/AbstractSession.java URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/AbstractSession.java?rev=771687&r1=771686&r2=771687&view=diff ============================================================================== --- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/AbstractSession.java (original) +++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/AbstractSession.java Tue May 5 12:09:58 2009 @@ -30,6 +30,7 @@ import javax.jcr.NamespaceRegistry; import javax.jcr.Node; import javax.jcr.PathNotFoundException; +import javax.jcr.Property; import javax.jcr.Repository; import javax.jcr.RepositoryException; import javax.jcr.Session; @@ -355,6 +356,14 @@ //-----------------------------------------------------< Item handling >-- + private String toRelativePath(String absPath) throws PathNotFoundException { + if (absPath.startsWith("/") && absPath.length() > 1) { + return absPath.substring(1); + } else { + throw new PathNotFoundException("Not an absolute path: " + absPath); + } + } + /** * Returns the node or property at the given path. *

@@ -369,6 +378,7 @@ * above call fails with a {@link PathNotFoundException} * * + * @see Session#getItem(String) * @param absPath absolute path * @return the node or property with the given path * @throws PathNotFoundException if the given path is invalid or not found @@ -376,20 +386,16 @@ */ public Item getItem(String absPath) throws PathNotFoundException, RepositoryException { - if (!absPath.startsWith("/")) { - throw new PathNotFoundException("Not an absolute path: " + absPath); - } - Node root = getRootNode(); - String relPath = absPath.substring(1); - if (relPath.length() == 0) { + if (absPath.equals("/")) { return root; - } - - try { - return root.getNode(relPath); - } catch (PathNotFoundException e) { - return root.getProperty(relPath); + } else { + String relPath = toRelativePath(absPath); + if (root.hasNode(relPath)) { + return root.getNode(relPath); + } else { + return root.getProperty(relPath); + } } } @@ -399,17 +405,98 @@ * if a {@link PathNotFoundException} was thrown. Other exceptions are * passed through. * + * @see Session#itemExists(String) * @param absPath absolute path * @return true if an item exists at the given path, * false otherwise * @throws RepositoryException if an error occurs */ public boolean itemExists(String absPath) throws RepositoryException { - try { - getItem(absPath); + if (absPath.equals("/")) { + return true; + } else { + Node root = getRootNode(); + String relPath = toRelativePath(absPath); + return root.hasNode(relPath) || root.hasProperty(relPath); + } + } + + /** + * Removes the identified item. Implemented by calling + * {@link Item#remove()} on the item removed by {@link #getItem(String)}. + * + * @see Session#removeItem(String) + * @param absolute path of the item to be removed + * @throws RepositoryException if the item can not be removed + */ + public void removeItem(String absPath) throws RepositoryException { + getItem(absPath).remove(); + } + + /** + * Returns the node with the given absolute path. + * + * @see Session#getNode(String) + * @param absPath absolute path + * @return node at the given path + * @throws RepositoryException if the node can not be accessed + */ + public Node getNode(String absPath) throws RepositoryException { + Node root = getRootNode(); + if (absPath.equals("/")) { + return root; + } else { + return root.getNode(toRelativePath(absPath)); + } + } + + /** + * Checks whether a node with the given absolute path exists. + * + * @see Session#nodeExists(String) + * @param absPath absolute path + * @return true if a node with the given path exists, + * false otherwise + * @throws RepositoryException if the path is invalid + */ + public boolean nodeExists(String absPath) throws RepositoryException { + if (absPath.equals("/")) { return true; - } catch (PathNotFoundException e) { + } else { + return getRootNode().hasNode(toRelativePath(absPath)); + } + } + + /** + * Returns the property with the given absolute path. + * + * @see Session#getProperty(String) + * @param absPath absolute path + * @return node at the given path + * @throws RepositoryException if the property can not be accessed + */ + public Property getProperty(String absPath) throws RepositoryException { + if (absPath.equals("/")) { + throw new RepositoryException("The root node is not a property"); + } else { + return getRootNode().getProperty(toRelativePath(absPath)); + } + } + + /** + * Checks whether a property with the given absolute path exists. + * + * @see Session#propertyExists(String) + * @param absPath absolute path + * @return true if a property with the given path exists, + * false otherwise + * @throws RepositoryException if the path is invalid + */ + public boolean propertyExists(String absPath) throws RepositoryException { + if (absPath.equals("/")) { return false; + } else { + return getRootNode().hasProperty(toRelativePath(absPath)); } }