Search in sources :

Example 31 with NamespaceRegistry

use of javax.jcr.NamespaceRegistry in project sling by apache.

the class JcrItemResourceTestBase method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    // create the session
    getSession();
    try {
        NamespaceRegistry nsr = session.getWorkspace().getNamespaceRegistry();
        nsr.registerNamespace(SlingConstants.NAMESPACE_PREFIX, JcrResourceConstants.SLING_NAMESPACE_URI);
    } catch (Exception e) {
    // don't care for now
    }
    rootPath = "/test" + System.currentTimeMillis();
    rootNode = getSession().getRootNode().addNode(rootPath.substring(1), "nt:unstructured");
    getSession().save();
}
Also used : NamespaceRegistry(javax.jcr.NamespaceRegistry) IOException(java.io.IOException)

Example 32 with NamespaceRegistry

use of javax.jcr.NamespaceRegistry in project sling by apache.

the class MockSessionTest method testNamespaces.

@Test
public void testNamespaces() throws RepositoryException {
    Session session = MockJcr.newSession();
    // test initial namespaces
    assertArrayEquals(new String[] { "jcr" }, session.getNamespacePrefixes());
    assertEquals("http://www.jcp.org/jcr/1.0", session.getNamespaceURI("jcr"));
    assertEquals("jcr", session.getNamespacePrefix("http://www.jcp.org/jcr/1.0"));
    // add dummy namespace
    session.setNamespacePrefix("dummy", "http://mydummy");
    assertEquals(ImmutableSet.of("jcr", "dummy"), ImmutableSet.copyOf(session.getNamespacePrefixes()));
    assertEquals("http://mydummy", session.getNamespaceURI("dummy"));
    assertEquals("dummy", session.getNamespacePrefix("http://mydummy"));
    // test via namespace registry
    NamespaceRegistry namespaceRegistry = session.getWorkspace().getNamespaceRegistry();
    assertEquals(ImmutableSet.of("jcr", "dummy"), ImmutableSet.copyOf(namespaceRegistry.getPrefixes()));
    assertEquals(ImmutableSet.of("http://www.jcp.org/jcr/1.0", "http://mydummy"), ImmutableSet.copyOf(namespaceRegistry.getURIs()));
    assertEquals("http://mydummy", namespaceRegistry.getURI("dummy"));
    assertEquals("dummy", namespaceRegistry.getPrefix("http://mydummy"));
    // remove dummy namespace
    namespaceRegistry.unregisterNamespace("dummy");
    assertEquals(ImmutableSet.of("jcr"), ImmutableSet.copyOf(session.getNamespacePrefixes()));
    assertEquals("http://www.jcp.org/jcr/1.0", session.getNamespaceURI("jcr"));
    assertEquals("jcr", session.getNamespacePrefix("http://www.jcp.org/jcr/1.0"));
}
Also used : NamespaceRegistry(javax.jcr.NamespaceRegistry) Session(javax.jcr.Session) Test(org.junit.Test)

Example 33 with NamespaceRegistry

use of javax.jcr.NamespaceRegistry in project sling by apache.

the class NamespacesVisitor method visitRegisterNamespace.

@Override
public void visitRegisterNamespace(RegisterNamespace rn) {
    try {
        final NamespaceRegistry reg = session.getWorkspace().getNamespaceRegistry();
        log.info("Registering namespace from {}", rn);
        reg.registerNamespace(rn.getPrefix(), rn.getURI());
    } catch (Exception e) {
        report(e, "Unable to register namespace from " + rn);
    }
}
Also used : NamespaceRegistry(javax.jcr.NamespaceRegistry)

Example 34 with NamespaceRegistry

use of javax.jcr.NamespaceRegistry 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

NamespaceRegistry (javax.jcr.NamespaceRegistry)34 Session (javax.jcr.Session)14 NamespaceException (javax.jcr.NamespaceException)6 Node (javax.jcr.Node)6 RepositoryException (javax.jcr.RepositoryException)6 NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)6 NodeTypeTemplate (javax.jcr.nodetype.NodeTypeTemplate)6 Test (org.junit.Test)6 InputStream (java.io.InputStream)4 JackrabbitWorkspace (org.apache.jackrabbit.api.JackrabbitWorkspace)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 BigDecimal (java.math.BigDecimal)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Binary (javax.jcr.Binary)3 Workspace (javax.jcr.Workspace)3 Value (javax.jcr.Value)2 ValueFactory (javax.jcr.ValueFactory)2 PropertyDefinitionTemplate (javax.jcr.nodetype.PropertyDefinitionTemplate)2 JackrabbitSession (org.apache.jackrabbit.api.JackrabbitSession)2