Search in sources :

Example 26 with NodeIterator

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

the class ItemSequenceTest method checkTreeProperty.

private static void checkTreeProperty(Node root, int minChildren, int maxChildren, Comparator<String> order) throws RepositoryException {
    int depth = -1;
    for (Node node : new TreeTraverser(root)) {
        String parentName = node.getName();
        if (node.hasNodes()) {
            int childCount = 0;
            for (NodeIterator nodes = node.getNodes(); nodes.hasNext(); ) {
                Node child = nodes.nextNode();
                childCount++;
                if (!root.isSame(node)) {
                    assertTrue("Mismatching order: node " + node + " contains child " + child, order.compare(parentName, child.getName()) <= 0);
                }
            }
            if (!root.isSame(node)) {
                assertTrue("Node " + node + " should have at least " + minChildren + " child nodes", minChildren <= childCount);
            }
            assertTrue("Node " + node + " should have no more than " + maxChildren + " child nodes", maxChildren >= childCount);
        } else {
            if (depth == -1) {
                depth = node.getDepth();
            } else {
                assertEquals("Node " + node + " has depth " + node.getDepth() + " instead of " + depth, depth, node.getDepth());
            }
            int propCount = 0;
            for (PropertyIterator properties = node.getProperties(); properties.hasNext(); ) {
                Property property = properties.nextProperty();
                String propertyName = property.getName();
                if (!JcrConstants.JCR_PRIMARYTYPE.equals(propertyName)) {
                    propCount++;
                    assertTrue("Mismatching order: node " + node + " contains property " + property, order.compare(parentName, propertyName) <= 0);
                }
            }
            if (propCount > 0) {
                assertTrue("Node " + node + " should have at least " + minChildren + " properties", minChildren <= propCount);
                assertTrue("Node" + node + " should have no more than " + maxChildren + " properties", maxChildren >= propCount);
            }
        }
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) Node(javax.jcr.Node) TreeTraverser(org.apache.jackrabbit.commons.flat.TreeTraverser) PropertyIterator(javax.jcr.PropertyIterator) Property(javax.jcr.Property)

Example 27 with NodeIterator

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

the class AccessControlImporterTest method createPolicyNode.

private NodeImpl createPolicyNode(NodeImpl target) throws Exception {
    try {
        InputStream in = new ByteArrayInputStream(XML_POLICY_ONLY.getBytes("UTF-8"));
        SessionImporter importer = new SessionImporter(target, sImpl, ImportUUIDBehavior.IMPORT_UUID_COLLISION_THROW);
        ImportHandler ih = new ImportHandler(importer, sImpl);
        new ParsingContentHandler(ih).parse(in);
        return (NodeImpl) target.getNode("test/rep:policy");
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    } finally {
        superuser.refresh(false);
        if (superuser.nodeExists("/test")) {
            NodeIterator it = superuser.getRootNode().getNodes("test");
            while (it.hasNext()) {
                it.nextNode().remove();
            }
        }
        superuser.save();
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) NodeImpl(org.apache.jackrabbit.core.NodeImpl) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ParsingContentHandler(org.apache.jackrabbit.commons.xml.ParsingContentHandler) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) NotExecutableException(org.apache.jackrabbit.test.NotExecutableException)

Example 28 with NodeIterator

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

the class ExportDocViewTest method checkChildNodes.

/**
     * Checks the child nodes of the given node against the child nodes of the
     * given xml element. The found text nodes of the xml element are hold in an
     * ArrayList and put on a stack for further checking if another child
     * element is between them.
     *
     * @param node
     * @param elem
     * @throws RepositoryException
     */
private void checkChildNodes(Node node, Element elem) throws RepositoryException, IOException {
    NodeIterator nodeIter = node.getNodes();
    if (getSize(node.getNodes()) == 0) {
        assertTrue("Exported node " + node.getPath() + " has child elements " + "although it has no child nodes ", 0 == countChildElems(elem));
    } else {
        // create a stack entry for the text child nodes
        // of the current xml element
        StackEntry entry = new StackEntry();
        entry.textValues = getChildTextNodeValues(elem);
        textValuesStack.push(entry);
        // xmltext nodes directly following each other
        // are serialized together as xml text
        List<Node> jcrTextNodes = new ArrayList<Node>();
        while (nodeIter.hasNext()) {
            Node childNode = nodeIter.nextNode();
            if (isXMLTextNode(childNode)) {
                jcrTextNodes.add(childNode);
            } else {
                if (jcrTextNodes.size() > 0) {
                    compareXmltextNodes(jcrTextNodes, elem);
                    // reset the Array
                    jcrTextNodes.clear();
                }
                compareChildTree(childNode, elem);
            }
        }
        // finally we are through the child nodes
        // so we delete the stackEntry
        textValuesStack.pop();
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) Node(javax.jcr.Node) ArrayList(java.util.ArrayList)

Example 29 with NodeIterator

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

the class ExportDocViewTest method setExportInvalidXmlNames.

/**
     * Loops through all child items of a given node to test if items with
     * invalid xml name are exported. (chapter 6.4.2.4 of the JCR
     * specification).
     *
     * @param node the root node of the tree to search
     * @param elem the parent element of the element to which the parent node of
     *             the given node is exported.
     * @throws RepositoryException
     */
private boolean setExportInvalidXmlNames(Node node, Element elem, boolean isSet) throws RepositoryException {
    if (!XMLChar.isValidName(node.getName())) {
        if (elem != null) {
            exportInvalidXmlNames = true;
            isSet = true;
        } else {
            exportInvalidXmlNames = false;
            isSet = true;
        }
    }
    // try properties
    if (!isSet) {
        PropertyIterator iter = node.getProperties();
        while (iter.hasNext()) {
            Property prop = iter.nextProperty();
            if (!exportMultivalProps && prop.getDefinition().isMultiple()) {
                continue;
            }
            if (!XMLChar.isValidName(prop.getName())) {
                // property exported?
                exportInvalidXmlNames = isExportedProp(prop, elem);
                isSet = true;
            }
        }
    }
    // try child nodes
    if (!isSet && !noRecurse) {
        // search again
        NodeIterator iter = node.getNodes();
        while (iter.hasNext()) {
            Node n = iter.nextNode();
            Element el = findElem(n, elem);
            isSet = setExportInvalidXmlNames(n, el, isSet);
        }
    }
    return isSet;
}
Also used : NodeIterator(javax.jcr.NodeIterator) Node(javax.jcr.Node) Element(org.w3c.dom.Element) PropertyIterator(javax.jcr.PropertyIterator) Property(javax.jcr.Property)

Example 30 with NodeIterator

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

the class ExportDocViewTest method setExportMultivalProps.

/**
     * Set the exportMultivalProps flag. Traverses the tree given by the node
     * and searches a multivalue property which is exported to an attribute of a
     * element of an element tree. (chapter 6.4.2.5 of the JCR specification).
     *
     * @param node
     * @param elem
     * @throws RepositoryException
     */
private boolean setExportMultivalProps(Node node, Element elem, boolean isSet) throws RepositoryException {
    Property[] props = searchMultivalProps(node);
    // multivalued property with valid xml name
    if (props[0] != null) {
        exportMultivalProps = isExportedProp(props[0], elem);
        isSet = true;
    } else {
        // invalid xml named multivalue property exported
        if (props[1] != null) {
            exportMultivalProps = isExportedProp(props[1], elem);
            if (!exportMultivalProps && exportInvalidXmlNames) {
                isSet = true;
            }
        }
    }
    if (!isSet && !noRecurse) {
        // search again
        NodeIterator iter = node.getNodes();
        while (iter.hasNext()) {
            Node n = iter.nextNode();
            Element el = findElem(n, elem);
            if (el != null) {
                isSet = setExportMultivalProps(n, el, isSet);
            } else {
                isSet = false;
            }
        }
    }
    return isSet;
}
Also used : NodeIterator(javax.jcr.NodeIterator) Node(javax.jcr.Node) Element(org.w3c.dom.Element) Property(javax.jcr.Property)

Aggregations

NodeIterator (javax.jcr.NodeIterator)307 Node (javax.jcr.Node)214 Session (javax.jcr.Session)55 QueryResult (javax.jcr.query.QueryResult)52 RepositoryException (javax.jcr.RepositoryException)40 Query (javax.jcr.query.Query)40 Test (org.junit.Test)36 QueryManager (javax.jcr.query.QueryManager)34 PropertyIterator (javax.jcr.PropertyIterator)30 ArrayList (java.util.ArrayList)26 Property (javax.jcr.Property)24 Version (javax.jcr.version.Version)23 NoSuchElementException (java.util.NoSuchElementException)19 Value (javax.jcr.Value)19 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)19 HashSet (java.util.HashSet)13 PathNotFoundException (javax.jcr.PathNotFoundException)12 JackrabbitSession (org.apache.jackrabbit.api.JackrabbitSession)11 NodeImpl (org.apache.jackrabbit.core.NodeImpl)11 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)11