Fields Summary |
---|
private int | countThis member is continually incremented when new prefixes need to be
generated. ("ns0" "ns1" ...) |
private Stack | m_prefixStackStack of prefixes that have mappings
The top of this stack is the prefix that was last mapped to an URI
For every prefix pushed on this stack a corresponding integer is pushed
on the m_nodeStack. That way all prefixes pushed at the current depth can
be removed at the same time. |
private Hashtable | m_namespacesEach entry (prefix) in this hashtable points to a Stack of URIs |
private Stack | m_nodeStackThe top of this stack contains the nested element depth
of the last declared a namespace.
Used to know how many prefix mappings to pop when leaving
the current element depth.
For every prefix mapping the current element depth is
pushed on this stack, as well as the prefix on the m_prefixStack
That way all prefixes pushed at the current depth can be
removed at the same time.
Used to ensure prefix/uri map scopes are closed correctly |
private static final String | EMPTYSTRING |
private static final String | XML_PREFIX |
Methods Summary |
---|
public java.lang.Object | clone()This method makes a clone of this object.
NamespaceMappings clone = new NamespaceMappings();
clone.m_prefixStack = (Stack)m_prefixStack.clone();
clone.m_nodeStack = (Stack) m_nodeStack.clone();
clone.m_namespaces = (Hashtable) m_namespaces.clone();
clone.count = count;
return clone;
|
public java.lang.String | generateNextPrefix()Generate a new namespace prefix ( ns0, ns1 ...) not used before
return "ns" + (count++);
|
private void | initNamespaces()This method initializes the namespace object with appropriate stacks
and predefines a few prefix/uri pairs which always exist.
// Define the default namespace (initially maps to "" uri)
Stack stack;
m_namespaces.put(EMPTYSTRING, stack = new Stack());
stack.push(EMPTYSTRING);
m_prefixStack.push(EMPTYSTRING);
m_namespaces.put(XML_PREFIX, stack = new Stack());
stack.push("http://www.w3.org/XML/1998/namespace");
m_prefixStack.push(XML_PREFIX);
m_nodeStack.push(new Integer(-1));
|
public java.lang.String | lookupNamespace(java.lang.String prefix)Use a namespace prefix to lookup a namespace URI.
final Stack stack = (Stack) m_namespaces.get(prefix);
return stack != null && !stack.isEmpty() ? (String) stack.peek() : null;
|
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.
String foundPrefix = null;
Enumeration prefixes = m_namespaces.keys();
while (prefixes.hasMoreElements())
{
String prefix = (String) prefixes.nextElement();
String uri2 = lookupNamespace(prefix);
if (uri2 != null && uri2.equals(uri))
{
foundPrefix = prefix;
break;
}
}
return foundPrefix;
|
public boolean | popNamespace(java.lang.String prefix)Undeclare the namespace that is currently pointed to by a given prefix
// Prefixes "xml" and "xmlns" cannot be redefined
if (prefix.startsWith(XML_PREFIX))
{
return false;
}
Stack stack;
if ((stack = (Stack) m_namespaces.get(prefix)) != null)
{
stack.pop();
return true;
}
return false;
|
public 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.
while (true)
{
if (m_nodeStack.isEmpty())
return;
Integer i = (Integer) (m_nodeStack.peek());
if (i.intValue() < elemDepth)
return;
/* the depth of the declared mapping is elemDepth or deeper
* so get rid of it
*/
m_nodeStack.pop();
final String prefix = (String) m_prefixStack.pop();
popNamespace(prefix);
if (saxHandler != null)
{
try
{
saxHandler.endPrefixMapping(prefix);
}
catch (SAXException e)
{
// not much we can do if they aren't willing to listen
}
}
}
|
public boolean | pushNamespace(java.lang.String prefix, java.lang.String uri, int elemDepth)Declare a prefix to point to a namespace URI
// Prefixes "xml" and "xmlns" cannot be redefined
if (prefix.startsWith(XML_PREFIX))
{
return false;
}
Stack stack;
// Get the stack that contains URIs for the specified prefix
if ((stack = (Stack) m_namespaces.get(prefix)) == null)
{
m_namespaces.put(prefix, stack = new Stack());
}
if (!stack.empty() && uri.equals(stack.peek()))
{
return false;
}
stack.push(uri);
m_prefixStack.push(prefix);
m_nodeStack.push(new Integer(elemDepth));
return true;
|
public final void | reset()
this.count = 0;
this.m_namespaces.clear();
this.m_nodeStack.clear();
this.m_prefixStack.clear();
initNamespaces();
|