Search in sources :

Example 1 with CompactNodeTypeDefReader

use of org.apache.jackrabbit.commons.cnd.CompactNodeTypeDefReader in project jackrabbit by apache.

the class AbstractRecord method readNodeTypeDef.

/**
 * {@inheritDoc}
 */
public QNodeTypeDefinition readNodeTypeDef() throws JournalException {
    try {
        StringReader sr = new StringReader(readString());
        CompactNodeTypeDefReader<QNodeTypeDefinition, NamespaceMapping> reader = new CompactNodeTypeDefReader<QNodeTypeDefinition, NamespaceMapping>(sr, "(internal)", new NamespaceMapping(nsResolver), new QDefinitionBuilderFactory());
        Collection<QNodeTypeDefinition> ntds = reader.getNodeTypeDefinitions();
        if (ntds.size() != 1) {
            throw new JournalException("Expected one node type definition: got " + ntds.size());
        }
        return ntds.iterator().next();
    } catch (ParseException e) {
        String msg = "Parse error while reading node type definition.";
        throw new JournalException(msg, e);
    }
}
Also used : QNodeTypeDefinition(org.apache.jackrabbit.spi.QNodeTypeDefinition) NamespaceMapping(org.apache.jackrabbit.spi.commons.namespace.NamespaceMapping) QDefinitionBuilderFactory(org.apache.jackrabbit.spi.commons.nodetype.QDefinitionBuilderFactory) StringReader(java.io.StringReader) ParseException(org.apache.jackrabbit.commons.cnd.ParseException) CompactNodeTypeDefReader(org.apache.jackrabbit.commons.cnd.CompactNodeTypeDefReader)

Example 2 with CompactNodeTypeDefReader

use of org.apache.jackrabbit.commons.cnd.CompactNodeTypeDefReader 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 3 with CompactNodeTypeDefReader

use of org.apache.jackrabbit.commons.cnd.CompactNodeTypeDefReader in project jackrabbit by apache.

the class CompactNodeTypeDefTest method testCompactNodeTypeDef.

public void testCompactNodeTypeDef() throws Exception {
    // Read in node type def from test file
    Reader reader = new InputStreamReader(getClass().getClassLoader().getResourceAsStream(TEST_FILE));
    CompactNodeTypeDefReader<QNodeTypeDefinition, NamespaceMapping> cndReader = new CompactNodeTypeDefReader<QNodeTypeDefinition, NamespaceMapping>(reader, TEST_FILE, new QDefinitionBuilderFactory());
    List<QNodeTypeDefinition> ntdList1 = cndReader.getNodeTypeDefinitions();
    NamespaceMapping nsm = cndReader.getNamespaceMapping();
    NamePathResolver resolver = new DefaultNamePathResolver(nsm);
    // Put imported node type def back into CND form with CND writer
    StringWriter sw = new StringWriter();
    CompactNodeTypeDefWriter.write(ntdList1, nsm, resolver, sw);
    // Rerun the reader on the product of the writer
    cndReader = new CompactNodeTypeDefReader<QNodeTypeDefinition, NamespaceMapping>(new StringReader(sw.toString()), TEST_FILE, new QDefinitionBuilderFactory());
    List<QNodeTypeDefinition> ntdList2 = cndReader.getNodeTypeDefinitions();
    if (ntdList1.size() == 0 || ntdList1.size() != ntdList2.size()) {
        fail("Exported node type definition was not successfully read back in");
    } else {
        for (int k = 0; k < ntdList1.size(); k++) {
            QNodeTypeDefinition ntd1 = ntdList1.get(k);
            QNodeTypeDefinition ntd2 = ntdList2.get(k);
            NodeTypeDefDiff diff = NodeTypeDefDiff.create(ntd1, ntd2);
            if (diff.isModified() && !diff.isTrivial()) {
                fail("Exported node type definition was not successfully read back in. " + ntd2.getName() + "differs from original");
            }
        }
    }
}
Also used : NamePathResolver(org.apache.jackrabbit.spi.commons.conversion.NamePathResolver) DefaultNamePathResolver(org.apache.jackrabbit.spi.commons.conversion.DefaultNamePathResolver) InputStreamReader(java.io.InputStreamReader) NamespaceMapping(org.apache.jackrabbit.spi.commons.namespace.NamespaceMapping) CompactNodeTypeDefReader(org.apache.jackrabbit.commons.cnd.CompactNodeTypeDefReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) QNodeTypeDefinition(org.apache.jackrabbit.spi.QNodeTypeDefinition) StringWriter(java.io.StringWriter) QDefinitionBuilderFactory(org.apache.jackrabbit.spi.commons.nodetype.QDefinitionBuilderFactory) StringReader(java.io.StringReader) DefaultNamePathResolver(org.apache.jackrabbit.spi.commons.conversion.DefaultNamePathResolver) CompactNodeTypeDefReader(org.apache.jackrabbit.commons.cnd.CompactNodeTypeDefReader) NodeTypeDefDiff(org.apache.jackrabbit.spi.commons.nodetype.NodeTypeDefDiff)

Example 4 with CompactNodeTypeDefReader

use of org.apache.jackrabbit.commons.cnd.CompactNodeTypeDefReader in project sling by apache.

the class NodeTypeDefinitionScanner method registerNamespaces.

/**
     * Registers only the namespaces found in node type definitions in classpath in JCR repository.
     * @param session Session
     * @param nodeTypeResources List of classpath resource URLs pointing to node type definitions
     */
private void registerNamespaces(Session session, List<String> nodeTypeResources) throws RepositoryException {
    ClassLoader classLoader = getClass().getClassLoader();
    Workspace workspace = session.getWorkspace();
    NamespaceRegistry namespaceRegistry = workspace.getNamespaceRegistry();
    ValueFactory valueFactory = session.getValueFactory();
    DefinitionBuilderFactory<NodeTypeTemplate, NamespaceRegistry> factory = new TemplateBuilderFactory(new DummyNodeTypeManager(), valueFactory, namespaceRegistry);
    for (String nodeTypeResource : nodeTypeResources) {
        InputStream is = classLoader.getResourceAsStream(nodeTypeResource);
        if (is == null) {
            continue;
        }
        try {
            Reader reader = new InputStreamReader(is);
            CompactNodeTypeDefReader<NodeTypeTemplate, NamespaceRegistry> cndReader = new CompactNodeTypeDefReader<NodeTypeTemplate, NamespaceRegistry>(reader, nodeTypeResource, factory);
            NamespaceRegistry mapping = cndReader.getNamespaceMapping();
            for (int i = 0; i < mapping.getURIs().length; i++) {
                String uri = mapping.getURIs()[i];
                String prefix = mapping.getPrefix(uri);
                try {
                    namespaceRegistry.registerNamespace(prefix, uri);
                } catch (RepositoryException ex) {
                // ignore
                }
            }
        } catch (Throwable ex) {
            log.warn("Unable to parse node type definition: " + nodeTypeResource, ex);
        } finally {
            IOUtils.closeQuietly(is);
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) TemplateBuilderFactory(org.apache.jackrabbit.commons.cnd.TemplateBuilderFactory) CompactNodeTypeDefReader(org.apache.jackrabbit.commons.cnd.CompactNodeTypeDefReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) RepositoryException(javax.jcr.RepositoryException) ValueFactory(javax.jcr.ValueFactory) NamespaceRegistry(javax.jcr.NamespaceRegistry) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) CompactNodeTypeDefReader(org.apache.jackrabbit.commons.cnd.CompactNodeTypeDefReader) Workspace(javax.jcr.Workspace)

Example 5 with CompactNodeTypeDefReader

use of org.apache.jackrabbit.commons.cnd.CompactNodeTypeDefReader in project jackrabbit-oak by apache.

the class NodeTypeRegistryTest method registerNodeType.

@Test
public void registerNodeType() throws Exception {
    registerNodeType(root, "oak6440-1.cnd");
    NodeTypeManager readOnlyNtMgr = new ReadOnlyNodeTypeManager() {

        private Root r = session.getLatestRoot();

        @Override
        protected Tree getTypes() {
            return r.getTree(NODE_TYPES_PATH);
        }
    };
    NodeTypeManager ntMgr = new ReadWriteNodeTypeManager() {

        @Override
        protected Tree getTypes() {
            return root.getTree(NODE_TYPES_PATH);
        }

        @Nonnull
        @Override
        protected Root getWriteRoot() {
            return root;
        }
    };
    ValueFactory valueFactory = new ValueFactoryImpl(root, new NamePathMapperImpl(new GlobalNameMapper(root)));
    NamespaceRegistry nsRegistry = new ReadOnlyNamespaceRegistry(root);
    DefinitionBuilderFactory<NodeTypeTemplate, NamespaceRegistry> factory = new TemplateBuilderFactory(ntMgr, valueFactory, nsRegistry);
    InputStream in = NodeTypeRegistryTest.class.getResourceAsStream("oak6440-2.cnd");
    List<NodeTypeTemplate> templates;
    try {
        CompactNodeTypeDefReader<NodeTypeTemplate, NamespaceRegistry> reader = new CompactNodeTypeDefReader<NodeTypeTemplate, NamespaceRegistry>(new InputStreamReader(in, UTF_8), "oak6440-2.cnd", factory);
        templates = reader.getNodeTypeDefinitions();
    } finally {
        in.close();
    }
    for (NodeTypeTemplate t : templates) {
        NodeTypeTemplateImpl template;
        if (t instanceof NodeTypeTemplateImpl) {
            template = (NodeTypeTemplateImpl) t;
        } else {
            template = new NodeTypeTemplateImpl(new GlobalNameMapper(root), t);
        }
        template.writeTo(root.getTree(NODE_TYPES_PATH), true);
    }
    NodeTypeDefinition beforeDef = readOnlyNtMgr.getNodeType("foo");
    NodeTypeDefinition afterDef = ntMgr.getNodeType("foo");
    NodeTypeDefDiff diff = NodeTypeDefDiff.create(beforeDef, afterDef);
    assertFalse(diff.isMajor());
}
Also used : ReadOnlyNodeTypeManager(org.apache.jackrabbit.oak.plugins.nodetype.ReadOnlyNodeTypeManager) NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) Root(org.apache.jackrabbit.oak.api.Root) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) ValueFactoryImpl(org.apache.jackrabbit.oak.plugins.value.jcr.ValueFactoryImpl) TemplateBuilderFactory(org.apache.jackrabbit.commons.cnd.TemplateBuilderFactory) NodeTypeDefinition(javax.jcr.nodetype.NodeTypeDefinition) ReadOnlyNodeTypeManager(org.apache.jackrabbit.oak.plugins.nodetype.ReadOnlyNodeTypeManager) ValueFactory(javax.jcr.ValueFactory) ReadOnlyNamespaceRegistry(org.apache.jackrabbit.oak.plugins.name.ReadOnlyNamespaceRegistry) NamePathMapperImpl(org.apache.jackrabbit.oak.namepath.impl.NamePathMapperImpl) NamespaceRegistry(javax.jcr.NamespaceRegistry) ReadOnlyNamespaceRegistry(org.apache.jackrabbit.oak.plugins.name.ReadOnlyNamespaceRegistry) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) GlobalNameMapper(org.apache.jackrabbit.oak.namepath.impl.GlobalNameMapper) CompactNodeTypeDefReader(org.apache.jackrabbit.commons.cnd.CompactNodeTypeDefReader) NodeTypeDefDiff(org.apache.jackrabbit.oak.plugins.nodetype.NodeTypeDefDiff) Test(org.junit.Test)

Aggregations

CompactNodeTypeDefReader (org.apache.jackrabbit.commons.cnd.CompactNodeTypeDefReader)6 InputStreamReader (java.io.InputStreamReader)4 StringReader (java.io.StringReader)3 NamespaceRegistry (javax.jcr.NamespaceRegistry)3 RepositoryException (javax.jcr.RepositoryException)3 NodeTypeTemplate (javax.jcr.nodetype.NodeTypeTemplate)3 ParseException (org.apache.jackrabbit.commons.cnd.ParseException)3 TemplateBuilderFactory (org.apache.jackrabbit.commons.cnd.TemplateBuilderFactory)3 QNodeTypeDefinition (org.apache.jackrabbit.spi.QNodeTypeDefinition)3 NamespaceMapping (org.apache.jackrabbit.spi.commons.namespace.NamespaceMapping)3 QDefinitionBuilderFactory (org.apache.jackrabbit.spi.commons.nodetype.QDefinitionBuilderFactory)3 InputStream (java.io.InputStream)2 Reader (java.io.Reader)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 ValueFactory (javax.jcr.ValueFactory)2 NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)2 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 List (java.util.List)1