net.sf.jsefa.xml.namespace
Class NamespaceManager

java.lang.Object
  extended by net.sf.jsefa.xml.namespace.NamespaceManager

public final class NamespaceManager
extends Object

A NamespaceManager manages namespace URIs and their prefixes. It allows for
1. the registration of preferred prefixes for URIs (for root NamespaceManagers only),
2. the registration of prefixes for URIs which are interpreted during serialization as being known at the current point in document,
3. the retrieval of a prefix to a given URI
4. the retrieval of the URI to a given prefix
5. the creation of a new prefix for a new URI

A NamespaceManager may have a parent NamespaceManager to which retrieval requests are delegated if it has no own registries or if it has no result to the request. A NamespaceManager without a parent is called a root namespace manager.

Note: Instances of this class are intentionally not thread-safe.

Author:
Norman Lahme-Huetig

Method Summary
static NamespaceManager create()
          Creates a new NamespaceManager.
 NamespaceManager createCopy()
          Creates a copy of this NamespaceManager.
 String createPrefix(String uri, boolean defaultAllowed)
          Creates a new prefix for the given URI.
static NamespaceManager createWithParent(NamespaceManager parent)
          Creates a new NamespaceManager with the given NamespaceManager as its parent.
 NamespaceManager getParent()
          Returns the parent namespace manager of this namespace manager - if exists.
 String getPrefix(String uri, boolean defaultAllowed)
          Returns the prefix which is registered for the given namespace uri.
 String getUri(String prefix)
          Returns the namespace uri the given prefix is registered for.
 void registerPreferredPrefix(String prefix, String uri)
          Registers the given prefix to be the preferred one for the given URI.
 void registerPrefix(String prefix, String uri)
          Registers the given prefix for the given namespace uri.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

create

public static NamespaceManager create()
Creates a new NamespaceManager.

Returns:
a NamespaceManager

createWithParent

public static NamespaceManager createWithParent(NamespaceManager parent)
Creates a new NamespaceManager with the given NamespaceManager as its parent.

Parameters:
parent - the parent of this namespace manager.
Returns:
a NamespaceManager

createCopy

public NamespaceManager createCopy()
Creates a copy of this NamespaceManager. The copy has its own registries but has the same (identical) parent as the this NamespaceManager.

Returns:
a copy of this NamespaceManager

getParent

public NamespaceManager getParent()
Returns the parent namespace manager of this namespace manager - if exists.

Returns:
the parent namespace manager or null if none exists.

registerPreferredPrefix

public void registerPreferredPrefix(String prefix,
                                    String uri)
Registers the given prefix to be the preferred one for the given URI. I. e. if a prefix is needed (call of createPrefix(java.lang.String, boolean)) for that URI the given preferred one is used and not an automatically created one.

Parameters:
prefix - the preferred prefix
uri - the URI

registerPrefix

public void registerPrefix(String prefix,
                           String uri)
Registers the given prefix for the given namespace uri.

During serialization a registered prefix is interpretated is being known at the current point in the xml document.

Note: Normally it is registerPreferredPrefix(java.lang.String, java.lang.String) what you want to call.

Parameters:
prefix - the prefix
uri - the uri
Throws:
NullPointerException - if one of the arguments is null
NamespaceRegistrationException - if
1. the prefix is already bound to another uri
2. the prefix is an explicit prefix and the uri is already bound to another explicit prefix

getPrefix

public String getPrefix(String uri,
                        boolean defaultAllowed)
Returns the prefix which is registered for the given namespace uri. If this namespace manager has no registration for the given uri, then its parent namespace manager is asked for it (in the case a parent exists).

If the parent namespace manager returns a prefix which is known for this namespace manager (the prefix is overwritten), then null is returned.

Parameters:
uri - the namespace uri
defaultAllowed - true, if the prefix may be the default one.
Returns:
the prefix or null if none is registered for the given uri.

createPrefix

public String createPrefix(String uri,
                           boolean defaultAllowed)
Creates a new prefix for the given URI. A preferred prefix is returned if it exists for the given URI and if the prefix is not already bound to another URI.

Parameters:
uri - the uri to get a prefix for (not null)
defaultAllowed - true, if the prefix may be the default one.
Returns:
the prefix

getUri

public String getUri(String prefix)
Returns the namespace uri the given prefix is registered for. If this namespace manager has no registration for the given prefix, than its parent namespace manager is asked for it (in the case a parent exists).

Parameters:
prefix - the prefix
Returns:
the uri or null if none is registered for the given prefix


Copyright © 2014. All rights reserved.