gnu.xml

Class XMLFilter

Implemented Interfaces:
ContentHandler, DocumentHandler, org.xml.sax.Locator, Consumer, PositionConsumer, SourceLocator, XConsumer

public class XMLFilter
extends java.lang.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

static int
COPY_NAMESPACES_INHERIT
static int
COPY_NAMESPACES_PRESERVE
int
copyNamespacesMode
protected int
ignoringLevel
Postive if all output should be ignored.
boolean
namespacePrefixes
True if namespace declarations should be passed through as attributes.
protected int
nesting
Twice the number of active startElement and startDocument calls.
Consumer
out
The specified target Consumer that accepts the output.
protected int
stringizingElementNesting
Value of nesting just before outermost startElement while stringizingLevel > 0.
protected int
stringizingLevel
If stringizingLevel > 0 then stringize rather than copy nodes.

Constructor Summary

XMLFilter(Consumer out)

Method Summary

void
beginEntity(Object baseUri)
void
characters(ch[] , int start, int length)
protected void
checkValidComment(char[] chars, int offset, int length)
protected boolean
checkWriteAtomic()
void
commentFromParser(char[] chars, int start, int length)
Process a comment, when called from an XML parser.
void
consume(SeqPosition position)
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
emitEndAttributes()
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
endAttribute()
End of an attribute or end of an actual parameter.
void
endDocument()
void
endElement()
void
endElement(String name)
void
endElement(String namespaceURI, String localName, String qName)
void
endEntity()
void
endPrefixMapping(String prefix)
void
error(char severity, String message)
NamespaceBinding
findNamespaceBinding(String prefix, String uri, NamespaceBinding oldBindings)
Functionally equivalent to new NamespaceBinding(prefix, uri, oldBindings, but uses "hash consing".
int
getColumnNumber()
String
getFileName()
int
getLineNumber()
String
getPublicId()
String
getSystemId()
void
ignorableWhitespace(ch[] , int start, int length)
boolean
ignoring()
True if consumer is ignoring rest of element.
boolean
isStableSourceLocation()
void
linefeedFromParser()
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(LineBufferedReader in)
void
setSourceLocator(SourceLocator locator)
void
skippedEntity(String name)
void
startAttribute(Object attrType)
Write a attribute for the current element.
void
startDocument()
void
startElement(Object type)
void
startElement(String name, AttributeList atts)
void
startElement(String namespaceURI, String localName, String qName, Attributes atts)
protected void
startElementCommon()
void
startPrefixMapping(String prefix, String uri)
void
textFromParser(char[] data, int start, int length)
void
write(CharSequence str, int start, int length)
void
write(String str)
void
write(char[] data, int start, int length)
Process raw text.
void
write(int v)
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
writeJoiner()
void
writeLong(long v)
void
writeObject(Object v)
If v is a node, make a copy of it.
void
writePosition(AbstractSequence seq, int ipos)
void
writeProcessingInstruction(String target, char[] content, int offset, int length)

Field Details

COPY_NAMESPACES_INHERIT

public static final int COPY_NAMESPACES_INHERIT
Field Value:
2

COPY_NAMESPACES_PRESERVE

public static final int COPY_NAMESPACES_PRESERVE
Field Value:
1

copyNamespacesMode

public int copyNamespacesMode

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 ==out or ==tlist.

stringizingElementNesting

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

stringizingLevel

protected int stringizingLevel
If 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 Details

XMLFilter

public XMLFilter(Consumer out)

Method Details

beginEntity

public void beginEntity(Object baseUri)
Specified by:
beginEntity in interface XConsumer

characters

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

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. "<!--" and "-->" are excluded).

consume

public void consume(SeqPosition position)
Specified by:
consume in interface PositionConsumer

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 '
'
) 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 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()
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 interface Consumer

endDocument

public void endDocument()
Specified by:
endDocument in interface Consumer

endElement

public void endElement()
Specified by:
endElement in interface Consumer

endElement

public void endElement(String name)
            throws SAXException

endElement

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

endEntity

public void endEntity()
Specified by:
endEntity in interface XConsumer

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 new NamespaceBinding(prefix, uri, oldBindings, but uses "hash consing".

getColumnNumber

public int getColumnNumber()
Specified by:
getColumnNumber in interface SourceLocator

getFileName

public String getFileName()
Specified by:
getFileName in interface SourceLocator

getLineNumber

public int getLineNumber()
Specified by:
getLineNumber in interface SourceLocator

getPublicId

public String getPublicId()
Specified by:
getPublicId in interface SourceLocator

getSystemId

public String getSystemId()
Specified by:
getSystemId in interface SourceLocator

ignorableWhitespace

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

ignoring

public boolean ignoring()
True if consumer is ignoring rest of element. The producer can use this information to skip ahead.
Specified by:
ignoring in interface Consumer

isStableSourceLocation

public boolean isStableSourceLocation()
Specified by:
isStableSourceLocation in interface SourceLocator

linefeedFromParser

public void linefeedFromParser()

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 a MappingInfo info is such that info.namespaces is equal to new NamespaceBinding(prefix, uri, oldBindings), where uri is 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)
Write a attribute for the current element. This is only allowed immediately after a startElement.
Specified by:
startAttribute in interface Consumer

startDocument

public void startDocument()
Specified by:
startDocument in interface Consumer

startElement

public void startElement(Object type)
Specified by:
startElement in interface Consumer

startElement

public void startElement(String name,
                         AttributeList atts)

startElement

public void startElement(String namespaceURI,
                         String localName,
                         String qName,
                         Attributes 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(CharSequence str,
                  int start,
                  int length)
Specified by:
write in interface Consumer

write

public void write(String str)
Specified by:
write in interface Consumer

write

public void write(char[] data,
                  int start,
                  int length)
Process raw text.
Specified by:
write in interface Consumer

write

public void write(int v)
Specified by:
write in interface Consumer

writeBoolean

public void writeBoolean(boolean v)
Specified by:
writeBoolean in interface Consumer

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. " and "]]>" are excluded).
Specified by:
writeCDATA in interface XConsumer


writeDocumentUri

public void writeDocumentUri(Object uri)




writeJoiner

protected void writeJoiner()