xml-general mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Nick Kew <n...@webthing.com>
Subject Re: How to start writing a non-blocking SAX parser
Date Sun, 28 Apr 2002 12:44:14 GMT

On Sun, 28 Apr 2002, Miguel A Paraz wrote:

> On Fri, Apr 26, 2002 at 06:20:00PM +0100, Nick Kew wrote:
> > On Sat, 27 Apr 2002, Miguel A Paraz wrote:
> >
> > > I'm just going through the stream and calling the content handler in the
> > > same way the blocking parse() method does.
> >
> > This is normal with SAX in those libraries I've used (expat, Xerces,
> > libxml2).  You just need to select a parse mode that gets its input
> > via a callback, and then provide a non-blocking callback.  Indeed,
> > you can do the same with DOM, though there's less to gain by it.
>
> I couldn't find a SAX parse mode that gets input via a callback.
> The variations of SAXParser.parse() vary on their input (File, InputSource,
> InputStream or URI).  What did I miss?

OK, I've now uploaded an update to mod_xml to do exactly this.
Your language looks like the Xerces SAX implementation, so why
don't I just cut & paste from mod_xml:

class APInputSource : public InputSource {
  class APInputStream : public BinInputStream {
    request_rec* r ;
    unsigned int pos ;
    APInputStream(request_rec* rec) : r(rec), pos(0) {}
    unsigned int readBytes( XMLByte* const toFill,
	 const unsigned int maxsz) {
      unsigned int bytes = ap_get_client_block(r, (char*)toFill, maxsz) ;
      pos += bytes ;
      return bytes ;
    }
    unsigned int curPos() const { return pos ; }
    friend APInputSource ;
  } ;
  request_rec* r ;
public:
  APInputSource(request_rec* rec) : r(rec) {}
  BinInputStream* makeStream() const {
    if ( ap_setup_client_block(r, REQUEST_CHUNKED_ERROR) != 0 )
          throw exception() ;	/* this should be relaxed but I
				   still need to RTFM */
    ap_should_client_block( r ) ;
    return new APInputStream ( r ) ;
  }
} ;

So a parser - either SAX or DOM - just needs:

   APInputSource mbuf(r) ;
   parser->parse(mbuf) ;

and it works with exactly the data Apache feeds it, as the data arrive.

The libxml2 implementation is very similar, while expat makes it even
easier.


-- 
Nick Kew

Available for contract work - Programming, Unix, Networking, Markup, etc.


---------------------------------------------------------------------
In case of troubles, e-mail:     webmaster@xml.apache.org
To unsubscribe, e-mail:          general-unsubscribe@xml.apache.org
For additional commands, e-mail: general-help@xml.apache.org


Mime
View raw message