7.2 Adding and Deleting Namespaces

As discussed in 6.3 Namespaces, above, each content repository has a single, persistent namespace registry represented by the NamespaceRegistry object accessed via Workspace.getNamespaceRegistry(). In level 1 only the NamespaceRegistry methods related to discovering information must function. In level 2 the NamespaceRegistry additionally allows for persistent changes to namespace mappings using the following methods:

javax.jcr.
NamespaceRegistry

void

registerNamespace(String prefix, String uri)

Sets a one-to-one mapping between prefix and uri in the global namespace registry of this repository.

Assigning a new prefix to a URI that already exists in the namespace registry erases the old prefix. In general this can be done, though an implementation is free to prevent particular remappings by throwing a NamespaceException.

On the other hand, taking a prefix that is already assigned to a URI and re-assigning it to a new URI in effect unregisters that URI. Therefore, the same restrictions apply to this operation as to NamespaceRegistry.unregisterNamespace:

  • Attempting to re-assign a built-in prefix (jcr, nt, mix, xml or the empty prefix) to a new URI will throw a NamespaceException.

  • Attempting to re-assign a prefix that is currently assigned to a URI that is present in content (either within an item name or within the value of a NAME or PATH property) will throw a NamespaceException. This includes prefixes in use within in-content node type definitions.

  • Attempting to register a namespace with a prefix that begins with the characters “xml” (in any combination of case) will throw a NamespaceException.

  • An implementation may prevent the re-assignment of any other namespace prefixes for implementation-specific reasons by throwing a NamespaceException.

In a level 1 implementation, this method always throws an UnsupportedRepositoryOperationException.

If the session associated with the Workspace object through which this registry was acquired does not have sufficient permissions to register the namespace an AccessDeniedException is thrown.

A RepositoryException is thrown if another error occurs.

void

unregisterNamespace(String prefix)

Removes a namespace mapping from the registry. The following restrictions apply:

  • Attempting to unregister a built-in namespace (jcr, nt, mix, xml or the empty namespace) will throw a NamespaceException.

  • Attempting to unregister a namespace that is currently present in content (either within an item name or within the value of a NAME or PATH property) will throw a NamespaceException. This includes prefixes in use within in-content node type definitions.

  • An attempt to unregister a namespace that is not currently registered will throw a NamespaceException.

  • An implementation may prevent the unregistering of any other namespace for implementation-specific reasons by throwing a NamespaceException.

In a level 1 implementation, this method always throws an UnsupportedRepositoryOperationException.

If the session associated with the Workspace object through which this registry was acquired does not have sufficient permissions to unregister the namespace an AccessDeniedException is thrown.

A RepositoryException is thrown if another error occurs.


Once registered, a prefix can be used in the name of any node or property in the repository. The prefix serves as shorthand for the URI to which it is mapped. Because the space of URIs is universal managed, the combination of the per-repository namespace and the larger URI namespace can be used to provide universally uniqueness of node or property names. Of course, just as in the case of XML namespaces, ensuring this universal uniqueness requires applications to map their application-specific prefixes to URIs that are uniquely identified with that particular application.

The namespace registry always contains at least the following built-in mappings, which cannot be removed through the API: