Este conteúdo não está disponível no idioma selecionado.
16.17. Namespace Registry
As we saw earlier, every ExecutionContext has a registry of namespaces. Namespaces are used throughout the graph API (as we'll see soon), and the prefix associated with each namespace makes for more readable string representations. The namespace registry tracks all of these namespaces and prefixes, and allows registrations to be added, modified, or removed. The interface for the NamespaceRegistry shows how these operations are done:
public interface NamespaceRegistry {
/**
* Return the namespace URI that is currently mapped to the empty prefix.
* @return the namespace URI that represents the default namespace,
* or null if there is no default namespace
*/
String getDefaultNamespaceUri();
/**
* Get the namespace URI for the supplied prefix.
* @param prefix the namespace prefix
* @return the namespace URI for the supplied prefix, or null if there is no
* namespace currently registered to use that prefix
* @throws IllegalArgumentException if the prefix is null
*/
String getNamespaceForPrefix( String prefix );
/**
* Return the prefix used for the supplied namespace URI.
* @param namespaceUri the namespace URI
* @param generateIfMissing true if the namespace URI has not already been registered and the
* method should auto-register the namespace with a generated prefix, or false if the
* method should never auto-register the namespace
* @return the prefix currently being used for the namespace, or "null" if the namespace has
* not been registered and "generateIfMissing" is "false"
* @throws IllegalArgumentException if the namespace URI is null
* @see #isRegisteredNamespaceUri(String)
*/
String getPrefixForNamespaceUri( String namespaceUri, boolean generateIfMissing );
/**
* Return whether there is a registered prefix for the supplied namespace URI.
* @param namespaceUri the namespace URI
* @return true if the supplied namespace has been registered with a prefix, or false otherwise
* @throws IllegalArgumentException if the namespace URI is null
*/
boolean isRegisteredNamespaceUri( String namespaceUri );
/**
* Register a new namespace using the supplied prefix, returning the namespace URI previously
* registered under that prefix.
* @param prefix the prefix for the namespace, or null if a namesapce prefix should be generated
* automatically
* @param namespaceUri the namespace URI
* @return the namespace URI that was previously registered with the supplied prefix, or null if the
* prefix was not previously bound to a namespace URI
* @throws IllegalArgumentException if the namespace URI is null
*/
String register( String prefix, String namespaceUri );
/**
* Unregister the namespace with the supplied URI.
* @param namespaceUri the namespace URI
* @return true if the namespace was removed, or false if the namespace was not registered
* @throws IllegalArgumentException if the namespace URI is null
* @throws NamespaceException if there is a problem unregistering the namespace
*/
boolean unregister( String namespaceUri );
/**
* Obtain the set of namespaces that are registered.
* @return the set of namespace URIs; never null
*/
Set<String> getRegisteredNamespaceUris();
/**
* Obtain a snapshot of all of the {@link Namespace namespaces} registered at the time this method
* is called. The resulting set is immutable, and will not reflect changes made to the registry.
* @return an immutable set of Namespace objects reflecting a snapshot of the registry; never null
*/
Set<Namespace> getNamespaces();
}
This interfaces exposes Namespace objects that are immutable:
@Immutable
interface Namespace extends Comparable<Namespace> {
/**
* Get the prefix for the namespace
* @return the prefix; never null but possibly the empty string
*/
String getPrefix();
/**
* Get the URI for the namespace
* @return the namespace URI; never null but possibly the empty string
*/
String getNamespaceUri();
}
ModeShape actually uses several implementations of NamespaceRegistry , but you can even implement your own and create
ExecutionContexts that use it:
NamespaceRegistry myRegistry = ...
ExecutionContext contextWithMyRegistry = context.with(myRegistry);