2 * Copyright (C) 2001, 2002 The Mir-coders group
\r
4 * This file is part of Mir.
\r
6 * Mir is free software; you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation; either version 2 of the License, or
\r
9 * (at your option) any later version.
\r
11 * Mir is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with Mir; if not, write to the Free Software
\r
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 * In addition, as a special exception, The Mir-coders gives permission to link
\r
21 * the code of this program with the com.oreilly.servlet library, any library
\r
22 * licensed under the Apache Software License, The Sun (tm) Java Advanced
\r
23 * Imaging library (JAI), The Sun JIMI library (or with modified versions of
\r
24 * the above that use the same license as the above), and distribute linked
\r
25 * combinations including the two. You must obey the GNU General Public
\r
26 * License in all respects for all of the code used other than the above
\r
27 * mentioned libraries. If you modify this file, you may extend this exception
\r
28 * to your version of the file, but you are not obligated to do so. If you do
\r
29 * not wish to do so, delete this exception statement from your version.
\r
37 import javax.xml.parsers.ParserConfigurationException;
\r
38 import javax.xml.parsers.SAXParser;
\r
39 import javax.xml.parsers.SAXParserFactory;
\r
41 import org.xml.sax.Attributes;
\r
42 import org.xml.sax.InputSource;
\r
43 import org.xml.sax.Locator;
\r
44 import org.xml.sax.SAXException;
\r
45 import org.xml.sax.SAXParseException;
\r
46 import org.xml.sax.helpers.DefaultHandler;
\r
48 import multex.Failure;
\r
51 public class XMLReader {
\r
52 private Locator locator;
\r
53 private String filename;
\r
55 public void parseFile(String aFileName, SectionHandler aRootHandler, List aUsedFiles) throws XMLReaderFailure, XMLReaderExc {
\r
57 SAXParserFactory parserFactory = SAXParserFactory.newInstance();
\r
59 parserFactory.setNamespaceAware(false);
\r
60 parserFactory.setValidating(true);
\r
62 XMLReaderHandler handler = new XMLReaderHandler(parserFactory, aRootHandler, aUsedFiles);
\r
64 handler.includeFile(aFileName);
\r
66 catch (Throwable e) {
\r
67 Throwable t = getRootCause(e);
\r
69 if (t instanceof XMLReaderExc) {
\r
70 ((XMLReaderExc) t).setLocation(filename, locator.getLineNumber(), locator.getColumnNumber());
\r
71 throw (XMLReaderExc) t;
\r
74 if (t instanceof XMLReaderFailure) {
\r
75 throw (XMLReaderFailure) t;
\r
78 throw new XMLReaderFailure(t);
\r
82 private Throwable getRootCause(Throwable t) {
\r
83 if (t instanceof SAXParseException && ((SAXParseException) t).getException()!=null) {
\r
84 return getRootCause(((SAXParseException) t).getException());
\r
86 else if (t instanceof Failure && ((Failure) t).getCause()!=null) {
\r
87 return getRootCause(((Failure) t).getCause());
\r
92 private class XMLReaderHandler extends DefaultHandler {
\r
93 private Stack includeFileStack;
\r
94 private SAXParserFactory parserFactory;
\r
95 private SectionsManager manager;
\r
96 private List usedFiles;
\r
97 private InputSource inputSource;
\r
99 public XMLReaderHandler(SAXParserFactory aParserFactory, SectionHandler aRootHandler, List aUsedFiles) {
\r
102 includeFileStack=new Stack();
\r
103 parserFactory=aParserFactory;
\r
104 includeFileStack = new Stack();
\r
105 manager = new SectionsManager();
\r
106 usedFiles = aUsedFiles;
\r
107 manager.pushHandler(aRootHandler);
\r
110 public String getLocatorDescription(Locator aLocator) {
\r
111 return aLocator.getPublicId()+" ("+aLocator.getLineNumber()+")";
\r
114 public void setDocumentLocator(Locator aLocator) {
\r
118 private void includeFile(String aFileName) throws XMLReaderExc, XMLReaderFailure, SAXParseException, SAXException {
\r
123 if (!includeFileStack.empty())
\r
124 file = new File(new File((String) includeFileStack.peek()).getParent(), aFileName);
\r
126 file = new File(aFileName);
\r
128 System.err.println("about to include "+file.getCanonicalPath());
\r
130 if (includeFileStack.contains(file.getCanonicalPath())) {
\r
131 throw new XMLReaderExc("recursive inclusion of file "+file.getCanonicalPath());
\r
134 usedFiles.add(file);
\r
136 parser=parserFactory.newSAXParser();
\r
138 inputSource = new InputSource(new FileInputStream(file));
\r
139 inputSource.setPublicId(file.getCanonicalPath());
\r
141 includeFileStack.push(file.getCanonicalPath());
\r
142 filename = file.getCanonicalPath();
\r
144 parser.parse(inputSource, this);
\r
147 includeFileStack.pop();
\r
149 if (!includeFileStack.empty())
\r
150 filename = (String) includeFileStack.peek();
\r
152 catch (ParserConfigurationException e) {
\r
153 throw new XMLReaderExc("Internal exception while including \""+aFileName+"\": "+e.getMessage());
\r
155 catch (SAXParseException e) {
\r
158 catch (XMLReaderFailure e) {
\r
161 catch (FileNotFoundException e) {
\r
162 throw new XMLReaderExc("Include file \""+aFileName+"\" not found: "+e.getMessage());
\r
164 catch (IOException e) {
\r
165 throw new XMLReaderExc("unable to open include file \""+aFileName+"\": "+e.getMessage());
\r
169 public void startElement(String aUri, String aTag, String aQualifiedName, Attributes anAttributes) throws SAXException {
\r
174 if (aQualifiedName.equals("include")) {
\r
175 String fileName=anAttributes.getValue("file");
\r
177 if (fileName==null) {
\r
178 throw new XMLReaderExc("include has no file attribute");
\r
181 includeFile(fileName);
\r
184 attributesMap = new HashMap();
\r
185 for (i=0; i<anAttributes.getLength(); i++)
\r
186 attributesMap.put(anAttributes.getQName(i), anAttributes.getValue(i));
\r
188 SectionHandler handler = manager.currentHandler().startElement(aQualifiedName, attributesMap);
\r
190 manager.pushHandler( handler );
\r
193 catch (XMLReaderExc e) {
\r
194 throw new SAXParseException(e.getMessage(), null, e);
\r
196 catch (Exception e) {
\r
197 throw new SAXException(e);
\r
201 public void endElement(String aUri, String aTag, String aQualifiedName) throws SAXException {
\r
204 if (!aQualifiedName.equals("include")) {
\r
205 SectionHandler handler = manager.popHandler();
\r
207 handler.finishSection();
\r
209 if (!manager.isEmpty()) {
\r
210 manager.currentHandler().endElement(handler);
\r
214 catch (XMLReaderExc e) {
\r
215 throw new SAXParseException(e.getMessage(), null, e);
\r
217 catch (Exception e) {
\r
218 throw new SAXException(e);
\r
222 public void characters(char[] aBuffer, int aStart, int anEnd) throws SAXParseException {
\r
223 String text = new String(aBuffer, aStart, anEnd).trim();
\r
224 if ( text.length() > 0) {
\r
225 throw new SAXParseException("Text not allowed", null, new XMLReaderExc("Text not allowed"));
\r
230 private class SectionsManager {
\r
231 Stack handlerStack;
\r
233 public SectionsManager() {
\r
234 handlerStack = new Stack();
\r
237 public void pushHandler(SectionHandler aSectionHandler) {
\r
238 handlerStack.push(aSectionHandler);
\r
241 public SectionHandler popHandler() {
\r
242 return (SectionHandler) handlerStack.pop();
\r
245 public SectionHandler currentHandler() {
\r
246 return (SectionHandler) handlerStack.peek();
\r
249 public boolean isEmpty() {
\r
250 return handlerStack.isEmpty();
\r
254 public static interface SectionHandler {
\r
255 public abstract SectionHandler startElement(String aTag, Map anAttributes) throws XMLReaderExc;
\r
257 public abstract void endElement(SectionHandler aHandler) throws XMLReaderExc;
\r
259 public void finishSection() throws XMLReaderExc;
\r
262 public static abstract class AbstractSectionHandler implements SectionHandler {
\r
263 public SectionHandler startElement(String aTag, Map anAttributes) throws XMLReaderExc {
\r
267 public void endElement(SectionHandler aHandler) throws XMLReaderExc {
\r
270 public void finishSection() throws XMLReaderExc {
\r
274 public static class XMLReaderExc extends Exc {
\r
275 private boolean hasLocation;
\r
276 private String filename;
\r
277 private int lineNr;
\r
278 private int columnNr;
\r
280 public XMLReaderExc(String aMessage) {
\r
282 hasLocation = false;
\r
285 protected void setLocation(String aFilename, int aLineNr, int aColumnNr) {
\r
286 filename = aFilename;
\r
288 columnNr = aColumnNr;
\r
289 hasLocation = true;
\r
292 public boolean getHasLocation() {
\r
293 return hasLocation;
\r
296 public int getLineNr() {
\r
300 public int getColumnNr() {
\r
304 public String getFilename() {
\r
309 public static class XMLReaderFailure extends Failure {
\r
310 public XMLReaderFailure(String aMessage, Throwable aCause) {
\r
311 super(aMessage, aCause);
\r
314 public XMLReaderFailure(Throwable aCause) {
\r
315 super(aCause.getMessage(), aCause);
\r