[
Lists Home |
Date Index |
Thread Index
]
- From: David Megginson <david@megginson.com>
- To: "XML Developers' List" <xml-dev@ic.ac.uk>
- Date: Fri, 7 May 1999 07:50:49 -0400 (EDT)
Lars Marius Garshol writes:
> * mads@pacbell.net
> |
> | 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
[snip]
You could start with this:
====================8<====================8<====================
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)
{
super();
this.parser = parser;
}
public synchronized void setLocale (Locale locale)
throws SAXException
{
parser.setLocale(locale);
}
public synchronized void setEntityResolver (EntityResolver resolver)
{
parser.setEntityResolver(resolver);
}
public synchronized void setDTDHandler (DTDHandler handler)
{
parser.setDTDHandler(handler);
}
public synchronized void setDocumentHandler (DocumentHandler handler)
{
parser.setDocumentHandler(handler);
}
public synchronized void setErrorHandler (ErrorHandler handler)
{
parser.setErrorHandler(handler);
}
public synchronized void parse (InputSource source)
throws SAXException, IOException
{
parser.parse(source);
}
public synchronized void parse (String systemId)
throws SAXException, IOException
{
parser.parse(systemId);
}
private Parser parser;
}
====================8<====================8<====================
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
--
David Megginson david@megginson.com
http://www.megginson.com/
xml-dev: A list for W3C XML Developers. To post, mailto:xml-dev@ic.ac.uk
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@ic.ac.uk the following message;
(un)subscribe xml-dev
To subscribe to the digests, mailto:majordomo@ic.ac.uk the following message;
subscribe xml-dev-digest
List coordinator, Henry Rzepa (mailto:rzepa@ic.ac.uk)
|