org.apache.soap.util.xml
Class NSStack

java.lang.Object
  |
  +--org.apache.soap.util.xml.NSStack

public class NSStack
extends java.lang.Object

This class implements a namespace stack for XML apps to use. If you need to keep track of namespaces in scope, then this class is for you. Every time you enter a new element and wish to add some namespace declarations that are to be visible within that element, you should call pushScope to create a new scope. Then, call addNSDeclaration any number of times to add new declarations for that scope. Scopes nest inside out; that is, any NS declaration added into a scope is visible from any scopes that are pushed later. When you want to see whether an NS declaration has already been made for a certain URI, you should call getPrefixInScopeForURI to get the prefix that has been bound to that URI. There is a covenience version of addNSDecalration which can be used if you want me to give you a not-so-random, yet unique, prefix for your namespace declaration.

Author:
Sanjiva Weerawarana (sanjiva@watson.ibm.com)

Constructor Summary
NSStack()
           
 
Method Summary
 java.lang.String addNSDeclaration(java.lang.String URI)
          Add a new declaration to the current scope using a unique prefix and return the prefix.
 void addNSDeclaration(java.lang.String prefix, java.lang.String URI)
          Add a new declaration to the current scope.
 java.lang.String getPrefixFromURI(java.lang.String URI)
          Return the prefix associated with the given namespace name by looking thru all the namespace declarations that are in scope.
 java.lang.String getPrefixFromURI(java.lang.String namespaceURI, java.io.Writer sink)
          Return the prefix associated with the given namespace name by looking thru all the namespace declarations that are in scope.
 java.lang.String getURIFromPrefix(java.lang.String prefix)
          Return the namespace name associated with the given prefix by looking thru all the namespace declarations that are in scope.
 void popScope()
          Leave a scope: this removes any NS declarations that were added in the last scope.
 void pushScope()
          Enter a new scope: after calling this I'm ready to accept new declarations into that scope.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

NSStack

public NSStack()
Method Detail

pushScope

public void pushScope()
Enter a new scope: after calling this I'm ready to accept new declarations into that scope.

popScope

public void popScope()
Leave a scope: this removes any NS declarations that were added in the last scope. Note that I don't bother to validate that you don't call popScope too many times; that's your problem.

addNSDeclaration

public void addNSDeclaration(java.lang.String prefix,
                             java.lang.String URI)
Add a new declaration to the current scope. This is visible within the current scope as well as from any nested scopes.
Parameters:
prefix - the prefix to be used for this namespace
URI - the namespace name of this namespace.

addNSDeclaration

public java.lang.String addNSDeclaration(java.lang.String URI)
Add a new declaration to the current scope using a unique prefix and return the prefix. This is useful when one just wants to add a decl and doesn't want to have to deal with creating unique prefixes. If the namespace name is already declared and in scope, then the previously declared prefix is returned.
Parameters:
URI - the namespace name of this namespace
Returns:
the unique prefix created or previously declared for this namespace

getPrefixFromURI

public java.lang.String getPrefixFromURI(java.lang.String URI)
Return the prefix associated with the given namespace name by looking thru all the namespace declarations that are in scope.
Parameters:
URI - the namespace name for whom a declared prefix is desired
Returns:
the prefix or null if namespace name not found

getPrefixFromURI

public java.lang.String getPrefixFromURI(java.lang.String namespaceURI,
                                         java.io.Writer sink)
                                  throws java.io.IOException
Return the prefix associated with the given namespace name by looking thru all the namespace declarations that are in scope. If the namespace declaration is not found, create one and return the generated prefix.
Parameters:
URI - the namespace name for whom a declared prefix is desired
Returns:
the prefix (will never return null)

getURIFromPrefix

public java.lang.String getURIFromPrefix(java.lang.String prefix)
Return the namespace name associated with the given prefix by looking thru all the namespace declarations that are in scope.
Parameters:
prefix - the prefix for whom a declared namespace name is desired
Returns:
the namespace name or null if prefix not found

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object


Copyright 2001 Oracle SOAP. All Rights Reserved.