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
Fields Modifier and Type Field Description static intCOPY_NAMESPACES_INHERITstatic intCOPY_NAMESPACES_MASKstatic intCOPY_NAMESPACES_PRESERVEintcopyNamespacesModeprotected intignoringLevelPositive if all output should be ignored.booleannamespacePrefixesTrue if namespace declarations should be passed through as attributes.protected intnestingTwice the number of active startElement and startDocument calls.ConsumeroutThe specified target Consumer that accepts the output.protected intstringizingElementNestingValue ofnestingjust before outermost startElement whilestringizingLevel > 0.protected intstringizingLevelIfstringizingLevel > 0then stringize rather than copy nodes.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description XMLFilterappend(char c)XMLFilterappend(CharSequence csq)XMLFilterappend(CharSequence csq, int start, int end)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.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()End of an attribute or end of an actual parameter.voidendDocument()voidendElement()voidendElement(String name)voidendElement(String namespaceURI, String localName, String qName)voidendEntity()voidendPrefixMapping(String prefix)voiderror(char severity, String message)NamespaceBindingfindNamespaceBinding(String prefix, String uri, NamespaceBinding oldBindings)Functionally equivalent tonew NamespaceBinding(prefix, uri, oldBindings, but uses "hash consing".intgetColumnNumber()Return current column number.intgetEndColumn()Column (one-origin) of end of range; unknown/unspecified is -1.intgetEndLine()Line number (one-origin) of end of range; unknown/unspecified is -1.StringgetFileName()Normally same as getSystemId.intgetLineNumber()Return current line number.StringgetPublicId()intgetStartColumn()Column (one-origin) of start of range; unknown/unspecified is -1.intgetStartLine()Line number (one-origin) of start of range; unknown/unspecified is -1.StringgetSystemId()voidignorableWhitespace(char[] ch, int start, int length)booleanignoring()True if consumer is ignoring rest of element.booleanisStableSourceLocation()True if position is unlikely to change.voidlinefeedFromParser()gnu.xml.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(gnu.kawa.io.InPort in)voidsetSourceLocator(SourceLocator locator)voidskippedEntity(String name)voidstartAttribute(Object attrType)Write a attribute for the current element.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(char[] data, int start, int length)Process raw text.voidwrite(int v)voidwrite(CharSequence str, int start, int length)voidwrite(String str)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)Consume a single position pair.voidwritePosition(SeqPosition position)Consume node at current position.voidwriteProcessingInstruction(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 java.util.function.DoubleConsumer
andThen
-
Methods inherited from interface java.util.function.IntConsumer
andThen
-
Methods inherited from interface java.util.function.LongConsumer
andThen
-
-
-
-
Field Detail
-
out
public Consumer out
The specified target Consumer that accepts the output. In contrast, base may be either==outor==tlist.
-
COPY_NAMESPACES_PRESERVE
public static final int COPY_NAMESPACES_PRESERVE
- See Also:
- Constant Field Values
-
COPY_NAMESPACES_INHERIT
public static final int COPY_NAMESPACES_INHERIT
- See Also:
- Constant Field Values
-
copyNamespacesMode
public transient int copyNamespacesMode
-
COPY_NAMESPACES_MASK
public static final int COPY_NAMESPACES_MASK
- See Also:
- Constant Field Values
-
nesting
protected int nesting
Twice the number of active startElement and startDocument calls.
-
stringizingLevel
protected int stringizingLevel
IfstringizingLevel > 0then 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 stringizingElementNesting
Value ofnestingjust 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 ignoringLevel
Positive 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 namespacePrefixes
True if namespace declarations should be passed through as attributes. Like SAX2's http://xml.org/features/namespace-prefixes.
-
-
Constructor Detail
-
XMLFilter
public XMLFilter(Consumer out)
-
-
Method Detail
-
setSourceLocator
public void setSourceLocator(gnu.kawa.io.InPort in)
-
setSourceLocator
public void setSourceLocator(SourceLocator locator)
-
setMessages
public void setMessages(SourceMessages messages)
-
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 infois such thatinfo.namespacesis equal tonew NamespaceBinding(prefix, uri, oldBindings), whereuriisnew String(uriChars, uriStart, uriLength).intern()).
-
endAttribute
public void endAttribute()
Description copied from interface:ConsumerEnd 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:
endAttributein interfaceConsumer
-
checkWriteAtomic
protected boolean checkWriteAtomic()
-
writeBoolean
public void writeBoolean(boolean v)
- Specified by:
writeBooleanin interfaceConsumer
-
writeFloat
public void writeFloat(float v)
- Specified by:
writeFloatin interfaceConsumer
-
writeDouble
public void writeDouble(double v)
- Specified by:
writeDoublein interfaceConsumer
-
writeDocumentUri
public void writeDocumentUri(Object uri)
-
writePosition
public void writePosition(SeqPosition position)
Description copied from interface:PositionConsumerConsume 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:
writePositionin interfacePositionConsumer
-
writePosition
public void writePosition(AbstractSequence seq, int ipos)
Description copied from interface:PositionConsumerConsume 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:
writePositionin interfacePositionConsumer
-
writeObject
public void writeObject(Object v)
If v is a node, make a copy of it.- Specified by:
writeObjectin interfaceConsumer
-
write
public void write(char[] data, int start, int length)Process raw text.
-
write
public void write(CharSequence str, int start, int length)
-
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:
writeCDATAin 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
public void startElement(Object type)
- Specified by:
startElementin interfaceConsumer
-
startAttribute
public void startAttribute(Object attrType)
Description copied from interface:ConsumerWrite a attribute for the current element. This is only allowed immediately after a startElement.- Specified by:
startAttributein 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:
endElementin 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:
writeCommentin 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
public void writeProcessingInstruction(String target, char[] content, int offset, int length)
- Specified by:
writeProcessingInstructionin 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:
startDocumentin interfaceConsumer- Specified by:
startDocumentin interfaceContentHandler- Specified by:
startDocumentin interfaceDocumentHandler
-
endDocument
public void endDocument()
- Specified by:
endDocumentin interfaceConsumer- Specified by:
endDocumentin interfaceContentHandler- Specified by:
endDocumentin interfaceDocumentHandler
-
emitDoctypeDecl
public void emitDoctypeDecl(char[] buffer, int target, int tlength, int data, int dlength)Process a DOCTYPE declaration.
-
beginEntity
public void beginEntity(Object baseUri)
- Specified by:
beginEntityin interfaceXConsumer
-
append
public XMLFilter append(char c)
- Specified by:
appendin interfaceAppendable- Specified by:
appendin interfaceConsumer
-
append
public XMLFilter append(CharSequence csq)
- Specified by:
appendin interfaceAppendable- Specified by:
appendin interfaceConsumer
-
append
public XMLFilter append(CharSequence csq, int start, int end)
- Specified by:
appendin interfaceAppendable- Specified by:
appendin interfaceConsumer
-
duplicateAttributeMessage
public static String duplicateAttributeMessage(Symbol attrSymbol, Object elementName)
-
error
public void error(char severity, String message)
-
ignoring
public boolean ignoring()
Description copied from interface:ConsumerTrue if consumer is ignoring rest of element. The producer can use this information to skip ahead.
-
setDocumentLocator
public void setDocumentLocator(Locator locator)
- Specified by:
setDocumentLocatorin interfaceContentHandler- Specified by:
setDocumentLocatorin interfaceDocumentHandler
-
startElement
public void startElement(String namespaceURI, String localName, String qName, Attributes atts)
- Specified by:
startElementin interfaceContentHandler
-
endElement
public void endElement(String namespaceURI, String localName, String qName)
- Specified by:
endElementin interfaceContentHandler
-
startElement
public void startElement(String name, AttributeList atts)
- Specified by:
startElementin interfaceDocumentHandler
-
endElement
public void endElement(String name) throws SAXException
- Specified by:
endElementin interfaceDocumentHandler- Throws:
SAXException
-
characters
public void characters(char[] ch, int start, int length) throws SAXException- Specified by:
charactersin interfaceContentHandler- Specified by:
charactersin interfaceDocumentHandler- Throws:
SAXException
-
ignorableWhitespace
public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException- Specified by:
ignorableWhitespacein interfaceContentHandler- Specified by:
ignorableWhitespacein interfaceDocumentHandler- Throws:
SAXException
-
processingInstruction
public void processingInstruction(String target, String data)
- Specified by:
processingInstructionin interfaceContentHandler- Specified by:
processingInstructionin interfaceDocumentHandler
-
startPrefixMapping
public void startPrefixMapping(String prefix, String uri)
- Specified by:
startPrefixMappingin interfaceContentHandler
-
endPrefixMapping
public void endPrefixMapping(String prefix)
- Specified by:
endPrefixMappingin interfaceContentHandler
-
skippedEntity
public void skippedEntity(String name)
- Specified by:
skippedEntityin interfaceContentHandler
-
getPublicId
public String getPublicId()
- Specified by:
getPublicIdin interfaceLocator- Specified by:
getPublicIdin interfaceSourceLocator- Specified by:
getPublicIdin interfaceSourceLocator
-
getSystemId
public String getSystemId()
- Specified by:
getSystemIdin interfaceLocator- Specified by:
getSystemIdin interfaceSourceLocator- Specified by:
getSystemIdin interfaceSourceLocator
-
getFileName
public String getFileName()
Description copied from interface:SourceLocatorNormally same as getSystemId.- Specified by:
getFileNamein interfaceSourceLocator
-
getLineNumber
public int getLineNumber()
Description copied from interface:SourceLocatorReturn current line number. Normally the same asgetStartLine(). The "first" line is line 1; unknown is -1.- Specified by:
getLineNumberin interfaceLocator- Specified by:
getLineNumberin interfaceSourceLocator- Specified by:
getLineNumberin interfaceSourceLocator
-
getColumnNumber
public int getColumnNumber()
Description copied from interface:SourceLocatorReturn current column number. Normally the same asgetStartColumn(). The "first" column is column 1; unknown is -1.- Specified by:
getColumnNumberin interfaceLocator- Specified by:
getColumnNumberin interfaceSourceLocator- Specified by:
getColumnNumberin interfaceSourceLocator
-
getStartLine
public int getStartLine()
Description copied from interface:SourceLocatorLine number (one-origin) of start of range; unknown/unspecified is -1.- Specified by:
getStartLinein interfaceSourceLocator
-
getStartColumn
public int getStartColumn()
Description copied from interface:SourceLocatorColumn (one-origin) of start of range; unknown/unspecified is -1.- Specified by:
getStartColumnin interfaceSourceLocator
-
getEndLine
public int getEndLine()
Description copied from interface:SourceLocatorLine number (one-origin) of end of range; unknown/unspecified is -1.- Specified by:
getEndLinein interfaceSourceLocator
-
getEndColumn
public int getEndColumn()
Description copied from interface:SourceLocatorColumn (one-origin) of end of range; unknown/unspecified is -1.- Specified by:
getEndColumnin interfaceSourceLocator
-
isStableSourceLocation
public boolean isStableSourceLocation()
Description copied from interface:SourceLocatorTrue if position is unlikely to change. True for an expression but not an input file.- Specified by:
isStableSourceLocationin interfaceSourceLocator
-
-