Path: blob/jdk8u272-b10-aarch32-20201026/jaxp/src/javax/xml/parsers/DocumentBuilder.java
48789 views
/*1* Copyright (c) 2000, 2006, 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.w3c.dom.Document;34import org.w3c.dom.DOMImplementation;3536import org.xml.sax.EntityResolver;37import org.xml.sax.ErrorHandler;38import org.xml.sax.InputSource;39import org.xml.sax.SAXException;4041/**42* Defines the API to obtain DOM Document instances from an XML43* document. Using this class, an application programmer can obtain a44* {@link Document} from XML.<p>45*46* An instance of this class can be obtained from the47* {@link DocumentBuilderFactory#newDocumentBuilder()} method. Once48* an instance of this class is obtained, XML can be parsed from a49* variety of input sources. These input sources are InputStreams,50* Files, URLs, and SAX InputSources.<p>51*52* Note that this class reuses several classes from the SAX API. This53* does not require that the implementor of the underlying DOM54* implementation use a SAX parser to parse XML document into a55* <code>Document</code>. It merely requires that the implementation56* communicate with the application using these existing APIs.57*58* @author <a href="mailto:[email protected]">Jeff Suttor</a>59*/6061public abstract class DocumentBuilder {626364/** Protected constructor */65protected DocumentBuilder () {66}6768/**69* <p>Reset this <code>DocumentBuilder</code> to its original configuration.</p>70*71* <p><code>DocumentBuilder</code> is reset to the same state as when it was created with72* {@link DocumentBuilderFactory#newDocumentBuilder()}.73* <code>reset()</code> is designed to allow the reuse of existing <code>DocumentBuilder</code>s74* thus saving resources associated with the creation of new <code>DocumentBuilder</code>s.</p>75*76* <p>The reset <code>DocumentBuilder</code> is not guaranteed to have the same {@link EntityResolver} or {@link ErrorHandler}77* <code>Object</code>s, e.g. {@link Object#equals(Object obj)}. It is guaranteed to have a functionally equal78* <code>EntityResolver</code> and <code>ErrorHandler</code>.</p>79*80* @throws UnsupportedOperationException When implementation does not81* override this method.82*83* @since 1.584*/85public void reset() {8687// implementors should override this method88throw new UnsupportedOperationException(89"This DocumentBuilder, \"" + this.getClass().getName() + "\", does not support the reset functionality."90+ " Specification \"" + this.getClass().getPackage().getSpecificationTitle() + "\""91+ " version \"" + this.getClass().getPackage().getSpecificationVersion() + "\""92);93}9495/**96* Parse the content of the given <code>InputStream</code> as an XML97* document and return a new DOM {@link Document} object.98* An <code>IllegalArgumentException</code> is thrown if the99* <code>InputStream</code> is null.100*101* @param is InputStream containing the content to be parsed.102*103* @return <code>Document</code> result of parsing the104* <code>InputStream</code>105*106* @throws IOException If any IO errors occur.107* @throws SAXException If any parse errors occur.108* @throws IllegalArgumentException When <code>is</code> is <code>null</code>109*110* @see org.xml.sax.DocumentHandler111*/112113public Document parse(InputStream is)114throws SAXException, IOException {115if (is == null) {116throw new IllegalArgumentException("InputStream cannot be null");117}118119InputSource in = new InputSource(is);120return parse(in);121}122123/**124* Parse the content of the given <code>InputStream</code> as an125* XML document and return a new DOM {@link Document} object.126* An <code>IllegalArgumentException</code> is thrown if the127* <code>InputStream</code> is null.128*129* @param is InputStream containing the content to be parsed.130* @param systemId Provide a base for resolving relative URIs.131*132* @return A new DOM Document object.133*134* @throws IOException If any IO errors occur.135* @throws SAXException If any parse errors occur.136* @throws IllegalArgumentException When <code>is</code> is <code>null</code>137*138* @see org.xml.sax.DocumentHandler139*/140141public Document parse(InputStream is, String systemId)142throws SAXException, IOException {143if (is == null) {144throw new IllegalArgumentException("InputStream cannot be null");145}146147InputSource in = new InputSource(is);148in.setSystemId(systemId);149return parse(in);150}151152/**153* Parse the content of the given URI as an XML document154* and return a new DOM {@link Document} object.155* An <code>IllegalArgumentException</code> is thrown if the156* URI is <code>null</code> null.157*158* @param uri The location of the content to be parsed.159*160* @return A new DOM Document object.161*162* @throws IOException If any IO errors occur.163* @throws SAXException If any parse errors occur.164* @throws IllegalArgumentException When <code>uri</code> is <code>null</code>165*166* @see org.xml.sax.DocumentHandler167*/168169public Document parse(String uri)170throws SAXException, IOException {171if (uri == null) {172throw new IllegalArgumentException("URI cannot be null");173}174175InputSource in = new InputSource(uri);176return parse(in);177}178179/**180* Parse the content of the given file as an XML document181* and return a new DOM {@link Document} object.182* An <code>IllegalArgumentException</code> is thrown if the183* <code>File</code> is <code>null</code> null.184*185* @param f The file containing the XML to parse.186*187* @throws IOException If any IO errors occur.188* @throws SAXException If any parse errors occur.189* @throws IllegalArgumentException When <code>f</code> is <code>null</code>190*191* @see org.xml.sax.DocumentHandler192* @return A new DOM Document object.193*/194195public Document parse(File f) throws SAXException, IOException {196if (f == null) {197throw new IllegalArgumentException("File cannot be null");198}199200//convert file to appropriate URI, f.toURI().toASCIIString()201//converts the URI to string as per rule specified in202//RFC 2396,203InputSource in = new InputSource(f.toURI().toASCIIString());204return parse(in);205}206207/**208* Parse the content of the given input source as an XML document209* and return a new DOM {@link Document} object.210* An <code>IllegalArgumentException</code> is thrown if the211* <code>InputSource</code> is <code>null</code> null.212*213* @param is InputSource containing the content to be parsed.214*215* @return A new DOM Document object.216*217* @throws IOException If any IO errors occur.218* @throws SAXException If any parse errors occur.219* @throws IllegalArgumentException When <code>is</code> is <code>null</code>220*221* @see org.xml.sax.DocumentHandler222*/223224public abstract Document parse(InputSource is)225throws SAXException, IOException;226227228/**229* Indicates whether or not this parser is configured to230* understand namespaces.231*232* @return true if this parser is configured to understand233* namespaces; false otherwise.234*/235236public abstract boolean isNamespaceAware();237238/**239* Indicates whether or not this parser is configured to240* validate XML documents.241*242* @return true if this parser is configured to validate243* XML documents; false otherwise.244*/245246public abstract boolean isValidating();247248/**249* Specify the {@link EntityResolver} to be used to resolve250* entities present in the XML document to be parsed. Setting251* this to <code>null</code> will result in the underlying252* implementation using it's own default implementation and253* behavior.254*255* @param er The <code>EntityResolver</code> to be used to resolve entities256* present in the XML document to be parsed.257*/258259public abstract void setEntityResolver(EntityResolver er);260261/**262* Specify the {@link ErrorHandler} to be used by the parser.263* Setting this to <code>null</code> will result in the underlying264* implementation using it's own default implementation and265* behavior.266*267* @param eh The <code>ErrorHandler</code> to be used by the parser.268*/269270public abstract void setErrorHandler(ErrorHandler eh);271272/**273* Obtain a new instance of a DOM {@link Document} object274* to build a DOM tree with.275*276* @return A new instance of a DOM Document object.277*/278279public abstract Document newDocument();280281/**282* Obtain an instance of a {@link DOMImplementation} object.283*284* @return A new instance of a <code>DOMImplementation</code>.285*/286287public abstract DOMImplementation getDOMImplementation();288289/** <p>Get current state of canonicalization.</p>290*291* @return current state canonicalization control292*/293/*294public boolean getCanonicalization() {295return canonicalState;296}297*/298299/** <p>Get a reference to the the {@link Schema} being used by300* the XML processor.</p>301*302* <p>If no schema is being used, <code>null</code> is returned.</p>303*304* @return {@link Schema} being used or <code>null</code>305* if none in use306*307* @throws UnsupportedOperationException When implementation does not308* override this method309*310* @since 1.5311*/312public Schema getSchema() {313throw new UnsupportedOperationException(314"This parser does not support specification \""315+ this.getClass().getPackage().getSpecificationTitle()316+ "\" version \""317+ this.getClass().getPackage().getSpecificationVersion()318+ "\""319);320}321322323/**324* <p>Get the XInclude processing mode for this parser.</p>325*326* @return327* the return value of328* the {@link DocumentBuilderFactory#isXIncludeAware()}329* when this parser was created from factory.330*331* @throws UnsupportedOperationException When implementation does not332* override this method333*334* @since 1.5335*336* @see DocumentBuilderFactory#setXIncludeAware(boolean)337*/338public boolean isXIncludeAware() {339throw new UnsupportedOperationException(340"This parser does not support specification \""341+ this.getClass().getPackage().getSpecificationTitle()342+ "\" version \""343+ this.getClass().getPackage().getSpecificationVersion()344+ "\""345);346}347}348349350