com.sun.xml.bind.v2.runtime.output
Class NamespaceContextImpl

java.lang.Object
  extended by com.sun.xml.bind.v2.runtime.output.NamespaceContextImpl
All Implemented Interfaces:
NamespaceContext2, NamespaceContext

public final class NamespaceContextImpl
extends Object
implements NamespaceContext2

Keeps track of in-scope namespace bindings for the marshaller.

This class is also used to keep track of tag names for each element for the marshaller (for the performance reason.)


Nested Class Summary
 class NamespaceContextImpl.Element
          This model of namespace declarations maintain the following invariants.
 
Field Summary
 boolean collectionMode
          True to allow new URIs to be declared.
 
Constructor Summary
NamespaceContextImpl(XMLSerializer owner)
           
 
Method Summary
 int count()
          Number of total bindings declared.
 String declareNamespace(String namespaceUri, String preferedPrefix, boolean requirePrefix)
          Declares a new namespace binding within the current context.
 int declareNsUri(String uri, String preferedPrefix, boolean requirePrefix)
          Returns the prefix index to the specified URI.
 int force(String uri, String prefix)
          Forcibly make a namespace declaration in effect.
 NamespaceContextImpl.Element getCurrent()
           
 String getNamespaceURI(int prefixIndex)
           
 String getNamespaceURI(String prefix)
          Gets the namespace URI that is bound to the specified prefix.
 String getPrefix(int prefixIndex)
          Gets the prefix from a prefix index.
 String getPrefix(String uri)
          Returns the prefix of the specified URI, or null if none exists.
 Iterator<String> getPrefixes(String uri)
           
 int getPrefixIndex(String uri)
          Returns the prefix index of the specified URI.
 NamespacePrefixMapper getPrefixMapper()
           
 int put(String uri, String prefix)
          Puts this new binding into the declared prefixes list without doing any duplicate check.
 void reset()
           
 void setPrefixMapper(NamespacePrefixMapper mapper)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

collectionMode

public boolean collectionMode
True to allow new URIs to be declared. False otherwise.

Constructor Detail

NamespaceContextImpl

public NamespaceContextImpl(XMLSerializer owner)
Method Detail

setPrefixMapper

public void setPrefixMapper(NamespacePrefixMapper mapper)

getPrefixMapper

public NamespacePrefixMapper getPrefixMapper()

reset

public void reset()

declareNsUri

public int declareNsUri(String uri,
                        String preferedPrefix,
                        boolean requirePrefix)
Returns the prefix index to the specified URI. This method allocates a new URI if necessary.


force

public int force(@NotNull
                 String uri,
                 @NotNull
                 String prefix)
Description copied from interface: NamespaceContext2
Forcibly make a namespace declaration in effect. If the (prefix,uri) binding is already in-scope, this method simply returns the assigned prefix index. Otherwise a new declaration will be put.

Specified by:
force in interface NamespaceContext2

put

public int put(@NotNull
               String uri,
               @Nullable
               String prefix)
Puts this new binding into the declared prefixes list without doing any duplicate check. This can be used to forcibly set namespace declarations.

Most of the time declareNamespace(String, String, boolean) shall be used.

Returns:
the index of this new binding.

getCurrent

public NamespaceContextImpl.Element getCurrent()

getPrefixIndex

public int getPrefixIndex(String uri)
Returns the prefix index of the specified URI. It is an error if the URI is not declared.


getPrefix

public String getPrefix(int prefixIndex)
Gets the prefix from a prefix index. The behavior is undefined if the index is out of range.


getNamespaceURI

public String getNamespaceURI(int prefixIndex)

getNamespaceURI

public String getNamespaceURI(String prefix)
Gets the namespace URI that is bound to the specified prefix.

Specified by:
getNamespaceURI in interface NamespaceContext
Returns:
null if the prefix is unbound.

getPrefix

public String getPrefix(String uri)
Returns the prefix of the specified URI, or null if none exists.

Specified by:
getPrefix in interface NamespaceContext

getPrefixes

public Iterator<String> getPrefixes(String uri)
Specified by:
getPrefixes in interface NamespaceContext

declareNamespace

public String declareNamespace(String namespaceUri,
                               String preferedPrefix,
                               boolean requirePrefix)
Description copied from interface: NamespaceContext2
Declares a new namespace binding within the current context.

The prefix is automatically assigned by MarshallingContext. If a given namespace URI is already declared, nothing happens.

It is NOT an error to declare the same namespace URI more than once.

For marshalling to work correctly, all namespace bindings for an element must be declared between its startElement method and its endAttributes event. Calling the same method with the same parameter between the endAttributes and the endElement returns the same prefix.

Specified by:
declareNamespace in interface NamespaceContext2
preferedPrefix - If the caller has any particular preference to the prefix, pass that as a parameter. The callee will try to honor it. Set null if there's no particular preference.
requirePrefix - If this parameter is true, this method must assign a prefix to this namespace, even if it's already bound to the default namespace. IOW, this method will never return null if this flag is true. This functionality is necessary to declare namespace URI used for attribute names.
Returns:
returns the assigned prefix. If the namespace is bound to the default namespace, null is returned.

count

public int count()
Number of total bindings declared.