create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / file / simplesax / SimpleSAX.java
index 62dc35a9b3f18f22d6f29a822f499bb2df4b7437..6f9bc2eb2b413d4bafbf7b7a93ff3e0ecd981b4f 100644 (file)
@@ -1,6 +1,8 @@
 package net.sf.openrocket.file.simplesax;
 
 import java.io.IOException;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.LinkedBlockingQueue;
 
 import net.sf.openrocket.aerodynamics.WarningSet;
 
@@ -16,14 +18,16 @@ import org.xml.sax.helpers.XMLReaderFactory;
  * both.  This holds true for both the OpenRocket and RockSim design formats and the
  * RockSim engine definition format.
  * <p>
- * The actual handling is performed by subclasses of {@link AbstractElementHandler}.  The 
- * initial handler is provided to the {@link #readXML(InputSource, AbstractElementHandler, WarningSet)}
+ * The actual handling is performed by subclasses of {@link ElementHandler}.  The 
+ * initial handler is provided to the {@link #readXML(InputSource, ElementHandler, WarningSet)}
  * method.
  * 
  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
  */
 public class SimpleSAX {
 
+       static final XMLReaderCache cache = new XMLReaderCache(10);
+
        /**
         * Read a simple XML file.
         * 
@@ -33,15 +37,37 @@ public class SimpleSAX {
         * @throws IOException          if an I/O exception occurs while reading.
         * @throws SAXException         if e.g. malformed XML is encountered.
         */
-       public static void readXML(InputSource source, AbstractElementHandler initialHandler,
+       public static void readXML(InputSource source, ElementHandler initialHandler,
                        WarningSet warnings) throws IOException, SAXException {
 
                DelegatorHandler xmlhandler = new DelegatorHandler(initialHandler, warnings);
 
-               XMLReader reader = XMLReaderFactory.createXMLReader();
+               XMLReader reader = cache.createXMLReader();
                reader.setContentHandler(xmlhandler);
                reader.setErrorHandler(xmlhandler);
                reader.parse(source);
+               cache.releaseXMLReader(reader);
+       }
+
+       private static class XMLReaderCache {
+
+               private final BlockingQueue<XMLReader> queue;
+               private XMLReaderCache( int maxSize ) {
+                       this.queue = new LinkedBlockingQueue<XMLReader>(maxSize);
+               }
+
+               private XMLReader createXMLReader() throws SAXException {
+
+                       XMLReader reader = queue.poll();
+                       if ( reader == null ) {
+                               reader = XMLReaderFactory.createXMLReader();
+                       }
+                       return reader;
+               }
+
+               private void releaseXMLReader( XMLReader reader ) {
+                       queue.offer( reader );
+               }
        }
-       
+
 }