001: /*
002: * $Id: SAXParser.java,v 1.4 2001/12/05 23:16:55 db Exp $
003: * Copyright (C) 2001 Andrew Selkirk
004: * Copyright (C) 2001 David Brownell
005: *
006: * This file is part of GNU JAXP, a library.
007: *
008: * GNU JAXP is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License as published by
010: * the Free Software Foundation; either version 2 of the License, or
011: * (at your option) any later version.
012: *
013: * GNU JAXP is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021: *
022: * As a special exception, if you link this library with other files to
023: * produce an executable, this library does not by itself cause the
024: * resulting executable to be covered by the GNU General Public License.
025: * This exception does not however invalidate any other reasons why the
026: * executable file might be covered by the GNU General Public License.
027: */
028:
029: package javax.xml.parsers;
030:
031: // Imports
032: import java.io.*;
033: import java.net.*;
034: import org.w3c.dom.*;
035: import org.xml.sax.*;
036: import org.xml.sax.helpers.*;
037:
038: /**
039: * Wraps a SAX2 (or SAX1) parser.
040: *
041: * <p>Note that parsing with methods on this interface requires use of one
042: * of the optional SAX base classes. It's usually preferable to use the
043: * SAX parser APIs directly. SAX gives much more flexibility about how
044: * application classes are organized, and about how the document entity is
045: * packaged for delivery to the parser. And JAXP doesn't otherwise provide
046: * access to the SAX2 extension handlers for lexical or declaration events.
047: *
048: * @author Andrew Selkirk
049: * @author David Brownell
050: * @version 1.0
051: */
052: public abstract class SAXParser {
053: /** Only subclasses may use the constructor. */
054: protected SAXParser() {
055: }
056:
057: //-------------------------------------------------------------
058: // Methods ----------------------------------------------------
059: //-------------------------------------------------------------
060:
061: public abstract void setProperty(String id, Object value)
062: throws SAXNotRecognizedException, SAXNotSupportedException;
063:
064: public abstract Object getProperty(String id)
065: throws SAXNotRecognizedException, SAXNotSupportedException;
066:
067: /**
068: * Parse using (deprecated) SAX1 style handlers,
069: * and a byte stream (with no URI).
070: * Avoid using this API, since relative URIs in the document need
071: * to be resolved against the document entity's URI, and good
072: * diagnostics also need that URI.
073: */
074: public void parse(InputStream stream, HandlerBase handler)
075: throws SAXException, IOException {
076: parse(new InputSource(stream), handler);
077: }
078:
079: /**
080: * Parse using (deprecated) SAX1 style handlers,
081: * and a byte stream with a specified URI.
082: */
083: public void parse(InputStream stream, HandlerBase handler,
084: String systemID) throws SAXException, IOException {
085: InputSource source;
086:
087: // Prepare Source
088: source = new InputSource(stream);
089: source.setSystemId(systemID);
090:
091: parse(source, handler);
092:
093: }
094:
095: /**
096: * Parse using SAX2 style handlers,
097: * and a byte stream (with no URI).
098: * Avoid using this API, since relative URIs in the document need
099: * to be resolved against the document entity's URI, and good
100: * diagnostics also need that URI.
101: */
102: public void parse(InputStream stream, DefaultHandler def)
103: throws SAXException, IOException {
104: parse(new InputSource(stream), def);
105: }
106:
107: /**
108: * Parse using SAX2 style handlers,
109: * and a byte stream with a specified URI.
110: */
111: public void parse(InputStream stream, DefaultHandler def,
112: String systemID) throws SAXException, IOException {
113: InputSource source;
114:
115: // Prepare Source
116: source = new InputSource(stream);
117: source.setSystemId(systemID);
118:
119: parse(source, def);
120:
121: }
122:
123: /**
124: * Parse using (deprecated) SAX1 style handlers,
125: * and a URI for the document entity.
126: */
127: public void parse(String uri, HandlerBase handler)
128: throws SAXException, IOException {
129: parse(new InputSource(uri), handler);
130: }
131:
132: /**
133: * Parse using SAX2 style handlers,
134: * and a URI for the document entity.
135: */
136: public void parse(String uri, DefaultHandler def)
137: throws SAXException, IOException {
138: parse(new InputSource(uri), def);
139: }
140:
141: /**
142: * Parse using (deprecated) SAX1 style handlers,
143: * turning a file name into the document URI.
144: */
145: public void parse(File file, HandlerBase handler)
146: throws SAXException, IOException {
147: InputSource in;
148:
149: in = new InputSource(DocumentBuilder.fileToURL(file));
150: parse(in, handler);
151: }
152:
153: /**
154: * Parse using SAX2 style handlers,
155: * turning a file name into the document URI.
156: */
157: public void parse(File file, DefaultHandler def)
158: throws SAXException, IOException {
159: InputSource in;
160:
161: in = new InputSource(DocumentBuilder.fileToURL(file));
162: parse(in, def);
163: }
164:
165: /**
166: * Parse using (deprecated) SAX1 style handlers.
167: */
168: public void parse(InputSource source, HandlerBase handler)
169: throws SAXException, IOException {
170: Parser parser;
171:
172: // Prepare Parser
173: parser = getParser();
174: parser.setDocumentHandler(handler);
175: parser.setDTDHandler(handler);
176: parser.setEntityResolver(handler);
177: parser.setErrorHandler(handler);
178:
179: // Parse
180: parser.parse(source);
181:
182: }
183:
184: /**
185: * Parse using SAX2 style handlers.
186: */
187: public void parse(InputSource source, DefaultHandler def)
188: throws SAXException, IOException {
189: XMLReader reader;
190:
191: // Prepare XML Reader
192: reader = getXMLReader();
193: reader.setContentHandler(def);
194: reader.setDTDHandler(def);
195: reader.setEntityResolver(def);
196: reader.setErrorHandler(def);
197:
198: // NOTE: this should NOT understand the
199: // extension handlers (lexical, decl).
200:
201: reader.parse(source);
202: }
203:
204: /**
205: * Get a (deprecated) SAX1 driver for the underlying parser.
206: */
207: public abstract Parser getParser() throws SAXException;
208:
209: /**
210: * Get a SAX2 driver for the underlying parser.
211: */
212: public abstract XMLReader getXMLReader() throws SAXException;
213:
214: public abstract boolean isNamespaceAware();
215:
216: public abstract boolean isValidating();
217: }
|