|
| 1 | +package com.ocpsoft.pretty.faces.util; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStream; |
| 5 | +import java.util.Stack; |
| 6 | + |
| 7 | +import javax.xml.parsers.ParserConfigurationException; |
| 8 | +import javax.xml.parsers.SAXParser; |
| 9 | +import javax.xml.parsers.SAXParserFactory; |
| 10 | + |
| 11 | +import org.xml.sax.Attributes; |
| 12 | +import org.xml.sax.SAXException; |
| 13 | +import org.xml.sax.helpers.DefaultHandler; |
| 14 | + |
| 15 | +/** |
| 16 | + * Base class for implementing XML parsers based on the SAX API. |
| 17 | + * |
| 18 | + * @author Christian Kaltepoth <christian@kaltepoth.de> |
| 19 | + */ |
| 20 | +public abstract class SimpleXMLParserBase extends DefaultHandler |
| 21 | +{ |
| 22 | + |
| 23 | + /** |
| 24 | + * Used to maintain a list of parents up to the root of the XML tree |
| 25 | + */ |
| 26 | + private final Stack<String> elements = new Stack<String>(); |
| 27 | + |
| 28 | + /** |
| 29 | + * Parses the supplied {@link InputStream}. |
| 30 | + * |
| 31 | + * @param stream |
| 32 | + * The stream to parse |
| 33 | + * @throws IOException |
| 34 | + * for any errors during the parsing process |
| 35 | + */ |
| 36 | + public final void parse(InputStream stream) throws IOException |
| 37 | + { |
| 38 | + |
| 39 | + // all XML exceptions are wrapped by IOExceptions |
| 40 | + try |
| 41 | + { |
| 42 | + |
| 43 | + // setup a namespace aware SAXParserFactory |
| 44 | + SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); |
| 45 | + saxParserFactory.setNamespaceAware(true); |
| 46 | + |
| 47 | + // parse the input stream |
| 48 | + SAXParser parser = saxParserFactory.newSAXParser(); |
| 49 | + parser.parse(stream, new SimpleHandler()); |
| 50 | + |
| 51 | + } |
| 52 | + catch (ParserConfigurationException e) |
| 53 | + { |
| 54 | + throw new IOException(e); |
| 55 | + } |
| 56 | + catch (SAXException e) |
| 57 | + { |
| 58 | + throw new IOException(e); |
| 59 | + } |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Called for every element start event |
| 65 | + * |
| 66 | + * @param name |
| 67 | + * The local name of the element |
| 68 | + */ |
| 69 | + public abstract void processStartElement(String name); |
| 70 | + |
| 71 | + /** |
| 72 | + * Called for every element end event |
| 73 | + * |
| 74 | + * @param name |
| 75 | + * The local name of the element |
| 76 | + */ |
| 77 | + public abstract void processEndElement(String name); |
| 78 | + |
| 79 | + /** |
| 80 | + * Called for every characters event |
| 81 | + * |
| 82 | + * @param text |
| 83 | + * The characters |
| 84 | + */ |
| 85 | + public abstract void processCharacters(String text); |
| 86 | + |
| 87 | + /** |
| 88 | + * This class maintaince the parent element stack and forwards all events to |
| 89 | + * the abstract methods of {@link SimpleXMLParserBase}. |
| 90 | + */ |
| 91 | + private class SimpleHandler extends DefaultHandler |
| 92 | + { |
| 93 | + |
| 94 | + @Override |
| 95 | + public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException |
| 96 | + { |
| 97 | + |
| 98 | + // add the element's local name to the parent stack |
| 99 | + elements.add(localName); |
| 100 | + |
| 101 | + // forward event |
| 102 | + processStartElement(localName); |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public void characters(char[] ch, int start, int length) throws SAXException |
| 108 | + { |
| 109 | + // forward event |
| 110 | + processCharacters(String.valueOf(ch, start, length)); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public void endElement(String uri, String localName, String qName) throws SAXException |
| 115 | + { |
| 116 | + // forward event |
| 117 | + processEndElement(localName); |
| 118 | + |
| 119 | + // remove the name on top of the stack |
| 120 | + String removedName = elements.pop(); |
| 121 | + |
| 122 | + // check if the removed name is the expected one |
| 123 | + if (!removedName.equals(localName)) |
| 124 | + { |
| 125 | + throw new IllegalStateException("Found '" + removedName + "' but expected '" + localName |
| 126 | + + "' on the stack!"); |
| 127 | + } |
| 128 | + |
| 129 | + } |
| 130 | + |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Checks whether the parent stack currently contains exactly the elements |
| 135 | + * supplied by the caller. This method can be used to find the current |
| 136 | + * position in the document. The first argument is always the root element of |
| 137 | + * the document, the second is a child of the root element, and so on. The |
| 138 | + * method uses the local names of the elements only. Namespaces are ignored. |
| 139 | + * |
| 140 | + * @param name |
| 141 | + * The names of the parent elements |
| 142 | + * @return <code>true</code> if the parent element stack contains exactly |
| 143 | + * these elements |
| 144 | + */ |
| 145 | + protected boolean elements(String... name) |
| 146 | + { |
| 147 | + if (name.length != elements.size()) |
| 148 | + { |
| 149 | + return false; |
| 150 | + } |
| 151 | + for (int i = 0; i < name.length; i++) |
| 152 | + { |
| 153 | + if (!name[i].equals(elements.get(i))) |
| 154 | + { |
| 155 | + return false; |
| 156 | + } |
| 157 | + } |
| 158 | + return true; |
| 159 | + } |
| 160 | + |
| 161 | +} |
0 commit comments