gnu.xml

Class XMLFilter

public class XMLFilter extends Object implements DocumentHandler, ContentHandler, SourceLocator, XConsumer, PositionConsumer

Fixup XML input events. Handles namespace resolution, and adds "namespace nodes" if needed. Does various error checking. This wrapper should be used when creating a NodeTree, as is done for XQuery node constructor expressions. Can also be called directly from XMLParser, in which case we use a slightly lower-level interface where we array char array segments rather than Strings. This is to avoid duplicate String allocation and interning. The combination XMLParser+XMLFilter+NodeTree makes for a fast and compact way to read an XML file into a DOM.
Field Summary
intcopyNamespacesMode
static intCOPY_NAMESPACES_INHERIT
static intCOPY_NAMESPACES_PRESERVE
protected intignoringLevel
Postive if all output should be ignored.
booleannamespacePrefixes
True if namespace declarations should be passed through as attributes.
protected intnesting
Twice the number of active startElement and startDocument calls.
Consumerout
The specified target Consumer that accepts the output.
protected intstringizingElementNesting
Value of {@code nesting} just before outermost startElement while {@code stringizingLevel > 0}.
protected intstringizingLevel
If {@code stringizingLevel > 0} then stringize rather than copy nodes.
Constructor Summary
XMLFilter(Consumer out)
Method Summary
voidbeginEntity(Object baseUri)
voidcharacters(char[] ch, int start, int length)
protected voidcheckValidComment(char[] chars, int offset, int length)
protected booleancheckWriteAtomic()
voidcommentFromParser(char[] chars, int start, int length)
Process a comment, when called from an XML parser.
voidconsume(SeqPosition position)
static StringduplicateAttributeMessage(Symbol attrSymbol, Object elementName)
voidemitCharacterReference(int value, char[] name, int start, int length)
Process a character entity reference.
voidemitDoctypeDecl(char[] buffer, int target, int tlength, int data, int dlength)
Process a DOCTYPE declaration.
voidemitEndAttributes()
Process the end of a start tag.
voidemitEndElement(char[] data, int start, int length)
Process an end tag.
voidemitEntityReference(char[] name, int start, int length)
Process an entity reference.
voidemitStartAttribute(char[] data, int start, int count)
Process an attribute, with the given attribute name.
voidemitStartElement(char[] data, int start, int count)
Process a start tag, with the given element name.
voidendAttribute()
voidendDocument()
voidendElement()
voidendElement(String namespaceURI, String localName, String qName)
voidendElement(String name)
voidendEntity()
voidendPrefixMapping(String prefix)
voiderror(char severity, String message)
NamespaceBindingfindNamespaceBinding(String prefix, String uri, NamespaceBinding oldBindings)
Functionally equivalent to {@code new NamespaceBinding(prefix, uri, oldBindings}, but uses "hash consing".
intgetColumnNumber()
StringgetFileName()
intgetLineNumber()
StringgetPublicId()
StringgetSystemId()
voidignorableWhitespace(char[] ch, int start, int length)
booleanignoring()
booleanisStableSourceLocation()
voidlinefeedFromParser()
MappingInfolookupNamespaceBinding(String prefix, char[] uriChars, int uriStart, int uriLength, int uriHash, NamespaceBinding oldBindings)
Return a MappingInfo containing a match namespaces.
voidprocessingInstruction(String target, String data)
voidprocessingInstructionFromParser(char[] buffer, int tstart, int tlength, int dstart, int dlength)
Process a processing instruction.
voidsetDocumentLocator(Locator locator)
voidsetMessages(SourceMessages messages)
voidsetSourceLocator(LineBufferedReader in)
voidsetSourceLocator(SourceLocator locator)
voidskippedEntity(String name)
voidstartAttribute(Object attrType)
voidstartDocument()
voidstartElement(Object type)
voidstartElement(String namespaceURI, String localName, String qName, Attributes atts)
voidstartElement(String name, AttributeList atts)
protected voidstartElementCommon()
voidstartPrefixMapping(String prefix, String uri)
voidtextFromParser(char[] data, int start, int length)
voidwrite(int v)
voidwrite(char[] data, int start, int length)
Process raw text.
voidwrite(String str)
voidwrite(CharSequence str, int start, int length)
voidwriteBoolean(boolean v)
voidwriteCDATA(char[] data, int start, int length)
Process a CDATA section.
voidwriteComment(char[] chars, int start, int length)
Process a comment.
voidwriteDocumentUri(Object uri)
voidwriteDouble(double v)
voidwriteFloat(float v)
voidwriteInt(int v)
protected voidwriteJoiner()
voidwriteLong(long v)
voidwriteObject(Object v)
If v is a node, make a copy of it.
voidwritePosition(AbstractSequence seq, int ipos)
voidwriteProcessingInstruction(String target, char[] content, int offset, int length)

Field Detail

copyNamespacesMode

public transient int copyNamespacesMode

COPY_NAMESPACES_INHERIT

public static final int COPY_NAMESPACES_INHERIT

COPY_NAMESPACES_PRESERVE

public static final int COPY_NAMESPACES_PRESERVE

ignoringLevel

protected int ignoringLevel
Postive if all output should be ignored. This happens if we're inside an attribute value inside an element which is stringized because it is in turm inside an outer attribute. Phew. If gets increment by nested attributes so we can tell when to stop.

namespacePrefixes

public boolean namespacePrefixes
True if namespace declarations should be passed through as attributes. Like SAX2's http://xml.org/features/namespace-prefixes.

nesting

protected int nesting
Twice the number of active startElement and startDocument calls.

out

public Consumer out
The specified target Consumer that accepts the output. In contrast, base may be either {@code ==out} or {@code ==tlist}.

stringizingElementNesting

protected int stringizingElementNesting
Value of {@code nesting} just before outermost startElement while {@code stringizingLevel > 0}. I.e. if we're nested inside a element nested inside an attribute then {@code stringizingElementNesting >= 0}, otherwise {@code stringizingElementNesting == -1}.

stringizingLevel

protected int stringizingLevel
If {@code stringizingLevel > 0} then stringize rather than copy nodes. It counts the number of nested startAttributes that are active. (In the future it should also count begun comment and processing-instruction constructors, when those support nesting.)

Constructor Detail

XMLFilter

public XMLFilter(Consumer out)

Method Detail

beginEntity

public void beginEntity(Object baseUri)

characters

public void characters(char[] ch, int start, int length)

checkValidComment

protected void checkValidComment(char[] chars, int offset, int length)

checkWriteAtomic

protected boolean checkWriteAtomic()

commentFromParser

public void commentFromParser(char[] chars, int start, int length)
Process a comment, when called from an XML parser. The data (starting at start for length chars). Does not include the delimiters (i.e. "" are excluded).

consume

public void consume(SeqPosition position)

duplicateAttributeMessage

public static String duplicateAttributeMessage(Symbol attrSymbol, Object elementName)

emitCharacterReference

public void emitCharacterReference(int value, char[] name, int start, int length)
Process a character entity reference. The string encoding of the character (e.g. "xFF" or "255") is given, as well as the character value.

emitDoctypeDecl

public void emitDoctypeDecl(char[] buffer, int target, int tlength, int data, int dlength)
Process a DOCTYPE declaration.

emitEndAttributes

public void emitEndAttributes()
Process the end of a start tag. There are no more attributes.

emitEndElement

public void emitEndElement(char[] data, int start, int length)
Process an end tag. An abbreviated tag (such as {@code '
'}) has a name==null.

emitEntityReference

public void emitEntityReference(char[] name, int start, int length)
Process an entity reference. The entity name is given. This handles the predefined entities, such as "<" and """.

emitStartAttribute

public void emitStartAttribute(char[] data, int start, int count)
Process an attribute, with the given attribute name. The attribute value is given using {@code write}. The value is terminated by either another emitStartAttribute or an emitEndAttributes.

emitStartElement

public void emitStartElement(char[] data, int start, int count)
Process a start tag, with the given element name.

endAttribute

public void endAttribute()

endDocument

public void endDocument()

endElement

public void endElement()

endElement

public void endElement(String namespaceURI, String localName, String qName)

endElement

public void endElement(String name)

endEntity

public void endEntity()

endPrefixMapping

public void endPrefixMapping(String prefix)

error

public void error(char severity, String message)

findNamespaceBinding

public NamespaceBinding findNamespaceBinding(String prefix, String uri, NamespaceBinding oldBindings)
Functionally equivalent to {@code new NamespaceBinding(prefix, uri, oldBindings}, but uses "hash consing".

getColumnNumber

public int getColumnNumber()

getFileName

public String getFileName()

getLineNumber

public int getLineNumber()

getPublicId

public String getPublicId()

getSystemId

public String getSystemId()

ignorableWhitespace

public void ignorableWhitespace(char[] ch, int start, int length)

ignoring

public boolean ignoring()

isStableSourceLocation

public boolean isStableSourceLocation()

linefeedFromParser

public void linefeedFromParser()

lookupNamespaceBinding

public MappingInfo lookupNamespaceBinding(String prefix, char[] uriChars, int uriStart, int uriLength, int uriHash, NamespaceBinding oldBindings)
Return a MappingInfo containing a match namespaces. Specifically, return a {@code MappingInfo info} is such that {@code info.namespaces} is equal to {@code new NamespaceBinding(prefix, uri, oldBindings)}, where {@code uri} is {@code new String(uriChars, uriStart, uriLength).intern())}.

processingInstruction

public void processingInstruction(String target, String data)

processingInstructionFromParser

public void processingInstructionFromParser(char[] buffer, int tstart, int tlength, int dstart, int dlength)
Process a processing instruction.

setDocumentLocator

public void setDocumentLocator(Locator locator)

setMessages

public void setMessages(SourceMessages messages)

setSourceLocator

public void setSourceLocator(LineBufferedReader in)

setSourceLocator

public void setSourceLocator(SourceLocator locator)

skippedEntity

public void skippedEntity(String name)

startAttribute

public void startAttribute(Object attrType)

startDocument

public void startDocument()

startElement

public void startElement(Object type)

startElement

public void startElement(String namespaceURI, String localName, String qName, Attributes atts)

startElement

public void startElement(String name, AttributeList atts)

startElementCommon

protected void startElementCommon()

startPrefixMapping

public void startPrefixMapping(String prefix, String uri)

textFromParser

public void textFromParser(char[] data, int start, int length)

write

public void write(int v)

write

public void write(char[] data, int start, int length)
Process raw text.

write

public void write(String str)

write

public void write(CharSequence str, int start, int length)

writeBoolean

public void writeBoolean(boolean v)

writeCDATA

public void writeCDATA(char[] data, int start, int length)
Process a CDATA section. The data (starting at start for length char). Does not include the delimiters (i.e. {@code ""} are excluded).

writeComment

public void writeComment(char[] chars, int start, int length)
Process a comment. The data (starting at start for length chars). Does not include the delimiters (i.e. "" are excluded).

writeDocumentUri

public void writeDocumentUri(Object uri)

writeDouble

public void writeDouble(double v)

writeFloat

public void writeFloat(float v)

writeInt

public void writeInt(int v)

writeJoiner

protected void writeJoiner()

writeLong

public void writeLong(long v)

writeObject

public void writeObject(Object v)
If v is a node, make a copy of it.

writePosition

public void writePosition(AbstractSequence seq, int ipos)

writeProcessingInstruction

public void writeProcessingInstruction(String target, char[] content, int offset, int length)