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();
}
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"));
}
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);
}
}
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);
}
}
}
Aggregations