Path: blob/master/src/java.xml/share/classes/org/xml/sax/ContentHandler.java
40948 views
/*1* Copyright (c) 2000, 2019, 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;262728/**29* Receive notification of the logical content of a document.30*31* <p>This is the main interface that most SAX applications32* implement: if the application needs to be informed of basic parsing33* events, it implements this interface and registers an instance with34* the SAX parser using the {@link org.xml.sax.XMLReader#setContentHandler35* setContentHandler} method. The parser uses the instance to report36* basic document-related events like the start and end of elements37* and character data.</p>38*39* <p>The order of events in this interface is very important, and40* mirrors the order of information in the document itself. For41* example, all of an element's content (character data, processing42* instructions, and/or subelements) will appear, in order, between43* the startElement event and the corresponding endElement event.</p>44*45* <p>This interface is similar to the now-deprecated SAX 1.046* DocumentHandler interface, but it adds support for Namespaces47* and for reporting skipped entities (in non-validating XML48* processors).</p>49*50* <p>Implementors should note that there is also a51* <code>ContentHandler</code> class in the <code>java.net</code>52* package; that means that it's probably a bad idea to do</p>53*54* <pre>import java.net.*;55* import org.xml.sax.*;56* </pre>57*58* <p>In fact, "import ...*" is usually a sign of sloppy programming59* anyway, so the user should consider this a feature rather than a60* bug.</p>61*62* @since 1.4, SAX 2.063* @author David Megginson64* @see org.xml.sax.XMLReader65* @see org.xml.sax.DTDHandler66* @see org.xml.sax.ErrorHandler67*/68public interface ContentHandler69{7071/**72* Receive an object for locating the origin of SAX document events.73*74* <p>SAX parsers are strongly encouraged (though not absolutely75* required) to supply a locator: if it does so, it must supply76* the locator to the application by invoking this method before77* invoking any of the other methods in the ContentHandler78* interface.</p>79*80* <p>The locator allows the application to determine the end81* position of any document-related event, even if the parser is82* not reporting an error. Typically, the application will83* use this information for reporting its own errors (such as84* character content that does not match an application's85* business rules). The information returned by the locator86* is probably not sufficient for use with a search engine.</p>87*88* <p>Note that the locator will return correct information only89* during the invocation SAX event callbacks after90* {@link #startDocument startDocument} returns and before91* {@link #endDocument endDocument} is called. The92* application should not attempt to use it at any other time.</p>93*94* @param locator an object that can return the location of95* any SAX document event96* @see org.xml.sax.Locator97*/98public void setDocumentLocator (Locator locator);99100101/**102* Receive notification of the beginning of a document.103*104* <p>The SAX parser will invoke this method only once, before any105* other event callbacks (except for {@link #setDocumentLocator106* setDocumentLocator}).</p>107*108* @throws org.xml.sax.SAXException any SAX exception, possibly109* wrapping another exception110* @see #endDocument111*/112public void startDocument ()113throws SAXException;114115/**116* Receives notification of the XML declaration.117*118* @implSpec119* The default implementation in the SAX API is to do nothing.120*121* @param version the version string as in the input document, null if not122* specified123* @param encoding the encoding string as in the input document, null if not124* specified125* @param standalone the standalone string as in the input document, null if126* not specified127*128* @throws SAXException if the application wants to report an error or129* interrupt the parsing process130*131* @since 14132*/133default void declaration(String version, String encoding, String standalone)134throws SAXException135{136//no op137}138139/**140* Receive notification of the end of a document.141*142* <p>143* This method is invoked by the parser to signal it has reached the end of144* the document after successfully completing the parsing process.145* After the event, the parser will return the control to the application.146*147* @apiNote In case of a fatal error, the parser may choose to stop the148* parsing process with a {@link SAXException}, in which case, this method149* will never be called. Refer to150* {@link ErrorHandler#fatalError(org.xml.sax.SAXParseException)}.151*152* @throws org.xml.sax.SAXException any SAX exception, possibly153* wrapping another exception154* @see #startDocument155*/156public void endDocument()157throws SAXException;158159160/**161* Begin the scope of a prefix-URI Namespace mapping.162*163* <p>The information from this event is not necessary for164* normal Namespace processing: the SAX XML reader will165* automatically replace prefixes for element and attribute166* names when the <code>http://xml.org/sax/features/namespaces</code>167* feature is <var>true</var> (the default).</p>168*169* <p>There are cases, however, when applications need to170* use prefixes in character data or in attribute values,171* where they cannot safely be expanded automatically; the172* start/endPrefixMapping event supplies the information173* to the application to expand prefixes in those contexts174* itself, if necessary.</p>175*176* <p>Note that start/endPrefixMapping events are not177* guaranteed to be properly nested relative to each other:178* all startPrefixMapping events will occur immediately before the179* corresponding {@link #startElement startElement} event,180* and all {@link #endPrefixMapping endPrefixMapping}181* events will occur immediately after the corresponding182* {@link #endElement endElement} event,183* but their order is not otherwise184* guaranteed.</p>185*186* <p>There should never be start/endPrefixMapping events for the187* "xml" prefix, since it is predeclared and immutable.</p>188*189* @param prefix the Namespace prefix being declared.190* An empty string is used for the default element namespace,191* which has no prefix.192* @param uri the Namespace URI the prefix is mapped to193* @throws org.xml.sax.SAXException the client may throw194* an exception during processing195* @see #endPrefixMapping196* @see #startElement197*/198public void startPrefixMapping (String prefix, String uri)199throws SAXException;200201202/**203* End the scope of a prefix-URI mapping.204*205* <p>See {@link #startPrefixMapping startPrefixMapping} for206* details. These events will always occur immediately after the207* corresponding {@link #endElement endElement} event, but the order of208* {@link #endPrefixMapping endPrefixMapping} events is not otherwise209* guaranteed.</p>210*211* @param prefix the prefix that was being mapped.212* This is the empty string when a default mapping scope ends.213* @throws org.xml.sax.SAXException the client may throw214* an exception during processing215* @see #startPrefixMapping216* @see #endElement217*/218public void endPrefixMapping (String prefix)219throws SAXException;220221222/**223* Receive notification of the beginning of an element.224*225* <p>The Parser will invoke this method at the beginning of every226* element in the XML document; there will be a corresponding227* {@link #endElement endElement} event for every startElement event228* (even when the element is empty). All of the element's content will be229* reported, in order, before the corresponding endElement230* event.</p>231*232* <p>This event allows up to three name components for each233* element:</p>234*235* <ol>236* <li>the Namespace URI;</li>237* <li>the local name; and</li>238* <li>the qualified (prefixed) name.</li>239* </ol>240*241* <p>Any or all of these may be provided, depending on the242* values of the <var>http://xml.org/sax/features/namespaces</var>243* and the <var>http://xml.org/sax/features/namespace-prefixes</var>244* properties:</p>245*246* <ul>247* <li>the Namespace URI and local name are required when248* the namespaces property is <var>true</var> (the default), and are249* optional when the namespaces property is <var>false</var> (if one is250* specified, both must be);</li>251* <li>the qualified name is required when the namespace-prefixes property252* is <var>true</var>, and is optional when the namespace-prefixes property253* is <var>false</var> (the default).</li>254* </ul>255*256* <p>Note that the attribute list provided will contain only257* attributes with explicit values (specified or defaulted):258* #IMPLIED attributes will be omitted. The attribute list259* will contain attributes used for Namespace declarations260* (xmlns* attributes) only if the261* <code>http://xml.org/sax/features/namespace-prefixes</code>262* property is true (it is false by default, and support for a263* true value is optional).</p>264*265* <p>Like {@link #characters characters()}, attribute values may have266* characters that need more than one <code>char</code> value. </p>267*268* @param uri the Namespace URI, or the empty string if the269* element has no Namespace URI or if Namespace270* processing is not being performed271* @param localName the local name (without prefix), or the272* empty string if Namespace processing is not being273* performed274* @param qName the qualified name (with prefix), or the275* empty string if qualified names are not available276* @param atts the attributes attached to the element. If277* there are no attributes, it shall be an empty278* Attributes object. The value of this object after279* startElement returns is undefined280* @throws org.xml.sax.SAXException any SAX exception, possibly281* wrapping another exception282* @see #endElement283* @see org.xml.sax.Attributes284* @see org.xml.sax.helpers.AttributesImpl285*/286public void startElement (String uri, String localName,287String qName, Attributes atts)288throws SAXException;289290291/**292* Receive notification of the end of an element.293*294* <p>The SAX parser will invoke this method at the end of every295* element in the XML document; there will be a corresponding296* {@link #startElement startElement} event for every endElement297* event (even when the element is empty).</p>298*299* <p>For information on the names, see startElement.</p>300*301* @param uri the Namespace URI, or the empty string if the302* element has no Namespace URI or if Namespace303* processing is not being performed304* @param localName the local name (without prefix), or the305* empty string if Namespace processing is not being306* performed307* @param qName the qualified XML name (with prefix), or the308* empty string if qualified names are not available309* @throws org.xml.sax.SAXException any SAX exception, possibly310* wrapping another exception311*/312public void endElement (String uri, String localName,313String qName)314throws SAXException;315316317/**318* Receive notification of character data.319*320* <p>The Parser will call this method to report each chunk of321* character data. SAX parsers may return all contiguous character322* data in a single chunk, or they may split it into several323* chunks; however, all of the characters in any single event324* must come from the same external entity so that the Locator325* provides useful information.</p>326*327* <p>The application must not attempt to read from the array328* outside of the specified range.</p>329*330* <p>Individual characters may consist of more than one Java331* <code>char</code> value. There are two important cases where this332* happens, because characters can't be represented in just sixteen bits.333* In one case, characters are represented in a <em>Surrogate Pair</em>,334* using two special Unicode values. Such characters are in the so-called335* "Astral Planes", with a code point above U+FFFF. A second case involves336* composite characters, such as a base character combining with one or337* more accent characters. </p>338*339* <p> Your code should not assume that algorithms using340* <code>char</code>-at-a-time idioms will be working in character341* units; in some cases they will split characters. This is relevant342* wherever XML permits arbitrary characters, such as attribute values,343* processing instruction data, and comments as well as in data reported344* from this method. It's also generally relevant whenever Java code345* manipulates internationalized text; the issue isn't unique to XML.</p>346*347* <p>Note that some parsers will report whitespace in element348* content using the {@link #ignorableWhitespace ignorableWhitespace}349* method rather than this one (validating parsers <em>must</em>350* do so).</p>351*352* @param ch the characters from the XML document353* @param start the start position in the array354* @param length the number of characters to read from the array355* @throws org.xml.sax.SAXException any SAX exception, possibly356* wrapping another exception357* @see #ignorableWhitespace358* @see org.xml.sax.Locator359*/360public void characters (char ch[], int start, int length)361throws SAXException;362363364/**365* Receive notification of ignorable whitespace in element content.366*367* <p>Validating Parsers must use this method to report each chunk368* of whitespace in element content (see the W3C XML 1.0369* recommendation, section 2.10): non-validating parsers may also370* use this method if they are capable of parsing and using371* content models.</p>372*373* <p>SAX parsers may return all contiguous whitespace in a single374* chunk, or they may split it into several chunks; however, all of375* the characters in any single event must come from the same376* external entity, so that the Locator provides useful377* information.</p>378*379* <p>The application must not attempt to read from the array380* outside of the specified range.</p>381*382* @param ch the characters from the XML document383* @param start the start position in the array384* @param length the number of characters to read from the array385* @throws org.xml.sax.SAXException any SAX exception, possibly386* wrapping another exception387* @see #characters388*/389public void ignorableWhitespace (char ch[], int start, int length)390throws SAXException;391392393/**394* Receive notification of a processing instruction.395*396* <p>The Parser will invoke this method once for each processing397* instruction found: note that processing instructions may occur398* before or after the main document element.</p>399*400* <p>A SAX parser must never report an XML declaration (XML 1.0,401* section 2.8) or a text declaration (XML 1.0, section 4.3.1)402* using this method.</p>403*404* <p>Like {@link #characters characters()}, processing instruction405* data may have characters that need more than one <code>char</code>406* value. </p>407*408* @param target the processing instruction target409* @param data the processing instruction data, or null if410* none was supplied. The data does not include any411* whitespace separating it from the target412* @throws org.xml.sax.SAXException any SAX exception, possibly413* wrapping another exception414*/415public void processingInstruction (String target, String data)416throws SAXException;417418419/**420* Receive notification of a skipped entity.421* This is not called for entity references within markup constructs422* such as element start tags or markup declarations. (The XML423* recommendation requires reporting skipped external entities.424* SAX also reports internal entity expansion/non-expansion, except425* within markup constructs.)426*427* <p>The Parser will invoke this method each time the entity is428* skipped. Non-validating processors may skip entities if they429* have not seen the declarations (because, for example, the430* entity was declared in an external DTD subset). All processors431* may skip external entities, depending on the values of the432* <code>http://xml.org/sax/features/external-general-entities</code>433* and the434* <code>http://xml.org/sax/features/external-parameter-entities</code>435* properties.</p>436*437* @param name the name of the skipped entity. If it is a438* parameter entity, the name will begin with '%', and if439* it is the external DTD subset, it will be the string440* "[dtd]"441* @throws org.xml.sax.SAXException any SAX exception, possibly442* wrapping another exception443*/444public void skippedEntity (String name)445throws SAXException;446}447448// end of ContentHandler.java449450451