public class NamespacesTable
extends java.lang.Object
This class also provides a very convenient and safe way of handling namespace declarations in SAX pipes. It also allows to filter duplicate namespace declarations that too often clutter up XML documents that went through several transformations, and avoid useless namespace declarations that aren't followed by element events.
Usage example in a SAX pipe:
NamespacesTable namespaces = new NamespacesTable(); ContentHandler nextHandler; public void startPrefixMapping(String prefix, String uri) throws SAXException { namespaces.addDeclaration(prefix, uri); } public void startElement(...) throws SAXException { // automatically start mappings for this scope namespaces.enterScope(nextHandler); nextHandler.startElement(...); } public void endElement(...) throws SAXException { nextHandler.endElement(...); // automatically end mappings for this scope namespaces.leaveScope(nextHandler); } public void endPrefixMapping(String prefix) throws SAXException { // Ignore, it is handled by leaveScope() }
Modifier and Type | Class and Description |
---|---|
static interface |
NamespacesTable.Declaration
A namespace declaration.
|
static interface |
NamespacesTable.Name
A namespace-aware name.
|
Constructor and Description |
---|
NamespacesTable()
Construct a new
NamespacesTable instance. |
Modifier and Type | Method and Description |
---|---|
NamespacesTable.Declaration |
addDeclaration(java.lang.String prefix,
java.lang.String uri)
Declare a new namespace prefix-uri mapping.
|
void |
clear()
Clear and reinitialize this namespace table before reuse.
|
void |
enterScope()
Enter a new scope.
|
void |
enterScope(org.xml.sax.ContentHandler handler)
Start all declared mappings of the current scope and enter a new scope.
|
NamespacesTable.Declaration[] |
getCurrentScopeDeclarations()
Get the declarations that were declared within the current scope.
|
java.lang.String |
getPrefix(java.lang.String uri)
Return one of the prefixes currently mapped to the specified URI or
null.
|
java.lang.String[] |
getPrefixes(java.lang.String uri)
Return an array with all prefixes currently mapped to the specified URI.
|
java.lang.String |
getUri(java.lang.String prefix)
Return the URI associated with the given prefix or null if the
prefix was not mapped.
|
void |
leaveScope()
Leave a scope.
|
void |
leaveScope(org.xml.sax.ContentHandler handler)
Leave a scope.
|
NamespacesTable.Declaration |
removeDeclaration(java.lang.String prefix)
Undeclare a namespace prefix-uri mapping.
|
NamespacesTable.Name |
resolve(java.lang.String uri,
java.lang.String raw,
java.lang.String prefix,
java.lang.String local)
Resolve a namespace-aware name against the current namespaces
declarations.
|
public NamespacesTable()
NamespacesTable
instance.public void clear()
public NamespacesTable.Declaration addDeclaration(java.lang.String prefix, java.lang.String uri)
Declaration
.public NamespacesTable.Declaration removeDeclaration(java.lang.String prefix)
When using enterScope()
/leaveScope()
, this method does nothing and always
returns null
, as declaration removal is handled in leaveScope()
.
Declaration
or null.public void enterScope()
Typically called in a SAX handler before sending a startElement()
event.
public void enterScope(org.xml.sax.ContentHandler handler) throws org.xml.sax.SAXException
Typically called in a SAX handler before sending a startElement()
event.
handler
- the handler that will receive startPrefixMapping events.org.xml.sax.SAXException
public void leaveScope()
enterScope()
are no more visible using the resolution methods, but
still available using getCurrentScopeDeclarations()
until the next call
to addDeclaration(String, String)
or enterScope()
.
Typically called in a SAX handler after sending a endElement()
event.
public void leaveScope(org.xml.sax.ContentHandler handler) throws org.xml.sax.SAXException
enterScope()
are no more visible using the resolution methods, but
still available using getCurrentScopeDeclarations()
until the next call
to addDeclaration(String, String)
or enterScope()
.
Typically called in a SAX handler after sending a endElement()
event.
handler
- the handler that will receive endPrefixMapping events.org.xml.sax.SAXException
public NamespacesTable.Declaration[] getCurrentScopeDeclarations()
public java.lang.String getUri(java.lang.String prefix)
public java.lang.String[] getPrefixes(java.lang.String uri)
String
array.public java.lang.String getPrefix(java.lang.String uri)
public NamespacesTable.Name resolve(java.lang.String uri, java.lang.String raw, java.lang.String prefix, java.lang.String local) throws org.xml.sax.SAXException
uri
- The namespace URI or null if not known.raw
- The raw (complete) name or null if not known.prefix
- The namespace prefix or null if not known.local
- The local name or null if not known.Name
.org.xml.sax.SAXException
- If the name cannot be resolved.Copyright © 2010 - 2020 Adobe. All Rights Reserved