public class IncrementalSAXSource_Filter extends Object implements IncrementalSAXSource, ContentHandler, DTDHandler, LexicalHandler, ErrorHandler, Runnable
IncrementalSAXSource_Filter implements IncrementalSAXSource, using a standard SAX2 event source as its input and parcelling out those events gradually in reponse to deliverMoreNodes() requests. Output from the filter will be passed along to a SAX handler registered as our listener, but those callbacks will pass through a counting stage which periodically yields control back to the controller coroutine.
%REVIEW%: This filter is not currenly intended to be reusable for parsing additional streams/documents. We may want to consider making it resettable at some point in the future. But it's a small object, so that'd be mostly a convenience issue; the cost of allocating each time is trivial compared to the cost of processing any nontrival stream.
For a brief usage example, see the unit-test main() method.
This is a simplification of the old CoroutineSAXParser, focusing specifically on filtering. The resulting controller protocol is _far_ simpler and less error-prone; the only controller operation is deliverMoreNodes(), and the only requirement is that deliverMoreNodes(false) be called if you want to discard the rest of the stream and the previous deliverMoreNodes() didn't return false.
| Constructor and Description |
|---|
IncrementalSAXSource_Filter() |
IncrementalSAXSource_Filter(CoroutineManager co,
int controllerCoroutineID)
Create a IncrementalSAXSource_Filter which is not yet bound to a specific
SAX event source.
|
| Modifier and Type | Method and Description |
|---|---|
void |
characters(char[] ch,
int start,
int length) |
void |
comment(char[] ch,
int start,
int length) |
protected void |
count_and_yield(boolean moreExpected)
In the SAX delegation code, I've inlined the count-down in
the hope of encouraging compilers to deliver better
performance.
|
static IncrementalSAXSource |
createIncrementalSAXSource(CoroutineManager co,
int controllerCoroutineID) |
Object |
deliverMoreNodes(boolean parsemore)
deliverMoreNodes() is a simple API which tells the coroutine
parser that we need more nodes.
|
void |
endCDATA() |
void |
endDocument() |
void |
endDTD() |
void |
endElement(String namespaceURI,
String localName,
String qName) |
void |
endEntity(String name) |
void |
endPrefixMapping(String prefix) |
void |
error(SAXParseException exception) |
void |
fatalError(SAXParseException exception) |
int |
getControllerCoroutineID() |
CoroutineManager |
getCoroutineManager() |
int |
getSourceCoroutineID() |
void |
ignorableWhitespace(char[] ch,
int start,
int length) |
void |
init(CoroutineManager co,
int controllerCoroutineID,
int sourceCoroutineID) |
void |
notationDecl(String a,
String b,
String c) |
void |
processingInstruction(String target,
String data) |
void |
run() |
void |
setContentHandler(ContentHandler handler)
Register a SAX-style content handler for us to output to
|
void |
setDocumentLocator(Locator locator) |
void |
setDTDHandler(DTDHandler handler)
Register a SAX-style DTD handler for us to output to
|
void |
setErrHandler(ErrorHandler handler) |
void |
setLexicalHandler(LexicalHandler handler)
Register a SAX-style lexical handler for us to output to
|
void |
setReturnFrequency(int events) |
void |
setXMLReader(XMLReader eventsource)
Bind our input streams to an XMLReader.
|
void |
skippedEntity(String name) |
void |
startCDATA() |
void |
startDocument() |
void |
startDTD(String name,
String publicId,
String systemId) |
void |
startElement(String namespaceURI,
String localName,
String qName,
Attributes atts) |
void |
startEntity(String name) |
void |
startParse(InputSource source)
Launch a thread that will run an XMLReader's parse() operation within
a thread, feeding events to this IncrementalSAXSource_Filter.
|
void |
startPrefixMapping(String prefix,
String uri) |
void |
unparsedEntityDecl(String a,
String b,
String c,
String d) |
void |
warning(SAXParseException exception) |
public IncrementalSAXSource_Filter()
public IncrementalSAXSource_Filter(CoroutineManager co, int controllerCoroutineID)
public static IncrementalSAXSource createIncrementalSAXSource(CoroutineManager co, int controllerCoroutineID)
public void init(CoroutineManager co, int controllerCoroutineID, int sourceCoroutineID)
public void setXMLReader(XMLReader eventsource)
public void setContentHandler(ContentHandler handler)
IncrementalSAXSourcesetContentHandler in interface IncrementalSAXSourcepublic void setDTDHandler(DTDHandler handler)
IncrementalSAXSourcesetDTDHandler in interface IncrementalSAXSourcepublic void setLexicalHandler(LexicalHandler handler)
IncrementalSAXSourcesetLexicalHandler in interface IncrementalSAXSourcepublic void setErrHandler(ErrorHandler handler)
public void setReturnFrequency(int events)
public void characters(char[] ch,
int start,
int length)
throws SAXException
characters in interface ContentHandlerSAXExceptionpublic void endDocument()
throws SAXException
endDocument in interface ContentHandlerSAXExceptionpublic void endElement(String namespaceURI, String localName, String qName) throws SAXException
endElement in interface ContentHandlerSAXExceptionpublic void endPrefixMapping(String prefix) throws SAXException
endPrefixMapping in interface ContentHandlerSAXExceptionpublic void ignorableWhitespace(char[] ch,
int start,
int length)
throws SAXException
ignorableWhitespace in interface ContentHandlerSAXExceptionpublic void processingInstruction(String target, String data) throws SAXException
processingInstruction in interface ContentHandlerSAXExceptionpublic void setDocumentLocator(Locator locator)
setDocumentLocator in interface ContentHandlerpublic void skippedEntity(String name) throws SAXException
skippedEntity in interface ContentHandlerSAXExceptionpublic void startDocument()
throws SAXException
startDocument in interface ContentHandlerSAXExceptionpublic void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException
startElement in interface ContentHandlerSAXExceptionpublic void startPrefixMapping(String prefix, String uri) throws SAXException
startPrefixMapping in interface ContentHandlerSAXExceptionpublic void comment(char[] ch,
int start,
int length)
throws SAXException
comment in interface LexicalHandlerSAXExceptionpublic void endCDATA()
throws SAXException
endCDATA in interface LexicalHandlerSAXExceptionpublic void endDTD()
throws SAXException
endDTD in interface LexicalHandlerSAXExceptionpublic void endEntity(String name) throws SAXException
endEntity in interface LexicalHandlerSAXExceptionpublic void startCDATA()
throws SAXException
startCDATA in interface LexicalHandlerSAXExceptionpublic void startDTD(String name, String publicId, String systemId) throws SAXException
startDTD in interface LexicalHandlerSAXExceptionpublic void startEntity(String name) throws SAXException
startEntity in interface LexicalHandlerSAXExceptionpublic void notationDecl(String a, String b, String c) throws SAXException
notationDecl in interface DTDHandlerSAXExceptionpublic void unparsedEntityDecl(String a, String b, String c, String d) throws SAXException
unparsedEntityDecl in interface DTDHandlerSAXExceptionpublic void error(SAXParseException exception) throws SAXException
error in interface ErrorHandlerSAXExceptionpublic void fatalError(SAXParseException exception) throws SAXException
fatalError in interface ErrorHandlerSAXExceptionpublic void warning(SAXParseException exception) throws SAXException
warning in interface ErrorHandlerSAXExceptionpublic int getSourceCoroutineID()
public int getControllerCoroutineID()
public CoroutineManager getCoroutineManager()
protected void count_and_yield(boolean moreExpected)
throws SAXException
In the SAX delegation code, I've inlined the count-down in the hope of encouraging compilers to deliver better performance. However, if we subclass (eg to directly connect the output to a DTM builder), that would require calling super in order to run that logic... which seems inelegant. Hence this routine for the convenience of subclasses: every [frequency] invocations, issue a co_yield.
moreExepected - Should always be true unless this is being called
at the end of endDocument() handling.SAXExceptionpublic void startParse(InputSource source) throws SAXException
startParse in interface IncrementalSAXSourceSAXException - is parse thread is already in progress
or parsing can not be started.public Object deliverMoreNodes(boolean parsemore)
deliverMoreNodes in interface IncrementalSAXSourceparsemore - If true, tells the incremental filter to generate
another chunk of output. If false, tells the filter that we're
satisfied and it can terminate parsing of this document.Copyright © 2018 JBoss by Red Hat. All rights reserved.