Path: blob/master/src/java.base/share/classes/jdk/internal/util/xml/SAXParser.java
41161 views
/*1* Copyright (c) 2012, 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 jdk.internal.util.xml;2627import java.io.File;28import java.io.IOException;29import java.io.InputStream;30import jdk.internal.org.xml.sax.InputSource;31import jdk.internal.org.xml.sax.SAXException;32import jdk.internal.org.xml.sax.XMLReader;33import jdk.internal.org.xml.sax.helpers.DefaultHandler;343536/**37* Defines the API that wraps an {@link org.xml.sax.XMLReader}38* implementation class. In JAXP 1.0, this class wrapped the39* {@link org.xml.sax.Parser} interface, however this interface was40* replaced by the {@link org.xml.sax.XMLReader}. For ease41* of transition, this class continues to support the same name42* and interface as well as supporting new methods.43*44* An instance of this class can be obtained from the45* {@link javax.xml.parsers.SAXParserFactory#newSAXParser()} method.46* Once an instance of this class is obtained, XML can be parsed from47* a variety of input sources. These input sources are InputStreams,48* Files, URLs, and SAX InputSources.<p>49*50* This static method creates a new factory instance based51* on a system property setting or uses the platform default52* if no property has been defined.<p>53*54* The system property that controls which Factory implementation55* to create is named <code>"javax.xml.parsers.SAXParserFactory"</code>.56* This property names a class that is a concrete subclass of this57* abstract class. If no property is defined, a platform default58* will be used.</p>59*60* As the content is parsed by the underlying parser, methods of the61* given62* {@link org.xml.sax.helpers.DefaultHandler} are called.<p>63*64* Implementors of this class which wrap an underlaying implementation65* can consider using the {@link org.xml.sax.helpers.ParserAdapter}66* class to initially adapt their SAX1 implementation to work under67* this revised class.68*69* @author <a href="mailto:[email protected]">Jeff Suttor</a>70* @version $Revision: 1.8 $, $Date: 2010-11-01 04:36:09 $71*72* @author Joe Wang73* This is a subset of that in JAXP, javax.xml.parsers.SAXParser74*75*/76public abstract class SAXParser {7778/**79* <p>Protected constructor to prevent instantiation.</p>80*/81protected SAXParser() {82}8384/**85* Parse the content of the given {@link java.io.InputStream}86* instance as XML using the specified87* {@link org.xml.sax.helpers.DefaultHandler}.88*89* @param is InputStream containing the content to be parsed.90* @param dh The SAX DefaultHandler to use.91*92* @throws IllegalArgumentException If the given InputStream is null.93* @throws IOException If any IO errors occur.94* @throws SAXException If any SAX errors occur during processing.95*96* @see org.xml.sax.DocumentHandler97*/98public void parse(InputStream is, DefaultHandler dh)99throws SAXException, IOException100{101if (is == null) {102throw new IllegalArgumentException("InputStream cannot be null");103}104105InputSource input = new InputSource(is);106this.parse(input, dh);107}108109/**110* Parse the content described by the giving Uniform Resource111* Identifier (URI) as XML using the specified112* {@link org.xml.sax.helpers.DefaultHandler}.113*114* @param uri The location of the content to be parsed.115* @param dh The SAX DefaultHandler to use.116*117* @throws IllegalArgumentException If the uri is null.118* @throws IOException If any IO errors occur.119* @throws SAXException If any SAX errors occur during processing.120*121* @see org.xml.sax.DocumentHandler122*/123public void parse(String uri, DefaultHandler dh)124throws SAXException, IOException125{126if (uri == null) {127throw new IllegalArgumentException("uri cannot be null");128}129130InputSource input = new InputSource(uri);131this.parse(input, dh);132}133134/**135* Parse the content of the file specified as XML using the136* specified {@link org.xml.sax.helpers.DefaultHandler}.137*138* @param f The file containing the XML to parse139* @param dh The SAX DefaultHandler to use.140*141* @throws IllegalArgumentException If the File object is null.142* @throws IOException If any IO errors occur.143* @throws SAXException If any SAX errors occur during processing.144*145* @see org.xml.sax.DocumentHandler146*/147public void parse(File f, DefaultHandler dh)148throws SAXException, IOException149{150if (f == null) {151throw new IllegalArgumentException("File cannot be null");152}153154//convert file to appropriate URI, f.toURI().toASCIIString()155//converts the URI to string as per rule specified in156//RFC 2396,157InputSource input = new InputSource(f.toURI().toASCIIString());158this.parse(input, dh);159}160161/**162* Parse the content given {@link org.xml.sax.InputSource}163* as XML using the specified164* {@link org.xml.sax.helpers.DefaultHandler}.165*166* @param is The InputSource containing the content to be parsed.167* @param dh The SAX DefaultHandler to use.168*169* @throws IllegalArgumentException If the <code>InputSource</code> object170* is <code>null</code>.171* @throws IOException If any IO errors occur.172* @throws SAXException If any SAX errors occur during processing.173*174* @see org.xml.sax.DocumentHandler175*/176public void parse(InputSource is, DefaultHandler dh)177throws SAXException, IOException178{179if (is == null) {180throw new IllegalArgumentException("InputSource cannot be null");181}182183XMLReader reader = this.getXMLReader();184if (dh != null) {185reader.setContentHandler(dh);186reader.setEntityResolver(dh);187reader.setErrorHandler(dh);188reader.setDTDHandler(dh);189}190reader.parse(is);191}192193/**194* Returns the {@link org.xml.sax.XMLReader} that is encapsulated by the195* implementation of this class.196*197* @return The XMLReader that is encapsulated by the198* implementation of this class.199*200* @throws SAXException If any SAX errors occur during processing.201*/202public abstract XMLReader getXMLReader() throws SAXException;203204/**205* Indicates whether or not this parser is configured to206* understand namespaces.207*208* @return true if this parser is configured to209* understand namespaces; false otherwise.210*/211public abstract boolean isNamespaceAware();212213/**214* Indicates whether or not this parser is configured to215* validate XML documents.216*217* @return true if this parser is configured to218* validate XML documents; false otherwise.219*/220public abstract boolean isValidating();221222/**223* <p>Get the XInclude processing mode for this parser.</p>224*225* @return226* the return value of227* the {@link SAXParserFactory#isXIncludeAware()}228* when this parser was created from factory.229*230* @throws UnsupportedOperationException When implementation does not231* override this method232*233* @since 1.5234*235* @see SAXParserFactory#setXIncludeAware(boolean)236*/237public boolean isXIncludeAware() {238throw new UnsupportedOperationException(239"This parser does not support specification \""240+ this.getClass().getPackage().getSpecificationTitle()241+ "\" version \""242+ this.getClass().getPackage().getSpecificationVersion()243+ "\"");244}245}246247248