Class IncrementalSAXSource_Filter
- All Implemented Interfaces:
Runnable,IncrementalSAXSource,ContentHandler,DTDHandler,ErrorHandler,LexicalHandler
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 Summary
ConstructorsConstructorDescriptionIncrementalSAXSource_Filter(CoroutineManager co, int controllerCoroutineID) Create a IncrementalSAXSource_Filter which is not yet bound to a specific SAX event source. -
Method Summary
Modifier and TypeMethodDescriptionvoidcharacters(char[] ch, int start, int length) voidcomment(char[] ch, int start, int length) static IncrementalSAXSourcecreateIncrementalSAXSource(CoroutineManager co, int controllerCoroutineID) deliverMoreNodes(boolean parsemore) deliverMoreNodes() is a simple API which tells the coroutine parser that we need more nodes.voidendCDATA()voidvoidendDTD()voidendElement(String namespaceURI, String localName, String qName) voidvoidendPrefixMapping(String prefix) voiderror(SAXParseException exception) voidfatalError(SAXParseException exception) intintvoidignorableWhitespace(char[] ch, int start, int length) voidinit(CoroutineManager co, int controllerCoroutineID, int sourceCoroutineID) voidnotationDecl(String a, String b, String c) voidprocessingInstruction(String target, String data) voidrun()voidsetContentHandler(ContentHandler handler) Register a SAX-style content handler for us to output tovoidsetDocumentLocator(Locator locator) voidsetDTDHandler(DTDHandler handler) Register a SAX-style DTD handler for us to output tovoidsetErrHandler(ErrorHandler handler) voidsetLexicalHandler(LexicalHandler handler) Register a SAX-style lexical handler for us to output tovoidsetReturnFrequency(int events) voidsetXMLReader(XMLReader eventsource) Bind our input streams to an XMLReader.voidskippedEntity(String name) voidvoidvoidvoidstartElement(String namespaceURI, String localName, String qName, Attributes atts) voidstartEntity(String name) voidstartParse(InputSource source) Launch a thread that will run an XMLReader's parse() operation within a thread, feeding events to this IncrementalSAXSource_Filter.voidstartPrefixMapping(String prefix, String uri) voidunparsedEntityDecl(String a, String b, String c, String d) voidwarning(SAXParseException exception) Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.xml.sax.ContentHandler
declaration
-
Constructor Details
-
IncrementalSAXSource_Filter
public IncrementalSAXSource_Filter() -
IncrementalSAXSource_Filter
Create a IncrementalSAXSource_Filter which is not yet bound to a specific SAX event source.
-
-
Method Details
-
createIncrementalSAXSource
public static IncrementalSAXSource createIncrementalSAXSource(CoroutineManager co, int controllerCoroutineID) -
init
-
setXMLReader
Bind our input streams to an XMLReader. Just a convenience routine; obviously you can explicitly register this as a listener with the same effect. -
setContentHandler
Description copied from interface:IncrementalSAXSourceRegister a SAX-style content handler for us to output to- Specified by:
setContentHandlerin interfaceIncrementalSAXSource
-
setDTDHandler
Description copied from interface:IncrementalSAXSourceRegister a SAX-style DTD handler for us to output to- Specified by:
setDTDHandlerin interfaceIncrementalSAXSource
-
setLexicalHandler
Description copied from interface:IncrementalSAXSourceRegister a SAX-style lexical handler for us to output to- Specified by:
setLexicalHandlerin interfaceIncrementalSAXSource
-
setErrHandler
-
setReturnFrequency
public void setReturnFrequency(int events) -
characters
- Specified by:
charactersin interfaceContentHandler- Throws:
SAXException
-
endDocument
- Specified by:
endDocumentin interfaceContentHandler- Throws:
SAXException
-
endElement
- Specified by:
endElementin interfaceContentHandler- Throws:
SAXException
-
endPrefixMapping
- Specified by:
endPrefixMappingin interfaceContentHandler- Throws:
SAXException
-
ignorableWhitespace
- Specified by:
ignorableWhitespacein interfaceContentHandler- Throws:
SAXException
-
processingInstruction
- Specified by:
processingInstructionin interfaceContentHandler- Throws:
SAXException
-
setDocumentLocator
- Specified by:
setDocumentLocatorin interfaceContentHandler
-
skippedEntity
- Specified by:
skippedEntityin interfaceContentHandler- Throws:
SAXException
-
startDocument
- Specified by:
startDocumentin interfaceContentHandler- Throws:
SAXException
-
startElement
public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException - Specified by:
startElementin interfaceContentHandler- Throws:
SAXException
-
startPrefixMapping
- Specified by:
startPrefixMappingin interfaceContentHandler- Throws:
SAXException
-
comment
- Specified by:
commentin interfaceLexicalHandler- Throws:
SAXException
-
endCDATA
- Specified by:
endCDATAin interfaceLexicalHandler- Throws:
SAXException
-
endDTD
- Specified by:
endDTDin interfaceLexicalHandler- Throws:
SAXException
-
endEntity
- Specified by:
endEntityin interfaceLexicalHandler- Throws:
SAXException
-
startCDATA
- Specified by:
startCDATAin interfaceLexicalHandler- Throws:
SAXException
-
startDTD
- Specified by:
startDTDin interfaceLexicalHandler- Throws:
SAXException
-
startEntity
- Specified by:
startEntityin interfaceLexicalHandler- Throws:
SAXException
-
notationDecl
- Specified by:
notationDeclin interfaceDTDHandler- Throws:
SAXException
-
unparsedEntityDecl
- Specified by:
unparsedEntityDeclin interfaceDTDHandler- Throws:
SAXException
-
error
- Specified by:
errorin interfaceErrorHandler- Throws:
SAXException
-
fatalError
- Specified by:
fatalErrorin interfaceErrorHandler- Throws:
SAXException
-
warning
- Specified by:
warningin interfaceErrorHandler- Throws:
SAXException
-
getSourceCoroutineID
public int getSourceCoroutineID() -
getControllerCoroutineID
public int getControllerCoroutineID() -
getCoroutineManager
- Returns:
- the CoroutineManager this CoroutineFilter object is bound to. If you're using the do...() methods, applications should only need to talk to the CoroutineManager once, to obtain the application's Coroutine ID.
-
startParse
Launch a thread that will run an XMLReader's parse() operation within a thread, feeding events to this IncrementalSAXSource_Filter. Mostly a convenience routine, but has the advantage that -- since we invoked parse() -- we can halt parsing quickly via a StopException rather than waiting for the SAX stream to end by itself.- Specified by:
startParsein interfaceIncrementalSAXSource- Throws:
SAXException- is parse thread is already in progress or parsing can not be started.
-
run
public void run() -
deliverMoreNodes
deliverMoreNodes() is a simple API which tells the coroutine parser that we need more nodes. This is intended to be called from one of our partner routines, and serves to encapsulate the details of how incremental parsing has been achieved.- Specified by:
deliverMoreNodesin interfaceIncrementalSAXSource- Parameters:
parsemore- 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.- Returns:
- Boolean.TRUE if there may be more events available by invoking deliverMoreNodes() again. Boolean.FALSE if parsing has run to completion (or been terminated by deliverMoreNodes(false). Or an exception object if something malfunctioned. %REVIEW% We _could_ actually throw the exception, but that would require runinng deliverMoreNodes() in a try/catch... and for many applications, exception will be simply be treated as "not TRUE" in any case.
-