Path: blob/master/src/java.xml/share/classes/javax/xml/parsers/SAXParser.java
40948 views
/*1* Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.xml.parsers;2627import java.io.File;28import java.io.IOException;29import java.io.InputStream;3031import javax.xml.validation.Schema;3233import org.xml.sax.HandlerBase;34import org.xml.sax.InputSource;35import org.xml.sax.Parser;36import org.xml.sax.SAXException;37import org.xml.sax.SAXNotRecognizedException;38import org.xml.sax.SAXNotSupportedException;39import org.xml.sax.XMLReader;40import org.xml.sax.helpers.DefaultHandler;414243/**44* Defines the API that wraps an {@link org.xml.sax.XMLReader}45* implementation class. In JAXP 1.0, this class wrapped the46* {@link org.xml.sax.Parser} interface, however this interface was47* replaced by the {@link org.xml.sax.XMLReader}. For ease48* of transition, this class continues to support the same name49* and interface as well as supporting new methods.50*51* An instance of this class can be obtained from the52* {@link javax.xml.parsers.SAXParserFactory#newSAXParser()} method.53* Once an instance of this class is obtained, XML can be parsed from54* a variety of input sources. These input sources are InputStreams,55* Files, URLs, and SAX InputSources.<p>56*57* This static method creates a new factory instance based58* on a system property setting or uses the platform default59* if no property has been defined.<p>60*61* The system property that controls which Factory implementation62* to create is named <code>"javax.xml.parsers.SAXParserFactory"</code>.63* This property names a class that is a concrete subclass of this64* abstract class. If no property is defined, a platform default65* will be used.</p>66*67* As the content is parsed by the underlying parser, methods of the68* given {@link org.xml.sax.HandlerBase} or the69* {@link org.xml.sax.helpers.DefaultHandler} are called.<p>70*71* Implementors of this class which wrap an underlying implementation72* can consider using the {@link org.xml.sax.helpers.ParserAdapter}73* class to initially adapt their SAX1 implementation to work under74* this revised class.75*76* @author Jeff Suttor77* @since 1.478*/79@SuppressWarnings("deprecation")80public abstract class SAXParser {8182/**83* <p>Protected constructor to prevent instantiation.84* Use {@link javax.xml.parsers.SAXParserFactory#newSAXParser()}.</p>85*/86protected SAXParser () {8788}8990/**91* <p>Reset this <code>SAXParser</code> to its original configuration.</p>92*93* <p><code>SAXParser</code> is reset to the same state as when it was created with94* {@link SAXParserFactory#newSAXParser()}.95* <code>reset()</code> is designed to allow the reuse of existing <code>SAXParser</code>s96* thus saving resources associated with the creation of new <code>SAXParser</code>s.</p>97*98* <p>The reset <code>SAXParser</code> is not guaranteed to have the same {@link Schema}99* <code>Object</code>, e.g. {@link Object#equals(Object obj)}. It is guaranteed to have a functionally equal100* <code>Schema</code>.</p>101*102* @throws UnsupportedOperationException When Implementations do not103* override this method104*105* @since 1.5106*/107public void reset() {108109// implementors should override this method110throw new UnsupportedOperationException(111"This SAXParser, \"" + this.getClass().getName() + "\", does not support the reset functionality."112+ " Specification \"" + this.getClass().getPackage().getSpecificationTitle() + "\""113+ " version \"" + this.getClass().getPackage().getSpecificationVersion() + "\""114);115}116117/**118* <p>Parse the content of the given {@link java.io.InputStream}119* instance as XML using the specified {@link org.xml.sax.HandlerBase}.120* <i> Use of the DefaultHandler version of this method is recommended as121* the HandlerBase class has been deprecated in SAX 2.0</i>.</p>122*123* @param is InputStream containing the content to be parsed.124* @param hb The SAX HandlerBase to use.125*126* @throws IllegalArgumentException If the given InputStream is null.127* @throws SAXException If parse produces a SAX error.128* @throws IOException If an IO error occurs interacting with the129* <code>InputStream</code>.130*131* @see org.xml.sax.DocumentHandler132*/133public void parse(InputStream is, HandlerBase hb)134throws SAXException, IOException {135if (is == null) {136throw new IllegalArgumentException("InputStream cannot be null");137}138139InputSource input = new InputSource(is);140this.parse(input, hb);141}142143/**144* <p>Parse the content of the given {@link java.io.InputStream}145* instance as XML using the specified {@link org.xml.sax.HandlerBase}.146* <i> Use of the DefaultHandler version of this method is recommended as147* the HandlerBase class has been deprecated in SAX 2.0</i>.</p>148*149* @param is InputStream containing the content to be parsed.150* @param hb The SAX HandlerBase to use.151* @param systemId The systemId which is needed for resolving relative URIs.152*153* @throws IllegalArgumentException If the given <code>InputStream</code> is154* <code>null</code>.155* @throws IOException If any IO error occurs interacting with the156* <code>InputStream</code>.157* @throws SAXException If any SAX errors occur during processing.158*159* @see org.xml.sax.DocumentHandler version of this method instead.160*/161public void parse(162InputStream is,163HandlerBase hb,164String systemId)165throws SAXException, IOException {166if (is == null) {167throw new IllegalArgumentException("InputStream cannot be null");168}169170InputSource input = new InputSource(is);171input.setSystemId(systemId);172this.parse(input, hb);173}174175/**176* Parse the content of the given {@link java.io.InputStream}177* instance as XML using the specified178* {@link org.xml.sax.helpers.DefaultHandler}.179*180* @param is InputStream containing the content to be parsed.181* @param dh The SAX DefaultHandler to use.182*183* @throws IllegalArgumentException If the given InputStream is null.184* @throws IOException If any IO errors occur.185* @throws SAXException If any SAX errors occur during processing.186*187* @see org.xml.sax.DocumentHandler188*/189public void parse(InputStream is, DefaultHandler dh)190throws SAXException, IOException {191if (is == null) {192throw new IllegalArgumentException("InputStream cannot be null");193}194195InputSource input = new InputSource(is);196this.parse(input, dh);197}198199/**200* Parse the content of the given {@link java.io.InputStream}201* instance as XML using the specified202* {@link org.xml.sax.helpers.DefaultHandler}.203*204* @param is InputStream containing the content to be parsed.205* @param dh The SAX DefaultHandler to use.206* @param systemId The systemId which is needed for resolving relative URIs.207*208* @throws IllegalArgumentException If the given InputStream is null.209* @throws IOException If any IO errors occur.210* @throws SAXException If any SAX errors occur during processing.211*212* @see org.xml.sax.DocumentHandler version of this method instead.213*/214public void parse(215InputStream is,216DefaultHandler dh,217String systemId)218throws SAXException, IOException {219if (is == null) {220throw new IllegalArgumentException("InputStream cannot be null");221}222223InputSource input = new InputSource(is);224input.setSystemId(systemId);225this.parse(input, dh);226}227228/**229* Parse the content described by the giving Uniform Resource230* Identifier (URI) as XML using the specified231* {@link org.xml.sax.HandlerBase}.232* <i> Use of the DefaultHandler version of this method is recommended as233* the <code>HandlerBase</code> class has been deprecated in SAX 2.0</i>234*235* @param uri The location of the content to be parsed.236* @param hb The SAX HandlerBase to use.237*238* @throws IllegalArgumentException If the uri is null.239* @throws IOException If any IO errors occur.240* @throws SAXException If any SAX errors occur during processing.241*242* @see org.xml.sax.DocumentHandler243*/244public void parse(String uri, HandlerBase hb)245throws SAXException, IOException {246if (uri == null) {247throw new IllegalArgumentException("uri cannot be null");248}249250InputSource input = new InputSource(uri);251this.parse(input, hb);252}253254/**255* Parse the content described by the giving Uniform Resource256* Identifier (URI) as XML using the specified257* {@link org.xml.sax.helpers.DefaultHandler}.258*259* @param uri The location of the content to be parsed.260* @param dh The SAX DefaultHandler to use.261*262* @throws IllegalArgumentException If the uri is null.263* @throws IOException If any IO errors occur.264* @throws SAXException If any SAX errors occur during processing.265*266* @see org.xml.sax.DocumentHandler267*/268public void parse(String uri, DefaultHandler dh)269throws SAXException, IOException {270if (uri == null) {271throw new IllegalArgumentException("uri cannot be null");272}273274InputSource input = new InputSource(uri);275this.parse(input, dh);276}277278/**279* Parse the content of the file specified as XML using the280* specified {@link org.xml.sax.HandlerBase}.281* <i> Use of the DefaultHandler version of this method is recommended as282* the HandlerBase class has been deprecated in SAX 2.0</i>283*284* @param f The file containing the XML to parse285* @param hb The SAX HandlerBase to use.286*287* @throws IllegalArgumentException If the File object is null.288* @throws IOException If any IO errors occur.289* @throws SAXException If any SAX errors occur during processing.290*291* @see org.xml.sax.DocumentHandler292*/293public void parse(File f, HandlerBase hb)294throws SAXException, IOException {295if (f == null) {296throw new IllegalArgumentException("File cannot be null");297}298299//convert file to appropriate URI, f.toURI().toASCIIString()300//converts the URI to string as per rule specified in301//RFC 2396,302InputSource input = new InputSource(f.toURI().toASCIIString());303this.parse(input, hb);304}305306/**307* Parse the content of the file specified as XML using the308* specified {@link org.xml.sax.helpers.DefaultHandler}.309*310* @param f The file containing the XML to parse311* @param dh The SAX DefaultHandler to use.312*313* @throws IllegalArgumentException If the File object is null.314* @throws IOException If any IO errors occur.315* @throws SAXException If any SAX errors occur during processing.316*317* @see org.xml.sax.DocumentHandler318*/319public void parse(File f, DefaultHandler dh)320throws SAXException, IOException {321if (f == null) {322throw new IllegalArgumentException("File cannot be null");323}324325//convert file to appropriate URI, f.toURI().toASCIIString()326//converts the URI to string as per rule specified in327//RFC 2396,328InputSource input = new InputSource(f.toURI().toASCIIString());329this.parse(input, dh);330}331332/**333* Parse the content given {@link org.xml.sax.InputSource}334* as XML using the specified335* {@link org.xml.sax.HandlerBase}.336* <i> Use of the DefaultHandler version of this method is recommended as337* the HandlerBase class has been deprecated in SAX 2.0</i>338*339* @param is The InputSource containing the content to be parsed.340* @param hb The SAX HandlerBase to use.341*342* @throws IllegalArgumentException If the <code>InputSource</code> object343* is <code>null</code>.344* @throws IOException If any IO errors occur.345* @throws SAXException If any SAX errors occur during processing.346*347* @see org.xml.sax.DocumentHandler348*/349public void parse(InputSource is, HandlerBase hb)350throws SAXException, IOException {351if (is == null) {352throw new IllegalArgumentException("InputSource cannot be null");353}354355Parser parser = this.getParser();356if (hb != null) {357parser.setDocumentHandler(hb);358parser.setEntityResolver(hb);359parser.setErrorHandler(hb);360parser.setDTDHandler(hb);361}362parser.parse(is);363}364365/**366* Parse the content given {@link org.xml.sax.InputSource}367* as XML using the specified368* {@link org.xml.sax.helpers.DefaultHandler}.369*370* @param is The InputSource containing the content to be parsed.371* @param dh The SAX DefaultHandler to use.372*373* @throws IllegalArgumentException If the <code>InputSource</code> object374* is <code>null</code>.375* @throws IOException If any IO errors occur.376* @throws SAXException If any SAX errors occur during processing.377*378* @see org.xml.sax.DocumentHandler379*/380public void parse(InputSource is, DefaultHandler dh)381throws SAXException, IOException {382if (is == null) {383throw new IllegalArgumentException("InputSource cannot be null");384}385386XMLReader reader = this.getXMLReader();387if (dh != null) {388reader.setContentHandler(dh);389reader.setEntityResolver(dh);390reader.setErrorHandler(dh);391reader.setDTDHandler(dh);392}393reader.parse(is);394}395396/**397* Returns the SAX parser that is encapsulated by the398* implementation of this class.399*400* @return The SAX parser that is encapsulated by the401* implementation of this class.402*403* @throws SAXException If any SAX errors occur during processing.404*/405public abstract org.xml.sax.Parser getParser() throws SAXException;406407/**408* Returns the {@link org.xml.sax.XMLReader} that is encapsulated by the409* implementation of this class.410*411* @return The XMLReader that is encapsulated by the412* implementation of this class.413*414* @throws SAXException If any SAX errors occur during processing.415*/416417public abstract org.xml.sax.XMLReader getXMLReader() throws SAXException;418419/**420* Indicates whether or not this parser is configured to421* understand namespaces.422*423* @return true if this parser is configured to424* understand namespaces; false otherwise.425*/426427public abstract boolean isNamespaceAware();428429/**430* Indicates whether or not this parser is configured to431* validate XML documents.432*433* @return true if this parser is configured to434* validate XML documents; false otherwise.435*/436437public abstract boolean isValidating();438439/**440* <p>Sets the particular property in the underlying implementation of441* {@link org.xml.sax.XMLReader}.442* A list of the core features and properties can be found at443* <a href="http://sax.sourceforge.net/?selected=get-set">444* http://sax.sourceforge.net/?selected=get-set</a>.</p>445* <p>446* All implementations that implement JAXP 1.5 or newer are required to447* support the {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_DTD} and448* {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_SCHEMA} properties.449* </p>450* <ul>451* <li>452* <p>453* Setting the {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_DTD} property454* restricts the access to external DTDs, external Entity References to455* the protocols specified by the property. If access is denied during parsing456* due to the restriction of this property, {@link org.xml.sax.SAXException}457* will be thrown by the parse methods defined by {@link javax.xml.parsers.SAXParser}.458* </p>459* <p>460* Setting the {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_SCHEMA} property461* restricts the access to external Schema set by the schemaLocation attribute to462* the protocols specified by the property. If access is denied during parsing463* due to the restriction of this property, {@link org.xml.sax.SAXException}464* will be thrown by the parse methods defined by the {@link javax.xml.parsers.SAXParser}.465* </p>466* </li>467* </ul>468*469* @param name The name of the property to be set.470* @param value The value of the property to be set.471*472* @throws SAXNotRecognizedException When the underlying XMLReader does473* not recognize the property name.474* @throws SAXNotSupportedException When the underlying XMLReader475* recognizes the property name but doesn't support the property.476*477* @see org.xml.sax.XMLReader#setProperty478*/479public abstract void setProperty(String name, Object value)480throws SAXNotRecognizedException, SAXNotSupportedException;481482/**483* <p>Returns the particular property requested for in the underlying484* implementation of {@link org.xml.sax.XMLReader}.</p>485*486* @param name The name of the property to be retrieved.487* @return Value of the requested property.488*489* @throws SAXNotRecognizedException When the underlying XMLReader does490* not recognize the property name.491* @throws SAXNotSupportedException When the underlying XMLReader492* recognizes the property name but doesn't support the property.493*494* @see org.xml.sax.XMLReader#getProperty495*/496public abstract Object getProperty(String name)497throws SAXNotRecognizedException, SAXNotSupportedException;498499/** <p>Get current state of canonicalization.</p>500*501* @return current state canonicalization control502*/503/*504public boolean getCanonicalization() {505return canonicalState;506}507*/508509/** <p>Get a reference to the the {@link Schema} being used by510* the XML processor.</p>511*512* <p>If no schema is being used, <code>null</code> is returned.</p>513*514* @return {@link Schema} being used or <code>null</code>515* if none in use516*517* @throws UnsupportedOperationException When implementation does not518* override this method519*520* @since 1.5521*/522public Schema getSchema() {523throw new UnsupportedOperationException(524"This parser does not support specification \""525+ this.getClass().getPackage().getSpecificationTitle()526+ "\" version \""527+ this.getClass().getPackage().getSpecificationVersion()528+ "\""529);530}531532/**533* <p>Get the XInclude processing mode for this parser.</p>534*535* @return536* the return value of537* the {@link SAXParserFactory#isXIncludeAware()}538* when this parser was created from factory.539*540* @throws UnsupportedOperationException When implementation does not541* override this method542*543* @since 1.5544*545* @see SAXParserFactory#setXIncludeAware(boolean)546*/547public boolean isXIncludeAware() {548throw new UnsupportedOperationException(549"This parser does not support specification \""550+ this.getClass().getPackage().getSpecificationTitle()551+ "\" version \""552+ this.getClass().getPackage().getSpecificationVersion()553+ "\""554);555}556}557558559