Java Code Examples for javax.jcr.Node#hasNodes()

The following examples show how to use javax.jcr.Node#hasNodes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JcrPackageRegistry.java    From jackrabbit-filevault with Apache License 2.0 6 votes vote down vote up
/**
 * internally adds the packages below {@code root} to the given list
 * recursively.
 *
 * @param root root node
 * @param packages list for the packages
 * @throws RepositoryException if an error occurs
 */
private void listPackages(Node root, Set<PackageId> packages) throws RepositoryException {
    for (NodeIterator iter = root.getNodes(); iter.hasNext();) {
        Node child = iter.nextNode();
        if (".snapshot".equals(child.getName())) {
            continue;
        }
        try (JcrPackageImpl pack = new JcrPackageImpl(this, child)) {
            if (pack.isValid()) {
                // skip packages with illegal names
                JcrPackageDefinition jDef = pack.getDefinition();
                if (jDef == null || !jDef.getId().isValid()) {
                    continue;
                }
                packages.add(jDef.getId());
            } else if (child.hasNodes()) {
                listPackages(child, packages);
            }
        }
    }
}
 
Example 2
Source File: JcrPackageManagerImplTest.java    From jackrabbit-filevault with Apache License 2.0 6 votes vote down vote up
private String getNextPath(String path) throws RepositoryException {
    Node currentNode = admin.getNode(path);
    if (currentNode.hasNodes()) {
        NodeIterator nodes = currentNode.getNodes();
        while (nodes.hasNext()) {
            Node node = nodes.nextNode();
            if ("jcr:system".equals(node.getName())) {
                continue;
            }
            String nodePath = node.getPath();
            if (visitedPaths.contains(nodePath)) {
                continue;
            } else {
                visitedPaths.add(nodePath);
            }
            return nodePath;
        }
        return getParentPath(path);
    } else {
        return getParentPath(path);
    }
}
 
Example 3
Source File: JcrMetadataRepository.java    From archiva with Apache License 2.0 6 votes vote down vote up
@Override
public void removeMetadataFacet(RepositorySession session, String repositoryId, String facetId, String name)
        throws MetadataRepositoryException {
    final Session jcrSession = getSession(session);
    try {
        Node root = jcrSession.getRootNode();
        String path = getFacetPath(repositoryId, facetId, name);
        if (root.hasNode(path)) {
            Node node = root.getNode(path);
            do {
                // also remove empty container nodes
                Node parent = node.getParent();
                node.remove();
                node = parent;
            }
            while (!node.hasNodes());
        }
    } catch (RepositoryException e) {
        throw new MetadataRepositoryException(e.getMessage(), e);
    }
}
 
Example 4
Source File: JcrStorageDao.java    From nextreports-server with Apache License 2.0 6 votes vote down vote up
public Entity[] getEntityChildren(String path) throws NotFoundException {
	checkPath(path);

	Node node = getNode(path);
	try {
		if (!node.hasNodes()) {
			return new Entity[0];
		}

		List<Entity> entities = new ArrayList<Entity>();
		NodeIterator nodes = node.getNodes();
		while (nodes.hasNext()) {
			Entity entity = getEntity(nodes.nextNode());
			if (entity != null) {
				entities.add(entity);
			}
		}

		return entities.toArray(new Entity[entities.size()]);
	} catch (RepositoryException e) {
		throw convertJcrAccessException(e);
	}
}
 
Example 5
Source File: JcrStorageDao.java    From nextreports-server with Apache License 2.0 6 votes vote down vote up
public int countEntityChildrenById(String id) throws NotFoundException {
	Node node = checkId(id);
	try {
		if (!node.hasNodes()) {
			return 0;
		}

		NodeIterator nodes = node.getNodes();
		// TODO it's OK ?
		return (int) nodes.getSize();
		/*
		 * int count = 0; while (nodes.hasNext()) { Node nextNode =
		 * nodes.nextNode(); if (isEntityNode(nextNode)) {; count++; } }
		 * 
		 * return count;
		 */
	} catch (RepositoryException e) {
		throw convertJcrAccessException(e);
	}
}
 
Example 6
Source File: FileArtifactHandler.java    From jackrabbit-filevault with Apache License 2.0 5 votes vote down vote up
/**
 * Checks if the given node is a nt_resource like structure that was modified. this is to test if a single
 * file artifact needs to recreate existing content of a sub-typed jcr:content node. see JCRVLT-177
 *
 * @param content the content node
 * @return {@code true} if modified
 * @throws RepositoryException if an error occurrs
 */
private boolean isModifiedNtResource(Node content) throws RepositoryException {
    if (content.getMixinNodeTypes().length > 0) {
        return true;
    }
    if (content.isNodeType(NodeType.NT_RESOURCE)) {
        return false;
    }
    // allow nt:unstructured with no child nodes
    return content.hasNodes();
}
 
Example 7
Source File: JcrPackageManagerImpl.java    From jackrabbit-filevault with Apache License 2.0 5 votes vote down vote up
/**
 * internally adds the packages below {@code root} to the given list
 * recursively.
 *
 * @param root root node
 * @param packages list for the packages
 * @param filter optional filter to filter out packages
 * @param built if {@code true} only packages with size > 0 are returned
 * @param shallow if {@code true} don't recurs
 * @throws RepositoryException if an error occurs
 */
private void listPackages(Node root, List<JcrPackage> packages,
                          WorkspaceFilter filter, boolean built, boolean shallow)
        throws RepositoryException {
    if (root != null) {
        for (NodeIterator iter = root.getNodes(); iter.hasNext();) {
            Node child = iter.nextNode();
            if (".snapshot".equals(child.getName())) {
                continue;
            }
            JcrPackageImpl pack = new JcrPackageImpl(registry, child);
            if (pack.isValid()) {
                // skip packages with illegal names
                JcrPackageDefinition jDef = pack.getDefinition();
                if (jDef != null && !jDef.getId().isValid()) {
                    continue;
                }
                if (filter == null || filter.contains(child.getPath())) {
                    if (!built || pack.getSize() > 0) {
                        packages.add(pack);
                    }
                }
            } else if (child.hasNodes() && !shallow){
                listPackages(child, packages, filter, built, false);
            }
        }
    }
}
 
Example 8
Source File: JcrMetadataRepository.java    From archiva with Apache License 2.0 5 votes vote down vote up
private void recurse(List<String> facets, String prefix, Node node)
        throws RepositoryException {
    for (Node n : JcrUtils.getChildNodes(node)) {
        String name = prefix + "/" + n.getName();
        if (n.hasNodes()) {
            recurse(facets, name, n);
        } else {
            // strip leading / first
            facets.add(name.substring(1));
        }
    }
}
 
Example 9
Source File: JcrStorageDao.java    From nextreports-server with Apache License 2.0 5 votes vote down vote up
public Entity[] getBaseEntityChildren(String path) throws NotFoundException {
	checkPath(path);

	Node node = getNode(path);
	try {
		if (!node.hasNodes()) {
			return new Entity[0];
		}

		List<Entity> entities = new ArrayList<Entity>();
		NodeIterator nodes = node.getNodes();
		while (nodes.hasNext()) {
			Node child = nodes.nextNode();
			if (child.getName().endsWith("_history")) {
				continue;
			}
			Entity entity = getEntity(child);
			if (entity != null) {
				entities.add(entity);
			}

		}

		return entities.toArray(new Entity[entities.size()]);
	} catch (RepositoryException e) {
		throw convertJcrAccessException(e);
	}
}
 
Example 10
Source File: SessionWrapper.java    From sling-whiteboard with Apache License 2.0 4 votes vote down vote up
public boolean hasNodes(Node node) throws RepositoryException {
    return node.hasNodes();
}
 
Example 11
Source File: JcrStorageDao.java    From nextreports-server with Apache License 2.0 4 votes vote down vote up
public Entity[] getEntityChildrenById(String id, long firstResult, long maxResults) throws NotFoundException {
	// long s1 = System.currentTimeMillis();
	// boolean sortable = true;
	Node node = checkId(id);
	try {
		if (!node.hasNodes()) {
			return new Entity[0];
		}

		List<Entity> entities = new ArrayList<Entity>();
		// System.out.println("---------->");
		// StopWatch watch = new StopWatch();
		// watch.start();

		NodeIterator nodes = node.getNodes();
		// String path = node.getPath();
		// String statement = "/jcr:root" + ISO9075.encodePath(path) +
		// "//*/)";
		// if (sortByName) {
		// statement.concat(" ").concat("order by jcr:name ascending");
		// }
		// System.out.println(">>> " + statement);
		// QueryResult queryResult = getTemplate().query(statement);
		// NodeIterator nodes = queryResult.getNodes();

		// TreeSet<Node> set = new TreeSet<Node>(new Comparator<Node>() {
		// @Override
		// public int compare(Node n1, Node n2) {
		// try {
		// String name1 = n1.getName();
		// String name2 = n2.getName();
		// return name1.compareTo(name2);
		// } catch (RepositoryException ex) {
		// ex.printStackTrace();
		// return 0;
		// }
		// }
		//
		// });
		// while (nodes.hasNext()) {
		// Node nextNode = nodes.nextNode();
		// set.add(nextNode);
		// }
		//
		// Iterator<Node> it;
		// if (sortable) {
		// it = set.iterator();
		// } else {
		// it = nodes;
		// }

		NodeIterator it = nodes;

		int position = 0;
		if (firstResult > 0) {
			while (position < firstResult) {
				it.next();
				position++;
			}
			// nodes.skip(firstResult);
		}
		int counter = 0;
		while (it.hasNext()) {
			if (counter == maxResults) {
				break;
			}
			Node nextNode = it.nextNode();
			// watch.suspend();
			Entity entity = getEntity(nextNode);
			if (entity != null) {
				entities.add(entity);
				counter++;
			}
			// watch.resume();
		}
		// watch.stop();
		// System.out.println("t = " + watch.getTime() + " ms");
		// System.out.println("< ---------");
		// return entities.toArray(new Entity[entities.size()]);

		// System.out.println("--> ended in " +
		// (System.currentTimeMillis()-s1) + " ms");
		return entities.toArray(new Entity[entities.size()]);
	} catch (RepositoryException e) {
		throw convertJcrAccessException(e);
	}
}