ec17b77514271200d5b23123dc3d4301a302b563
[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
5 import net.sf.openrocket.aerodynamics.WarningSet;
6
7 import org.xml.sax.InputSource;
8 import org.xml.sax.SAXException;
9 import org.xml.sax.XMLReader;
10 import org.xml.sax.helpers.XMLReaderFactory;
11
12
13 /**
14  * A "simple SAX" XML reader.  This system imposes the limit that an XML element may
15  * contain either textual (non-whitespace) content OR additional elements, but not
16  * both.  This holds true for both the OpenRocket and RockSim design formats and the
17  * RockSim engine definition format.
18  * <p>
19  * The actual handling is performed by subclasses of {@link ElementHandler}.  The 
20  * initial handler is provided to the {@link #readXML(InputSource, ElementHandler, WarningSet)}
21  * method.
22  * 
23  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
24  */
25 public class SimpleSAX {
26         
27         /**
28          * Read a simple XML file.
29          * 
30          * @param source                        the SAX input source.
31          * @param initialHandler        the initial content handler.
32          * @param warnings                      a warning set to store warning (cannot be <code>null</code>).
33          * @throws IOException          if an I/O exception occurs while reading.
34          * @throws SAXException         if e.g. malformed XML is encountered.
35          */
36         public static void readXML(InputSource source, ElementHandler initialHandler,
37                         WarningSet warnings) throws IOException, SAXException {
38                 
39                 DelegatorHandler xmlhandler = new DelegatorHandler(initialHandler, warnings);
40                 
41                 XMLReader reader = XMLReaderFactory.createXMLReader();
42                 reader.setContentHandler(xmlhandler);
43                 reader.setErrorHandler(xmlhandler);
44                 reader.parse(source);
45         }
46         
47 }