org.apache.ws.commons.util

Class NamespaceContextImpl

public class NamespaceContextImpl extends Object implements NamespaceContext

Default implementation of javax.xml.namespace.NamespaceContext.
Method Summary
StringcheckContext(int i)
This method is used to restore the namespace state after an element is created.
voidendPrefixMapping(String pPrefix)
Removes the declaration of the prefix, which has been defined last.
StringgetAttributePrefix(String pURI)
Returns a non-empty prefix currently mapped to the given URL or null, if there is no such mapping.
intgetContext()
Returns the current number of assigned prefixes.
StringgetNamespaceURI(String pPrefix)
Given a prefix, returns the URI to which the prefix is currently mapped or null, if there is no such mapping.

Note: This methods behaviour is precisely defined by NamespaceContext#getNamespaceURI(java.lang.String).

StringgetPrefix(String pURI)
Returns a prefix currently mapped to the given URI or null, if there is no such mapping.
IteratorgetPrefixes(String pURI)
Returns a collection to all prefixes bound to the given namespace URI.
ListgetPrefixes()
Returns a list of all prefixes, which are currently declared, in the order of declaration.
booleanisPrefixDeclared(String pPrefix)
Returns whether a given prefix is currently declared.
voidreset()
Resets the NamespaceSupport's state.
voidstartPrefixMapping(String pPrefix, String pURI)
Declares a new prefix.

Method Detail

checkContext

public String checkContext(int i)
This method is used to restore the namespace state after an element is created. It takes as input a state, as returned by getContext.
For any prefix, which was since saving the state, the prefix is returned and deleted from the internal list. In other words, a typical use looks like this:
   NamespaceSupport nss;
   ContentHandler h;
   int context = nss.getContext();
   h.startElement("foo", "bar", "f:bar", new AttributesImpl());
   ...
   h.endElement("foo", "bar", "f:bar");
   for (;;) {
     String prefix = nss.checkContext(context);
     if (prefix == null) {
       break;
     }
     h.endPrefixMapping(prefix);
   }
 

endPrefixMapping

public void endPrefixMapping(String pPrefix)
Removes the declaration of the prefix, which has been defined last. Typically called from within org.xml.sax.ContextHandler#endPrefixMapping(java.lang.String).

Throws: IllegalArgumentException The prefix is null. IllegalStateException The prefix is not the prefix, which has been defined last. In other words, the calls to NamespaceContextImpl, and endPrefixMapping aren't in LIFO order.

getAttributePrefix

public String getAttributePrefix(String pURI)
Returns a non-empty prefix currently mapped to the given URL or null, if there is no such mapping. This method may be used to find a possible prefix for an attributes namespace URI. For elements you should use getPrefix.

Parameters: pURI Thhe namespace URI in question

Throws: IllegalArgumentException The namespace URI is null.

getContext

public int getContext()
Returns the current number of assigned prefixes. Note, that a prefix may be assigned in several nested elements, in which case every assignment is counted.
This method is typically called before invoking the method org.xml.sax.ContentHandler#startElement(String, String, String, org.xml.sax.Attributes). The return value is used as a saveable state. After invoking org.xml.sax.ContentHandler#endElement(String, String, String), the state is restored by calling NamespaceContextImpl.

getNamespaceURI

public String getNamespaceURI(String pPrefix)
Given a prefix, returns the URI to which the prefix is currently mapped or null, if there is no such mapping.

Note: This methods behaviour is precisely defined by NamespaceContext#getNamespaceURI(java.lang.String).

Parameters: pPrefix The prefix in question

getPrefix

public String getPrefix(String pURI)
Returns a prefix currently mapped to the given URI or null, if there is no such mapping. This method may be used to find a possible prefix for an elements namespace URI. For attributes you should use getAttributePrefix. Note: This methods behaviour is precisely defined by NamespaceContext#getPrefix(java.lang.String).

Parameters: pURI The namespace URI in question

Throws: IllegalArgumentException The namespace URI is null.

getPrefixes

public Iterator getPrefixes(String pURI)
Returns a collection to all prefixes bound to the given namespace URI. Note: This methods behaviour is precisely defined by NamespaceContext#getPrefixes(java.lang.String).

Parameters: pURI The namespace prefix in question

getPrefixes

public List getPrefixes()
Returns a list of all prefixes, which are currently declared, in the order of declaration. Duplicates are possible, if a prefix has been assigned to more than one URI, or repeatedly to the same URI.

isPrefixDeclared

public boolean isPrefixDeclared(String pPrefix)
Returns whether a given prefix is currently declared.

reset

public void reset()
Resets the NamespaceSupport's state. Allows reusing the object.

startPrefixMapping

public void startPrefixMapping(String pPrefix, String pURI)
Declares a new prefix. Typically called from within org.xml.sax.ContextHandler#startPrefixMapping(java.lang.String, java.lang.String).

Throws: IllegalArgumentException Prefix or URI are null.

Copyright © 2005-2009 Apache Software Foundation. All Rights Reserved.