Search in sources :

Example 61 with NodeTypeIterator

use of javax.jcr.nodetype.NodeTypeIterator in project jackrabbit by apache.

the class AbstractNodeType method getSubtypes.

//--------------------------------------------------------------------------
/**
     * Returns the node types derived from this node type.
     *
     * @param directOnly if <code>true</code> only direct subtypes will be considered
     *
     * @return an <code>NodeTypeIterator</code>.
     * @see NodeType#getSubtypes
     * @see NodeType#getDeclaredSubtypes
     */
public NodeTypeIterator getSubtypes(boolean directOnly) {
    NodeTypeIterator iter;
    try {
        iter = ntMgr.getAllNodeTypes();
    } catch (RepositoryException e) {
        // should never get here
        log.error("failed to retrieve registered node types", e);
        return NodeTypeIteratorAdapter.EMPTY;
    }
    ArrayList<NodeType> result = new ArrayList<NodeType>();
    String thisName = getName();
    while (iter.hasNext()) {
        NodeType nt = iter.nextNodeType();
        if (!nt.getName().equals(thisName)) {
            if (directOnly) {
                // direct subtypes only
                for (String name : nt.getDeclaredSupertypeNames()) {
                    if (name.equals(thisName)) {
                        result.add(nt);
                        break;
                    }
                }
            } else {
                // direct and indirect subtypes
                if (nt.isNodeType(thisName)) {
                    result.add(nt);
                }
            }
        }
    }
    return new NodeTypeIteratorAdapter(result);
}
Also used : NodeType(javax.jcr.nodetype.NodeType) ArrayList(java.util.ArrayList) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator) RepositoryException(javax.jcr.RepositoryException) NodeTypeIteratorAdapter(org.apache.jackrabbit.commons.iterator.NodeTypeIteratorAdapter)

Example 62 with NodeTypeIterator

use of javax.jcr.nodetype.NodeTypeIterator in project jackrabbit by apache.

the class PropertyDefTest method testGetValueConstraints.

/**
     * Tests if value constraints match the pattern specified by the required
     * property type.
     * <p>
     * The test runs for all value constraints of all properties of all
     * available node types.
     */
public void testGetValueConstraints() throws RepositoryException {
    NodeTypeIterator types = manager.getAllNodeTypes();
    // loop all node types
    while (types.hasNext()) {
        NodeType type = types.nextNodeType();
        PropertyDefinition[] defs = type.getPropertyDefinitions();
        for (int i = 0; i < defs.length; i++) {
            PropertyDefinition def = defs[i];
            String[] constraints = def.getValueConstraints();
            if (constraints != null) {
                for (int j = 0; j < constraints.length; j++) {
                    Matcher matcher;
                    switch(defs[i].getRequiredType()) {
                        case PropertyType.STRING:
                        case PropertyType.UNDEFINED:
                            // any value matches
                            break;
                        case PropertyType.BINARY:
                            matcher = CONSTRAINTSPATTERN_BINARY.matcher(constraints[j]);
                            assertTrue("Value constraint does not match " + "the pattern of PropertyType.BINARY ", matcher.matches());
                            break;
                        case PropertyType.DATE:
                            matcher = CONSTRAINTSPATTERN_DATE.matcher(constraints[j]);
                            assertTrue("Value constraint does not match " + "the pattern of PropertyType.DATE ", matcher.matches());
                            break;
                        case PropertyType.LONG:
                            matcher = CONSTRAINTSPATTERN_LONG.matcher(constraints[j]);
                            assertTrue("Value constraint does not match " + "the pattern of PropertyType.LONG", matcher.matches());
                            break;
                        case PropertyType.DOUBLE:
                            matcher = CONSTRAINTSPATTERN_DOUBLE.matcher(constraints[j]);
                            assertTrue("Value constraint does not match " + "the pattern of PropertyType.DOUBLE", matcher.matches());
                            break;
                        case PropertyType.NAME:
                            matcher = PropertyUtil.PATTERN_NAME.matcher(constraints[j]);
                            assertTrue("Value constraint does not match " + "the pattern of PropertyType.NAME", matcher.matches());
                            checkPrefix(constraints[j]);
                            break;
                        case PropertyType.PATH:
                            matcher = CONSTRAINTSPATTERN_PATH.matcher(constraints[j]);
                            assertTrue("Value constraint does not match " + "the pattern of PropertyType.PATH", matcher.matches());
                            String[] elems = constraints[j].split("/");
                            for (int k = 0; k < elems.length; k++) {
                                checkPrefix(elems[k]);
                            }
                            break;
                        case PropertyType.REFERENCE:
                            matcher = PropertyUtil.PATTERN_NAME.matcher(constraints[j]);
                            assertTrue("Value constraint does not match " + "the pattern of PropertyType.REFERENCE", matcher.matches());
                            checkPrefix(constraints[j]);
                            break;
                        case PropertyType.BOOLEAN:
                            assertTrue("Value constraint does not match " + "the pattern of PropertyType.BOOLEAN", constraints[j].equals("true") || constraints[j].equals("false"));
                            break;
                    }
                }
            }
        }
    }
}
Also used : Matcher(java.util.regex.Matcher) NodeType(javax.jcr.nodetype.NodeType) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition)

Example 63 with NodeTypeIterator

use of javax.jcr.nodetype.NodeTypeIterator in project jackrabbit-oak by apache.

the class IndexDefinition method getAllNodeTypes.

private static List<String> getAllNodeTypes(ReadOnlyNodeTypeManager ntReg) {
    try {
        List<String> typeNames = newArrayList();
        NodeTypeIterator ntItr = ntReg.getAllNodeTypes();
        while (ntItr.hasNext()) {
            typeNames.add(ntItr.nextNodeType().getName());
        }
        return typeNames;
    } catch (RepositoryException e) {
        throw new RuntimeException(e);
    }
}
Also used : NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator) RepositoryException(javax.jcr.RepositoryException)

Example 64 with NodeTypeIterator

use of javax.jcr.nodetype.NodeTypeIterator in project jackrabbit-oak by apache.

the class NodeTypeIndexingUtils method createPrimaryTypeSynonymsFile.

public static File createPrimaryTypeSynonymsFile(String path, Session session) throws Exception {
    File file = new File(path);
    StringWriter stringWriter = new StringWriter();
    NodeTypeIterator allNodeTypes = session.getWorkspace().getNodeTypeManager().getAllNodeTypes();
    while (allNodeTypes.hasNext()) {
        NodeType nodeType = allNodeTypes.nextNodeType();
        NodeType[] superTypes = nodeType.getSupertypes();
        if (superTypes != null && superTypes.length > 0) {
            stringWriter.append(nodeType.getName()).append(" => ");
            for (int i = 0; i < superTypes.length; i++) {
                stringWriter.append(superTypes[i].getName());
                if (i < superTypes.length - 1) {
                    stringWriter.append(',');
                }
                stringWriter.append(' ');
            }
            stringWriter.append('\n');
        }
    }
    FileOutputStream fileOutputStream = new FileOutputStream(file);
    fileOutputStream.write(stringWriter.toString().getBytes("UTF-8"));
    fileOutputStream.flush();
    fileOutputStream.close();
    if (file.exists() || file.createNewFile()) {
        return file;
    } else {
        throw new IOException("primary types synonyms file could not be created");
    }
}
Also used : StringWriter(java.io.StringWriter) NodeType(javax.jcr.nodetype.NodeType) FileOutputStream(java.io.FileOutputStream) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator) IOException(java.io.IOException) File(java.io.File)

Example 65 with NodeTypeIterator

use of javax.jcr.nodetype.NodeTypeIterator in project sling by apache.

the class MockNodeTypeGenerator method setUp.

public void setUp() throws RepositoryException, IOException {
    // create mocks
    request = mock(SlingHttpServletRequest.class);
    response = mock(SlingHttpServletResponse.class);
    resource = mock(Resource.class);
    currentNode = mock(Node.class);
    session = mock(Session.class);
    workspace = mock(Workspace.class);
    ntManager = mock(NodeTypeManager.class);
    nodeTypeIterator = mock(NodeTypeIterator.class);
    outStream = new ByteArrayOutputStream();
    // stubbing
    when(request.getResource()).thenReturn(resource);
    when(request.getMethod()).thenReturn(HttpConstants.METHOD_GET);
    when(response.getWriter()).thenReturn(new PrintWriter(outStream, true));
    when(resource.adaptTo(Node.class)).thenReturn(currentNode);
    when(currentNode.getSession()).thenReturn(session);
    when(session.getWorkspace()).thenReturn(workspace);
    when(workspace.getNodeTypeManager()).thenReturn(ntManager);
    when(ntManager.getAllNodeTypes()).thenReturn(nodeTypeIterator);
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) Node(javax.jcr.Node) Resource(org.apache.sling.api.resource.Resource) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) Session(javax.jcr.Session) Workspace(javax.jcr.Workspace) PrintWriter(java.io.PrintWriter)

Aggregations

NodeTypeIterator (javax.jcr.nodetype.NodeTypeIterator)65 NodeType (javax.jcr.nodetype.NodeType)53 NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)28 PropertyDefinition (javax.jcr.nodetype.PropertyDefinition)15 NodeDefinition (javax.jcr.nodetype.NodeDefinition)13 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)11 Node (javax.jcr.Node)9 RepositoryException (javax.jcr.RepositoryException)9 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)8 ArrayList (java.util.ArrayList)6 Session (javax.jcr.Session)6 HashSet (java.util.HashSet)4 Value (javax.jcr.Value)3 QueryObjectModel (javax.jcr.query.qom.QueryObjectModel)3 NodeTypeIteratorAdapter (org.apache.jackrabbit.commons.iterator.NodeTypeIteratorAdapter)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 Property (javax.jcr.Property)2