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.
33 import java.io.BufferedInputStream;
35 import java.io.FileInputStream;
36 import java.io.InputStream;
37 import java.io.InputStreamReader;
38 import java.io.Reader;
39 import java.io.StringReader;
40 import java.io.UnsupportedEncodingException;
41 import java.util.HashMap;
43 import java.util.Stack;
45 import mir.util.ExceptionFunctions;
46 import mir.util.xml.html.XMLHTMLParserProvider;
48 public class XMLParserEngine {
49 private Map providers;
50 private XMLParserProvider defaultProvider;
52 private static XMLParserEngine instance = new XMLParserEngine();
54 public static XMLParserEngine getInstance() {
58 private XMLParserEngine() {
59 providers = new HashMap();
60 defaultProvider = new XMLSAXParserProvider(false);
61 providers.put("xml", defaultProvider);
62 providers.put("xml.namespaceaware", new XMLSAXParserProvider(true));
63 providers.put("html", new XMLHTMLParserProvider());
70 * @throws XMLParserFailure
71 * @throws XMLParserExc
73 public void parseString(String aProvider, String aString, SectionHandler aRootHandler) throws XMLParserFailure, XMLParserExc{
75 parse(aProvider, new StringReader(aString), aRootHandler);
78 throw new XMLParserFailure(t);
87 public void parse(String aProvider, File aFile, SectionHandler aRootHandler) throws XMLParserFailure, XMLParserExc {
88 // filename = aFileName;
91 parse(aProvider, new BufferedInputStream(new FileInputStream(aFile), 8192), aRootHandler);
93 catch (XMLParserExc e) {
97 throw new XMLParserFailure(t);
102 * Parse xml coming from an input stream with the system default encoding
104 public void parse(String aProvider, InputStream anInputStream, SectionHandler aRootHandler) throws XMLParserFailure, XMLParserExc{
105 parse(aProvider, new InputStreamReader(anInputStream), aRootHandler);
109 * Parse xml coming from an input stream according to the given encoding
111 public void parse(String aProvider, InputStream anInputStream, String anEncoding, SectionHandler aRootHandler) throws XMLParserFailure, XMLParserExc{
113 parse(aProvider, new InputStreamReader(anInputStream, anEncoding), aRootHandler);
115 catch (UnsupportedEncodingException e) {
116 throw new XMLParserExc("Unsupported encoding: " + anEncoding);
123 * @param aRootHandler
124 * @throws mir.util.xml.XMLParserFailure
125 * @throws mir.util.xml.XMLParserExc
127 public void parse(String aProvider, Reader aReader, SectionHandler aRootHandler) throws XMLParserFailure, XMLParserExc{
128 XMLParserProvider provider = (XMLParserProvider) providers.get(aProvider);
131 provider = defaultProvider;
133 XMLParserRunner runner = new XMLParserRunner(aRootHandler);
136 provider.parse(aReader, runner);
138 catch (Throwable e) {
139 Throwable t = ExceptionFunctions.traceCauseException(e);
141 if (t instanceof XMLParserExc && runner.getLocator()!=null) {
142 ((XMLParserExc) t).setLocation(runner.getLocator().getLineNr(), runner.getLocator().getColumnNr());
145 if (t instanceof XMLParserExc) {
146 throw (XMLParserExc) t;
149 if (t instanceof XMLParserFailure) {
150 throw (XMLParserFailure) t;
153 throw new XMLParserFailure(t);
157 public interface XMLLocator {
158 public int getLineNr();
159 public int getColumnNr();
162 public interface XMLParserReceiver {
164 * Provides an object with which it's possible to track the parser's
167 public void setLocator(XMLLocator aLocator);
170 * Extra document data, outside of tags and CDATA, will be passed verbatim
171 * to this method. This may include comments, DTDs, XML specifications,
174 public void extra(String aPreAmble) throws XMLParserExc, XMLParserFailure;
176 public void startElement(XMLName anElement, Map anAttributes) throws XMLParserExc, XMLParserFailure;
177 public void endElement(XMLName anElement) throws XMLParserExc, XMLParserFailure;
178 public void startDocument() throws XMLParserExc, XMLParserFailure;
179 public void endDocument() throws XMLParserExc, XMLParserFailure;
180 public void text(String aText) throws XMLParserExc, XMLParserFailure;
183 public interface XMLParserProvider {
184 public void parse(Reader aReader, XMLParserReceiver aReceiver) throws XMLParserExc, XMLParserFailure;
187 private class XMLParserRunner implements XMLParserReceiver {
188 private SectionsManager manager;
189 private XMLLocator locator;
190 private SectionHandler rootHandler;
192 public XMLParserRunner(SectionHandler aRootHandler) {
196 manager = new SectionsManager();
197 rootHandler = aRootHandler;
200 public void setLocator(XMLLocator aLocator) {
204 public void extra(String anExtraData) throws XMLParserExc, XMLParserFailure {
205 SectionHandler handler = manager.currentHandler();
207 handler.extra(anExtraData);
210 public void startElement(XMLName anElement, Map anAttributes) throws XMLParserExc, XMLParserFailure {
211 SectionHandler handler = manager.currentHandler().startElement(anElement, anAttributes);
213 handler.startSection();
215 manager.pushHandler(handler);
218 public void endElement(XMLName anElement) throws XMLParserExc, XMLParserFailure {
219 SectionHandler handler = manager.popHandler();
221 handler.finishSection();
223 manager.currentHandler().endElement(handler);
226 public void startDocument() throws XMLParserExc, XMLParserFailure {
227 rootHandler.startSection();
228 manager.pushHandler(rootHandler);
231 public void endDocument() throws XMLParserExc, XMLParserFailure {
232 SectionHandler handler = manager.popHandler();
234 handler.finishSection();
237 public void text(String aText) throws XMLParserExc, XMLParserFailure {
238 manager.currentHandler().characters(aText);
241 public XMLLocator getLocator() {
249 * <p>Description: </p>
250 * <p>Copyright: Copyright (c) 2003</p>
252 * @author not attributable
255 private class SectionsManager {
256 private Stack handlerStack;
258 public SectionsManager() {
259 handlerStack = new Stack();
262 public void pushHandler(SectionHandler aSectionHandler) {
263 handlerStack.push(aSectionHandler);
266 public SectionHandler popHandler() {
267 return (SectionHandler) handlerStack.pop();
270 public SectionHandler currentHandler() {
271 return (SectionHandler) handlerStack.peek();
274 public boolean isEmpty() {
275 return handlerStack.isEmpty();