2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
6 * Mir is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Mir is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Mir; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * In addition, as a special exception, The Mir-coders gives permission to link
21 * the code of this program with any library licensed under the Apache Software License,
22 * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
23 * (or with modified versions of the above that use the same license as the above),
24 * and distribute linked combinations including the two. You must obey the
25 * GNU General Public License in all respects for all of the code used other than
26 * the above mentioned libraries. If you modify this file, you may extend this
27 * exception to your version of the file, but you are not obligated to do so.
28 * If you do not wish to do so, delete this exception statement from your version.
32 import java.io.FileInputStream;
33 import java.io.InputStream;
34 import java.util.HashMap;
36 import java.util.Stack;
38 import javax.xml.parsers.ParserConfigurationException;
39 import javax.xml.parsers.SAXParser;
40 import javax.xml.parsers.SAXParserFactory;
43 import multex.Failure;
45 import org.xml.sax.Attributes;
46 import org.xml.sax.InputSource;
47 import org.xml.sax.Locator;
48 import org.xml.sax.SAXException;
49 import org.xml.sax.SAXParseException;
50 import org.xml.sax.helpers.DefaultHandler;
52 public class XMLReader {
53 private Locator locator;
54 private String filename;
55 private boolean namespaceAware;
61 public XMLReader(boolean aNameSpaceAware) {
62 namespaceAware = aNameSpaceAware;
66 public void parseFile(String aFileName, SectionHandler aRootHandler) throws XMLReaderFailure, XMLReaderExc {
69 parseInputStream(new FileInputStream(aFileName), aRootHandler);
72 throw new XMLReaderFailure(t);
76 public void parseInputStream(InputStream anInputStream, SectionHandler aRootHandler) throws XMLReaderFailure, XMLReaderExc {
78 SAXParserFactory parserFactory = SAXParserFactory.newInstance();
80 parserFactory.setNamespaceAware(true);
81 parserFactory.setValidating(true);
83 XMLReaderHandler handler = new XMLReaderHandler(parserFactory, aRootHandler);
85 handler.processInputStream(anInputStream);
88 Throwable t = ExceptionFunctions.traceCauseException(e);
90 if (t instanceof XMLReaderExc) {
91 throw (XMLReaderExc) t;
94 if (t instanceof XMLReaderFailure) {
95 throw (XMLReaderFailure) t;
98 throw new XMLReaderFailure(t);
102 private class XMLReaderHandler extends DefaultHandler {
103 private SAXParserFactory parserFactory;
104 private SectionsManager manager;
105 private InputSource inputSource;
107 public XMLReaderHandler(SAXParserFactory aParserFactory, SectionHandler aRootHandler) {
110 parserFactory=aParserFactory;
111 manager = new SectionsManager();
112 manager.pushHandler(aRootHandler);
115 public void setDocumentLocator(Locator aLocator) {
119 private void processInputStream(InputStream anInputStream) throws XMLReaderExc, XMLReaderFailure {
121 SAXParser parser=parserFactory.newSAXParser();
123 inputSource = new InputSource(anInputStream);
124 parser.parse(inputSource, this);
126 catch (Throwable e) {
127 Throwable t = ExceptionFunctions.traceCauseException(e);
129 if (t instanceof XMLReaderExc) {
130 if (locator!=null && filename!=null)
131 ((XMLReaderExc) t).setLocation(filename, locator.getLineNumber(), locator.getColumnNumber());
132 throw (XMLReaderExc) t;
135 if (t instanceof SAXParseException) {
136 XMLReaderExc r = new XMLReaderExc(t.getMessage());
138 if (locator!=null && filename!=null)
139 r.setLocation(filename, locator.getLineNumber(), locator.getColumnNumber());
144 if (t instanceof XMLReaderFailure) {
145 throw (XMLReaderFailure) t;
148 if (t instanceof ParserConfigurationException) {
149 throw new XMLReaderFailure("Internal exception: "+t.toString(), t);
152 throw new XMLReaderFailure(t);
156 public void startElement(String aUri, String aLocalName, String aQualifiedName, Attributes anAttributes) throws SAXException {
161 attributesMap = new HashMap();
164 for (i=0; i<anAttributes.getLength(); i++)
165 attributesMap.put(new XMLName(anAttributes.getURI(i), anAttributes.getLocalName(i)), anAttributes.getValue(i));
167 for (i=0; i<anAttributes.getLength(); i++)
168 attributesMap.put(anAttributes.getLocalName(i), anAttributes.getValue(i));
170 SectionHandler handler = manager.currentHandler().startElement(new XMLName(aUri, aLocalName), attributesMap);
172 manager.pushHandler( handler );
174 catch (XMLReaderExc e) {
175 throw new SAXParseException(e.getMessage(), null, e);
177 catch (Exception e) {
178 throw new SAXException(e);
182 public void endElement(String aUri, String aLocalName, String aQualifiedName) throws SAXException {
185 if (!aQualifiedName.equals("include")) {
186 SectionHandler handler = manager.popHandler();
188 handler.finishSection();
190 if (!manager.isEmpty()) {
191 manager.currentHandler().endElement(handler);
195 catch (XMLReaderExc e) {
196 throw new SAXParseException(e.getMessage(), null, e);
198 catch (Exception e) {
199 throw new SAXException(e);
203 public void characters(char[] aBuffer, int aStart, int anEnd) throws SAXException {
205 String text = new String(aBuffer, aStart, anEnd);
207 manager.currentHandler().characters(text);
209 catch (XMLReaderExc e) {
210 throw new SAXParseException(e.getMessage(), null, e);
212 catch (Exception e) {
213 throw new SAXException(e);
218 private class SectionsManager {
221 public SectionsManager() {
222 handlerStack = new Stack();
225 public void pushHandler(SectionHandler aSectionHandler) {
226 handlerStack.push(aSectionHandler);
229 public SectionHandler popHandler() {
230 return (SectionHandler) handlerStack.pop();
233 public SectionHandler currentHandler() {
234 return (SectionHandler) handlerStack.peek();
237 public boolean isEmpty() {
238 return handlerStack.isEmpty();
242 public static interface SectionHandler {
243 public abstract SectionHandler startElement(XMLName aTag, Map anAttributes) throws XMLReaderExc;
245 public abstract void endElement(SectionHandler aHandler) throws XMLReaderExc;
247 public void characters(String aCharacters) throws XMLReaderExc;
249 public void finishSection() throws XMLReaderExc;
252 public static abstract class AbstractSectionHandler implements SectionHandler {
253 public SectionHandler startElement(XMLName aTag, Map anAttributes) throws XMLReaderExc {
254 return startElement(aTag.getLocalName(), anAttributes);
257 public SectionHandler startElement(String aLocalName, Map anAttributes) throws XMLReaderExc {
261 public void endElement(SectionHandler aHandler) throws XMLReaderExc {
264 public void finishSection() throws XMLReaderExc {
267 public void characters(String aCharacters) throws XMLReaderExc {
268 if ( aCharacters.trim().length() > 0) {
269 throw new XMLReaderExc("Text not allowed");
274 public static class XMLReaderExc extends Exc {
275 private boolean hasLocation;
276 private String filename;
278 private int columnNr;
280 public XMLReaderExc(String aMessage) {
285 protected void setLocation(String aFilename, int aLineNr, int aColumnNr) {
286 filename = aFilename;
288 columnNr = aColumnNr;
292 public boolean getHasLocation() {
296 public int getLineNr() {
300 public int getColumnNr() {
304 public String getFilename() {
309 public static class XMLReaderFailure extends Failure {
310 public XMLReaderFailure(String aMessage, Throwable aCause) {
311 super(aMessage, aCause);
314 public XMLReaderFailure(Throwable aCause) {
315 super(aCause.getMessage(), aCause);
319 public static class XMLName {
320 private String namespaceURI;
321 private String localName;
323 public XMLName(String aLocalName) {
324 this(aLocalName, null);
327 public XMLName(String aNamespaceURI, String aLocalName) {
328 namespaceURI = aNamespaceURI;
329 localName = aLocalName;
332 public String getNamespaceURI() {
336 public String getLocalName() {
340 public int hashCode() {
341 if (namespaceURI == null)
342 return localName.hashCode();
344 return localName.hashCode() + 3*namespaceURI.hashCode();
347 public String toString() {
348 return namespaceURI+":"+localName;
351 public boolean equals(Object anObject) {
352 if (anObject instanceof XMLName) {
353 if (namespaceURI==null)
354 return (((XMLName) anObject).namespaceURI == null) &&
355 localName.equals(((XMLName) anObject).localName);
357 return namespaceURI.equals(((XMLName) anObject).namespaceURI) &&
358 localName.equals(((XMLName) anObject).localName);