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

Thread-safe SAX parsing (was Re: Java based XML parsers for server use.)

  • From: David Megginson <david@m...>
  • To: "XML Developers' List" <xml-dev@i...>
  • Date: Fri, 7 May 1999 07:50:49 -0400 (EDT)

safe sax
Lars Marius Garshol writes:

 > * mads@p...
 > |
 > | Are there any Java based XML parsers that are suitable for server
 > | side use? One of the primary requirements for such a parser is that
 > | should be thread safe. i.e.., more than one thread should be able to
 > | use the parser simultaneously.
 > I have to confess that I haven't looked at this aspect of parsers


You could start with this:

import java.io.IOException;
import java.util.Locale;

import org.xml.sax.Parser;
import org.xml.sax.InputSource;
import org.xml.sax.EntityResolver;
import org.xml.sax.DTDHandler;
import org.xml.sax.DocumentHandler;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;

public class SafeParser implements Parser
  public SafeParser (Parser parser)
    this.parser = parser;

  public synchronized void setLocale (Locale locale)
    throws SAXException

  public synchronized void setEntityResolver (EntityResolver resolver)

  public synchronized void setDTDHandler (DTDHandler handler)

  public synchronized void setDocumentHandler (DocumentHandler handler)

  public synchronized void setErrorHandler (ErrorHandler handler)

  public synchronized void parse (InputSource source)
    throws SAXException, IOException

  public synchronized void parse (String systemId)
    throws SAXException, IOException

  private Parser parser;

Although I am far from an expert in concurrent programming, my feeble
brain thinks that this should work provided that the following
conditions hold true:

1. the implementor of the original parser doesn't use static variables
   to store parse information -- in other words, the parser is
   reentrant (they all should be: anyone competent enough to write an
   XML parser in the first place would probably write a reentrant
   one); and 

2. no two instantiations of the parser use the same Reader or
   InputStream instantiation (that would be dumb anyway).

Once the parse begins, the SAX parser itself controls flow of
processing, so I cannot imagine how another thread could mess up the
actual parsing (there's no way to change the parser's state externally
once it begins parsing).  To be perfectly safe, you could always
synchronize on the InputSource and/or the InputStream/Reader that you
are using.

Perhaps people with more experience in Java concurrent programming can 
take (friendly) shots at this suggestion.

All the best,


David Megginson                 david@m...

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/ and on CD-ROM/ISBN 981-02-3594-1
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...)


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.
First Name
Last Name
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.