Search in sources :

Example 26 with Item

use of javax.jcr.Item in project jackrabbit by apache.

the class FilteringItemVisitor method visit.

/**
 * Called when the Visitor is passed to a <code>Node</code>.
 * <p>
 * It calls <code>TraversingItemVisitor.entering(Node, int)</code> followed by
 * <code>TraversingItemVisitor.leaving(Node, int)</code>. Implement these abstract methods to
 * specify behaviour on 'arrival at' and 'after leaving' the <code>Node</code>.
 * <p>
 * If this method throws, the visiting process is aborted.
 *
 * @param node the <code>Node</code> that is accepting this visitor.
 * @throws RepositoryException if an error occurrs
 */
public void visit(Node node) throws RepositoryException {
    if (this.traversalPredicate.evaluate(node)) {
        if (this.includePredicate == this.traversalPredicate || this.includePredicate.evaluate(node)) {
            try {
                if (!breadthFirst) {
                    // depth-first traversal
                    entering(node, currentLevel);
                    if (maxLevel == -1 || currentLevel < maxLevel) {
                        currentLevel++;
                        if (this.walkProperties) {
                            PropertyIterator propIter = node.getProperties();
                            while (propIter.hasNext()) {
                                propIter.nextProperty().accept(this);
                            }
                        }
                        NodeIterator nodeIter = node.getNodes();
                        while (nodeIter.hasNext()) {
                            nodeIter.nextNode().accept(this);
                        }
                        currentLevel--;
                    }
                    leaving(node, currentLevel);
                } else {
                    // breadth-first traversal
                    entering(node, currentLevel);
                    leaving(node, currentLevel);
                    if (maxLevel == -1 || currentLevel < maxLevel) {
                        if (this.walkProperties) {
                            PropertyIterator propIter = node.getProperties();
                            while (propIter.hasNext()) {
                                nextQueue.addLast(propIter.nextProperty());
                            }
                        }
                        NodeIterator nodeIter = node.getNodes();
                        while (nodeIter.hasNext()) {
                            nextQueue.addLast(nodeIter.nextNode());
                        }
                    }
                    while (!currentQueue.isEmpty() || !nextQueue.isEmpty()) {
                        if (currentQueue.isEmpty()) {
                            currentLevel++;
                            currentQueue = nextQueue;
                            nextQueue = new LinkedList();
                        }
                        Item e = (Item) currentQueue.removeFirst();
                        e.accept(this);
                    }
                    currentLevel = 0;
                }
            } catch (RepositoryException re) {
                currentLevel = 0;
                throw re;
            }
        }
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) Item(javax.jcr.Item) PropertyIterator(javax.jcr.PropertyIterator) RepositoryException(javax.jcr.RepositoryException) LinkedList(java.util.LinkedList)

Example 27 with Item

use of javax.jcr.Item in project jackrabbit by apache.

the class GetOrNullTest method testGetItemOrNullNonExistingNode.

public void testGetItemOrNullNonExistingNode() throws RepositoryException {
    JackrabbitSession js = (JackrabbitSession) superuser;
    Item item = js.getItemOrNull(PATH_NON_EXISTING_NODE);
    assertNull(item);
}
Also used : Item(javax.jcr.Item) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession)

Example 28 with Item

use of javax.jcr.Item in project jackrabbit by apache.

the class GetOrNullTest method testGetItemOrNullExistingProperty.

public void testGetItemOrNullExistingProperty() throws RepositoryException {
    JackrabbitSession js = (JackrabbitSession) superuser;
    Item item = js.getItemOrNull(PATH_EXISTING_PROPERTY);
    assertNotNull(item);
    assertTrue(item instanceof Property);
    assertEquals(item.getPath(), PATH_EXISTING_PROPERTY);
}
Also used : Item(javax.jcr.Item) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession) Property(javax.jcr.Property)

Example 29 with Item

use of javax.jcr.Item in project jackrabbit by apache.

the class LocateCorrespondingNodeReport method getCorrespondingResourceHref.

/**
 * Retrieve the href of the corresponding resource in the indicated workspace.
 *
 * @param resource
 * @param session Session object used to access the {@link Node} object
 * represented by the given resource.
 * @param workspaceHref
 * @return
 * @throws RepositoryException
 */
private static String getCorrespondingResourceHref(DavResource resource, Session session, String workspaceHref) throws RepositoryException {
    DavResourceLocator rLoc = resource.getLocator();
    String itemPath = rLoc.getRepositoryPath();
    Item item = session.getItem(itemPath);
    if (item.isNode()) {
        String workspaceName = rLoc.getFactory().createResourceLocator(rLoc.getPrefix(), workspaceHref).getWorkspaceName();
        String corrPath = ((Node) item).getCorrespondingNodePath(workspaceName);
        DavResourceLocator corrLoc = rLoc.getFactory().createResourceLocator(rLoc.getPrefix(), "/" + workspaceName, corrPath, false);
        return corrLoc.getHref(true);
    } else {
        throw new PathNotFoundException("Node with path " + itemPath + " does not exist.");
    }
}
Also used : Item(javax.jcr.Item) Node(javax.jcr.Node) PathNotFoundException(javax.jcr.PathNotFoundException) DavResourceLocator(org.apache.jackrabbit.webdav.DavResourceLocator)

Example 30 with Item

use of javax.jcr.Item in project jackrabbit by apache.

the class ResourceFactoryImpl method getNode.

/**
 * Returns the <code>Node</code> corresponding to the given locator or
 * <code>null</code> if it does not exist or if the existing item represents
 * a <code>Property</code>.
 *
 * @param sessionImpl
 * @param locator
 * @return
 * @throws RepositoryException
 */
private Node getNode(DavSession sessionImpl, DavResourceLocator locator) throws RepositoryException {
    Node node = null;
    try {
        String repoPath = locator.getRepositoryPath();
        if (repoPath != null) {
            Session session = ((JcrDavSession) sessionImpl).getRepositorySession();
            Item item = session.getItem(repoPath);
            if (item instanceof Node) {
                node = (Node) item;
            }
        // else: item is a property -> return null
        }
    } catch (PathNotFoundException e) {
    // item does not exist (yet). return null -> create null-resource
    }
    return node;
}
Also used : Item(javax.jcr.Item) Node(javax.jcr.Node) JcrDavSession(org.apache.jackrabbit.webdav.jcr.JcrDavSession) PathNotFoundException(javax.jcr.PathNotFoundException) JcrDavSession(org.apache.jackrabbit.webdav.jcr.JcrDavSession) Session(javax.jcr.Session) DavSession(org.apache.jackrabbit.webdav.DavSession)

Aggregations

Item (javax.jcr.Item)138 Node (javax.jcr.Node)61 RepositoryException (javax.jcr.RepositoryException)34 Session (javax.jcr.Session)26 Property (javax.jcr.Property)24 PathNotFoundException (javax.jcr.PathNotFoundException)20 ArrayList (java.util.ArrayList)7 JcrCallback (org.springframework.extensions.jcr.JcrCallback)7 PrintWriter (java.io.PrintWriter)6 ItemNotFoundException (javax.jcr.ItemNotFoundException)6 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)6 IOException (java.io.IOException)5 HashSet (java.util.HashSet)5 PropertyIterator (javax.jcr.PropertyIterator)5 ValueFormatException (javax.jcr.ValueFormatException)5 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)5 JackrabbitSession (org.apache.jackrabbit.api.JackrabbitSession)5 MetadataAccessException (com.thinkbiganalytics.metadata.api.MetadataAccessException)4 MetadataExecutionException (com.thinkbiganalytics.metadata.api.MetadataExecutionException)4 JcrPath (com.thinkbiganalytics.metadata.modeshape.support.JcrPath)4