create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / file / simplesax / SimpleSAX.java
1 package net.sf.openrocket.file.simplesax;
2
3 import java.io.IOException;
4 import java.util.concurrent.BlockingQueue;
5 import java.util.concurrent.LinkedBlockingQueue;
6
7 import net.sf.openrocket.aerodynamics.WarningSet;
8
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;
13
14
15 /**
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.
20  * <p>
21  * The actual handling is performed by subclasses of {@link ElementHandler}.  The 
22  * initial handler is provided to the {@link #readXML(InputSource, ElementHandler, WarningSet)}
23  * method.
24  * 
25  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
26  */
27 public class SimpleSAX {
28
29         static final XMLReaderCache cache = new XMLReaderCache(10);
30
31         /**
32          * Read a simple XML file.
33          * 
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.
39          */
40         public static void readXML(InputSource source, ElementHandler initialHandler,
41                         WarningSet warnings) throws IOException, SAXException {
42
43                 DelegatorHandler xmlhandler = new DelegatorHandler(initialHandler, warnings);
44
45                 XMLReader reader = cache.createXMLReader();
46                 reader.setContentHandler(xmlhandler);
47                 reader.setErrorHandler(xmlhandler);
48                 reader.parse(source);
49                 cache.releaseXMLReader(reader);
50         }
51
52         private static class XMLReaderCache {
53
54                 private final BlockingQueue<XMLReader> queue;
55                 private XMLReaderCache( int maxSize ) {
56                         this.queue = new LinkedBlockingQueue<XMLReader>(maxSize);
57                 }
58
59                 private XMLReader createXMLReader() throws SAXException {
60
61                         XMLReader reader = queue.poll();
62                         if ( reader == null ) {
63                                 reader = XMLReaderFactory.createXMLReader();
64                         }
65                         return reader;
66                 }
67
68                 private void releaseXMLReader( XMLReader reader ) {
69                         queue.offer( reader );
70                 }
71         }
72
73 }