Path: blob/jdk8u272-b10-aarch32-20201026/jaxp/src/javax/xml/parsers/SAXParser.java
48789 views
/*1* Copyright (c) 2000, 2013, 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 underlaying 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 <a href="mailto:[email protected]">Jeff Suttor</a>77*/78public abstract class SAXParser {7980/**81* <p>Protected constructor to prevent instaniation.82* Use {@link javax.xml.parsers.SAXParserFactory#newSAXParser()}.</p>83*/84protected SAXParser () {8586}8788/**89* <p>Reset this <code>SAXParser</code> to its original configuration.</p>90*91* <p><code>SAXParser</code> is reset to the same state as when it was created with92* {@link SAXParserFactory#newSAXParser()}.93* <code>reset()</code> is designed to allow the reuse of existing <code>SAXParser</code>s94* thus saving resources associated with the creation of new <code>SAXParser</code>s.</p>95*96* <p>The reset <code>SAXParser</code> is not guaranteed to have the same {@link Schema}97* <code>Object</code>, e.g. {@link Object#equals(Object obj)}. It is guaranteed to have a functionally equal98* <code>Schema</code>.</p>99*100* @throws UnsupportedOperationException When Implementations do not101* override this method102*103* @since 1.5104*/105public void reset() {106107// implementors should override this method108throw new UnsupportedOperationException(109"This SAXParser, \"" + this.getClass().getName() + "\", does not support the reset functionality."110+ " Specification \"" + this.getClass().getPackage().getSpecificationTitle() + "\""111+ " version \"" + this.getClass().getPackage().getSpecificationVersion() + "\""112);113}114115/**116* <p>Parse the content of the given {@link java.io.InputStream}117* instance as XML using the specified {@link org.xml.sax.HandlerBase}.118* <i> Use of the DefaultHandler version of this method is recommended as119* the HandlerBase class has been deprecated in SAX 2.0</i>.</p>120*121* @param is InputStream containing the content to be parsed.122* @param hb The SAX HandlerBase to use.123*124* @throws IllegalArgumentException If the given InputStream is null.125* @throws SAXException If parse produces a SAX error.126* @throws IOException If an IO error occurs interacting with the127* <code>InputStream</code>.128*129* @see org.xml.sax.DocumentHandler130*/131public void parse(InputStream is, HandlerBase hb)132throws SAXException, IOException {133if (is == null) {134throw new IllegalArgumentException("InputStream cannot be null");135}136137InputSource input = new InputSource(is);138this.parse(input, hb);139}140141/**142* <p>Parse the content of the given {@link java.io.InputStream}143* instance as XML using the specified {@link org.xml.sax.HandlerBase}.144* <i> Use of the DefaultHandler version of this method is recommended as145* the HandlerBase class has been deprecated in SAX 2.0</i>.</p>146*147* @param is InputStream containing the content to be parsed.148* @param hb The SAX HandlerBase to use.149* @param systemId The systemId which is needed for resolving relative URIs.150*151* @throws IllegalArgumentException If the given <code>InputStream</code> is152* <code>null</code>.153* @throws IOException If any IO error occurs interacting with the154* <code>InputStream</code>.155* @throws SAXException If any SAX errors occur during processing.156*157* @see org.xml.sax.DocumentHandler version of this method instead.158*/159public void parse(160InputStream is,161HandlerBase hb,162String systemId)163throws SAXException, IOException {164if (is == null) {165throw new IllegalArgumentException("InputStream cannot be null");166}167168InputSource input = new InputSource(is);169input.setSystemId(systemId);170this.parse(input, hb);171}172173/**174* Parse the content of the given {@link java.io.InputStream}175* instance as XML using the specified176* {@link org.xml.sax.helpers.DefaultHandler}.177*178* @param is InputStream containing the content to be parsed.179* @param dh The SAX DefaultHandler to use.180*181* @throws IllegalArgumentException If the given InputStream is null.182* @throws IOException If any IO errors occur.183* @throws SAXException If any SAX errors occur during processing.184*185* @see org.xml.sax.DocumentHandler186*/187public void parse(InputStream is, DefaultHandler dh)188throws SAXException, IOException {189if (is == null) {190throw new IllegalArgumentException("InputStream cannot be null");191}192193InputSource input = new InputSource(is);194this.parse(input, dh);195}196197/**198* Parse the content of the given {@link java.io.InputStream}199* instance as XML using the specified200* {@link org.xml.sax.helpers.DefaultHandler}.201*202* @param is InputStream containing the content to be parsed.203* @param dh The SAX DefaultHandler to use.204* @param systemId The systemId which is needed for resolving relative URIs.205*206* @throws IllegalArgumentException If the given InputStream is null.207* @throws IOException If any IO errors occur.208* @throws SAXException If any SAX errors occur during processing.209*210* @see org.xml.sax.DocumentHandler version of this method instead.211*/212public void parse(213InputStream is,214DefaultHandler dh,215String systemId)216throws SAXException, IOException {217if (is == null) {218throw new IllegalArgumentException("InputStream cannot be null");219}220221InputSource input = new InputSource(is);222input.setSystemId(systemId);223this.parse(input, dh);224}225226/**227* Parse the content described by the giving Uniform Resource228* Identifier (URI) as XML using the specified229* {@link org.xml.sax.HandlerBase}.230* <i> Use of the DefaultHandler version of this method is recommended as231* the <code>HandlerBase</code> class has been deprecated in SAX 2.0</i>232*233* @param uri The location of the content to be parsed.234* @param hb The SAX HandlerBase to use.235*236* @throws IllegalArgumentException If the uri is null.237* @throws IOException If any IO errors occur.238* @throws SAXException If any SAX errors occur during processing.239*240* @see org.xml.sax.DocumentHandler241*/242public void parse(String uri, HandlerBase hb)243throws SAXException, IOException {244if (uri == null) {245throw new IllegalArgumentException("uri cannot be null");246}247248InputSource input = new InputSource(uri);249this.parse(input, hb);250}251252/**253* Parse the content described by the giving Uniform Resource254* Identifier (URI) as XML using the specified255* {@link org.xml.sax.helpers.DefaultHandler}.256*257* @param uri The location of the content to be parsed.258* @param dh The SAX DefaultHandler to use.259*260* @throws IllegalArgumentException If the uri is null.261* @throws IOException If any IO errors occur.262* @throws SAXException If any SAX errors occur during processing.263*264* @see org.xml.sax.DocumentHandler265*/266public void parse(String uri, DefaultHandler dh)267throws SAXException, IOException {268if (uri == null) {269throw new IllegalArgumentException("uri cannot be null");270}271272InputSource input = new InputSource(uri);273this.parse(input, dh);274}275276/**277* Parse the content of the file specified as XML using the278* specified {@link org.xml.sax.HandlerBase}.279* <i> Use of the DefaultHandler version of this method is recommended as280* the HandlerBase class has been deprecated in SAX 2.0</i>281*282* @param f The file containing the XML to parse283* @param hb The SAX HandlerBase to use.284*285* @throws IllegalArgumentException If the File object is null.286* @throws IOException If any IO errors occur.287* @throws SAXException If any SAX errors occur during processing.288*289* @see org.xml.sax.DocumentHandler290*/291public void parse(File f, HandlerBase hb)292throws SAXException, IOException {293if (f == null) {294throw new IllegalArgumentException("File cannot be null");295}296297//convert file to appropriate URI, f.toURI().toASCIIString()298//converts the URI to string as per rule specified in299//RFC 2396,300InputSource input = new InputSource(f.toURI().toASCIIString());301this.parse(input, hb);302}303304/**305* Parse the content of the file specified as XML using the306* specified {@link org.xml.sax.helpers.DefaultHandler}.307*308* @param f The file containing the XML to parse309* @param dh The SAX DefaultHandler to use.310*311* @throws IllegalArgumentException If the File object is null.312* @throws IOException If any IO errors occur.313* @throws SAXException If any SAX errors occur during processing.314*315* @see org.xml.sax.DocumentHandler316*/317public void parse(File f, DefaultHandler dh)318throws SAXException, IOException {319if (f == null) {320throw new IllegalArgumentException("File cannot be null");321}322323//convert file to appropriate URI, f.toURI().toASCIIString()324//converts the URI to string as per rule specified in325//RFC 2396,326InputSource input = new InputSource(f.toURI().toASCIIString());327this.parse(input, dh);328}329330/**331* Parse the content given {@link org.xml.sax.InputSource}332* as XML using the specified333* {@link org.xml.sax.HandlerBase}.334* <i> Use of the DefaultHandler version of this method is recommended as335* the HandlerBase class has been deprecated in SAX 2.0</i>336*337* @param is The InputSource containing the content to be parsed.338* @param hb The SAX HandlerBase to use.339*340* @throws IllegalArgumentException If the <code>InputSource</code> object341* is <code>null</code>.342* @throws IOException If any IO errors occur.343* @throws SAXException If any SAX errors occur during processing.344*345* @see org.xml.sax.DocumentHandler346*/347public void parse(InputSource is, HandlerBase hb)348throws SAXException, IOException {349if (is == null) {350throw new IllegalArgumentException("InputSource cannot be null");351}352353Parser parser = this.getParser();354if (hb != null) {355parser.setDocumentHandler(hb);356parser.setEntityResolver(hb);357parser.setErrorHandler(hb);358parser.setDTDHandler(hb);359}360parser.parse(is);361}362363/**364* Parse the content given {@link org.xml.sax.InputSource}365* as XML using the specified366* {@link org.xml.sax.helpers.DefaultHandler}.367*368* @param is The InputSource containing the content to be parsed.369* @param dh The SAX DefaultHandler to use.370*371* @throws IllegalArgumentException If the <code>InputSource</code> object372* is <code>null</code>.373* @throws IOException If any IO errors occur.374* @throws SAXException If any SAX errors occur during processing.375*376* @see org.xml.sax.DocumentHandler377*/378public void parse(InputSource is, DefaultHandler dh)379throws SAXException, IOException {380if (is == null) {381throw new IllegalArgumentException("InputSource cannot be null");382}383384XMLReader reader = this.getXMLReader();385if (dh != null) {386reader.setContentHandler(dh);387reader.setEntityResolver(dh);388reader.setErrorHandler(dh);389reader.setDTDHandler(dh);390}391reader.parse(is);392}393394/**395* Returns the SAX parser that is encapsultated by the396* implementation of this class.397*398* @return The SAX parser that is encapsultated by the399* implementation of this class.400*401* @throws SAXException If any SAX errors occur during processing.402*/403public abstract org.xml.sax.Parser getParser() throws SAXException;404405/**406* Returns the {@link org.xml.sax.XMLReader} that is encapsulated by the407* implementation of this class.408*409* @return The XMLReader that is encapsulated by the410* implementation of this class.411*412* @throws SAXException If any SAX errors occur during processing.413*/414415public abstract org.xml.sax.XMLReader getXMLReader() throws SAXException;416417/**418* Indicates whether or not this parser is configured to419* understand namespaces.420*421* @return true if this parser is configured to422* understand namespaces; false otherwise.423*/424425public abstract boolean isNamespaceAware();426427/**428* Indicates whether or not this parser is configured to429* validate XML documents.430*431* @return true if this parser is configured to432* validate XML documents; false otherwise.433*/434435public abstract boolean isValidating();436437/**438* <p>Sets the particular property in the underlying implementation of439* {@link org.xml.sax.XMLReader}.440* A list of the core features and properties can be found at441* <a href="http://sax.sourceforge.net/?selected=get-set">442* http://sax.sourceforge.net/?selected=get-set</a>.</p>443* <p>444* All implementations that implement JAXP 1.5 or newer are required to445* support the {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_DTD} and446* {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_SCHEMA} properties.447* </p>448* <ul>449* <li>450* <p>451* Setting the {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_DTD} property452* restricts the access to external DTDs, external Entity References to453* the protocols specified by the property. If access is denied during parsing454* due to the restriction of this property, {@link org.xml.sax.SAXException}455* will be thrown by the parse methods defined by {@link javax.xml.parsers.SAXParser}.456* </p>457* <p>458* Setting the {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_SCHEMA} property459* restricts the access to external Schema set by the schemaLocation attribute to460* the protocols specified by the property. If access is denied during parsing461* due to the restriction of this property, {@link org.xml.sax.SAXException}462* will be thrown by the parse methods defined by the {@link javax.xml.parsers.SAXParser}.463* </p>464* </li>465* </ul>466*467* @param name The name of the property to be set.468* @param value The value of the property to be set.469*470* @throws SAXNotRecognizedException When the underlying XMLReader does471* not recognize the property name.472* @throws SAXNotSupportedException When the underlying XMLReader473* recognizes the property name but doesn't support the property.474*475* @see org.xml.sax.XMLReader#setProperty476*/477public abstract void setProperty(String name, Object value)478throws SAXNotRecognizedException, SAXNotSupportedException;479480/**481* <p>Returns the particular property requested for in the underlying482* implementation of {@link org.xml.sax.XMLReader}.</p>483*484* @param name The name of the property to be retrieved.485* @return Value of the requested property.486*487* @throws SAXNotRecognizedException When the underlying XMLReader does488* not recognize the property name.489* @throws SAXNotSupportedException When the underlying XMLReader490* recognizes the property name but doesn't support the property.491*492* @see org.xml.sax.XMLReader#getProperty493*/494public abstract Object getProperty(String name)495throws SAXNotRecognizedException, SAXNotSupportedException;496497/** <p>Get current state of canonicalization.</p>498*499* @return current state canonicalization control500*/501/*502public boolean getCanonicalization() {503return canonicalState;504}505*/506507/** <p>Get a reference to the the {@link Schema} being used by508* the XML processor.</p>509*510* <p>If no schema is being used, <code>null</code> is returned.</p>511*512* @return {@link Schema} being used or <code>null</code>513* if none in use514*515* @throws UnsupportedOperationException When implementation does not516* override this method517*518* @since 1.5519*/520public Schema getSchema() {521throw new UnsupportedOperationException(522"This parser does not support specification \""523+ this.getClass().getPackage().getSpecificationTitle()524+ "\" version \""525+ this.getClass().getPackage().getSpecificationVersion()526+ "\""527);528}529530/**531* <p>Get the XInclude processing mode for this parser.</p>532*533* @return534* the return value of535* the {@link SAXParserFactory#isXIncludeAware()}536* when this parser was created from factory.537*538* @throws UnsupportedOperationException When implementation does not539* override this method540*541* @since 1.5542*543* @see SAXParserFactory#setXIncludeAware(boolean)544*/545public boolean isXIncludeAware() {546throw new UnsupportedOperationException(547"This parser does not support specification \""548+ this.getClass().getPackage().getSpecificationTitle()549+ "\" version \""550+ this.getClass().getPackage().getSpecificationVersion()551+ "\""552);553}554}555556557