1 package net.sf.openrocket.file.simplesax;
3 import java.io.IOException;
4 import java.util.concurrent.BlockingQueue;
5 import java.util.concurrent.LinkedBlockingQueue;
7 import net.sf.openrocket.aerodynamics.WarningSet;
9 import org.xml.sax.InputSource;
10 import org.xml.sax.SAXException;
11 import org.xml.sax.XMLReader;
12 import org.xml.sax.helpers.XMLReaderFactory;
16 * A "simple SAX" XML reader. This system imposes the limit that an XML element may
17 * contain either textual (non-whitespace) content OR additional elements, but not
18 * both. This holds true for both the OpenRocket and RockSim design formats and the
19 * RockSim engine definition format.
21 * The actual handling is performed by subclasses of {@link ElementHandler}. The
22 * initial handler is provided to the {@link #readXML(InputSource, ElementHandler, WarningSet)}
25 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
27 public class SimpleSAX {
29 static final XMLReaderCache cache = new XMLReaderCache(10);
32 * Read a simple XML file.
34 * @param source the SAX input source.
35 * @param initialHandler the initial content handler.
36 * @param warnings a warning set to store warning (cannot be <code>null</code>).
37 * @throws IOException if an I/O exception occurs while reading.
38 * @throws SAXException if e.g. malformed XML is encountered.
40 public static void readXML(InputSource source, ElementHandler initialHandler,
41 WarningSet warnings) throws IOException, SAXException {
43 DelegatorHandler xmlhandler = new DelegatorHandler(initialHandler, warnings);
45 XMLReader reader = cache.createXMLReader();
46 reader.setContentHandler(xmlhandler);
47 reader.setErrorHandler(xmlhandler);
49 cache.releaseXMLReader(reader);
52 private static class XMLReaderCache {
54 private final BlockingQueue<XMLReader> queue;
55 private XMLReaderCache( int maxSize ) {
56 this.queue = new LinkedBlockingQueue<XMLReader>(maxSize);
59 private XMLReader createXMLReader() throws SAXException {
61 XMLReader reader = queue.poll();
62 if ( reader == null ) {
63 reader = XMLReaderFactory.createXMLReader();
68 private void releaseXMLReader( XMLReader reader ) {
69 queue.offer( reader );