org.apache.xml.serializer
Class NamespaceMappings

java.lang.Object
  extended by org.apache.xml.serializer.NamespaceMappings

public class NamespaceMappings
extends java.lang.Object

This class keeps track of the currently defined namespaces. Conceptually the prefix/uri/depth triplets are pushed on a stack pushed on a stack. The depth indicates the nesting depth of the element for which the mapping was made.

For example:

 
   
      
      
    
    
    
 
 
When the element is encounted the prefix "p1" associated with uri "def" is pushed on the stack with depth 1. When the first is encountered "p2" and "ghi" are pushed with depth 2. When the is encountered "p3" and "jkl" are pushed with depth 3. When occurs the popNamespaces(3) will pop "p3"/"jkl" off the stack. Of course popNamespaces(2) would pop anything with depth 2 or greater. So prefix/uri pairs are pushed and poped off the stack as elements are processed. At any given moment of processing the currently visible prefixes are on the stack and a prefix can be found given a uri, or a uri can be found given a prefix. This class is intended for internal use only. However, it is made public because other packages require it.


Nested Class Summary
(package private) static class NamespaceMappings.MappingRecord
          Just a little class that ties the 3 fields together into one object, and this simplifies the pushing and popping of namespaces to one push or one pop on one stack rather than on 3 separate stacks.
private  class NamespaceMappings.Stack
          Rather than using java.util.Stack, this private class provides a minimal subset of methods and is faster because it is not thread-safe.
 
Field Summary
private  int count
          This member is continually incremented when new prefixes need to be generated.
private static java.lang.String EMPTYSTRING
           
private  java.util.Hashtable m_namespaces
          Each entry (prefix) in this hashtable points to a Stack of URIs This table maps a prefix (String) to a Stack of NamespaceNodes.
private  NamespaceMappings.Stack m_nodeStack
          This stack is used as a convenience.
private static java.lang.String XML_PREFIX
           
 
Constructor Summary
NamespaceMappings()
          Default constructor
 
Method Summary
 java.lang.Object clone()
          This method makes a clone of this object.
private  NamespaceMappings.Stack createPrefixStack(java.lang.String prefix)
          A more type-safe way of saving stacks under the m_namespaces Hashtable.
 java.lang.String generateNextPrefix()
          Generate a new namespace prefix ( ns0, ns1 ...) not used before
(package private)  NamespaceMappings.MappingRecord getMappingFromPrefix(java.lang.String prefix)
           
(package private)  NamespaceMappings.MappingRecord getMappingFromURI(java.lang.String uri)
           
private  NamespaceMappings.Stack getPrefixStack(java.lang.String prefix)
          A more type-safe way to get a stack of prefix mappings from the Hashtable m_namespaces (this is the only method that does the type cast).
private  void initNamespaces()
          This method initializes the namespace object with appropriate stacks and predefines a few prefix/uri pairs which always exist.
 java.lang.String[] lookupAllPrefixes(java.lang.String uri)
          Given a namespace uri, get all prefixes bound to the Namespace URI in the current scope.
 java.lang.String lookupNamespace(java.lang.String prefix)
          Use a namespace prefix to lookup a namespace URI.
 java.lang.String lookupPrefix(java.lang.String uri)
          Given a namespace uri, and the namespaces mappings for the current element, return the current prefix for that uri.
(package private)  boolean popNamespace(java.lang.String prefix)
          Undeclare the namespace that is currently pointed to by a given prefix
(package private)  void popNamespaces(int elemDepth, org.xml.sax.ContentHandler saxHandler)
          Pop, or undeclare all namespace definitions that are currently declared at the given element depth, or deepter.
 boolean pushNamespace(java.lang.String prefix, java.lang.String uri, int elemDepth)
          Declare a mapping of a prefix to namespace URI at the given element depth.
(package private)  void reset()
           
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

count

private int count
This member is continually incremented when new prefixes need to be generated. ("ns0" "ns1" ...)


m_namespaces

private java.util.Hashtable m_namespaces
Each entry (prefix) in this hashtable points to a Stack of URIs This table maps a prefix (String) to a Stack of NamespaceNodes. All Namespace nodes in that retrieved stack have the same prefix, though possibly different URI's or depths. Such a stack must have mappings at deeper depths push later on such a stack. Mappings pushed earlier on the stack will have smaller values for MappingRecord.m_declarationDepth.


m_nodeStack

private NamespaceMappings.Stack m_nodeStack
This stack is used as a convenience. It contains the pushed NamespaceNodes (shallowest to deepest) and is used to delete NamespaceNodes when leaving the current element depth to returning to the parent. The mappings of the deepest depth can be popped of the top and the same node can be removed from the appropriate prefix stack. All prefixes pushed at the current depth can be removed at the same time by using this stack to ensure prefix/uri map scopes are closed correctly.


EMPTYSTRING

private static final java.lang.String EMPTYSTRING
See Also:
Constant Field Values

XML_PREFIX

private static final java.lang.String XML_PREFIX
See Also:
Constant Field Values
Constructor Detail

NamespaceMappings

public NamespaceMappings()
Default constructor

See Also:
Object.Object()
Method Detail

initNamespaces

private void initNamespaces()
This method initializes the namespace object with appropriate stacks and predefines a few prefix/uri pairs which always exist.


lookupNamespace

public java.lang.String lookupNamespace(java.lang.String prefix)
Use a namespace prefix to lookup a namespace URI.

Parameters:
prefix - String the prefix of the namespace
Returns:
the URI corresponding to the prefix, returns "" if there is no visible mapping.

getMappingFromPrefix

NamespaceMappings.MappingRecord getMappingFromPrefix(java.lang.String prefix)

lookupPrefix

public java.lang.String lookupPrefix(java.lang.String uri)
Given a namespace uri, and the namespaces mappings for the current element, return the current prefix for that uri.

Parameters:
uri - the namespace URI to be search for
Returns:
an existing prefix that maps to the given URI, null if no prefix maps to the given namespace URI.

getMappingFromURI

NamespaceMappings.MappingRecord getMappingFromURI(java.lang.String uri)

popNamespace

boolean popNamespace(java.lang.String prefix)
Undeclare the namespace that is currently pointed to by a given prefix


pushNamespace

public boolean pushNamespace(java.lang.String prefix,
                             java.lang.String uri,
                             int elemDepth)
Declare a mapping of a prefix to namespace URI at the given element depth.

Parameters:
prefix - a String with the prefix for a qualified name
uri - a String with the uri to which the prefix is to map
elemDepth - the depth of current declaration

popNamespaces

void popNamespaces(int elemDepth,
                   org.xml.sax.ContentHandler saxHandler)
Pop, or undeclare all namespace definitions that are currently declared at the given element depth, or deepter.

Parameters:
elemDepth - the element depth for which mappings declared at this depth or deeper will no longer be valid
saxHandler - The ContentHandler to notify of any endPrefixMapping() calls. This parameter can be null.

generateNextPrefix

public java.lang.String generateNextPrefix()
Generate a new namespace prefix ( ns0, ns1 ...) not used before

Returns:
String a new namespace prefix ( ns0, ns1, ns2 ...)

clone

public java.lang.Object clone()
                       throws java.lang.CloneNotSupportedException
This method makes a clone of this object.

Overrides:
clone in class java.lang.Object
Throws:
java.lang.CloneNotSupportedException

reset

final void reset()

getPrefixStack

private NamespaceMappings.Stack getPrefixStack(java.lang.String prefix)
A more type-safe way to get a stack of prefix mappings from the Hashtable m_namespaces (this is the only method that does the type cast).


createPrefixStack

private NamespaceMappings.Stack createPrefixStack(java.lang.String prefix)
A more type-safe way of saving stacks under the m_namespaces Hashtable.


lookupAllPrefixes

public java.lang.String[] lookupAllPrefixes(java.lang.String uri)
Given a namespace uri, get all prefixes bound to the Namespace URI in the current scope.

Parameters:
uri - the namespace URI to be search for
Returns:
An array of Strings which are all prefixes bound to the namespace URI in the current scope. An array of zero elements is returned if no prefixes map to the given namespace URI.