Search in sources :

Example 46 with NodeTypeTemplate

use of javax.jcr.nodetype.NodeTypeTemplate 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)

Aggregations

NodeTypeTemplate (javax.jcr.nodetype.NodeTypeTemplate)46 NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)29 Node (javax.jcr.Node)17 PropertyDefinitionTemplate (javax.jcr.nodetype.PropertyDefinitionTemplate)13 Test (org.junit.Test)12 Session (javax.jcr.Session)11 NodeDefinitionTemplate (javax.jcr.nodetype.NodeDefinitionTemplate)11 List (java.util.List)7 Workspace (javax.jcr.Workspace)7 NamespaceRegistry (javax.jcr.NamespaceRegistry)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)5 NodeTypeDefinition (javax.jcr.nodetype.NodeTypeDefinition)5 JackrabbitWorkspace (org.apache.jackrabbit.api.JackrabbitWorkspace)5 InputStream (java.io.InputStream)4 ArrayList (java.util.ArrayList)4 AccessDeniedException (javax.jcr.AccessDeniedException)4 RepositoryException (javax.jcr.RepositoryException)4 JackrabbitNode (org.apache.jackrabbit.api.JackrabbitNode)4 BigDecimal (java.math.BigDecimal)3