Search in sources :

Example 1 with NamespaceRegistry

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

the class NamespaceRegistryTest method testReRegisteredNamespace2.

/**
     * Test if a replace namespace prefix cannot be used as key any more to
     * retrieve the uri in the <code>NamespaceRegistry</code> retrieved by
     * another Session object.
     *
     * @throws RepositoryException
     */
public void testReRegisteredNamespace2() throws RepositoryException {
    Session otherSession = getHelper().getReadOnlySession();
    try {
        NamespaceRegistry other = otherSession.getWorkspace().getNamespaceRegistry();
        nsRegistry.registerNamespace(testPrefix, testURI);
        other.getPrefix(testURI);
        String replacePrefix = getUnusedPrefix();
        nsRegistry.registerNamespace(replacePrefix, testURI);
        String otherPrefix = other.getPrefix(testURI);
        assertEquals("Namespace with prefix " + testPrefix + " has been reregistered with new prefix " + replacePrefix, replacePrefix, otherPrefix);
    } finally {
        otherSession.logout();
    }
}
Also used : NamespaceRegistry(javax.jcr.NamespaceRegistry) Session(javax.jcr.Session)

Example 2 with NamespaceRegistry

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

the class NamespaceRegistryTest method testRegisteredNamespaceVisibility.

/**
     * Test if a new registered namespace is immediately visible through another
     * session object.
     *
     * @throws RepositoryException
     */
public void testRegisteredNamespaceVisibility() throws RepositoryException {
    Session otherSession = getHelper().getReadOnlySession();
    try {
        NamespaceRegistry other = otherSession.getWorkspace().getNamespaceRegistry();
        nsRegistry.registerNamespace(testPrefix, testURI);
        String otherUri = other.getURI(testPrefix);
        String otherPrefix = other.getPrefix(testURI);
        assertTrue("Namespace registered must be immediately visible to any other session.", testURI.equals(otherUri) && testPrefix.equals(otherPrefix));
    } finally {
        otherSession.logout();
    }
}
Also used : NamespaceRegistry(javax.jcr.NamespaceRegistry) Session(javax.jcr.Session)

Example 3 with NamespaceRegistry

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

the class QueryTest method testRemappedNamespace.

public void testRemappedNamespace() throws RepositoryException, NotExecutableException {
    String namespaceURI = "http://jackrabbit.apache.org/spi/test";
    String defaultPrefix = "spiTest";
    NamespaceRegistry nsReg = superuser.getWorkspace().getNamespaceRegistry();
    try {
        nsReg.getPrefix(namespaceURI);
    } catch (RepositoryException e) {
        nsReg.registerNamespace(defaultPrefix, namespaceURI);
    }
    Node n = testRootNode.addNode("spiTest:node");
    superuser.save();
    for (int i = 0; i < 10; i++) {
        String prefix = defaultPrefix + i;
        superuser.setNamespacePrefix(prefix, namespaceURI);
        executeXPathQuery(superuser, testPath + "/" + prefix + ":node", new Node[] { n });
    }
}
Also used : NamespaceRegistry(javax.jcr.NamespaceRegistry) Node(javax.jcr.Node) RepositoryException(javax.jcr.RepositoryException)

Example 4 with NamespaceRegistry

use of javax.jcr.NamespaceRegistry in project jackrabbit-oak by apache.

the class AccessControlManagerImplTest method registerNamespace.

private void registerNamespace(String prefix, String uri) throws Exception {
    NamespaceRegistry nsRegistry = new ReadWriteNamespaceRegistry(root) {

        @Override
        protected Root getWriteRoot() {
            return root;
        }
    };
    nsRegistry.registerNamespace(prefix, uri);
}
Also used : NamespaceRegistry(javax.jcr.NamespaceRegistry) ReadWriteNamespaceRegistry(org.apache.jackrabbit.oak.plugins.name.ReadWriteNamespaceRegistry) ReadWriteNamespaceRegistry(org.apache.jackrabbit.oak.plugins.name.ReadWriteNamespaceRegistry)

Example 5 with NamespaceRegistry

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

the class CndImporter method registerNodeTypes.

/**
     * Registers nodetypes in <code>cnd</code> format.
     * @param cnd  a reader to the cnd. The reader is closed on return.
     * @param systemId  a informative id of the given cnd input.
     * @param nodeTypeManager  the {@link NodeTypeManager} used for creating and registering the
     *     {@link NodeTypeTemplate}s, {@link NodeDefinitionTemplate}s and {@link PropertyDefinitionTemplate}s
     *     defined in the cnd.
     * @param namespaceRegistry  the {@link NamespaceRegistry} used for registering namespaces defined in
     *     the cnd.
     * @param valueFactory  the {@link ValueFactory} used to create
     *     {@link PropertyDefinitionTemplate#setDefaultValues(javax.jcr.Value[]) default value(s)}.
     * @param reregisterExisting  <code>true</code> if existing node types should be re-registered
     *     with those present in the cnd. <code>false</code> otherwise.
     * @return  the registered node types
     *
     * @throws ParseException  if the cnd cannot be parsed
     * @throws InvalidNodeTypeDefinitionException  if a <code>NodeTypeDefinition</code> is invalid.
     * @throws NodeTypeExistsException  if <code>reregisterExisting</code> is <code>false</code> and a
     *     <code>NodeTypeDefinition</code> specifies a node type name that is already registered.
     * @throws UnsupportedRepositoryOperationException  if the <code>NodeTypeManager</code> does not
     *     support node type registration.
     * @throws IOException  if closing the cnd reader fails
     * @throws RepositoryException  if another error occurs.
     */
public static NodeType[] registerNodeTypes(Reader cnd, String systemId, NodeTypeManager nodeTypeManager, NamespaceRegistry namespaceRegistry, ValueFactory valueFactory, boolean reregisterExisting) throws ParseException, InvalidNodeTypeDefinitionException, NodeTypeExistsException, UnsupportedRepositoryOperationException, RepositoryException, IOException {
    try {
        DefinitionBuilderFactory<NodeTypeTemplate, NamespaceRegistry> factory = new TemplateBuilderFactory(nodeTypeManager, valueFactory, namespaceRegistry);
        CompactNodeTypeDefReader<NodeTypeTemplate, NamespaceRegistry> cndReader = new CompactNodeTypeDefReader<NodeTypeTemplate, NamespaceRegistry>(cnd, systemId, factory);
        Map<String, NodeTypeTemplate> templates = new HashMap<String, NodeTypeTemplate>();
        for (NodeTypeTemplate template : cndReader.getNodeTypeDefinitions()) {
            templates.put(template.getName(), template);
        }
        List<NodeTypeTemplate> toRegister = new ArrayList<NodeTypeTemplate>(templates.size());
        for (NodeTypeTemplate ntt : templates.values()) {
            if (reregisterExisting || !nodeTypeManager.hasNodeType(ntt.getName())) {
                ensureNtBase(ntt, templates, nodeTypeManager);
                toRegister.add(ntt);
            }
        }
        NodeTypeIterator registered = nodeTypeManager.registerNodeTypes(toRegister.toArray(new NodeTypeTemplate[toRegister.size()]), true);
        return toArray(registered);
    } finally {
        cnd.close();
    }
}
Also used : NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) NamespaceRegistry(javax.jcr.NamespaceRegistry) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator)

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