[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

SAX, expat and JNI

  • From: Joerg Brunsmann <Joerg.Brunsmann@F...>
  • To: xml-dev@i...
  • Date: Tue, 21 Apr 1998 17:45:10 +0200

sax expat
Hi,

assuming that interpreted Java byte code is still slower than compiled C
code, it is desirable to use James' expat XML Parser in conjunction with
SAX to gain maximum performance while parsing XML documents. Therefore I
suggest to use the Java native method interface (JNI) to invoke expat.
How can this be achieved? First of all, we need a shared library (on
Windows a DLL) which contains the expat code and in addition to that
these handlers:

---------------------- start of C code ----------------------------

static void characterData(void *userData, const char *s, int len) {
  callback into Java VM here for
    expatHandler.doCharacterData
}

static void startElement(void *userData, 
  const char *name, 
  const char **atts)
{
  callback into Java VM here for
    expatHandler.doStartElement
}

static void endElement(void *userData, const char *name)
{
  callback into Java VM here for
    expatHandler.doEndElement
}

static void processingInstruction(void *userData, 
  const char *target, 
  const char * data)
{
  callback into Java VM here for
    expatHandler.doProcessingInstruction
}

void initParser() {
  XML_Parser parser = XML_ParserCreate(encoding);
  XML_SetElementHandler(parser, startElement, endElement);
  XML_SetCharacterDataHandler(parser, characterData);
  XML_SetProcessingInstructionHandler(parser, processingInstruction);
}

void doParse() {
 XML_Parse(parser, data, size, 1));
}
---------------------- end of c code -----------------------------

We now can define the Java class which interfaces to the expat parser
contained in a shared libray. This java code look like this:

----------------- start of expatHandler --------------------------

public class expatHandler {

  void doCharacterData(String s, int len) {
    documentHandler.characters(s);
  }

  void doStartElement(String name, String[] atts) {
    documentHandler.startElement(s,convertAttsToAttributeMap(atts));
  }

  void doEndElement(String name) {
    documentHandler.endElement(name);
  }

  void doProcessingInstruction(String target, String data) {
    documentHandler.processingInstruction (target,data);
  }

  native public void doParse();
  native public void initParser();

  static {
    loadLibrary("expat");
  }
}

----------------- end of expatHandler -------------------------

It is then straightforward to extend this expatHandler to declare the
SAX driver for expat:

----------------- start of expat driver ------------------------


package com.microstar.sax;

/**
  * A SAX driver for James Clark's expat XML parser 
  */

public class expatDriver extends expatHandler
  implements org.xml.sax.Parser {

  public void setEntityHandler (EntityHandler handler); {
    this.entityHandler = handler;
  }

  public void setDocumentHandler (DocumentHandler handler) {
    this.documentHandler = handler;
  }

  public void setErrorHandler (ErrorHandler handler) {
    this.errorHandler = handler;
  }

  public void parse (String publicId, String systemId)
    throws java.lang.Exception
  {
    ...
    initParser();
    documentHandler.startDocument();
    doParse();
    ...
    documentHandler.endDocument();
    ...
  }

}

----------------- end of expat driver -----------------------


Does this make sense? Comments?

Who will volunteer? ;-)

Cheers,

Joerg

xml-dev: A list for W3C XML Developers. To post, mailto:xml-dev@i...
Archived as: http://www.lists.ic.ac.uk/hypermail/xml-dev/
To (un)subscribe, mailto:majordomo@i... the following message;
(un)subscribe xml-dev
To subscribe to the digests, mailto:majordomo@i... the following message;
subscribe xml-dev-digest
List coordinator, Henry Rzepa (mailto:rzepa@i...)


PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.