001 /*
002 * Copyright 2000-2005 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 // ContentHandler.java - handle main document content.
027 // http://www.saxproject.org
028 // Written by David Megginson
029 // NO WARRANTY! This class is in the public domain.
030 // $Id: ContentHandler.java,v 1.2 2004/11/03 22:44:51 jsuttor Exp $
031 package org.xml.sax;
032
033 /**
034 * Receive notification of the logical content of a document.
035 *
036 * <blockquote>
037 * <em>This module, both source code and documentation, is in the
038 * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
039 * See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>
040 * for further information.
041 * </blockquote>
042 *
043 * <p>This is the main interface that most SAX applications
044 * implement: if the application needs to be informed of basic parsing
045 * events, it implements this interface and registers an instance with
046 * the SAX parser using the {@link org.xml.sax.XMLReader#setContentHandler
047 * setContentHandler} method. The parser uses the instance to report
048 * basic document-related events like the start and end of elements
049 * and character data.</p>
050 *
051 * <p>The order of events in this interface is very important, and
052 * mirrors the order of information in the document itself. For
053 * example, all of an element's content (character data, processing
054 * instructions, and/or subelements) will appear, in order, between
055 * the startElement event and the corresponding endElement event.</p>
056 *
057 * <p>This interface is similar to the now-deprecated SAX 1.0
058 * DocumentHandler interface, but it adds support for Namespaces
059 * and for reporting skipped entities (in non-validating XML
060 * processors).</p>
061 *
062 * <p>Implementors should note that there is also a
063 * <code>ContentHandler</code> class in the <code>java.net</code>
064 * package; that means that it's probably a bad idea to do</p>
065 *
066 * <pre>import java.net.*;
067 * import org.xml.sax.*;
068 * </pre>
069 *
070 * <p>In fact, "import ...*" is usually a sign of sloppy programming
071 * anyway, so the user should consider this a feature rather than a
072 * bug.</p>
073 *
074 * @since SAX 2.0
075 * @author David Megginson
076 * @version 2.0.1+ (sax2r3pre1)
077 * @see org.xml.sax.XMLReader
078 * @see org.xml.sax.DTDHandler
079 * @see org.xml.sax.ErrorHandler
080 */
081 public interface ContentHandler {
082
083 /**
084 * Receive an object for locating the origin of SAX document events.
085 *
086 * <p>SAX parsers are strongly encouraged (though not absolutely
087 * required) to supply a locator: if it does so, it must supply
088 * the locator to the application by invoking this method before
089 * invoking any of the other methods in the ContentHandler
090 * interface.</p>
091 *
092 * <p>The locator allows the application to determine the end
093 * position of any document-related event, even if the parser is
094 * not reporting an error. Typically, the application will
095 * use this information for reporting its own errors (such as
096 * character content that does not match an application's
097 * business rules). The information returned by the locator
098 * is probably not sufficient for use with a search engine.</p>
099 *
100 * <p>Note that the locator will return correct information only
101 * during the invocation SAX event callbacks after
102 * {@link #startDocument startDocument} returns and before
103 * {@link #endDocument endDocument} is called. The
104 * application should not attempt to use it at any other time.</p>
105 *
106 * @param locator an object that can return the location of
107 * any SAX document event
108 * @see org.xml.sax.Locator
109 */
110 public void setDocumentLocator(Locator locator);
111
112 /**
113 * Receive notification of the beginning of a document.
114 *
115 * <p>The SAX parser will invoke this method only once, before any
116 * other event callbacks (except for {@link #setDocumentLocator
117 * setDocumentLocator}).</p>
118 *
119 * @throws org.xml.sax.SAXException any SAX exception, possibly
120 * wrapping another exception
121 * @see #endDocument
122 */
123 public void startDocument() throws SAXException;
124
125 /**
126 * Receive notification of the end of a document.
127 *
128 * <p><strong>There is an apparent contradiction between the
129 * documentation for this method and the documentation for {@link
130 * org.xml.sax.ErrorHandler#fatalError}. Until this ambiguity is
131 * resolved in a future major release, clients should make no
132 * assumptions about whether endDocument() will or will not be
133 * invoked when the parser has reported a fatalError() or thrown
134 * an exception.</strong></p>
135 *
136 * <p>The SAX parser will invoke this method only once, and it will
137 * be the last method invoked during the parse. The parser shall
138 * not invoke this method until it has either abandoned parsing
139 * (because of an unrecoverable error) or reached the end of
140 * input.</p>
141 *
142 * @throws org.xml.sax.SAXException any SAX exception, possibly
143 * wrapping another exception
144 * @see #startDocument
145 */
146 public void endDocument() throws SAXException;
147
148 /**
149 * Begin the scope of a prefix-URI Namespace mapping.
150 *
151 * <p>The information from this event is not necessary for
152 * normal Namespace processing: the SAX XML reader will
153 * automatically replace prefixes for element and attribute
154 * names when the <code>http://xml.org/sax/features/namespaces</code>
155 * feature is <var>true</var> (the default).</p>
156 *
157 * <p>There are cases, however, when applications need to
158 * use prefixes in character data or in attribute values,
159 * where they cannot safely be expanded automatically; the
160 * start/endPrefixMapping event supplies the information
161 * to the application to expand prefixes in those contexts
162 * itself, if necessary.</p>
163 *
164 * <p>Note that start/endPrefixMapping events are not
165 * guaranteed to be properly nested relative to each other:
166 * all startPrefixMapping events will occur immediately before the
167 * corresponding {@link #startElement startElement} event,
168 * and all {@link #endPrefixMapping endPrefixMapping}
169 * events will occur immediately after the corresponding
170 * {@link #endElement endElement} event,
171 * but their order is not otherwise
172 * guaranteed.</p>
173 *
174 * <p>There should never be start/endPrefixMapping events for the
175 * "xml" prefix, since it is predeclared and immutable.</p>
176 *
177 * @param prefix the Namespace prefix being declared.
178 * An empty string is used for the default element namespace,
179 * which has no prefix.
180 * @param uri the Namespace URI the prefix is mapped to
181 * @throws org.xml.sax.SAXException the client may throw
182 * an exception during processing
183 * @see #endPrefixMapping
184 * @see #startElement
185 */
186 public void startPrefixMapping(String prefix, String uri)
187 throws SAXException;
188
189 /**
190 * End the scope of a prefix-URI mapping.
191 *
192 * <p>See {@link #startPrefixMapping startPrefixMapping} for
193 * details. These events will always occur immediately after the
194 * corresponding {@link #endElement endElement} event, but the order of
195 * {@link #endPrefixMapping endPrefixMapping} events is not otherwise
196 * guaranteed.</p>
197 *
198 * @param prefix the prefix that was being mapped.
199 * This is the empty string when a default mapping scope ends.
200 * @throws org.xml.sax.SAXException the client may throw
201 * an exception during processing
202 * @see #startPrefixMapping
203 * @see #endElement
204 */
205 public void endPrefixMapping(String prefix) throws SAXException;
206
207 /**
208 * Receive notification of the beginning of an element.
209 *
210 * <p>The Parser will invoke this method at the beginning of every
211 * element in the XML document; there will be a corresponding
212 * {@link #endElement endElement} event for every startElement event
213 * (even when the element is empty). All of the element's content will be
214 * reported, in order, before the corresponding endElement
215 * event.</p>
216 *
217 * <p>This event allows up to three name components for each
218 * element:</p>
219 *
220 * <ol>
221 * <li>the Namespace URI;</li>
222 * <li>the local name; and</li>
223 * <li>the qualified (prefixed) name.</li>
224 * </ol>
225 *
226 * <p>Any or all of these may be provided, depending on the
227 * values of the <var>http://xml.org/sax/features/namespaces</var>
228 * and the <var>http://xml.org/sax/features/namespace-prefixes</var>
229 * properties:</p>
230 *
231 * <ul>
232 * <li>the Namespace URI and local name are required when
233 * the namespaces property is <var>true</var> (the default), and are
234 * optional when the namespaces property is <var>false</var> (if one is
235 * specified, both must be);</li>
236 * <li>the qualified name is required when the namespace-prefixes property
237 * is <var>true</var>, and is optional when the namespace-prefixes property
238 * is <var>false</var> (the default).</li>
239 * </ul>
240 *
241 * <p>Note that the attribute list provided will contain only
242 * attributes with explicit values (specified or defaulted):
243 * #IMPLIED attributes will be omitted. The attribute list
244 * will contain attributes used for Namespace declarations
245 * (xmlns* attributes) only if the
246 * <code>http://xml.org/sax/features/namespace-prefixes</code>
247 * property is true (it is false by default, and support for a
248 * true value is optional).</p>
249 *
250 * <p>Like {@link #characters characters()}, attribute values may have
251 * characters that need more than one <code>char</code> value. </p>
252 *
253 * @param uri the Namespace URI, or the empty string if the
254 * element has no Namespace URI or if Namespace
255 * processing is not being performed
256 * @param localName the local name (without prefix), or the
257 * empty string if Namespace processing is not being
258 * performed
259 * @param qName the qualified name (with prefix), or the
260 * empty string if qualified names are not available
261 * @param atts the attributes attached to the element. If
262 * there are no attributes, it shall be an empty
263 * Attributes object. The value of this object after
264 * startElement returns is undefined
265 * @throws org.xml.sax.SAXException any SAX exception, possibly
266 * wrapping another exception
267 * @see #endElement
268 * @see org.xml.sax.Attributes
269 * @see org.xml.sax.helpers.AttributesImpl
270 */
271 public void startElement(String uri, String localName,
272 String qName, Attributes atts) throws SAXException;
273
274 /**
275 * Receive notification of the end of an element.
276 *
277 * <p>The SAX parser will invoke this method at the end of every
278 * element in the XML document; there will be a corresponding
279 * {@link #startElement startElement} event for every endElement
280 * event (even when the element is empty).</p>
281 *
282 * <p>For information on the names, see startElement.</p>
283 *
284 * @param uri the Namespace URI, or the empty string if the
285 * element has no Namespace URI or if Namespace
286 * processing is not being performed
287 * @param localName the local name (without prefix), or the
288 * empty string if Namespace processing is not being
289 * performed
290 * @param qName the qualified XML name (with prefix), or the
291 * empty string if qualified names are not available
292 * @throws org.xml.sax.SAXException any SAX exception, possibly
293 * wrapping another exception
294 */
295 public void endElement(String uri, String localName, String qName)
296 throws SAXException;
297
298 /**
299 * Receive notification of character data.
300 *
301 * <p>The Parser will call this method to report each chunk of
302 * character data. SAX parsers may return all contiguous character
303 * data in a single chunk, or they may split it into several
304 * chunks; however, all of the characters in any single event
305 * must come from the same external entity so that the Locator
306 * provides useful information.</p>
307 *
308 * <p>The application must not attempt to read from the array
309 * outside of the specified range.</p>
310 *
311 * <p>Individual characters may consist of more than one Java
312 * <code>char</code> value. There are two important cases where this
313 * happens, because characters can't be represented in just sixteen bits.
314 * In one case, characters are represented in a <em>Surrogate Pair</em>,
315 * using two special Unicode values. Such characters are in the so-called
316 * "Astral Planes", with a code point above U+FFFF. A second case involves
317 * composite characters, such as a base character combining with one or
318 * more accent characters. </p>
319 *
320 * <p> Your code should not assume that algorithms using
321 * <code>char</code>-at-a-time idioms will be working in character
322 * units; in some cases they will split characters. This is relevant
323 * wherever XML permits arbitrary characters, such as attribute values,
324 * processing instruction data, and comments as well as in data reported
325 * from this method. It's also generally relevant whenever Java code
326 * manipulates internationalized text; the issue isn't unique to XML.</p>
327 *
328 * <p>Note that some parsers will report whitespace in element
329 * content using the {@link #ignorableWhitespace ignorableWhitespace}
330 * method rather than this one (validating parsers <em>must</em>
331 * do so).</p>
332 *
333 * @param ch the characters from the XML document
334 * @param start the start position in the array
335 * @param length the number of characters to read from the array
336 * @throws org.xml.sax.SAXException any SAX exception, possibly
337 * wrapping another exception
338 * @see #ignorableWhitespace
339 * @see org.xml.sax.Locator
340 */
341 public void characters(char ch[], int start, int length)
342 throws SAXException;
343
344 /**
345 * Receive notification of ignorable whitespace in element content.
346 *
347 * <p>Validating Parsers must use this method to report each chunk
348 * of whitespace in element content (see the W3C XML 1.0
349 * recommendation, section 2.10): non-validating parsers may also
350 * use this method if they are capable of parsing and using
351 * content models.</p>
352 *
353 * <p>SAX parsers may return all contiguous whitespace in a single
354 * chunk, or they may split it into several chunks; however, all of
355 * the characters in any single event must come from the same
356 * external entity, so that the Locator provides useful
357 * information.</p>
358 *
359 * <p>The application must not attempt to read from the array
360 * outside of the specified range.</p>
361 *
362 * @param ch the characters from the XML document
363 * @param start the start position in the array
364 * @param length the number of characters to read from the array
365 * @throws org.xml.sax.SAXException any SAX exception, possibly
366 * wrapping another exception
367 * @see #characters
368 */
369 public void ignorableWhitespace(char ch[], int start, int length)
370 throws SAXException;
371
372 /**
373 * Receive notification of a processing instruction.
374 *
375 * <p>The Parser will invoke this method once for each processing
376 * instruction found: note that processing instructions may occur
377 * before or after the main document element.</p>
378 *
379 * <p>A SAX parser must never report an XML declaration (XML 1.0,
380 * section 2.8) or a text declaration (XML 1.0, section 4.3.1)
381 * using this method.</p>
382 *
383 * <p>Like {@link #characters characters()}, processing instruction
384 * data may have characters that need more than one <code>char</code>
385 * value. </p>
386 *
387 * @param target the processing instruction target
388 * @param data the processing instruction data, or null if
389 * none was supplied. The data does not include any
390 * whitespace separating it from the target
391 * @throws org.xml.sax.SAXException any SAX exception, possibly
392 * wrapping another exception
393 */
394 public void processingInstruction(String target, String data)
395 throws SAXException;
396
397 /**
398 * Receive notification of a skipped entity.
399 * This is not called for entity references within markup constructs
400 * such as element start tags or markup declarations. (The XML
401 * recommendation requires reporting skipped external entities.
402 * SAX also reports internal entity expansion/non-expansion, except
403 * within markup constructs.)
404 *
405 * <p>The Parser will invoke this method each time the entity is
406 * skipped. Non-validating processors may skip entities if they
407 * have not seen the declarations (because, for example, the
408 * entity was declared in an external DTD subset). All processors
409 * may skip external entities, depending on the values of the
410 * <code>http://xml.org/sax/features/external-general-entities</code>
411 * and the
412 * <code>http://xml.org/sax/features/external-parameter-entities</code>
413 * properties.</p>
414 *
415 * @param name the name of the skipped entity. If it is a
416 * parameter entity, the name will begin with '%', and if
417 * it is the external DTD subset, it will be the string
418 * "[dtd]"
419 * @throws org.xml.sax.SAXException any SAX exception, possibly
420 * wrapping another exception
421 */
422 public void skippedEntity(String name) throws SAXException;
423 }
424
425 // end of ContentHandler.java
|