Path: blob/master/src/java.xml/share/classes/org/xml/sax/XMLReader.java
40948 views
/*1* Copyright (c) 2000, 2020, 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 org.xml.sax;2627import java.io.IOException;282930/**31* Interface for reading an XML document using callbacks.32*33*34* <p>XMLReader is the interface that an XML parser's SAX2 driver must35* implement. This interface allows an application to set and36* query features and properties in the parser, to register37* event handlers for document processing, and to initiate38* a document parse.</p>39*40* <p>All SAX interfaces are assumed to be synchronous: the41* {@link #parse parse} methods must not return until parsing42* is complete, and readers must wait for an event-handler callback43* to return before reporting the next event.</p>44*45* <p>This interface replaces the (now deprecated) SAX 1.0 {@link46* org.xml.sax.Parser Parser} interface. The XMLReader interface47* contains two important enhancements over the old Parser48* interface (as well as some minor ones):</p>49*50* <ol>51* <li>it adds a standard way to query and set features and52* properties; and</li>53* <li>it adds Namespace support, which is required for many54* higher-level XML standards.</li>55* </ol>56*57* <p>There are adapters available to convert a SAX1 Parser to58* a SAX2 XMLReader and vice-versa.</p>59*60* @apiNote Despite its name, this interface does61* <em>not</em> extend the standard Java {@link java.io.Reader Reader}62* interface, because reading XML is a fundamentally different activity63* than reading character data.64*65* @since 1.4, SAX 2.066* @author David Megginson67* @see org.xml.sax.XMLFilter68* @see org.xml.sax.helpers.ParserAdapter69* @see org.xml.sax.helpers.XMLReaderAdapter70*/71public interface XMLReader72{737475////////////////////////////////////////////////////////////////////76// Configuration.77////////////////////////////////////////////////////////////////////787980/**81* Look up the value of a feature flag.82*83* <p>The feature name is any fully-qualified URI. It is84* possible for an XMLReader to recognize a feature name but85* temporarily be unable to return its value.86* Some feature values may be available only in specific87* contexts, such as before, during, or after a parse.88* Also, some feature values may not be programmatically accessible.89* (In the case of an adapter for SAX1 {@link Parser}, there is no90* implementation-independent way to expose whether the underlying91* parser is performing validation, expanding external entities,92* and so forth.) </p>93*94* <p>All XMLReaders are required to recognize the95* http://xml.org/sax/features/namespaces and the96* http://xml.org/sax/features/namespace-prefixes feature names.</p>97*98* <p>Typical usage is something like this:</p>99*100* <pre>101* XMLReader r = new MySAXDriver();102*103* // try to activate validation104* try {105* r.setFeature("http://xml.org/sax/features/validation", true);106* } catch (SAXException e) {107* System.err.println("Cannot activate validation.");108* }109*110* // register event handlers111* r.setContentHandler(new MyContentHandler());112* r.setErrorHandler(new MyErrorHandler());113*114* // parse the first document115* try {116* r.parse("http://www.foo.com/mydoc.xml");117* } catch (IOException e) {118* System.err.println("I/O exception reading XML document");119* } catch (SAXException e) {120* System.err.println("XML exception reading document.");121* }122* </pre>123*124* <p>Implementors are free (and encouraged) to invent their own features,125* using names built on their own URIs.</p>126*127* @param name The feature name, which is a fully-qualified URI.128* @return The current value of the feature (true or false).129* @throws org.xml.sax.SAXNotRecognizedException If the feature130* value can't be assigned or retrieved.131* @throws org.xml.sax.SAXNotSupportedException When the132* XMLReader recognizes the feature name but133* cannot determine its value at this time.134* @see #setFeature135*/136public boolean getFeature (String name)137throws SAXNotRecognizedException, SAXNotSupportedException;138139140/**141* Set the value of a feature flag.142*143* <p>The feature name is any fully-qualified URI. It is144* possible for an XMLReader to expose a feature value but145* to be unable to change the current value.146* Some feature values may be immutable or mutable only147* in specific contexts, such as before, during, or after148* a parse.</p>149*150* <p>All XMLReaders are required to support setting151* http://xml.org/sax/features/namespaces to true and152* http://xml.org/sax/features/namespace-prefixes to false.</p>153*154* @param name The feature name, which is a fully-qualified URI.155* @param value The requested value of the feature (true or false).156* @throws org.xml.sax.SAXNotRecognizedException If the feature157* value can't be assigned or retrieved.158* @throws org.xml.sax.SAXNotSupportedException When the159* XMLReader recognizes the feature name but160* cannot set the requested value.161* @see #getFeature162*/163public void setFeature (String name, boolean value)164throws SAXNotRecognizedException, SAXNotSupportedException;165166167/**168* Look up the value of a property.169*170* <p>The property name is any fully-qualified URI. It is171* possible for an XMLReader to recognize a property name but172* temporarily be unable to return its value.173* Some property values may be available only in specific174* contexts, such as before, during, or after a parse.</p>175*176* <p>XMLReaders are not required to recognize any specific177* property names, though an initial core set is documented for178* SAX2.</p>179*180* <p>Implementors are free (and encouraged) to invent their own properties,181* using names built on their own URIs.</p>182*183* @param name The property name, which is a fully-qualified URI.184* @return The current value of the property.185* @throws org.xml.sax.SAXNotRecognizedException If the property186* value can't be assigned or retrieved.187* @throws org.xml.sax.SAXNotSupportedException When the188* XMLReader recognizes the property name but189* cannot determine its value at this time.190* @see #setProperty191*/192public Object getProperty (String name)193throws SAXNotRecognizedException, SAXNotSupportedException;194195196/**197* Set the value of a property.198*199* <p>The property name is any fully-qualified URI. It is200* possible for an XMLReader to recognize a property name but201* to be unable to change the current value.202* Some property values may be immutable or mutable only203* in specific contexts, such as before, during, or after204* a parse.</p>205*206* <p>XMLReaders are not required to recognize setting207* any specific property names, though a core set is defined by208* SAX2.</p>209*210* <p>This method is also the standard mechanism for setting211* extended handlers.</p>212*213* @param name The property name, which is a fully-qualified URI.214* @param value The requested value for the property.215* @throws org.xml.sax.SAXNotRecognizedException If the property216* value can't be assigned or retrieved.217* @throws org.xml.sax.SAXNotSupportedException When the218* XMLReader recognizes the property name but219* cannot set the requested value.220*/221public void setProperty (String name, Object value)222throws SAXNotRecognizedException, SAXNotSupportedException;223224225226////////////////////////////////////////////////////////////////////227// Event handlers.228////////////////////////////////////////////////////////////////////229230231/**232* Allow an application to register an entity resolver.233*234* <p>If the application does not register an entity resolver,235* the XMLReader will perform its own default resolution.</p>236*237* <p>Applications may register a new or different resolver in the238* middle of a parse, and the SAX parser must begin using the new239* resolver immediately.</p>240*241* @param resolver The entity resolver.242* @see #getEntityResolver243*/244public void setEntityResolver (EntityResolver resolver);245246247/**248* Return the current entity resolver.249*250* @return The current entity resolver, or null if none251* has been registered.252* @see #setEntityResolver253*/254public EntityResolver getEntityResolver ();255256257/**258* Allow an application to register a DTD event handler.259*260* <p>If the application does not register a DTD handler, all DTD261* events reported by the SAX parser will be silently ignored.</p>262*263* <p>Applications may register a new or different handler in the264* middle of a parse, and the SAX parser must begin using the new265* handler immediately.</p>266*267* @param handler The DTD handler.268* @see #getDTDHandler269*/270public void setDTDHandler (DTDHandler handler);271272273/**274* Return the current DTD handler.275*276* @return The current DTD handler, or null if none277* has been registered.278* @see #setDTDHandler279*/280public DTDHandler getDTDHandler ();281282283/**284* Allow an application to register a content event handler.285*286* <p>If the application does not register a content handler, all287* content events reported by the SAX parser will be silently288* ignored.</p>289*290* <p>Applications may register a new or different handler in the291* middle of a parse, and the SAX parser must begin using the new292* handler immediately.</p>293*294* @param handler The content handler.295* @see #getContentHandler296*/297public void setContentHandler (ContentHandler handler);298299300/**301* Return the current content handler.302*303* @return The current content handler, or null if none304* has been registered.305* @see #setContentHandler306*/307public ContentHandler getContentHandler ();308309310/**311* Allow an application to register an error event handler.312*313* <p>If the application does not register an error handler, all314* error events reported by the SAX parser will be silently315* ignored; however, normal processing may not continue. It is316* highly recommended that all SAX applications implement an317* error handler to avoid unexpected bugs.</p>318*319* <p>Applications may register a new or different handler in the320* middle of a parse, and the SAX parser must begin using the new321* handler immediately.</p>322*323* @param handler The error handler.324* @see #getErrorHandler325*/326public void setErrorHandler (ErrorHandler handler);327328329/**330* Return the current error handler.331*332* @return The current error handler, or null if none333* has been registered.334* @see #setErrorHandler335*/336public ErrorHandler getErrorHandler ();337338339340////////////////////////////////////////////////////////////////////341// Parsing.342////////////////////////////////////////////////////////////////////343344/**345* Parse an XML document.346*347* <p>The application can use this method to instruct the XML348* reader to begin parsing an XML document from any valid input349* source (a character stream, a byte stream, or a URI).</p>350*351* <p>Applications may not invoke this method while a parse is in352* progress (they should create a new XMLReader instead for each353* nested XML document). Once a parse is complete, an354* application may reuse the same XMLReader object, possibly with a355* different input source.356* Configuration of the XMLReader object (such as handler bindings and357* values established for feature flags and properties) is unchanged358* by completion of a parse, unless the definition of that aspect of359* the configuration explicitly specifies other behavior.360* (For example, feature flags or properties exposing361* characteristics of the document being parsed.)362* </p>363*364* <p>During the parse, the XMLReader will provide information365* about the XML document through the registered event366* handlers.</p>367*368* <p>This method is synchronous: it will not return until parsing369* has ended. If a client application wants to terminate370* parsing early, it should throw an exception.</p>371*372* @param input The input source for the top-level of the373* XML document.374* @throws org.xml.sax.SAXException Any SAX exception, possibly375* wrapping another exception.376* @throws java.io.IOException An IO exception from the parser,377* possibly from a byte stream or character stream378* supplied by the application.379* @see org.xml.sax.InputSource380* @see #parse(java.lang.String)381* @see #setEntityResolver382* @see #setDTDHandler383* @see #setContentHandler384* @see #setErrorHandler385*/386public void parse (InputSource input)387throws IOException, SAXException;388389390/**391* Parse an XML document from a system identifier (URI).392*393* <p>This method is a shortcut for the common case of reading a394* document from a system identifier. It is the exact395* equivalent of the following:</p>396*397* <pre>398* parse(new InputSource(systemId));399* </pre>400*401* <p>If the system identifier is a URL, it must be fully resolved402* by the application before it is passed to the parser.</p>403*404* @param systemId The system identifier (URI).405* @throws org.xml.sax.SAXException Any SAX exception, possibly406* wrapping another exception.407* @throws java.io.IOException An IO exception from the parser,408* possibly from a byte stream or character stream409* supplied by the application.410* @see #parse(org.xml.sax.InputSource)411*/412public void parse (String systemId)413throws IOException, SAXException;414415}416417418