1 package net.sf.openrocket.file.simplesax;
3 import java.util.HashMap;
5 import net.sf.openrocket.aerodynamics.Warning;
6 import net.sf.openrocket.aerodynamics.WarningSet;
7 import net.sf.openrocket.util.SimpleStack;
9 import org.xml.sax.Attributes;
10 import org.xml.sax.SAXException;
11 import org.xml.sax.helpers.DefaultHandler;
14 * The actual SAX handler class. Contains the necessary methods for parsing the SAX source.
15 * Delegates the actual content parsing to {@link ElementHandler} objects.
17 class DelegatorHandler extends DefaultHandler {
18 private final WarningSet warnings;
20 private final SimpleStack<ElementHandler> handlerStack = new SimpleStack<ElementHandler>();
21 private final SimpleStack<StringBuilder> elementData = new SimpleStack<StringBuilder>();
22 private final SimpleStack<HashMap<String, String>> elementAttributes = new SimpleStack<HashMap<String, String>>();
25 // Ignore all elements as long as ignore > 0
26 private int ignore = 0;
29 public DelegatorHandler(ElementHandler initialHandler, WarningSet warnings) {
30 this.warnings = warnings;
31 handlerStack.add(initialHandler);
32 elementData.add(new StringBuilder()); // Just in case
36 ///////// SAX handlers
39 public void startElement(String uri, String localName, String name,
40 Attributes attributes) throws SAXException {
48 // Check for unknown namespace
49 if (!uri.equals("")) {
50 warnings.add(Warning.fromString("Unknown namespace element '" + uri
51 + "' encountered, ignoring."));
56 // Add layer to data stacks
57 elementData.push(new StringBuilder());
58 elementAttributes.push(copyAttributes(attributes));
61 ElementHandler h = handlerStack.peek();
62 h = h.openElement(localName, elementAttributes.peek(), warnings);
66 // Start ignoring elements
73 * Stores encountered characters in the elementData stack.
76 public void characters(char[] chars, int start, int length) throws SAXException {
81 StringBuilder sb = elementData.peek();
82 sb.append(chars, start, length);
87 * Removes the last layer from the stack.
90 public void endElement(String uri, String localName, String name) throws SAXException {
98 // Remove data from stack
99 String data = elementData.pop().toString(); // throws on error
100 HashMap<String, String> attr = elementAttributes.pop();
102 // Remove last handler and call the next one
105 h = handlerStack.pop();
106 h.endHandler(localName, attr, data, warnings);
108 h = handlerStack.peek();
109 h.closeElement(localName, attr, data, warnings);
113 private static HashMap<String, String> copyAttributes(Attributes atts) {
114 HashMap<String, String> ret = new HashMap<String, String>();
115 for (int i = 0; i < atts.getLength(); i++) {
116 ret.put(atts.getLocalName(i), atts.getValue(i));