1 /* -*- c-basic-offset: 4; indent-tabs-mode: nil; -*- //------100-columns-wide------>|*/
2 // for license please see accompanying LICENSE.txt file (available also at http://www.xmlpull.org/)
3
4 package org.codehaus.plexus.util.xml.pull;
5
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.io.Reader;
9
10 /**
11 * XML Pull Parser is an interface that defines parsing functionality provided in
12 * <a href="http://www.xmlpull.org/">XMLPULL V1 API</a> (visit this website to learn more about API and its
13 * implementations).
14 * <p>
15 * There are following different kinds of parser depending on which features are set:
16 * <ul>
17 * <li><b>non-validating</b> parser as defined in XML 1.0 spec when FEATURE_PROCESS_DOCDECL is set to true
18 * <li><b>validating parser</b> as defined in XML 1.0 spec when FEATURE_VALIDATION is true (and that implies that
19 * FEATURE_PROCESS_DOCDECL is true)
20 * <li>when FEATURE_PROCESS_DOCDECL is false (this is default and if different value is required necessary must be
21 * changed before parsing is started) then parser behaves like XML 1.0 compliant non-validating parser under condition
22 * that <em>no DOCDECL is present</em> in XML documents (internal entities can still be defined with
23 * defineEntityReplacementText()). This mode of operation is intended <b>for operation in constrained environments</b>
24 * such as J2ME.
25 * </ul>
26 * <p>
27 * There are two key methods: next() and nextToken(). While next() provides access to high level parsing events,
28 * nextToken() allows access to lower level tokens.
29 * <p>
30 * The current event state of the parser can be determined by calling the <a href="#getEventType()">getEventType()</a>
31 * method. Initially, the parser is in the <a href="#START_DOCUMENT">START_DOCUMENT</a> state.
32 * <p>
33 * The method <a href="#next()">next()</a> advances the parser to the next event. The int value returned from next
34 * determines the current parser state and is identical to the value returned from following calls to getEventType ().
35 * <p>
36 * The following event types are seen by next()
37 * <dl>
38 * <dt><a href="#START_TAG">START_TAG</a>
39 * <dd>An XML start tag was read.
40 * <dt><a href="#TEXT">TEXT</a>
41 * <dd>Text content was read; the text content can be retrieved using the getText() method. (when in validating mode
42 * next() will not report ignorable whitespaces, use nextToken() instead)
43 * <dt><a href="#END_TAG">END_TAG</a>
44 * <dd>An end tag was read
45 * <dt><a href="#END_DOCUMENT">END_DOCUMENT</a>
46 * <dd>No more events are available
47 * </dl>
48 * <p>
49 * after first next() or nextToken() (or any other next*() method) is called user application can obtain XML version,
50 * standalone and encoding from XML declaration in following ways:
51 * <ul>
52 * <li><b>version</b>: getProperty("<a href=
53 * "http://xmlpull.org/v1/doc/properties.html#xmldecl-version">http://xmlpull.org/v1/doc/properties.html#xmldecl-version</a>")
54 * returns String ("1.0") or null if XMLDecl was not read or if property is not supported
55 * <li><b>standalone</b>: getProperty("<a href=
56 * "http://xmlpull.org/v1/doc/features.html#xmldecl-standalone">http://xmlpull.org/v1/doc/features.html#xmldecl-standalone</a>")
57 * returns Boolean: null if there was no standalone declaration or if property is not supported otherwise returns
58 * Boolean(true) if standalone="yes" and Boolean(false) when standalone="no"
59 * <li><b>encoding</b>: obtained from getInputEncoding() null if stream had unknown encoding (not set in setInputStream)
60 * and it was not declared in XMLDecl
61 * </ul>
62 * A minimal example for using this API may look as follows:
63 *
64 * <pre>
65 * import java.io.IOException;
66 * import java.io.StringReader;
67 *
68 * import org.xmlpull.v1.XmlPullParser;
69 * import org.xmlpull.v1.XmlPullParserException;
70 * import org.xmlpull.v1.XmlPullParserFactory;
71 *
72 * public class SimpleXmlPullApp
73 * {
74 *
75 * public static void main (String args[])
76 * throws XmlPullParserException, IOException
77 * {
78 * XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
79 * factory.setNamespaceAware(true);
80 * XmlPullParser xpp = factory.newPullParser();
81 *
82 * xpp.setInput( new StringReader ( "<foo%gt;Hello World!</foo>" ) );
83 * int eventType = xpp.getEventType();
84 * while (eventType != xpp.END_DOCUMENT) {
85 * if(eventType == xpp.START_DOCUMENT) {
86 * System.out.println("Start document");
87 * } else if(eventType == xpp.END_DOCUMENT) {
88 * System.out.println("End document");
89 * } else if(eventType == xpp.START_TAG) {
90 * System.out.println("Start tag "+xpp.getName());
91 * } else if(eventType == xpp.END_TAG) {
92 * System.out.println("End tag "+xpp.getName());
93 * } else if(eventType == xpp.TEXT) {
94 * System.out.println("Text "+xpp.getText());
95 * }
96 * eventType = xpp.next();
97 * }
98 * }
99 * }
100 * </pre>
101 * <p>
102 * The above example will generate the following output:
103 *
104 * <pre>
105 * Start document
106 * Start tag foo
107 * Text Hello World!
108 * End tag foo
109 * </pre>
110 *
111 * For more details on API usage, please refer to the quick Introduction available at
112 * <a href="http://www.xmlpull.org">http://www.xmlpull.org</a>
113 *
114 * @see #defineEntityReplacementText
115 * @see #getName
116 * @see #getNamespace
117 * @see #getText
118 * @see #next
119 * @see #nextToken
120 * @see #setInput
121 * @see #FEATURE_PROCESS_DOCDECL
122 * @see #FEATURE_VALIDATION
123 * @see #START_DOCUMENT
124 * @see #START_TAG
125 * @see #TEXT
126 * @see #END_TAG
127 * @see #END_DOCUMENT
128 * @author <a href="http://www-ai.cs.uni-dortmund.de/PERSONAL/haustein.html">Stefan Haustein</a>
129 * @author <a href="http://www.extreme.indiana.edu/~aslom/">Aleksander Slominski</a>
130 */
131 public interface XmlPullParser {
132
133 /** This constant represents the default namespace (empty string "") */
134 String NO_NAMESPACE = "";
135
136 // ----------------------------------------------------------------------------
137 // EVENT TYPES as reported by next()
138
139 /**
140 * Signalize that parser is at the very beginning of the document and nothing was read yet. This event type can only
141 * be observed by calling getEvent() before the first call to next(), nextToken, or nextTag()).
142 *
143 * @see #next
144 * @see #nextToken
145 */
146 int START_DOCUMENT = 0;
147
148 /**
149 * Logical end of the xml document. Returned from getEventType, next() and nextToken() when the end of the input
150 * document has been reached.
151 * <p>
152 * <strong>NOTE:</strong> calling again <a href="#next()">next()</a> or <a href="#nextToken()">nextToken()</a> will
153 * result in exception being thrown.
154 *
155 * @see #next
156 * @see #nextToken
157 */
158 int END_DOCUMENT = 1;
159
160 /**
161 * Returned from getEventType(), <a href="#next()">next()</a>, <a href="#nextToken()">nextToken()</a> when a start
162 * tag was read. The name of start tag is available from getName(), its namespace and prefix are available from
163 * getNamespace() and getPrefix() if <a href='#FEATURE_PROCESS_NAMESPACES'>namespaces are enabled</a>. See
164 * getAttribute* methods to retrieve element attributes. See getNamespace* methods to retrieve newly declared
165 * namespaces.
166 *
167 * @see #next
168 * @see #nextToken
169 * @see #getName
170 * @see #getPrefix
171 * @see #getNamespace
172 * @see #getAttributeCount
173 * @see #getDepth
174 * @see #getNamespaceCount
175 * @see #getNamespace
176 * @see #FEATURE_PROCESS_NAMESPACES
177 */
178 int START_TAG = 2;
179
180 /**
181 * Returned from getEventType(), <a href="#next()">next()</a>, or <a href="#nextToken()">nextToken()</a> when an end
182 * tag was read. The name of start tag is available from getName(), its namespace and prefix are available from
183 * getNamespace() and getPrefix().
184 *
185 * @see #next
186 * @see #nextToken
187 * @see #getName
188 * @see #getPrefix
189 * @see #getNamespace
190 * @see #FEATURE_PROCESS_NAMESPACES
191 */
192 int END_TAG = 3;
193
194 /**
195 * Character data was read and will is available by calling getText().
196 * <p>
197 * <strong>Please note:</strong> <a href="#next()">next()</a> will accumulate multiple events into one TEXT event,
198 * skipping IGNORABLE_WHITESPACE, PROCESSING_INSTRUCTION and COMMENT events, In contrast,
199 * <a href="#nextToken()">nextToken()</a> will stop reading text when any other event is observed. Also, when the
200 * state was reached by calling next(), the text value will be normalized, whereas getText() will return
201 * unnormalized content in the case of nextToken(). This allows an exact roundtrip without changing line ends when
202 * examining low level events, whereas for high level applications the text is normalized appropriately.
203 *
204 * @see #next
205 * @see #nextToken
206 * @see #getText
207 */
208 int TEXT = 4;
209
210 // ----------------------------------------------------------------------------
211 // additional events exposed by lower level nextToken()
212
213 /**
214 * A CDATA sections was just read; this token is available only from calls to
215 * <a href="#nextToken()">nextToken()</a>. A call to next() will accumulate various text events into a single event
216 * of type TEXT. The text contained in the CDATA section is available by calling getText().
217 *
218 * @see #nextToken
219 * @see #getText
220 */
221 int CDSECT = 5;
222
223 /**
224 * An entity reference was just read; this token is available from <a href="#nextToken()">nextToken()</a> only. The
225 * entity name is available by calling getName(). If available, the replacement text can be obtained by calling
226 * getTextt(); otherwise, the user is responsible for resolving the entity reference. This event type is never
227 * returned from next(); next() will accumulate the replacement text and other text events to a single TEXT event.
228 *
229 * @see #nextToken
230 * @see #getText
231 */
232 int ENTITY_REF = 6;
233
234 /**
235 * Ignorable whitespace was just read. This token is available only from <a href="#nextToken()">nextToken()</a>).
236 * For non-validating parsers, this event is only reported by nextToken() when outside the root element. Validating
237 * parsers may be able to detect ignorable whitespace at other locations. The ignorable whitespace string is
238 * available by calling getText()
239 * <p>
240 * <strong>NOTE:</strong> this is different from calling the isWhitespace() method, since text content may be
241 * whitespace but not ignorable. Ignorable whitespace is skipped by next() automatically; this event type is never
242 * returned from next().
243 *
244 * @see #nextToken
245 * @see #getText
246 */
247 int IGNORABLE_WHITESPACE = 7;
248
249 /**
250 * An XML processing instruction declaration was just read. This event type is available only via
251 * <a href="#nextToken()">nextToken()</a>. getText() will return text that is inside the processing instruction.
252 * Calls to next() will skip processing instructions automatically.
253 *
254 * @see #nextToken
255 * @see #getText
256 */
257 int PROCESSING_INSTRUCTION = 8;
258
259 /**
260 * An XML comment was just read. This event type is this token is available via
261 * <a href="#nextToken()">nextToken()</a> only; calls to next() will skip comments automatically. The content of the
262 * comment can be accessed using the getText() method.
263 *
264 * @see #nextToken
265 * @see #getText
266 */
267 int COMMENT = 9;
268
269 /**
270 * An XML document type declaration was just read. This token is available from
271 * <a href="#nextToken()">nextToken()</a> only. The unparsed text inside the doctype is available via the getText()
272 * method.
273 *
274 * @see #nextToken
275 * @see #getText
276 */
277 int DOCDECL = 10;
278
279 /**
280 * This array can be used to convert the event type integer constants such as START_TAG or TEXT to to a string. For
281 * example, the value of TYPES[START_TAG] is the string "START_TAG". This array is intended for diagnostic output
282 * only. Relying on the contents of the array may be dangerous since malicious applications may alter the array,
283 * although it is final, due to limitations of the Java language.
284 */
285 String[] TYPES = {
286 "START_DOCUMENT",
287 "END_DOCUMENT",
288 "START_TAG",
289 "END_TAG",
290 "TEXT",
291 "CDSECT",
292 "ENTITY_REF",
293 "IGNORABLE_WHITESPACE",
294 "PROCESSING_INSTRUCTION",
295 "COMMENT",
296 "DOCDECL"
297 };
298
299 // ----------------------------------------------------------------------------
300 // namespace related features
301
302 /**
303 * This feature determines whether the parser processes namespaces. As for all features, the default value is false.
304 * <p>
305 * <strong>NOTE:</strong> The value can not be changed during parsing an must be set before parsing.
306 *
307 * @see #getFeature
308 * @see #setFeature
309 */
310 String FEATURE_PROCESS_NAMESPACES = "http://xmlpull.org/v1/doc/features.html#process-namespaces";
311
312 /**
313 * This feature determines whether namespace attributes are exposed via the attribute access methods. Like all
314 * features, the default value is false. This feature cannot be changed during parsing.
315 *
316 * @see #getFeature
317 * @see #setFeature
318 */
319 String FEATURE_REPORT_NAMESPACE_ATTRIBUTES = "http://xmlpull.org/v1/doc/features.html#report-namespace-prefixes";
320
321 /**
322 * This feature determines whether the document declaration is processed. If set to false, the DOCDECL event type is
323 * reported by nextToken() and ignored by next(). If this feature is activated, then the document declaration must
324 * be processed by the parser.
325 * <p>
326 * <strong>Please note:</strong> If the document type declaration was ignored, entity references may cause
327 * exceptions later in the parsing process. The default value of this feature is false. It cannot be changed during
328 * parsing.
329 *
330 * @see #getFeature
331 * @see #setFeature
332 */
333 String FEATURE_PROCESS_DOCDECL = "http://xmlpull.org/v1/doc/features.html#process-docdecl";
334
335 /**
336 * If this feature is activated, all validation errors as defined in the XML 1.0 specification are reported. This
337 * implies that FEATURE_PROCESS_DOCDECL is true and both, the internal and external document type declaration will
338 * be processed.
339 * <p>
340 * <strong>Please Note:</strong> This feature can not be changed during parsing. The default value is false.
341 *
342 * @see #getFeature
343 * @see #setFeature
344 */
345 String FEATURE_VALIDATION = "http://xmlpull.org/v1/doc/features.html#validation";
346
347 /**
348 * Use this call to change the general behaviour of the parser, such as namespace processing or doctype declaration
349 * handling. This method must be called before the first call to next or nextToken. Otherwise, an exception is
350 * thrown.
351 * <p>
352 * Example: call setFeature(FEATURE_PROCESS_NAMESPACES, true) in order to switch on namespace processing. The
353 * initial settings correspond to the properties requested from the XML Pull Parser factory. If none were requested,
354 * all features are deactivated by default.
355 * @param name feature name
356 * @param state feature state
357 * @exception XmlPullParserException If the feature is not supported or can not be set
358 * @exception IllegalArgumentException If string with the feature name is null
359 */
360 void setFeature(String name, boolean state) throws XmlPullParserException;
361
362 /**
363 * Returns the current value of the given feature.
364 * <p>
365 * <strong>Please note:</strong> unknown features are <strong>always</strong> returned as false.
366 *
367 * @param name The name of feature to be retrieved.
368 * @return The value of the feature.
369 * @exception IllegalArgumentException if string the feature name is null
370 */
371 boolean getFeature(String name);
372
373 /**
374 * Set the value of a property. The property name is any fully-qualified URI.
375 * @param name property name
376 * @param value property value
377 * @exception XmlPullParserException If the property is not supported or can not be set
378 * @exception IllegalArgumentException If string with the property name is null
379 * @throws XmlPullParserException parsing issue
380 */
381 void setProperty(String name, Object value) throws XmlPullParserException;
382
383 /**
384 * Look up the value of a property. The property name is any fully-qualified URI.
385 * <p>
386 * <strong>NOTE:</strong> unknown properties are <strong>always</strong> returned as null.
387 *
388 * @param name The name of property to be retrieved.
389 * @return The value of named property.
390 */
391 Object getProperty(String name);
392
393 /**
394 * Set the input source for parser to the given reader and resets the parser. The event type is set to the initial
395 * value START_DOCUMENT. Setting the reader to null will just stop parsing and reset parser state, allowing the
396 * parser to free internal resources such as parsing buffers.
397 * @param in the Reader
398 * @throws XmlPullParserException parsing issue
399 */
400 void setInput(Reader in) throws XmlPullParserException;
401
402 /**
403 * Sets the input stream the parser is going to process. This call resets the parser state and sets the event type
404 * to the initial value START_DOCUMENT.
405 * <p>
406 * <strong>NOTE:</strong> If an input encoding string is passed, it MUST be used. Otherwise, if inputEncoding is
407 * null, the parser SHOULD try to determine input encoding following XML 1.0 specification (see below). If encoding
408 * detection is supported then following feature <a href=
409 * "http://xmlpull.org/v1/doc/features.html#detect-encoding">http://xmlpull.org/v1/doc/features.html#detect-encoding</a>
410 * MUST be true and otherwise it must be false
411 *
412 * @param inputStream contains a raw byte input stream of possibly unknown encoding (when inputEncoding is null).
413 * @param inputEncoding if not null it MUST be used as encoding for inputStream
414 * @throws XmlPullParserException parsing issue
415 */
416 void setInput(InputStream inputStream, String inputEncoding) throws XmlPullParserException;
417
418 /**
419 * @return the input encoding if known, null otherwise. If setInput(InputStream, inputEncoding) was called with an
420 * inputEncoding value other than null, this value must be returned from this method. Otherwise, if inputEncoding is
421 * null and the parser supports the encoding detection feature
422 * (http://xmlpull.org/v1/doc/features.html#detect-encoding), it must return the detected encoding. If
423 * setInput(Reader) was called, null is returned. After first call to next if XML declaration was present this
424 * method will return encoding declared.
425 */
426 String getInputEncoding();
427
428 /**
429 * Set new value for entity replacement text as defined in
430 * <a href="http://www.w3.org/TR/REC-xml#intern-replacement">XML 1.0 Section 4.5 Construction of Internal Entity
431 * Replacement Text</a>. If FEATURE_PROCESS_DOCDECL or FEATURE_VALIDATION are set, calling this function will result
432 * in an exception -- when processing of DOCDECL is enabled, there is no need to the entity replacement text
433 * manually.
434 * <p>
435 * The motivation for this function is to allow very small implementations of XMLPULL that will work in J2ME
436 * environments. Though these implementations may not be able to process the document type declaration, they still
437 * can work with known DTDs by using this function.
438 * <p>
439 * <b>Please notes:</b> The given value is used literally as replacement text and it corresponds to declaring entity
440 * in DTD that has all special characters escaped: left angle bracket is replaced with &lt;, ampersand with
441 * &amp; and so on.
442 * <p>
443 * <b>Note:</b> The given value is the literal replacement text and must not contain any other entity reference (if
444 * it contains any entity reference there will be no further replacement).
445 * <p>
446 * <b>Note:</b> The list of pre-defined entity names will always contain standard XML entities such as amp
447 * (&amp;), lt (&lt;), gt (&gt;), quot (&quot;), and apos (&apos;). Those cannot be redefined by
448 * this method!
449 * @param entityName entity name
450 * @param replacementText remplacement
451 * @see #setInput
452 * @see #FEATURE_PROCESS_DOCDECL
453 * @see #FEATURE_VALIDATION
454 * @throws XmlPullParserException parsing issue
455 */
456 void defineEntityReplacementText(String entityName, String replacementText) throws XmlPullParserException;
457
458 /**
459 * @return the numbers of elements in the namespace stack for the given depth. If namespaces are not enabled, 0 is
460 * returned.
461 * <p>
462 * <b>NOTE:</b> when parser is on END_TAG then it is allowed to call this function with getDepth()+1 argument to
463 * retrieve position of namespace prefixes and URIs that were declared on corresponding START_TAG.
464 * <p>
465 * <b>NOTE:</b> to retrieve lsit of namespaces declared in current element:
466 *
467 * <pre>
468 * XmlPullParser pp = ...
469 * int nsStart = pp.getNamespaceCount(pp.getDepth()-1);
470 * int nsEnd = pp.getNamespaceCount(pp.getDepth());
471 * for (int i = nsStart; i > nsEnd; i++) {
472 * String prefix = pp.getNamespacePrefix(i);
473 * String ns = pp.getNamespaceUri(i);
474 * // ...
475 * }
476 * </pre>
477 *
478 * @see #getNamespacePrefix
479 * @see #getNamespaceUri
480 * @see #getNamespace()
481 * @see #getNamespace(String)
482 * @param depth depth
483 * @throws XmlPullParserException parsing issue
484 */
485 int getNamespaceCount(int depth) throws XmlPullParserException;
486
487 /**
488 * @return Returns the namespace prefix for the given position in the namespace stack. Default namespace declaration
489 * (xmlns='...') will have null as prefix. If the given index is out of range, an exception is thrown.
490 *
491 * <b>Please note:</b> when the parser is on an END_TAG, namespace prefixes that were declared in the corresponding
492 * START_TAG are still accessible although they are no longer in scope.
493 * namespace prefix
494 * @param pos namespace stack position
495 * @throws XmlPullParserException parsing issue
496 */
497 String getNamespacePrefix(int pos) throws XmlPullParserException;
498
499 /**
500 * @return Returns the namespace URI for the given position in the namespace stack If the position is out of range, an
501 * exception is thrown.
502 *
503 * <b>NOTE:</b> when parser is on END_TAG then namespace prefixes that were declared in corresponding START_TAG are
504 * still accessible even though they are not in scope
505 * @throws XmlPullParserException parsing issue
506 * @param pos namespace stack position
507 */
508 String getNamespaceUri(int pos) throws XmlPullParserException;
509
510 /**
511 * @return the URI corresponding to the given prefix, depending on current state of the parser.
512 * <p>
513 * If the prefix was not declared in the current scope, null is returned. The default namespace is included in the
514 * namespace table and is available via getNamespace (null).
515 * <p>
516 * This method is a convenience method for
517 *
518 * <pre>
519 * for ( int i = getNamespaceCount( getDepth() ) - 1; i >= 0; i-- )
520 * {
521 * if ( getNamespacePrefix( i ).equals( prefix ) )
522 * {
523 * return getNamespaceUri( i );
524 * }
525 * }
526 * return null;
527 * </pre>
528 * <p>
529 * <strong>Please note:</strong> parser implementations may provide more efficient lookup, e.g. using a Hashtable.
530 * The 'xml' prefix is bound to "http://www.w3.org/XML/1998/namespace", as defined in the
531 * <a href="http://www.w3.org/TR/REC-xml-names/#ns-using">Namespaces in XML</a> specification. Analogous, the
532 * 'xmlns' prefix is resolved to <a href="http://www.w3.org/2000/xmlns/">http://www.w3.org/2000/xmlns/</a>
533 * @param prefix given prefix
534 * @see #getNamespaceCount
535 * @see #getNamespacePrefix
536 * @see #getNamespaceUri
537 */
538 String getNamespace(String prefix);
539
540 // --------------------------------------------------------------------------
541 // miscellaneous reporting methods
542
543 /**
544 * @return the current depth of the element. Outside the root element, the depth is 0. The depth is incremented by 1
545 * when a start tag is reached. The depth is decremented AFTER the end tag event was observed.
546 *
547 * <pre>
548 * <!-- outside --> 0
549 * <root> 1
550 * sometext 1
551 * <foobar> 2
552 * </foobar> 2
553 * </root> 1
554 * <!-- outside --> 0
555 * </pre>
556 */
557 int getDepth();
558
559 /**
560 * @return a short text describing the current parser state, including the position, a description of the current
561 * event and the data source if known. This method is especially useful to provide meaningful error messages and for
562 * debugging purposes.
563 */
564 String getPositionDescription();
565
566 /**
567 * Returns the current line number, starting from 1. When the parser does not know the current line number or can
568 * not determine it, -1 is returned (e.g. for WBXML).
569 *
570 * @return current line number or -1 if unknown.
571 */
572 int getLineNumber();
573
574 /**
575 * Returns the current column number, starting from 0. When the parser does not know the current column number or
576 * can not determine it, -1 is returned (e.g. for WBXML).
577 *
578 * @return current column number or -1 if unknown.
579 */
580 int getColumnNumber();
581
582 // --------------------------------------------------------------------------
583 // TEXT related methods
584
585 /**
586 * @return Checks whether the current TEXT event contains only whitespace characters. For IGNORABLE_WHITESPACE, this is
587 * always true. For TEXT and CDSECT, false is returned when the current event text contains at least one non-white
588 * space character. For any other event type an exception is thrown.
589 * <p>
590 * <b>Please note:</b> non-validating parsers are not able to distinguish whitespace and ignorable whitespace,
591 * except from whitespace outside the root element. Ignorable whitespace is reported as separate event, which is
592 * exposed via nextToken only.
593 * @throws XmlPullParserException parsing issue
594 */
595 boolean isWhitespace() throws XmlPullParserException;
596
597 /**
598 * @return the text content of the current event as String. The value returned depends on current event type, for
599 * example for TEXT event it is element content (this is typical case when next() is used). See description of
600 * nextToken() for detailed description of possible returned values for different types of events.
601 * <p>
602 * <strong>NOTE:</strong> in case of ENTITY_REF, this method returns the entity replacement text (or null if not
603 * available). This is the only case where getText() and getTextCharacters() return different values.
604 *
605 * @see #getEventType
606 * @see #next
607 * @see #nextToken
608 */
609 String getText();
610
611 /**
612 * Returns the buffer that contains the text of the current event, as well as the start offset and length relevant
613 * for the current event. See getText(), next() and nextToken() for description of possible returned values.
614 * <p>
615 * <strong>Please note:</strong> this buffer must not be modified and its content MAY change after a call to next()
616 * or nextToken(). This method will always return the same value as getText(), except for ENTITY_REF. In the case of
617 * ENTITY ref, getText() returns the replacement text and this method returns the actual input buffer containing the
618 * entity name. If getText() returns null, this method returns null as well and the values returned in the holder
619 * array MUST be -1 (both start and length).
620 *
621 * @see #getText
622 * @see #next
623 * @see #nextToken
624 * @param holderForStartAndLength Must hold an 2-element int array into which the start offset and length values
625 * will be written.
626 * @return char buffer that contains the text of the current event (null if the current event has no text
627 * associated).
628 */
629 char[] getTextCharacters(int[] holderForStartAndLength);
630
631 // --------------------------------------------------------------------------
632 // START_TAG / END_TAG shared methods
633
634 /**
635 * @return the namespace URI of the current element. The default namespace is represented as empty string. If
636 * namespaces are not enabled, an empty String ("") is always returned. The current event must be START_TAG or
637 * END_TAG; otherwise, null is returned.
638 */
639 String getNamespace();
640
641 /**
642 * @return For START_TAG or END_TAG events, the (local) name of the current element is returned when namespaces are enabled.
643 * When namespace processing is disabled, the raw name is returned. For ENTITY_REF events, the entity name is
644 * returned. If the current event is not START_TAG, END_TAG, or ENTITY_REF, null is returned.
645 * <p>
646 * <b>Please note:</b> To reconstruct the raw element name when namespaces are enabled and the prefix is not null,
647 * you will need to add the prefix and a colon to localName..
648 */
649 String getName();
650
651 /**
652 * @return the prefix of the current element. If the element is in the default namespace (has no prefix), null is
653 * returned. If namespaces are not enabled, or the current event is not START_TAG or END_TAG, null is returned.
654 */
655 String getPrefix();
656
657 /**
658 * @return true if the current event is START_TAG and the tag is degenerated (e.g. <foobar/>).
659 * <p>
660 * <b>NOTE:</b> if the parser is not on START_TAG, an exception will be thrown.
661 * @throws XmlPullParserException parsing issue
662 */
663 boolean isEmptyElementTag() throws XmlPullParserException;
664
665 // --------------------------------------------------------------------------
666 // START_TAG Attributes retrieval methods
667
668 /**
669 * @return the number of attributes of the current start tag, or -1 if the current event type is not START_TAG
670 *
671 * @see #getAttributeNamespace
672 * @see #getAttributeName
673 * @see #getAttributePrefix
674 * @see #getAttributeValue
675 */
676 int getAttributeCount();
677
678 /**
679 * Returns the namespace URI of the attribute with the given index (starts from 0). Returns an empty string ("") if
680 * namespaces are not enabled or the attribute has no namespace. Throws an IndexOutOfBoundsException if the index is
681 * out of range or the current event type is not START_TAG.
682 * <p>
683 * <strong>NOTE:</strong> if FEATURE_REPORT_NAMESPACE_ATTRIBUTES is set then namespace attributes (xmlns:ns='...')
684 * must be reported with namespace <a href="http://www.w3.org/2000/xmlns/">http://www.w3.org/2000/xmlns/</a> (visit
685 * this URL for description!). The default namespace attribute (xmlns="...") will be reported with empty namespace.
686 * <p>
687 * <strong>NOTE:</strong>The xml prefix is bound as defined in
688 * <a href="http://www.w3.org/TR/REC-xml-names/#ns-using">Namespaces in XML</a> specification to
689 * "http://www.w3.org/XML/1998/namespace".
690 *
691 * @param index zero based index of attribute
692 * @return attribute namespace, empty string ("") is returned if namespaces processing is not enabled or namespaces
693 * processing is enabled but attribute has no namespace (it has no prefix).
694 */
695 String getAttributeNamespace(int index);
696
697 /**
698 * Returns the local name of the specified attribute if namespaces are enabled or just attribute name if namespaces
699 * are disabled. Throws an IndexOutOfBoundsException if the index is out of range or current event type is not
700 * START_TAG.
701 *
702 * @param index zero based index of attribute
703 * @return attribute name (null is never returned)
704 */
705 String getAttributeName(int index);
706
707 /**
708 * Returns the prefix of the specified attribute Returns null if the element has no prefix. If namespaces are
709 * disabled it will always return null. Throws an IndexOutOfBoundsException if the index is out of range or current
710 * event type is not START_TAG.
711 *
712 * @param index zero based index of attribute
713 * @return attribute prefix or null if namespaces processing is not enabled.
714 */
715 String getAttributePrefix(int index);
716
717 /**
718 * Returns the type of the specified attribute If parser is non-validating it MUST return CDATA.
719 *
720 * @param index zero based index of attribute
721 * @return attribute type (null is never returned)
722 */
723 String getAttributeType(int index);
724
725 /**
726 * Returns if the specified attribute was not in input was declared in XML. If parser is non-validating it MUST
727 * always return false. This information is part of XML infoset:
728 *
729 * @param index zero based index of attribute
730 * @return false if attribute was in input
731 */
732 boolean isAttributeDefault(int index);
733
734 /**
735 * Returns the given attributes value. Throws an IndexOutOfBoundsException if the index is out of range or current
736 * event type is not START_TAG.
737 * <p>
738 * <strong>NOTE:</strong> attribute value must be normalized (including entity replacement text if PROCESS_DOCDECL
739 * is false) as described in <a href="http://www.w3.org/TR/REC-xml#AVNormalize">XML 1.0 section 3.3.3
740 * Attribute-Value Normalization</a>
741 *
742 * @see #defineEntityReplacementText
743 * @param index zero based index of attribute
744 * @return value of attribute (null is never returned)
745 */
746 String getAttributeValue(int index);
747
748 /**
749 * Returns the attributes value identified by namespace URI and namespace localName. If namespaces are disabled
750 * namespace must be null. If current event type is not START_TAG then IndexOutOfBoundsException will be thrown.
751 * <p>
752 * <strong>NOTE:</strong> attribute value must be normalized (including entity replacement text if PROCESS_DOCDECL
753 * is false) as described in <a href="http://www.w3.org/TR/REC-xml#AVNormalize">XML 1.0 section 3.3.3
754 * Attribute-Value Normalization</a>
755 *
756 * @see #defineEntityReplacementText
757 * @param namespace Namespace of the attribute if namespaces are enabled otherwise must be null
758 * @param name If namespaces enabled local name of attribute otherwise just attribute name
759 * @return value of attribute or null if attribute with given name does not exist
760 */
761 String getAttributeValue(String namespace, String name);
762
763 // --------------------------------------------------------------------------
764 // actual parsing methods
765
766 /**
767 * @return the type of the current event (START_TAG, END_TAG, TEXT, etc.)
768 *
769 * @see #next()
770 * @see #nextToken()
771 * @throws XmlPullParserException parsing issue
772 */
773 int getEventType() throws XmlPullParserException;
774
775 /**
776 * @return Get next parsing event - element content wil be coalesced and only one TEXT event must be returned for whole
777 * element content (comments and processing instructions will be ignored and entity references must be expanded or
778 * exception mus be thrown if entity reference can not be expanded). If element content is empty (content is "")
779 * then no TEXT event will be reported.
780 * <p>
781 * <b>NOTE:</b> empty element (such as <tag/>) will be reported with two separate events: START_TAG, END_TAG - it
782 * must be so to preserve parsing equivalency of empty element to <tag></tag>. (see isEmptyElementTag ())
783 *
784 * @see #isEmptyElementTag
785 * @see #START_TAG
786 * @see #TEXT
787 * @see #END_TAG
788 * @see #END_DOCUMENT
789 * @throws XmlPullParserException parsing issue
790 * @throws IOException io issue
791 */
792 int next() throws XmlPullParserException, IOException;
793
794 /**
795 * This method works similarly to next() but will expose additional event types (COMMENT, CDSECT, DOCDECL,
796 * ENTITY_REF, PROCESSING_INSTRUCTION, or IGNORABLE_WHITESPACE) if they are available in input.
797 * <p>
798 * If special feature <a href="http://xmlpull.org/v1/doc/features.html#xml-roundtrip">FEATURE_XML_ROUNDTRIP</a>
799 * (identified by URI: http://xmlpull.org/v1/doc/features.html#xml-roundtrip) is enabled it is possible to do XML
800 * document round trip ie. reproduce exactly on output the XML input using getText(): returned content is always
801 * unnormalized (exactly as in input). Otherwise returned content is end-of-line normalized as described
802 * <a href="http://www.w3.org/TR/REC-xml#sec-line-ends">XML 1.0 End-of-Line Handling</a> and. Also when this feature
803 * is enabled exact content of START_TAG, END_TAG, DOCDECL and PROCESSING_INSTRUCTION is available.
804 * <p>
805 * Here is the list of tokens that can be returned from nextToken() and what getText() and getTextCharacters()
806 * @return
807 * <dl>
808 * <dt>START_DOCUMENT
809 * <dd>null
810 * <dt>END_DOCUMENT
811 * <dd>null
812 * <dt>START_TAG
813 * <dd>null unless FEATURE_XML_ROUNDTRIP enabled and then returns XML tag, ex: <tag attr='val'>
814 * <dt>END_TAG
815 * <dd>null unless FEATURE_XML_ROUNDTRIP id enabled and then returns XML tag, ex: </tag>
816 * <dt>TEXT
817 * <dd>return element content. <br>
818 * Note: that element content may be delivered in multiple consecutive TEXT events.
819 * <dt>IGNORABLE_WHITESPACE
820 * <dd>return characters that are determined to be ignorable white space. If the FEATURE_XML_ROUNDTRIP is enabled
821 * all whitespace content outside root element will always reported as IGNORABLE_WHITESPACE otherwise reporting is
822 * optional. <br>
823 * Note: that element content may be delivered in multiple consecutive IGNORABLE_WHITESPACE events.
824 * <dt>CDSECT
825 * <dd>return text <em>inside</em> CDATA (ex. 'fo<o' from <!CDATA[fo<o]]>)
826 * <dt>PROCESSING_INSTRUCTION
827 * <dd>if FEATURE_XML_ROUNDTRIP is true return exact PI content ex: 'pi foo' from <?pi foo?> otherwise it may be
828 * exact PI content or concatenation of PI target, space and data so for example for <?target data?> string
829 * "target data" may be returned if FEATURE_XML_ROUNDTRIP is false.
830 * <dt>COMMENT
831 * <dd>return comment content ex. 'foo bar' from <!--foo bar-->
832 * <dt>ENTITY_REF
833 * <dd>getText() MUST return entity replacement text if PROCESS_DOCDECL is false otherwise getText() MAY return
834 * null, additionally getTextCharacters() MUST return entity name (for example 'entity_name' for &entity_name;).
835 * <br>
836 * <b>NOTE:</b> this is the only place where value returned from getText() and getTextCharacters() <b>are
837 * different</b> <br>
838 * <b>NOTE:</b> it is user responsibility to resolve entity reference if PROCESS_DOCDECL is false and there is no
839 * entity replacement text set in defineEntityReplacementText() method (getText() will be null) <br>
840 * <b>NOTE:</b> character entities (ex. &#32;) and standard entities such as &amp; &lt; &gt;
841 * &quot; &apos; are reported as well and are <b>not</b> reported as TEXT tokens but as ENTITY_REF tokens!
842 * This requirement is added to allow to do roundtrip of XML documents!
843 * <dt>DOCDECL
844 * <dd>if FEATURE_XML_ROUNDTRIP is true or PROCESS_DOCDECL is false then return what is inside of DOCDECL for
845 * example it returns:
846 *
847 * <pre>
848 * " titlepage SYSTEM "http://www.foo.bar/dtds/typo.dtd"
849 * [<!ENTITY % active.links "INCLUDE">]"
850 * </pre>
851 * <p>
852 * for input document that contained:
853 *
854 * <pre>
855 * <!DOCTYPE titlepage SYSTEM "http://www.foo.bar/dtds/typo.dtd"
856 * [<!ENTITY % active.links "INCLUDE">]>
857 * </pre>
858 *
859 * otherwise if FEATURE_XML_ROUNDTRIP is false and PROCESS_DOCDECL is true then what is returned is undefined (it
860 * may be even null)</dd>
861 * </dl>
862 * <p>
863 * <strong>NOTE:</strong> there is no guarantee that there will only one TEXT or IGNORABLE_WHITESPACE event from
864 * nextToken() as parser may chose to deliver element content in multiple tokens (dividing element content into
865 * chunks)
866 * <p>
867 * <strong>NOTE:</strong> whether returned text of token is end-of-line normalized is depending on
868 * FEATURE_XML_ROUNDTRIP.
869 * <p>
870 * <strong>NOTE:</strong> XMLDecl (<?xml ...?>) is not reported but its content is available through optional
871 * properties (see class description above).
872 * @throws XmlPullParserException parsing issue
873 * @throws IOException io issue
874 * @see #next
875 * @see #START_TAG
876 * @see #TEXT
877 * @see #END_TAG
878 * @see #END_DOCUMENT
879 * @see #COMMENT
880 * @see #DOCDECL
881 * @see #PROCESSING_INSTRUCTION
882 * @see #ENTITY_REF
883 * @see #IGNORABLE_WHITESPACE
884 */
885 int nextToken() throws XmlPullParserException, IOException;
886
887 // -----------------------------------------------------------------------------
888 // utility methods to mak XML parsing easier ...
889
890 /**
891 * Test if the current event is of the given type and if the namespace and name do match. null will match any
892 * namespace and any name. If the test is not passed, an exception is thrown. The exception text indicates the
893 * parser position, the expected event and the current event that is not meeting the requirement.
894 * <p>
895 * Essentially it does this
896 *
897 * <pre>
898 * if ( type != getEventType() || ( namespace != null && !namespace.equals( getNamespace() ) )
899 * || ( name != null && !name.equals( getName() ) ) )
900 * throw new XmlPullParserException( "expected " + TYPES[type] + getPositionDescription() );
901 * </pre>
902 * @param type type
903 * @param name name
904 * @param namespace namespace
905 * @throws XmlPullParserException parsing issue
906 * @throws IOException io issue
907 */
908 void require(int type, String namespace, String name) throws XmlPullParserException, IOException;
909
910 /**
911 * If current event is START_TAG then if next element is TEXT then element content is returned or if next event is
912 * END_TAG then empty string is returned, otherwise exception is thrown. After calling this function successfully
913 * parser will be positioned on END_TAG.
914 * <p>
915 * The motivation for this function is to allow to parse consistently both empty elements and elements that has non
916 * empty content, for example for input:
917 * <ol>
918 * <li><tag>foo</tag>
919 * <li><tag></tag> (which is equivalent to <tag/> both input can be parsed with the same code:
920 *
921 * <pre>
922 * p.nextTag()
923 * p.requireEvent(p.START_TAG, "", "tag");
924 * String content = p.nextText();
925 * p.requireEvent(p.END_TAG, "", "tag");
926 * </pre></li></ol>
927 *
928 * This function together with nextTag make it very easy to parse XML that has no mixed content.
929 * <p>
930 * Essentially it does this
931 *
932 * <pre>
933 * if ( getEventType() != START_TAG )
934 * {
935 * throw new XmlPullParserException( "parser must be on START_TAG to read next text", this, null );
936 * }
937 * int eventType = next();
938 * if ( eventType == TEXT )
939 * {
940 * String result = getText();
941 * eventType = next();
942 * if ( eventType != END_TAG )
943 * {
944 * throw new XmlPullParserException( "event TEXT it must be immediately followed by END_TAG", this, null );
945 * }
946 * return result;
947 * }
948 * else if ( eventType == END_TAG )
949 * {
950 * return "";
951 * }
952 * else
953 * {
954 * throw new XmlPullParserException( "parser must be on START_TAG or TEXT to read text", this, null );
955 * }
956 * </pre>
957 * @return see description
958 * @throws XmlPullParserException parsing issue
959 * @throws IOException io issue
960 */
961 String nextText() throws XmlPullParserException, IOException;
962
963 /**
964 * Call next() and return event if it is START_TAG or END_TAG otherwise throw an exception. It will skip whitespace
965 * TEXT before actual tag if any.
966 * <p>
967 * essentially it does this
968 *
969 * <pre>
970 * int eventType = next();
971 * if ( eventType == TEXT && isWhitespace() )
972 * { // skip whitespace
973 * eventType = next();
974 * }
975 * if ( eventType != START_TAG && eventType != END_TAG )
976 * {
977 * throw new XmlPullParserException( "expected start or end tag", this, null );
978 * }
979 * return eventType;
980 * </pre>
981 * @return see description
982 * @throws XmlPullParserException parsing issue
983 * @throws
984 * IOException io issue
985 */
986 int nextTag() throws XmlPullParserException, IOException;
987 }