com.sun.xml.stream.buffer.stax
Class NamespaceContexHelper

java.lang.Object
  extended by com.sun.xml.stream.buffer.stax.NamespaceContexHelper
All Implemented Interfaces:
Iterable<NamespaceContextEx.Binding>, NamespaceContext, NamespaceContextEx

public final class NamespaceContexHelper
extends Object
implements NamespaceContextEx

A helper class for managing the declaration of namespaces.

A namespace is declared on a namespace context. Namespace contexts are pushed on and popped off the namespace context stack.

A declared namespace will be in scope iff the context that it was declared on has not been popped off the stack.

When instantiated the namespace stack consists of the root namespace context, which contains, by default, the "xml" and "xmlns" declarations. Namespaces may be declarations may be declared on the root context. The root context cannot be popped but can be reset to contain just the "xml" and "xmlns" declarations.

Implementation note: determining the prefix from a namespace URI (or vice versa) is efficient when there are few namespace declarations i.e. what is considered to be the case for namespace declarations in 'average' XML documents. The look up of a namespace URI given a prefix is performed in O(n) time. The look up of a prefix given a namespace URI is performed in O(2n) time.

The implementation does not scale when there are many namespace declarations. TODO: Use a hash map when there are many namespace declarations.

Author:
Paul.Sandoz@Sun.Com

Nested Class Summary
 
Nested classes/interfaces inherited from interface org.jvnet.staxex.NamespaceContextEx
NamespaceContextEx.Binding
 
Constructor Summary
NamespaceContexHelper()
          Create a new NamespaceContexHelper.
 
Method Summary
 void declareDefaultNamespace(String namespaceURI)
          Declare a default namespace.
 void declareNamespace(String prefix, String namespaceURI)
          Declare a namespace.
 String getNamespaceURI(String prefix)
           
 String getPrefix(String namespaceURI)
           
 Iterator getPrefixes(String namespaceURI)
           
 Iterator<NamespaceContextEx.Binding> iterator()
          Iterates all the in-scope namespace bindings.
 void popContext()
          Pop the namespace context off the stack.
 void pushContext()
          Push a namespace context on the stack.
 void resetContexts()
          Reset namespace contexts.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NamespaceContexHelper

public NamespaceContexHelper()
Create a new NamespaceContexHelper.

Method Detail

getNamespaceURI

public String getNamespaceURI(String prefix)
Specified by:
getNamespaceURI in interface NamespaceContext

getPrefix

public String getPrefix(String namespaceURI)
Specified by:
getPrefix in interface NamespaceContext

getPrefixes

public Iterator getPrefixes(String namespaceURI)
Specified by:
getPrefixes in interface NamespaceContext

iterator

public Iterator<NamespaceContextEx.Binding> iterator()
Description copied from interface: NamespaceContextEx
Iterates all the in-scope namespace bindings.

This method enumerates all the active in-scope namespace bindings. This does not include implicit bindings, such as "xml"->"http://www.w3.org/XML/1998/namespace" or ""->"" (the implicit default namespace URI.)

The returned iterator may not include the same prefix more than once. For example, the returned iterator may only contain f=ns2 if the document is as follows and this method is used at the bar element.


 <foo xmlns:f='ns1'>
   <bar xmlns:f='ns2'>
     ...
 

The iteration may be done in no particular order.

Specified by:
iterator in interface Iterable<NamespaceContextEx.Binding>
Specified by:
iterator in interface NamespaceContextEx
Returns:
may return an empty iterator, but never null.

declareDefaultNamespace

public void declareDefaultNamespace(String namespaceURI)
Declare a default namespace.

Parameters:
namespaceURI - the namespace URI to declare, may be null.

declareNamespace

public void declareNamespace(String prefix,
                             String namespaceURI)
Declare a namespace.

The namespace will be declared on the current namespace context.

The namespace can be removed by popping the current namespace context, or, if the declaration occured in the root context, by reseting the namespace context.

A default namespace can be declared by passing "" as the value of the prefix parameter. A namespace may be undeclared by passing null as the value of the namespaceURI parameter.

Parameters:
prefix - the namespace prefix to declare, may not be null.
namespaceURI - the namespace URI to declare, may be null.
Throws:
IllegalArgumentException, - if the prefix is null.

pushContext

public void pushContext()
Push a namespace context on the stack.


popContext

public void popContext()
Pop the namespace context off the stack.

Namespaces declared within the context (to be popped) will be removed and no longer be in scope.


resetContexts

public void resetContexts()
Reset namespace contexts.

Pop all namespace contexts and reset the root context.



Copyright © 2005-2015 Oracle Corporation. All Rights Reserved.