Search in sources :

Example 66 with NodeType

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

the class NodeDiscoveringNodeTypesTest method testGetPrimaryNodeType.

/**
 * Test if getPrimaryNodeType() returns the node type according to the
 * property "jcr:primaryType"
 */
public void testGetPrimaryNodeType() throws NotExecutableException, RepositoryException {
    if (childNode == null) {
        throw new NotExecutableException("Workspace does not have sufficient content for this test. " + "Root node must have at least one child node.");
    }
    NodeType type = childNode.getPrimaryNodeType();
    String name = childNode.getProperty(jcrPrimaryType).getString();
    assertEquals("getPrimaryNodeType() must return the node type stored " + "as property \"jcr:primaryType\"", name, type.getName());
    assertFalse("getPrimaryNodeType() must return a primary node type", type.isMixin());
}
Also used : NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) NodeType(javax.jcr.nodetype.NodeType)

Example 67 with NodeType

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

the class NodeDiscoveringNodeTypesTest method testIsNodeType.

/**
 * Test if isNodeTye(String nodeTypeName) returns true if nodeTypeName is the
 * name of the primary node type, the name of a mixin node type and the name
 * of a supertype.
 */
public void testIsNodeType() throws NotExecutableException, RepositoryException {
    String nodeTypeName;
    // test with primary node's name
    nodeTypeName = testRootNode.getPrimaryNodeType().getName();
    assertTrue("isNodeType(String nodeTypeName) must return true if " + "nodeTypeName is the name of the primary node type", testRootNode.isNodeType(nodeTypeName));
    String expNodeTypeName = getQualifiedName(testRootNode.getSession(), nodeTypeName);
    assertTrue("isNodeType(String expNodeTypeName) must return true if " + "expNodeTypeName is the name of the primary node type", testRootNode.isNodeType(expNodeTypeName));
    // test with mixin node's name
    // (if such a node is available)
    Node nodeWithMixin = locateNodeWithMixinNodeTypes(testRootNode);
    if (nodeWithMixin != null) {
        NodeType[] types = nodeWithMixin.getMixinNodeTypes();
        nodeTypeName = types[0].getName();
        expNodeTypeName = getQualifiedName(testRootNode.getSession(), nodeTypeName);
        assertTrue("isNodeType(String nodeTypeName) must return true if " + "nodeTypeName is the name of one of the " + "mixin node types", nodeWithMixin.isNodeType(nodeTypeName));
        assertTrue("isNodeType(String expNodeTypeName) must return true if " + "expNodeTypeName is the name of one of the " + "mixin node types", nodeWithMixin.isNodeType(expNodeTypeName));
    }
    // test with the name of predefined supertype "nt:base"
    assertTrue("isNodeType(String nodeTypeName) must return true if " + "nodeTypeName is the name of a node type of a supertype", testRootNode.isNodeType(ntBase));
    assertTrue("isNodeType(String nodeTypeName) must return true if " + "nodeTypeName is the name of a node type of a supertype", testRootNode.isNodeType(NodeType.NT_BASE));
}
Also used : Node(javax.jcr.Node) NodeType(javax.jcr.nodetype.NodeType)

Example 68 with NodeType

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

the class EventFilter method blocks.

/**
 * Returns <code>true</code> if this <code>EventFilter</code> does not allow
 * the specified <code>EventState</code>; <code>false</code> otherwise.
 *
 * @param eventState the <code>EventState</code> in question.
 * @return <code>true</code> if this <code>EventFilter</code> blocks the
 *         <code>EventState</code>.
 * @throws RepositoryException if an error occurs while checking.
 */
boolean blocks(EventState eventState) throws RepositoryException {
    // first do cheap checks
    // check event type
    long type = eventState.getType();
    if ((eventTypes & type) == 0) {
        return true;
    }
    // check for session local changes
    if (noLocal && session.equals(eventState.getSession())) {
        // listener does not wish to get local events
        return true;
    }
    if (noExternal && eventState.isExternal()) {
        return true;
    }
    if (noInternal && !eventState.isExternal()) {
        return true;
    }
    // UUIDs, types, and paths do not need to match for persist
    if (eventState.getType() == Event.PERSIST) {
        return false;
    }
    // check UUIDs
    NodeId parentId = eventState.getParentId();
    if (ids != null) {
        boolean match = false;
        for (int i = 0; i < ids.length && !match; i++) {
            match |= parentId.equals(ids[i]);
        }
        if (!match) {
            return true;
        }
    }
    // check node types
    if (nodeTypes != null) {
        Set<NodeType> eventTypes = eventState.getNodeTypes(session.getNodeTypeManager());
        boolean match = false;
        for (int i = 0; i < nodeTypes.length && !match; i++) {
            for (NodeType eventType : eventTypes) {
                NodeTypeImpl nodeType = (NodeTypeImpl) eventType;
                match |= nodeType.getQName().equals(nodeTypes[i].getQName()) || nodeType.isDerivedFrom(nodeTypes[i].getQName());
            }
        }
        if (!match) {
            return true;
        }
    }
    // finally check paths
    Path eventPath = eventState.getParentPath();
    boolean match = false;
    for (Path path : paths) {
        if (eventPath.equals(path) || isDeep && eventPath.isDescendantOf(path)) {
            match = true;
            break;
        }
    }
    return !match;
}
Also used : Path(org.apache.jackrabbit.spi.Path) NodeTypeImpl(org.apache.jackrabbit.core.nodetype.NodeTypeImpl) NodeType(javax.jcr.nodetype.NodeType) NodeId(org.apache.jackrabbit.core.id.NodeId)

Example 69 with NodeType

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

the class NodeTypeManagerImpl method registerNodeTypes.

/**
 * Registers the node types defined in the given input stream depending
 * on the content type specified for the stream. This will also register
 * any namespaces identified in the input stream if they have not already
 * been registered.
 *
 * @param in node type XML stream
 * @param contentType type of the input stream
 * @param reregisterExisting flag indicating whether node types should be
 *                           reregistered if they already exist
 * @return registered node types
 * @throws IOException if the input stream could not be read or parsed
 * @throws RepositoryException if the node types are invalid or another
 *                             repository error occurs
 */
public NodeType[] registerNodeTypes(InputStream in, String contentType, boolean reregisterExisting) throws IOException, RepositoryException {
    // make sure the editing session is allowed to register node types.
    context.getAccessManager().checkRepositoryPermission(Permission.NODE_TYPE_DEF_MNGMT);
    try {
        Map<String, String> namespaceMap = new HashMap<String, String>();
        List<QNodeTypeDefinition> nodeTypeDefs = new ArrayList<QNodeTypeDefinition>();
        if (contentType.equalsIgnoreCase(TEXT_XML) || contentType.equalsIgnoreCase(APPLICATION_XML)) {
            try {
                NodeTypeReader ntr = new NodeTypeReader(in);
                Properties namespaces = ntr.getNamespaces();
                if (namespaces != null) {
                    Enumeration<?> prefixes = namespaces.propertyNames();
                    while (prefixes.hasMoreElements()) {
                        String prefix = (String) prefixes.nextElement();
                        String uri = namespaces.getProperty(prefix);
                        namespaceMap.put(prefix, uri);
                    }
                }
                QNodeTypeDefinition[] defs = ntr.getNodeTypeDefs();
                nodeTypeDefs.addAll(Arrays.asList(defs));
            } catch (NameException e) {
                throw new RepositoryException("Illegal JCR name", e);
            }
        } else if (contentType.equalsIgnoreCase(TEXT_X_JCR_CND)) {
            try {
                NamespaceMapping mapping = new NamespaceMapping(context.getSessionImpl());
                CompactNodeTypeDefReader<QNodeTypeDefinition, NamespaceMapping> reader = new CompactNodeTypeDefReader<QNodeTypeDefinition, NamespaceMapping>(new InputStreamReader(in), "cnd input stream", mapping, new QDefinitionBuilderFactory());
                namespaceMap.putAll(mapping.getPrefixToURIMapping());
                for (QNodeTypeDefinition ntDef : reader.getNodeTypeDefinitions()) {
                    nodeTypeDefs.add(ntDef);
                }
            } catch (ParseException e) {
                IOException e2 = new IOException(e.getMessage());
                e2.initCause(e);
                throw e2;
            }
        } else {
            throw new UnsupportedRepositoryOperationException("Unsupported content type: " + contentType);
        }
        new NamespaceHelper(context.getSessionImpl()).registerNamespaces(namespaceMap);
        if (reregisterExisting) {
            NodeTypeRegistry registry = context.getNodeTypeRegistry();
            // split the node types into new and already registered node types.
            // this way we can register new node types together with already
            // registered node types which make circular dependencies possible
            List<QNodeTypeDefinition> newNodeTypeDefs = new ArrayList<QNodeTypeDefinition>();
            List<QNodeTypeDefinition> registeredNodeTypeDefs = new ArrayList<QNodeTypeDefinition>();
            for (QNodeTypeDefinition nodeTypeDef : nodeTypeDefs) {
                if (registry.isRegistered(nodeTypeDef.getName())) {
                    registeredNodeTypeDefs.add(nodeTypeDef);
                } else {
                    newNodeTypeDefs.add(nodeTypeDef);
                }
            }
            ArrayList<NodeType> nodeTypes = new ArrayList<NodeType>();
            // register new node types
            nodeTypes.addAll(registerNodeTypes(newNodeTypeDefs));
            // re-register already existing node types
            for (QNodeTypeDefinition nodeTypeDef : registeredNodeTypeDefs) {
                registry.reregisterNodeType(nodeTypeDef);
                nodeTypes.add(getNodeType(nodeTypeDef.getName()));
            }
            return nodeTypes.toArray(new NodeType[nodeTypes.size()]);
        } else {
            Collection<NodeType> types = registerNodeTypes(nodeTypeDefs);
            return types.toArray(new NodeType[types.size()]);
        }
    } catch (InvalidNodeTypeDefException e) {
        throw new RepositoryException("Invalid node type definition", e);
    }
}
Also used : UnsupportedRepositoryOperationException(javax.jcr.UnsupportedRepositoryOperationException) HashMap(java.util.HashMap) NamespaceMapping(org.apache.jackrabbit.spi.commons.namespace.NamespaceMapping) ArrayList(java.util.ArrayList) Properties(java.util.Properties) QNodeTypeDefinition(org.apache.jackrabbit.spi.QNodeTypeDefinition) NamespaceHelper(org.apache.jackrabbit.commons.NamespaceHelper) QDefinitionBuilderFactory(org.apache.jackrabbit.spi.commons.nodetype.QDefinitionBuilderFactory) CompactNodeTypeDefReader(org.apache.jackrabbit.commons.cnd.CompactNodeTypeDefReader) InputStreamReader(java.io.InputStreamReader) NodeTypeReader(org.apache.jackrabbit.core.nodetype.xml.NodeTypeReader) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) NameException(org.apache.jackrabbit.spi.commons.conversion.NameException) NodeType(javax.jcr.nodetype.NodeType) ParseException(org.apache.jackrabbit.commons.cnd.ParseException)

Example 70 with NodeType

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

the class NodeTypeManagerImpl method getMixinNodeTypes.

/**
 * {@inheritDoc}
 */
public NodeTypeIterator getMixinNodeTypes() throws RepositoryException {
    Name[] ntNames = context.getNodeTypeRegistry().getRegisteredNodeTypes();
    Arrays.sort(ntNames);
    ArrayList<NodeType> list = new ArrayList<NodeType>(ntNames.length);
    for (Name ntName : ntNames) {
        NodeType nt = getNodeType(ntName);
        if (nt.isMixin()) {
            list.add(nt);
        }
    }
    return new NodeTypeIteratorAdapter(list);
}
Also used : NodeType(javax.jcr.nodetype.NodeType) ArrayList(java.util.ArrayList) NodeTypeIteratorAdapter(org.apache.jackrabbit.commons.iterator.NodeTypeIteratorAdapter) Name(org.apache.jackrabbit.spi.Name)

Aggregations

NodeType (javax.jcr.nodetype.NodeType)272 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)84 PropertyDefinition (javax.jcr.nodetype.PropertyDefinition)81 Node (javax.jcr.Node)63 Value (javax.jcr.Value)60 NodeTypeIterator (javax.jcr.nodetype.NodeTypeIterator)55 NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)46 NodeDefinition (javax.jcr.nodetype.NodeDefinition)45 RepositoryException (javax.jcr.RepositoryException)41 ArrayList (java.util.ArrayList)30 Test (org.junit.Test)30 Session (javax.jcr.Session)29 NodeTypeIteratorAdapter (org.apache.jackrabbit.commons.iterator.NodeTypeIteratorAdapter)16 NoSuchNodeTypeException (javax.jcr.nodetype.NoSuchNodeTypeException)15 Name (org.apache.jackrabbit.spi.Name)15 HashSet (java.util.HashSet)14 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)12 NodeIterator (javax.jcr.NodeIterator)7 Property (javax.jcr.Property)7 QNodeTypeDefinition (org.apache.jackrabbit.spi.QNodeTypeDefinition)7