001: /* SAXParser.java NanoXML/SAX
002: *
003: * $Revision: 1.5 $
004: * $Date: 2002/03/24 11:39:20 $
005: * $Name: RELEASE_2_2_1 $
006: *
007: * This file is part of the SAX adapter for NanoXML 2 for Java.
008: * Copyright (C) 2000-2002 Marc De Scheemaecker, All Rights Reserved.
009: *
010: * This software is provided 'as-is', without any express or implied warranty.
011: * In no event will the authors be held liable for any damages arising from the
012: * use of this software.
013: *
014: * Permission is granted to anyone to use this software for any purpose,
015: * including commercial applications, and to alter it and redistribute it
016: * freely, subject to the following restrictions:
017: *
018: * 1. The origin of this software must not be misrepresented; you must not
019: * claim that you wrote the original software. If you use this software in
020: * a product, an acknowledgment in the product documentation would be
021: * appreciated but is not required.
022: *
023: * 2. Altered source versions must be plainly marked as such, and must not be
024: * misrepresented as being the original software.
025: *
026: * 3. This notice may not be removed or altered from any source distribution.
027: */
028:
029: package net.n3.nanoxml.sax;
030:
031: import java.io.FileNotFoundException;
032: import java.io.InputStream;
033: import java.io.InputStreamReader;
034: import java.io.IOException;
035: import java.io.Reader;
036: import java.io.UnsupportedEncodingException;
037: import java.net.MalformedURLException;
038: import java.util.Locale;
039: import net.n3.nanoxml.IXMLBuilder;
040: import net.n3.nanoxml.IXMLParser;
041: import net.n3.nanoxml.IXMLReader;
042: import net.n3.nanoxml.StdXMLReader;
043: import net.n3.nanoxml.XMLParserFactory;
044: import org.xml.sax.Parser;
045: import org.xml.sax.DocumentHandler;
046: import org.xml.sax.DTDHandler;
047: import org.xml.sax.EntityResolver;
048: import org.xml.sax.ErrorHandler;
049: import org.xml.sax.HandlerBase;
050: import org.xml.sax.InputSource;
051: import org.xml.sax.Locator;
052: import org.xml.sax.SAXException;
053: import org.xml.sax.helpers.AttributeListImpl;
054: import org.xml.sax.helpers.LocatorImpl;
055:
056: /**
057: * SAXParser implements the SAX Parser interface. It is the frontend to SAX
058: * for the NanoXML parser.
059: *
060: * @author Marc De Scheemaecker
061: * @version $Name: RELEASE_2_2_1 $, $Revision: 1.5 $
062: */
063: public class SAXParser implements Parser {
064:
065: /**
066: * The SAX adapter.
067: */
068: private SAXAdapter adapter;
069:
070: /**
071: * The client error handler.
072: */
073: private ErrorHandler errorHandler;
074:
075: /**
076: * The entity resolver.
077: */
078: private SAXEntityResolver entityResolver;
079:
080: /**
081: * Creates the SAX parser.
082: */
083: public SAXParser() {
084: this .adapter = new SAXAdapter();
085: this .errorHandler = new HandlerBase();
086: this .entityResolver = new SAXEntityResolver();
087: }
088:
089: /**
090: * Cleans up the object when it's destroyed.
091: */
092: protected void finalize() throws Throwable {
093: this .adapter = null;
094: this .errorHandler = null;
095: this .entityResolver = null;
096: super .finalize();
097: }
098:
099: /**
100: * Sets the locale. Only locales using the language english are accepted.
101: *
102: * @param locale the locale
103: *
104: * @exception org.xml.sax.SAXException
105: * if <CODE>locale</CODE> is <CODE>null</CODE> or the associated
106: * language is not english.
107: */
108: public void setLocale(Locale locale) throws SAXException {
109: if ((locale == null) || (!locale.getLanguage().equals("en"))) {
110: throw new SAXException(
111: "NanoXML/SAX doesn't support locale: " + locale);
112: }
113: }
114:
115: /**
116: * Sets the entity resolver.
117: *
118: * @param resolver the entity resolver
119: */
120: public void setEntityResolver(EntityResolver resolver) {
121: this .entityResolver.setEntityResolver(resolver);
122: }
123:
124: /**
125: * Sets the DTD handler. As the parser is non-validating, this handler is
126: * never called.
127: *
128: * @param handler the DTD handler
129: */
130: public void setDTDHandler(DTDHandler handler) {
131: // nothing to do
132: }
133:
134: /**
135: * Allows an application to register a document event handler.
136: *
137: * @param handler the document handler
138: */
139: public void setDocumentHandler(DocumentHandler handler) {
140: this .adapter.setDocumentHandler(handler);
141: }
142:
143: /**
144: * Allow an application to register an error event handler.
145: *
146: * @param handler the error handler
147: */
148: public void setErrorHandler(ErrorHandler handler) {
149: this .errorHandler = handler;
150: }
151:
152: /**
153: * Creates the XML parser.
154: */
155: private IXMLParser createParser() throws SAXException {
156: try {
157: return XMLParserFactory.createDefaultXMLParser();
158: } catch (Exception exception) {
159: throw new SAXException(exception);
160: }
161: }
162:
163: /**
164: * Parse an XML document.
165: *
166: * @param source the input source
167: */
168: public void parse(InputSource source) throws SAXException,
169: IOException {
170: IXMLParser parser = this .createParser();
171: parser.setBuilder(this .adapter);
172: parser.setResolver(this .entityResolver);
173: Reader reader = source.getCharacterStream();
174:
175: if (reader != null) {
176: parser.setReader(new StdXMLReader(reader));
177: } else {
178: InputStream stream = source.getByteStream();
179:
180: if (stream != null) {
181: String encoding = source.getEncoding();
182:
183: if (encoding != null) {
184: try {
185: reader = new InputStreamReader(stream, encoding);
186: parser.setReader(new StdXMLReader(reader));
187: } catch (UnsupportedEncodingException exception) {
188: throw new SAXException(exception);
189: }
190: } else { // if encoding == null
191: parser.setReader(new StdXMLReader(stream));
192: }
193: } else { // if stream == null
194: parser.setReader(new StdXMLReader(source.getPublicId(),
195: source.getSystemId()));
196: }
197: }
198:
199: try {
200: parser.parse();
201: this .adapter.endDocument();
202: } catch (IOException exception) {
203: throw exception;
204: } catch (Exception exception) {
205: throw new SAXException(exception);
206: } finally {
207: reader.close();
208: }
209: }
210:
211: /**
212: * Parse an XML document from a system identifier (URI).
213: *
214: * @param systemId the system ID
215: */
216: public void parse(String systemId) throws SAXException, IOException {
217: IXMLParser parser = this .createParser();
218: parser.setBuilder(this .adapter);
219: parser.setReader(new StdXMLReader(null, systemId));
220:
221: try {
222: parser.parse();
223: this .adapter.endDocument();
224: } catch (IOException exception) {
225: throw exception;
226: } catch (Exception exception) {
227: throw new SAXException(exception);
228: }
229: }
230:
231: }
|