Package gnu.xml
Class XMLFilter
java.lang.Object
gnu.xml.XMLFilter
- All Implemented Interfaces:
Consumer
,PositionConsumer
,XConsumer
,SourceLocator
,Appendable
,Consumer<Object>
,DoubleConsumer
,IntConsumer
,LongConsumer
,SourceLocator
,ContentHandler
,DocumentHandler
,Locator
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 use 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.
-
Nested Class Summary
Nested classes/interfaces inherited from interface gnu.text.SourceLocator
SourceLocator.Simple
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
static final int
static final int
int
protected int
Positive if all output should be ignored.boolean
True if namespace declarations should be passed through as attributes.protected int
Twice the number of active startElement and startDocument calls.The specified target Consumer that accepts the output.protected int
Value ofnesting
just before outermost startElement whilestringizingLevel > 0
.protected int
IfstringizingLevel > 0
then stringize rather than copy nodes. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionappend
(char c) append
(CharSequence csq) append
(CharSequence csq, int start, int end) void
beginEntity
(Object baseUri) void
characters
(char[] ch, int start, int length) protected void
checkValidComment
(char[] chars, int offset, int length) protected boolean
void
commentFromParser
(char[] chars, int start, int length) Process a comment, when called from an XML parser.static String
duplicateAttributeMessage
(Symbol attrSymbol, Object elementName) void
emitCharacterReference
(int value, char[] name, int start, int length) Process a character entity reference.void
emitDoctypeDecl
(char[] buffer, int target, int tlength, int data, int dlength) Process a DOCTYPE declaration.void
Process the end of a start tag.void
emitEndElement
(char[] data, int start, int length) Process an end tag.void
emitEntityReference
(char[] name, int start, int length) Process an entity reference.void
emitStartAttribute
(char[] data, int start, int count) Process an attribute, with the given attribute name.void
emitStartElement
(char[] data, int start, int count) Process a start tag, with the given element name.void
End of an attribute or end of an actual parameter.void
void
void
endElement
(String name) void
endElement
(String namespaceURI, String localName, String qName) void
void
endPrefixMapping
(String prefix) void
findNamespaceBinding
(String prefix, String uri, NamespaceBinding oldBindings) Functionally equivalent tonew NamespaceBinding(prefix, uri, oldBindings
, but uses "hash consing".int
Return current column number.int
Column (one-origin) of end of range; unknown/unspecified is -1.int
Line number (one-origin) of end of range; unknown/unspecified is -1.Normally same as getSystemId.int
Return current line number.int
Column (one-origin) of start of range; unknown/unspecified is -1.int
Line number (one-origin) of start of range; unknown/unspecified is -1.void
ignorableWhitespace
(char[] ch, int start, int length) boolean
ignoring()
True if consumer is ignoring rest of element.boolean
True if position is unlikely to change.void
gnu.xml.MappingInfo
lookupNamespaceBinding
(String prefix, char[] uriChars, int uriStart, int uriLength, int uriHash, NamespaceBinding oldBindings) Return a MappingInfo containing a match namespaces.void
processingInstruction
(String target, String data) void
processingInstructionFromParser
(char[] buffer, int tstart, int tlength, int dstart, int dlength) Process a processing instruction.void
setDocumentLocator
(Locator locator) void
setMessages
(SourceMessages messages) void
setSourceLocator
(gnu.kawa.io.InPort in) void
setSourceLocator
(SourceLocator locator) void
skippedEntity
(String name) void
startAttribute
(Object attrType) Write a attribute for the current element.void
void
startElement
(Object type) void
startElement
(String namespaceURI, String localName, String qName, Attributes atts) void
startElement
(String name, AttributeList atts) protected void
void
startPrefixMapping
(String prefix, String uri) void
textFromParser
(char[] data, int start, int length) void
write
(char[] data, int start, int length) Process raw text.void
write
(int v) void
write
(CharSequence str, int start, int length) void
void
writeBoolean
(boolean v) void
writeCDATA
(char[] data, int start, int length) Process a CDATA section.void
writeComment
(char[] chars, int start, int length) Process a comment.void
writeDocumentUri
(Object uri) void
writeDouble
(double v) void
writeFloat
(float v) void
writeInt
(int v) protected void
void
writeLong
(long v) void
If v is a node, make a copy of it.void
writePosition
(AbstractSequence seq, int ipos) Consume a single position pair.void
writePosition
(SeqPosition position) Consume node at current position.void
writeProcessingInstruction
(String target, char[] content, int offset, int length) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.xml.sax.ContentHandler
declaration
Methods inherited from interface java.util.function.DoubleConsumer
andThen
Methods inherited from interface java.util.function.IntConsumer
andThen
Methods inherited from interface java.util.function.LongConsumer
andThen
-
Field Details
-
out
The specified target Consumer that accepts the output. In contrast, base may be either==out
or==tlist
. -
COPY_NAMESPACES_PRESERVE
public static final int COPY_NAMESPACES_PRESERVE- See Also:
-
COPY_NAMESPACES_INHERIT
public static final int COPY_NAMESPACES_INHERIT- See Also:
-
copyNamespacesMode
public transient int copyNamespacesMode -
COPY_NAMESPACES_MASK
public static final int COPY_NAMESPACES_MASK- See Also:
-
nesting
protected int nestingTwice the number of active startElement and startDocument calls. -
stringizingLevel
protected int stringizingLevelIfstringizingLevel > 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.) -
stringizingElementNesting
protected int stringizingElementNestingValue ofnesting
just before outermost startElement whilestringizingLevel > 0
. I.e. if we're nested inside a element nested inside an attribute thenstringizingElementNesting >= 0
, otherwisestringizingElementNesting == -1
. -
ignoringLevel
protected int ignoringLevelPositive 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 turn inside an outer attribute. Phew. It gets incremented by nested attributes so we can tell when to stop. -
namespacePrefixes
public boolean namespacePrefixesTrue if namespace declarations should be passed through as attributes. Like SAX2's http://xml.org/features/namespace-prefixes.
-
-
Constructor Details
-
XMLFilter
-
-
Method Details
-
setSourceLocator
public void setSourceLocator(gnu.kawa.io.InPort in) -
setSourceLocator
-
setMessages
-
findNamespaceBinding
public NamespaceBinding findNamespaceBinding(String prefix, String uri, NamespaceBinding oldBindings) Functionally equivalent tonew NamespaceBinding(prefix, uri, oldBindings
, but uses "hash consing". -
lookupNamespaceBinding
public gnu.xml.MappingInfo lookupNamespaceBinding(String prefix, char[] uriChars, int uriStart, int uriLength, int uriHash, NamespaceBinding oldBindings) Return a MappingInfo containing a match namespaces. Specifically, return aMappingInfo info
is such thatinfo.namespaces
is equal tonew NamespaceBinding(prefix, uri, oldBindings)
, whereuri
isnew String(uriChars, uriStart, uriLength).intern())
. -
endAttribute
public void endAttribute()Description copied from interface:Consumer
End of an attribute or end of an actual parameter. The former use matches a startAttribute; the latter may not, and can be used to separate parameters in a parameter list. This double duty suggsts the method should at least be re-named.- Specified by:
endAttribute
in interfaceConsumer
-
checkWriteAtomic
protected boolean checkWriteAtomic() -
write
public void write(int v) -
writeBoolean
public void writeBoolean(boolean v) - Specified by:
writeBoolean
in interfaceConsumer
-
writeFloat
public void writeFloat(float v) - Specified by:
writeFloat
in interfaceConsumer
-
writeDouble
public void writeDouble(double v) - Specified by:
writeDouble
in interfaceConsumer
-
writeInt
public void writeInt(int v) -
writeLong
public void writeLong(long v) -
writeDocumentUri
-
writePosition
Description copied from interface:PositionConsumer
Consume node at current position. The caller may invalidate or change the position after consume returns, so if the consumer wants to save it, it needs to copy it.- Specified by:
writePosition
in interfacePositionConsumer
-
writePosition
Description copied from interface:PositionConsumer
Consume a single position pair. This PositionConsumer may assume the sequence does no reference management; i.e. that copyPos is trivial and releasePos is a no-op. If that is not the case, use consume(TreePosition) instead.- Specified by:
writePosition
in interfacePositionConsumer
-
writeObject
If v is a node, make a copy of it.- Specified by:
writeObject
in interfaceConsumer
-
write
public void write(char[] data, int start, int length) Process raw text. -
write
-
write
-
linefeedFromParser
public void linefeedFromParser() -
textFromParser
public void textFromParser(char[] data, int start, int length) -
writeJoiner
protected void writeJoiner() -
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."<![CDATA["
and"]]>"
are excluded).- Specified by:
writeCDATA
in interfaceXConsumer
-
startElementCommon
protected void startElementCommon() -
emitStartElement
public void emitStartElement(char[] data, int start, int count) Process a start tag, with the given element name. -
startElement
- Specified by:
startElement
in interfaceConsumer
-
startAttribute
Description copied from interface:Consumer
Write a attribute for the current element. This is only allowed immediately after a startElement.- Specified by:
startAttribute
in interfaceConsumer
-
emitStartAttribute
public void emitStartAttribute(char[] data, int start, int count) Process an attribute, with the given attribute name. The attribute value is given usingwrite
. The value is terminated by either another emitStartAttribute or an emitEndAttributes. -
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'<br/>'
) has a name==null. -
endElement
public void endElement()- Specified by:
endElement
in interfaceConsumer
-
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 """. -
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. -
checkValidComment
protected void checkValidComment(char[] chars, int offset, int length) -
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).- Specified by:
writeComment
in interfaceXConsumer
-
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). -
writeProcessingInstruction
- Specified by:
writeProcessingInstruction
in interfaceXConsumer
-
processingInstructionFromParser
public void processingInstructionFromParser(char[] buffer, int tstart, int tlength, int dstart, int dlength) Process a processing instruction. -
startDocument
public void startDocument()- Specified by:
startDocument
in interfaceConsumer
- Specified by:
startDocument
in interfaceContentHandler
- Specified by:
startDocument
in interfaceDocumentHandler
-
endDocument
public void endDocument()- Specified by:
endDocument
in interfaceConsumer
- Specified by:
endDocument
in interfaceContentHandler
- Specified by:
endDocument
in interfaceDocumentHandler
-
emitDoctypeDecl
public void emitDoctypeDecl(char[] buffer, int target, int tlength, int data, int dlength) Process a DOCTYPE declaration. -
beginEntity
- Specified by:
beginEntity
in interfaceXConsumer
-
endEntity
public void endEntity() -
append
- Specified by:
append
in interfaceAppendable
- Specified by:
append
in interfaceConsumer
-
append
- Specified by:
append
in interfaceAppendable
- Specified by:
append
in interfaceConsumer
-
append
- Specified by:
append
in interfaceAppendable
- Specified by:
append
in interfaceConsumer
-
duplicateAttributeMessage
-
error
-
ignoring
public boolean ignoring()Description copied from interface:Consumer
True if consumer is ignoring rest of element. The producer can use this information to skip ahead. -
setDocumentLocator
- Specified by:
setDocumentLocator
in interfaceContentHandler
- Specified by:
setDocumentLocator
in interfaceDocumentHandler
-
startElement
- Specified by:
startElement
in interfaceContentHandler
-
endElement
- Specified by:
endElement
in interfaceContentHandler
-
startElement
- Specified by:
startElement
in interfaceDocumentHandler
-
endElement
- Specified by:
endElement
in interfaceDocumentHandler
- Throws:
SAXException
-
characters
- Specified by:
characters
in interfaceContentHandler
- Specified by:
characters
in interfaceDocumentHandler
- Throws:
SAXException
-
ignorableWhitespace
- Specified by:
ignorableWhitespace
in interfaceContentHandler
- Specified by:
ignorableWhitespace
in interfaceDocumentHandler
- Throws:
SAXException
-
processingInstruction
- Specified by:
processingInstruction
in interfaceContentHandler
- Specified by:
processingInstruction
in interfaceDocumentHandler
-
startPrefixMapping
- Specified by:
startPrefixMapping
in interfaceContentHandler
-
endPrefixMapping
- Specified by:
endPrefixMapping
in interfaceContentHandler
-
skippedEntity
- Specified by:
skippedEntity
in interfaceContentHandler
-
getPublicId
- Specified by:
getPublicId
in interfaceLocator
- Specified by:
getPublicId
in interfaceSourceLocator
- Specified by:
getPublicId
in interfaceSourceLocator
-
getSystemId
- Specified by:
getSystemId
in interfaceLocator
- Specified by:
getSystemId
in interfaceSourceLocator
- Specified by:
getSystemId
in interfaceSourceLocator
-
getFileName
Description copied from interface:SourceLocator
Normally same as getSystemId.- Specified by:
getFileName
in interfaceSourceLocator
-
getLineNumber
public int getLineNumber()Description copied from interface:SourceLocator
Return current line number. Normally the same asgetStartLine()
. The "first" line is line 1; unknown is -1.- Specified by:
getLineNumber
in interfaceLocator
- Specified by:
getLineNumber
in interfaceSourceLocator
- Specified by:
getLineNumber
in interfaceSourceLocator
-
getColumnNumber
public int getColumnNumber()Description copied from interface:SourceLocator
Return current column number. Normally the same asgetStartColumn()
. The "first" column is column 1; unknown is -1.- Specified by:
getColumnNumber
in interfaceLocator
- Specified by:
getColumnNumber
in interfaceSourceLocator
- Specified by:
getColumnNumber
in interfaceSourceLocator
-
getStartLine
public int getStartLine()Description copied from interface:SourceLocator
Line number (one-origin) of start of range; unknown/unspecified is -1.- Specified by:
getStartLine
in interfaceSourceLocator
-
getStartColumn
public int getStartColumn()Description copied from interface:SourceLocator
Column (one-origin) of start of range; unknown/unspecified is -1.- Specified by:
getStartColumn
in interfaceSourceLocator
-
getEndLine
public int getEndLine()Description copied from interface:SourceLocator
Line number (one-origin) of end of range; unknown/unspecified is -1.- Specified by:
getEndLine
in interfaceSourceLocator
-
getEndColumn
public int getEndColumn()Description copied from interface:SourceLocator
Column (one-origin) of end of range; unknown/unspecified is -1.- Specified by:
getEndColumn
in interfaceSourceLocator
-
isStableSourceLocation
public boolean isStableSourceLocation()Description copied from interface:SourceLocator
True if position is unlikely to change. True for an expression but not an input file.- Specified by:
isStableSourceLocation
in interfaceSourceLocator
-