org.apache.xml.dtm.ref

Class IncrementalSAXSource_Filter

Implemented Interfaces:
ContentHandler, DTDHandler, ErrorHandler, IncrementalSAXSource, LexicalHandler, Runnable

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 Summary

IncrementalSAXSource_Filter()
IncrementalSAXSource_Filter(CoroutineManager co, int controllerCoroutineID)
Create a IncrementalSAXSource_Filter which is not yet bound to a specific SAX event source.

Method Summary

void
characters(char[] ch, int start, int length)
void
comment(char[] ch, int start, int length)
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
endDTD()
void
endDocument()
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
setDTDHandler(DTDHandler handler)
void
setDocumentLocator(Locator locator)
void
setErrHandler(ErrorHandler handler)
void
setLexicalHandler(LexicalHandler handler)
void
setReturnFrequency(int events)
void
setXMLReader(XMLReader eventsource)
Bind our input streams to an XMLReader.
void
skippedEntity(String name)
void
startCDATA()
void
startDTD(String name, String publicId, String systemId)
void
startDocument()
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)

Constructor Details

IncrementalSAXSource_Filter

public IncrementalSAXSource_Filter()

IncrementalSAXSource_Filter

public IncrementalSAXSource_Filter(CoroutineManager co,
                                   int controllerCoroutineID)
Create a IncrementalSAXSource_Filter which is not yet bound to a specific SAX event source.

Method Details

characters

public void characters(char[] ch,
                       int start,
                       int length)
            throws org.xml.sax.SAXException

comment

public void comment(char[] ch,
                    int start,
                    int length)
            throws org.xml.sax.SAXException

createIncrementalSAXSource

public static IncrementalSAXSource createIncrementalSAXSource(CoroutineManager co,
                                                              int controllerCoroutineID)

deliverMoreNodes

public Object deliverMoreNodes(boolean parsemore)
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:
deliverMoreNodes in interface IncrementalSAXSource
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.

endCDATA

public void endCDATA()
            throws org.xml.sax.SAXException

endDTD

public void endDTD()
            throws org.xml.sax.SAXException

endDocument

public void endDocument()
            throws org.xml.sax.SAXException

endElement

public void endElement(String namespaceURI,
                       String localName,
                       String qName)
            throws org.xml.sax.SAXException

endEntity

public void endEntity(String name)
            throws org.xml.sax.SAXException

endPrefixMapping

public void endPrefixMapping(String prefix)
            throws org.xml.sax.SAXException

error

public void error(SAXParseException exception)
            throws SAXException

fatalError

public void fatalError(SAXParseException exception)
            throws SAXException

getControllerCoroutineID

public int getControllerCoroutineID()

getCoroutineManager

public CoroutineManager 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.

getSourceCoroutineID

public int getSourceCoroutineID()

ignorableWhitespace

public void ignorableWhitespace(char[] ch,
                                int start,
                                int length)
            throws org.xml.sax.SAXException

init

public void init(CoroutineManager co,
                 int controllerCoroutineID,
                 int sourceCoroutineID)

notationDecl

public void notationDecl(String a,
                         String b,
                         String c)
            throws SAXException

processingInstruction

public void processingInstruction(String target,
                                  String data)
            throws org.xml.sax.SAXException

run

public void run()

setContentHandler

public void setContentHandler(ContentHandler handler)
Register a SAX-style content handler for us to output to
Specified by:
setContentHandler in interface IncrementalSAXSource

setDTDHandler

public void setDTDHandler(DTDHandler handler)

setDocumentLocator

public void setDocumentLocator(Locator locator)

setErrHandler

public void setErrHandler(ErrorHandler handler)

setLexicalHandler

public void setLexicalHandler(LexicalHandler handler)

setReturnFrequency

public void setReturnFrequency(int events)

setXMLReader

public void setXMLReader(XMLReader eventsource)
Bind our input streams to an XMLReader. Just a convenience routine; obviously you can explicitly register this as a listener with the same effect.

skippedEntity

public void skippedEntity(String name)
            throws org.xml.sax.SAXException

startCDATA

public void startCDATA()
            throws org.xml.sax.SAXException

startDTD

public void startDTD(String name,
                     String publicId,
                     String systemId)
            throws org.xml.sax.SAXException

startDocument

public void startDocument()
            throws org.xml.sax.SAXException

startElement

public void startElement(String namespaceURI,
                         String localName,
                         String qName,
                         Attributes atts)
            throws org.xml.sax.SAXException

startEntity

public void startEntity(String name)
            throws org.xml.sax.SAXException

startParse

public void startParse(InputSource source)
            throws SAXException
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:
startParse in interface IncrementalSAXSource

startPrefixMapping

public void startPrefixMapping(String prefix,
                               String uri)
            throws org.xml.sax.SAXException

unparsedEntityDecl

public void unparsedEntityDecl(String a,
                               String b,
                               String c,
                               String d)
            throws SAXException

warning

public void warning(SAXParseException exception)
            throws SAXException

Copyright B) 2004 Apache XML Project. All Rights Reserved.