Debian Codegraph @

doc/sax-2.0-adv.html

raw
1 <!-- $Id: sax-2.0-adv.html,v 1.6 2001/11/09 18:19:48 darobin Exp $ -->
2 <html>
3 <head>
4 <title>Advanced Features of the Perl SAX 2.0 Binding</title>
5 <meta name="keywords" content="XML SGML SAX Perl libxml libxml-perl" />
6 </head>
7 <body>
8
9 <h1>Advanced SAX</h1>
10
11 <p>The classes, methods, and features described below are
12 not commonly used in most applications and can be ignored by most
13 users. If however you find that you are not getting the granularity
14 you expect from Basic SAX, this would be the place to look for more.
15 Advanced SAX isn't advanced in the sense that it is harder, or requires
16 better programming skills. It is simply more complete, and has been
17 separated to keep Basic SAX simple in terms of the number of events
18 one would have to deal with.
19 </p>
20
21 <h2><a name="Parsers">SAX Parsers</a></h2>
22
23 <p>SAX supports several classes of event handlers: content handlers,
24 declaration handlers, DTD handlers, error handlers, entity resolvers,
25 and other extensions. For each class of events, a seperate handler
26 can be used to handle those events. If a handler is not defined for a
27 class of events, then the default handler, <tt>Handler</tt>, is used.
28 Each of these handlers is described in the sections below.
29 Applications may change an event handler in the middle of the parse
30 and the SAX parser will begin using the new handler immediately.</p>
31
32 <p>SAX's basic interface defines methods for parsing system
33 identifiers (URIs), open files, and strings. Behind the scenes,
34 though, SAX uses a <tt>Source</tt> hash that contains that
35 information, plus encoding, system and public identifiers if
36 available. These are described below under the <tt>Source</tt>
37 option.</p>
38
39 <p>SAX parsers accept all features as options to the <tt>parse()</tt>
40 methods and on the parser's constructor. Features are described in
41 the next section.</p>
42
43 <p>
44 <dl><dt><b><tt class='function'>parse</tt></b>(<var>options</var>)</dt>
45 <dd>
46 Parses the XML instance identified by the <tt>Source</tt> option.
47 <var>options</var> can be a list of option, value pairs or a hash.
48 <tt>parse()</tt> returns the result of calling the
49 <tt>end_document()</tt> handler.</dd></dl></p>
50
51 <p>
52 <dl><dt><b><tt>ContentHandler</tt></b></dt>
53 <dd>
54 Object to receive document content events. The
55 <tt>ContentHandler</tt>, with additional events defined below, is the
56 class of events described in <a href="sax-2.0.html#BasicHandler">Basic
57 SAX Handler</a>.If the application does not register a content handler
58 or content event handlers on the default handler, content events
59 reported by the SAX parser will be silently ignored.</dd></dl></p>
60
61 <p>
62 <dl><dt><b><tt>DTDHandler</tt></b></dt>
63 <dd>
64 Object to receive basic DTD events. If the application does not
65 register a DTD handler or DTD event handlers on the default handler,
66 DTD events reported by the SAX parser will be silently
67 ignored.</dd></dl></p>
68
69 <p>
70 <dl><dt><b><tt>EntityResolver</tt></b></dt>
71 <dd>
72 Object to resolve external entities. If the application does not
73 register an entity resolver or entity events on the default handler,
74 the SAX parser will perform its own default resolution.</dd></dl></p>
75
76 <p>
77 <dl><dt><b><tt>ErrorHandler</tt></b></dt>
78 <dd>
79 Object to receive error-message events. If the application does not
80 register an error handler or error event handlers on the default
81 handler, all error events reported by the SAX parser will be silently
82 ignored; however, normal processing may not continue. It is highly
83 recommended that all SAX applications implement an error handler to
84 avoid unexpected bugs.</dd></dl></p>
85
86 <p>
87 <dl><dt><b><tt>Source</tt></b></dt>
88 <dd>
89 A hash containing information about the XML instance to be parsed.
90 See <a href="#InputSources">Input Sources</a> below. Note that
91 <tt>Source</tt> cannot be changed during the parse</dd></dl></p>
92
93 <p>
94 <dl>
95 <dt><strong><tt>Features</tt></strong></dt>
96 <dd>
97 A hash containing Feature information, as described below.
98 Features can be set at runtime but not directly on the Features
99 hash (at least, not reliably. You can do it, but the results
100 might not be what you expect as it doesn't give the parser a
101 chance to look at what you've set so that it can't react properly
102 to errors, or Features that it doesn't support). You should use
103 the <code>set_feature()</code> method instead.
104 </dd>
105 </dl>
106 </p>
107
108
109 <h2><a name="Features">Features</a></h2>
110
111 <p>Features are as defined in <a
112 href="http://sax.sourceforge.net/apidoc/org/xml/sax/package-summary.html#package_description">SAX2: Features
113 and Properties</a>, but not of course limited to those. You may add
114 your own Features. Also, Java has an artificial distinction between
115 Features and Properties which is unnecessary. In Perl, both have been
116 merged under the same name.
117 </p>
118
119 <p>Features can be passed as options when creating a parser or calling
120 a <tt>parse()</tt> method. They may also be set using the
121 set_feature().
122 </p>
123
124 <pre>
125 $parser = AnySAXParser->new(
126 Features => {
127 'http://xml.org/sax/features/namespaces' => 0,
128 },
129 );
130 $parser->parse(
131 Features => {
132 'http://xml.org/sax/features/namespaces' => 0,
133 },
134 );
135 $parser->set_feature('http://xml.org/sax/properties/xml-string', 1);
136 $string = $parser->get_feature('http://xml.org/sax/properties/xml-string');
137 </pre>
138
139 <p>
140 When performing namespace processing, Perl SAX parsers always provide
141 both the raw tag name in <tt>Name</tt> and the namespace names in
142 <tt>NamespaceURI</tt>, <tt>LocalName</tt>, and <tt>Prefix</tt>.
143 Therefore, the
144 "<tt>http://xml.org/sax/features/namespace-prefixes</tt>" Feature is
145 ignored.
146 </p>
147
148 <p>
149 Also, Features are things that are supposed to be <strong>turned
150 on</strong>, and thus should normally be off by default, especially if
151 the parser doesn't support turning them off. Due to backwards
152 compatibility problems, the one exception to this rule is the
153 "<tt>http://xml.org/sax/features/namespaces</tt>" Feature which is on by
154 default and which a number of parsers may not be able to turn off. Thus,
155 a parser claiming to support this Feature (and all SAX2 parsers must
156 support it) may in fact only support turning it on. This is only a minor
157 problem as turning it off basically amounts to returning to SAX1, which
158 can be accomplished by a filter (eg XML::Filter::SAX2toSAX1).
159 </p>
160
161 <p>
162 In addition to the Features described in the SAX spec
163 itself, a number of new ones may be defined for Perl. An example of
164 this would be http://xmlns.perl.org/sax/node-factory which
165 when supported by the parser would be settable to a NodeFactory object
166 that would be in charge of creating SAX nodes different from those that
167 are normally received by event handlers. See
168 <a href='http://xmlns.perl.org/'>http://xmlns.perl.org/</a> (currently
169 in alpha state) for details on how to register Features.
170 </p>
171
172 <p>
173 The following methods are used to get and set features:
174 </p>
175
176 <p>
177 <dl><dt><b><tt class='function'>get_feature</tt></b>(<var>name</var>)</dt>
178 <dd>
179 Look up the value of a feature.
180
181 <p>The feature name is any fully-qualified URI. It is possible for an
182 SAX parser to recognize a feature name but to be unable to return its
183 value; this is especially true in the case of an adapter for a SAX1
184 Parser, which has no way of knowing whether the underlying parser is
185 validating, for example.</p>
186
187 <p>Some feature values may be available only in specific contexts,
188 such as before, during, or after a parse.</p>
189
190 <tt>get_feature()</tt> returns the value of the feature, which is usually
191 either a boolean or an object, and will throw
192 <tt>XML::SAX::Exception::NotRecognized</tt> when the SAX parser does not
193 recognize the feature name and <tt>XML::SAX::Exception::NotSupported</tt>
194 when the SAX parser recognizes the feature name but cannot determine its
195 value at this time.</dd></dl></p>
196
197 <p>
198 <dl><dt><b><tt class='function'>set_feature</tt></b>(<var>name</var>,
199 <var>value</var>)</dt>
200 <dd>
201 Set the state of a feature.
202
203 <p>The feature name is any fully-qualified URI. It is possible for an
204 SAX parser to recognize a feature name but to be unable to set its
205 value; this is especially true in the case of an adapter for a SAX1
206 Parser, which has no way of affecting whether the underlying parser is
207 validating, for example.</p>
208
209 <p>Some feature values may be immutable or mutable only in specific
210 contexts, such as before, during, or after a parse.</p>
211
212 <tt>set_feature()</tt> will throw <tt>XML::SAX::Exception::NotRecognized</tt>
213 when the SAX parser does not recognize the feature name and
214 <tt>XML::SAX::Exception::NotSupported</tt> when the SAX parser recognizes the
215 feature name but cannot set the requested value.
216
217 <p>
218 This method is also the standard mechanism for setting extended handlers,
219 such as "<code>http://xml.org/sax/handlers/DeclHandler</code>".
220 </p>
221 </dd></dl></p>
222
223
224 <p>
225 <dl>
226 <dt><strong><code class='function'>get_features</code></strong>()</dt>
227 <dd>
228 Look up all Features that this parser claims to support.
229 <p>
230 This method returns a hash of Features which the parser
231 claims to support. The value of the hash is currently
232 unspecified though it may be used later. This method is meant
233 to be inherited so that Features supported by the base parser
234 class (XML::SAX::Base) are declared to be supported by
235 subclasses.
236 </p>
237 <p>
238 Calling this method is probably only moderately useful to end
239 users. It is mostly meant for use by XML::SAX, so that it can
240 query parsers for Feature support and return an appropriate
241 parser depending on the Features that are required.
242 </p>
243 </dd>
244 </dl>
245 </p>
246
247
248
249 <h2><a name="InputSources">Input Sources</a></h2>
250
251 <p>Input sources may be provided to parser objects or are returned by
252 entity resolvers. An input source is a hash with these
253 properties:</p>
254
255 <dl>
256 <dt><b><tt>PublicId</tt></b></dt>
257 <dd>The public identifier of this input source.
258
259 <p>The public identifier is always optional: if the application writer
260 includes one, it will be provided as part of the location
261 information.</p></dd>
262
263 <dt><b><tt>SystemId</tt></b></dt>
264 <dd>The system identifier (URI) of this input source.
265
266 <p>The system identifier is optional if there is a byte stream or a
267 character stream, but it is still useful to provide one, since the
268 application can use it to resolve relative URIs and can include it in
269 error messages and warnings (the parser will attempt to open a
270 connection to the URI only if there is no byte stream or character
271 stream specified).</p>
272
273 If the application knows the character encoding of the object
274 pointed to by the system identifier, it can register the encoding
275 using the <tt>Encoding</tt> property.</dd>
276
277 <dt><b><tt>ByteStream</tt></b></dt>
278 <dd>The byte stream for this input source.
279
280 <p>The SAX parser will ignore this if there is also a character stream
281 specified, but it will use a byte stream in preference to opening a
282 URI connection itself.</p>
283
284 If the application knows the character encoding of the byte stream, it
285 should set the <tt>Encoding</tt> property.</dd>
286
287 <dt><b><tt>CharacterStream</tt></b></dt>
288 <dd>The character stream for this input source.
289
290 <p>If there is a character stream specified, the SAX parser will
291 ignore any byte stream and will not attempt to open a URI connection
292 to the system identifier.</p>
293
294 <p>Note: A CharacterStream is a filehandle that does not need any encoding
295 translation done on it. This is implemented as a regular filehandle
296 and only works under Perl 5.7.2 or higher using PerlIO. To get a single
297 character, or number of characters from it, use the perl core read()
298 function. To get a single byte from it (or number of bytes), you can
299 use sysread(). The encoding of the stream should be in the Encoding
300 entry for the Source.</p>
301
302 </dd>
303
304 <dt><b><tt>Encoding</tt></b></dt>
305 <dd>The character encoding, if known.
306
307 <p>The encoding must be a string acceptable for an XML encoding
308 declaration (see section 4.3.3 of the XML 1.0 recommendation).</p>
309
310 This property has no effect when the application provides a character
311 stream.</dd>
312 </dl>
313
314 <h2><a name="Handlers">SAX Handlers</a></h2>
315
316 <p>SAX supports several classes of event handlers: content handlers,
317 declaration handlers, DTD handlers, error handlers, entity resolvers,
318 and other extensions. This section defines each of these classes of
319 events.</p>
320
321 <h3>Content Events</h3>
322
323 <p>This is the main interface that most SAX applications implement: if
324 the application needs to be informed of basic parsing events, it
325 implements this interface and registers an instance with the SAX
326 parser using the <tt>ContentHandler</tt> property. The parser uses
327 the instance to report basic document-related events like the start
328 and end of elements and character data.</p>
329
330 <p>The order of events in this interface is very important, and
331 mirrors the order of information in the document itself. For example,
332 all of an element's content (character data, processing instructions,
333 and/or subelements) will appear, in order, between the
334 <tt>start_element</tt> event and the corresponding
335 <tt>end_element</tt> event.</p>
336
337
338 <p>
339 <dl><dt><b><tt class='function'>set_document_locator</tt></b>(<var>locator</var>)</dt>
340 <dd>
341 Receive an object for locating the origin of SAX document events.
342
343 <p>SAX parsers are strongly encouraged (though not absolutely
344 required) to supply a locator: if it does so, it must supply the
345 locator to the application by invoking this method before invoking any
346 of the other methods in the ContentHandler interface.</p>
347
348 <p>The locator allows the application to determine the end position of
349 any document-related event, even if the parser is not reporting an
350 error. Typically, the application will use this information for
351 reporting its own errors (such as character content that does not
352 match an application's business rules). The information provided by
353 the locator is probably not sufficient for use with a search
354 engine.</p>
355
356 <p>Note that the locator will provide correct information only during
357 the invocation of the events in this interface. The application should
358 not attempt to use it at any other time.</p>
359
360 <p>The locator is a hash with these properties:</p>
361
362 <blockquote>
363 <table>
364 <tr><td><b><tt>ColumnNumber</tt></b></td>
365 <td>The column number of the end of the text where the exception
366 occurred.</td></tr>
367 <tr><td><b><tt>LineNumber</tt></b></td>
368 <td>The line number of the end of the text where the exception
369 occurred.</td></tr>
370 <tr><td><b><tt>PublicId</tt></b></td>
371 <td>The public identifier of the entity where the exception
372 occurred.</td></tr>
373 <tr><td><b><tt>SystemId</tt></b></td>
374 <td>The system identifier of the entity where the exception
375 occurred.</td></tr>
376 </table>
377 </blockquote></dd>
378 </dl></p>
379
380 <p>
381 <dl><dt><b><tt class='function'>start_prefix_mapping</tt></b>(<var>mapping</var>)</dt>
382 <dd>
383 Begin the scope of a prefix-URI Namespace mapping.
384
385 <p>The information from this event is not necessary for normal
386 Namespace processing: the SAX XML reader will automatically replace
387 prefixes for element and attribute names when the
388 "<tt>http://xml.org/sax/features/namespaces</tt>" feature is true (the
389 default).</p>
390
391 <p>There are cases, however, when applications need to use prefixes in
392 character data or in attribute values, where they cannot safely be
393 expanded automatically; the start/end_prefix_mapping event supplies the
394 information to the application to expand prefixes in those contexts
395 itself, if necessary.</p>
396
397 <p>Note that <tt>start</tt>/<tt>end_prefix_mapping()</tt> events are
398 not guaranteed to be properly nested relative to each-other: all
399 <tt>start_prefix_apping()</tt> events will occur before the
400 corresponding <tt>start_element()</tt> event, and all
401 <tt>end_prefix_mapping</tt> events will occur after the corresponding
402 <tt>end_element()</tt> event, but their order is not
403 guaranteed.
404 </p>
405
406 <p><var>mapping</var> is a hash with these properties:</p>
407
408 <blockquote>
409 <table>
410 <tr><td><b><tt>Prefix</tt></b></td>
411 <td>The Namespace prefix being declared.</td></tr>
412 <tr><td><b><tt>NamespaceURI</tt></b></td>
413 <td>The Namespace URI the prefix is mapped to.</td></tr>
414 </table>
415 </blockquote></dd>
416 </dl></p>
417
418 <p>
419 <dl><dt><b><tt class='function'>end_prefix_mapping</tt></b>(<var>mapping</var>)</dt>
420 <dd>
421 End the scope of a prefix-URI mapping.
422
423 <p>See <tt>start_prefix_mapping()</tt> for details. This event will
424 always occur after the corresponding <tt>end_element</tt> event, but
425 the order of <tt>end_prefix_mapping</tt> events is not otherwise
426 guaranteed.</p>
427
428 <p><var>mapping</var> is a hash with this property:</p>
429
430 <blockquote>
431 <table>
432 <tr><td><b><tt>Prefix</tt></b></td>
433 <td>The Namespace prefix that was being mapped.</td></tr>
434 </table>
435 </blockquote></dd>
436 </dl></p>
437
438 <p>
439 <dl><dt><b><tt class='function'>processing_instruction</tt></b>(<var>pi</var>)</dt>
440 <dd>
441 Receive notification of a processing instruction.
442
443 <p>The Parser will invoke this method once for each processing
444 instruction found: note that processing instructions may occur before
445 or after the main document element.</p>
446
447 <p>A SAX parser should never report an XML declaration (XML 1.0,
448 section 2.8) or a text declaration (XML 1.0, section 4.3.1) using this
449 method.</p>
450
451 <p><var>pi</var> is a hash with these properties:</p>
452
453 <blockquote>
454 <table>
455 <tr><td><b><tt>Target</tt></b></td>
456 <td>The processing instruction target.</td></tr>
457 <tr><td><b><tt>Data</tt></b></td>
458 <td>The processing instruction data, or null if none was
459 supplied.</td></tr>
460 </table>
461 </blockquote></dd>
462 </dl></p>
463
464 <p>
465 <dl><dt><b><tt class='function'>skipped_entity</tt></b>(<var>entity</var>)</dt>
466 <dd>
467 Receive notification of a skipped entity.
468
469 <p>The Parser will invoke this method once for each entity skipped.
470 Non-validating processors may skip entities if they have not seen the
471 declarations (because, for example, the entity was declared in an
472 external DTD subset). All processors may skip external entities,
473 depending on the values of the
474 "<tt>http://xml.org/sax/features/external-general-entities</tt>" and the
475 "<tt>http://xml.org/sax/features/external-parameter-entities</tt>"
476 Features.</p>
477
478 <p><var>entity</var> is a hash with these properties:</p>
479
480 <blockquote>
481 <table>
482 <tr><td><b><tt>Name</tt></b></td>
483 <td>The name of the skipped entity. If it is a parameter
484 entity, the name will begin with '<tt>%</tt>'.</td></tr>
485 </table>
486 </blockquote></dd>
487 </dl></p>
488
489 <h3>Declaration Events</h3>
490
491 <p>This is an optional extension handler for SAX2 to provide
492 information about DTD declarations in an XML document. XML readers are
493 not required to support this handler.</p>
494
495 <p>Note that data-related DTD declarations (unparsed entities and
496 notations) are already reported through the DTDHandler interface.</p>
497
498 <p>If you are using the declaration handler together with a lexical
499 handler, all of the events will occur between the <tt>start_dtd</tt>
500 and the <tt>end_dtd</tt> events.</p>
501
502 <p>To set a seperate DeclHandler for an XML reader, set the
503 "<tt>http://xml.org/sax/handlers/DeclHandler</tt>" Feature with the
504 object to received declaration events. If the reader does not support
505 declaration events, it will throw a <tt>XML::SAX::Exception::NotRecognized</tt>
506 or a <tt>XML::SAX::Exception::NotSupported</tt> when you attempt to register
507 the handler. Declaration event handlers on the default handler are
508 automatically recognized and used.</p>
509
510
511 <p>
512 <dl><dt><b><tt class='function'>element_decl</tt></b>(<var>element</var>)</dt>
513 <dd>
514 Report an element type declaration.
515
516 <p>The content model will consist of the string "EMPTY", the string
517 "ANY", or a parenthesised group, optionally followed by an occurrence
518 indicator. The model will be normalized so that all whitespace is
519 removed, and will include the enclosing parentheses.</p>
520
521 <p><var>element</var> is a hash with these properties:</p>
522
523 <blockquote>
524 <table>
525 <tr><td><b><tt>Name</tt></b></td>
526 <td>The element type name.</td></tr>
527 <tr><td><b><tt>Model</tt></b></td>
528 <td>The content model as a normalized string.</td></tr>
529 </table>
530 </blockquote></dd>
531 </dl></p>
532
533 <p>
534 <dl><dt><b><tt class='function'>attribute_decl</tt></b>(<var>attribute</var>)</dt>
535 <dd>
536 Report an attribute type declaration.
537
538 <p>Only the effective (first) declaration for an attribute will be
539 reported. The type will be one of the strings "<tt>CDATA</tt>",
540 "<tt>ID</tt>", "<tt>IDREF</tt>", "<tt>IDREFS</tt>",
541 "<tt>NMTOKEN</tt>", "<tt>NMTOKENS</tt>", "<tt>ENTITY</tt>",
542 "<tt>ENTITIES</tt>", or "<tt>NOTATION</tt>", or a parenthesized token
543 group with the separator "<tt>|</tt>" and all whitespace removed.</p>
544
545 <p><var>attribute</var> is a hash with these properties:</p>
546
547 <blockquote>
548 <table>
549 <tr><td><b><tt>eName</tt></b></td>
550 <td>The name of the associated element.</td></tr>
551 <tr><td><b><tt>aName</tt></b></td>
552 <td>The name of the attribute.</td></tr>
553 <tr><td><b><tt>Type</tt></b></td>
554 <td>A string representing the attribute type.</td></tr>
555 <tr><td><b><tt>ValueDefault</tt></b></td>
556 <td>A string representing the attribute default ("<tt>#IMPLIED</tt>",
557 "<tt>#REQUIRED</tt>", or "<tt>#FIXED</tt>") or undef if none of these
558 applies.</td></tr>
559 <tr><td><b><tt>Value</tt></b></td>
560 <td>A string representing the attribute's default value, or null if
561 there is none.</td></tr>
562 </table>
563 </blockquote></dd>
564 </dl></p>
565
566 <p>
567 <dl><dt><b><tt class='function'>internal_entity_decl</tt></b>(<var>entity</var>)</dt>
568 <dd>
569 Report an internal entity declaration.
570
571 <p>Only the effective (first) declaration for each entity will be
572 reported.</p>
573
574 <p><var>entity</var> is a hash with these properties:</p>
575
576 <blockquote>
577 <table>
578 <tr><td><b><tt>Name</tt></b></td>
579 <td>The name of the entity. If it is a parameter entity, the name will
580 begin with '%'.</td></tr>
581 <tr><td><b><tt>Value</tt></b></td>
582 <td>The replacement text of the entity.</td></tr>
583 </table>
584 </blockquote></dd>
585 </dl></p>
586
587 <p>
588 <dl><dt><b><tt class='function'>external_entity_decl</tt></b>(<var>entity</var>)</dt>
589 <dd>
590 Report a parsed external entity declaration.
591
592 <p>Only the effective (first) declaration for each entity will be
593 reported.</p>
594
595 <p><var>entity</var> is a hash with these properties:</p>
596
597 <blockquote>
598 <table>
599 <tr><td><b><tt>Name</tt></b></td>
600 <td>The name of the entity. If it is a parameter entity, the name will
601 begin with '%'.</td></tr>
602 <tr><td><b><tt>PublicId</tt></b></td>
603 <td>The public identifier of the entity, or <tt>undef</tt> if none was
604 declared.</td></tr>
605 <tr><td><b><tt>SystemId</tt></b></td>
606 <td>The system identifier of the entity.</td></tr>
607 </table>
608 </blockquote></dd>
609 </dl></p>
610
611 <h3>DTD Events</h3>
612
613 <p>If a SAX application needs information about notations and unparsed
614 entities, then the application implements this interface. The parser
615 uses the instance to report notation and unparsed entity declarations
616 to the application.</p>
617
618 <p>The SAX parser may report these events in any order, regardless of
619 the order in which the notations and unparsed entities were declared;
620 however, all DTD events must be reported after the document handler's
621 <tt>start_document()</tt> event, and before the first
622 <tt>start_element()</tt> event.</p>
623
624 <p>It is up to the application to store the information for future use
625 (perhaps in a hash table or object tree). If the application
626 encounters attributes of type "<tt>NOTATION</tt>", "<tt>ENTITY</tt>",
627 or "<tt>ENTITIES</tt>", it can use the information that it obtained
628 through this interface to find the entity and/or notation
629 corresponding with the attribute value.</p>
630
631 <p>
632 <dl><dt><b><tt class='function'>notation_decl</tt></b>(<var>notation</var>)</dt>
633 <dd>
634 Receive notification of a notation declaration event.
635
636 <p>It is up to the application to record the notation for later
637 reference, if necessary.</p>
638
639 <p>If a system identifier is present, and it is a URL, the SAX parser
640 must resolve it fully before passing it to the application.</p>
641
642 <p><var>notation</var> is a hash with these properties:</p>
643
644 <blockquote>
645 <table>
646 <tr><td><b><tt>Name</tt></b></td>
647 <td>The notation name.</td></tr>
648 <tr><td><b><tt>PublicId</tt></b></td>
649 <td>The public identifier of the entity, or <tt>undef</tt> if none was
650 declared.</td></tr>
651 <tr><td><b><tt>SystemId</tt></b></td>
652 <td>The system identifier of the entity, or <tt>undef</tt> if none was
653 declared.</td></tr>
654 </table>
655 </blockquote></dd>
656 </dl></p>
657
658 <p>
659 <dl><dt><b><tt class='function'>unparsed_entity_decl</tt></b>(<var>entity</var>)</dt>
660 <dd>
661 Receive notification of an unparsed entity declaration event.
662
663 <p>Note that the notation name corresponds to a notation reported by
664 the <tt>notation_decl()</tt> event. It is up to the application to
665 record the entity for later reference, if necessary.</p>
666
667 <p>If the system identifier is a URL, the parser must resolve it fully
668 before passing it to the application.</p>
669
670 <p><var>entity</var> is a hash with these properties:</p>
671
672 <blockquote>
673 <table>
674 <tr><td><b><tt>Name</tt></b></td>
675 <td>The unparsed entity's name.</td></tr>
676 <tr><td><b><tt>PublicId</tt></b></td>
677 <td>The public identifier of the entity, or <tt>undef</tt> if none was
678 declared.</td></tr>
679 <tr><td><b><tt>SystemId</tt></b></td>
680 <td>The system identifier of the entity.</td></tr>
681 <tr><td><b><tt>Notation</tt></b></td>
682 <td>The name of the associated notation.</td></tr>
683 </table>
684 </blockquote></dd>
685 </dl></p>
686
687 <h3>Entity Resolver</h3>
688
689 <p>If a SAX application needs to implement customized handling for
690 external entities, it must implement this interface.</p>
691
692 <p>The parser will then allow the application to intercept any
693 external entities (including the external DTD subset and external
694 parameter entities, if any) before including them.</p>
695
696 <p>
697 Many SAX applications will not need to implement this interface,
698 but it will be especially useful for applications that build XML
699 documents from databases or other specialised input sources, or for
700 applications that use URI types that are either not URLs, or that
701 have schemes unknown to the parser.
702 </p>
703
704 <p>
705 <dl><dt><b><tt class='function'>resolve_entity</tt></b>(<var>entity</var>)</dt>
706 <dd>
707 Allow the application to resolve external entities.
708
709 <p>The Parser will call this method before opening any external entity
710 except the top-level document entity (including the external DTD
711 subset, external entities referenced within the DTD, and external
712 entities referenced within the document element): the application may
713 request that the parser resolve the entity itself, that it use an
714 alternative URI, or that it use an entirely different input
715 source.</p>
716
717 <p>Application writers can use this method to redirect external system
718 identifiers to secure and/or local URIs, to look up public identifiers
719 in a catalogue, or to read an entity from a database or other input
720 source (including, for example, a dialog box).</p>
721
722 <p>If the system identifier is a URL, the SAX parser must resolve it
723 fully before reporting it to the application.</p>
724
725 <p><var>entity</var> is a hash with these properties:</p>
726
727 <blockquote>
728 <table>
729 <tr><td><b><tt>PublicId</tt></b></td>
730 <td>The public identifier of the entity being referenced, or
731 <tt>undef</tt> if none was declared.</td></tr>
732 <tr><td><b><tt>SystemId</tt></b></td>
733 <td>The system identifier of the entity being referenced.</td></tr>
734 </table>
735 </blockquote></dd>
736 </dl></p>
737
738 <h3>Error Events</h3>
739
740 <p>If a SAX application needs to implement customized error handling,
741 it must implement this interface. The parser will then report all
742 errors and warnings through this interface.</p>
743
744 <p>The parser shall use this interface to report errors instead or in
745 addition to throwing an exception: for errors and warnings the recommended
746 approach is to leave the application throw its own exceptions and to not
747 throw them in the parser. For fatal errors however, it is not uncommon that
748 the parser will throw an exception after having reported the error as it
749 renders any continuation of parsing impossible.
750 </p>
751
752 <p>All error handlers receive a hash, <var>exception</var>, with the
753 properties defined in <a
754 href="sax-2.0.html#Exceptions">Exceptions</a>.</p>
755
756 <p>
757 <dl><dt><b><tt class='function'>warning</tt></b>(<var>exception</var>)</dt>
758 <dd>
759 Receive notification of a warning.
760
761 <p>SAX parsers will use this method to report conditions that are not
762 errors or fatal errors as defined by the XML 1.0 recommendation. The
763 default behaviour is to take no action.</p>
764
765 The SAX parser must continue to provide normal parsing events after
766 invoking this method: it should still be possible for the application
767 to process the document through to the end.</dd></dl></p>
768
769 <p>
770 <dl><dt><b><tt class='function'>error</tt></b>(<var>exception</var>)</dt>
771 <dd>
772 Receive notification of a recoverable error.
773
774 <p>This corresponds to the definition of "error" in section 1.2 of the
775 W3C XML 1.0 Recommendation. For example, a validating parser would use
776 this callback to report the violation of a validity constraint. The
777 default behaviour is to take no action.</p>
778
779 The SAX parser must continue to provide normal parsing events after
780 invoking this method: it should still be possible for the application
781 to process the document through to the end. If the application cannot
782 do so, then the parser should report a fatal error even if the XML 1.0
783 recommendation does not require it to do so.</dd></dl></p>
784
785 <p>
786 <dl><dt><b><tt class='function'>fatal_error</tt></b>(<var>exception</var>)</dt>
787 <dd>
788 Receive notification of a non-recoverable error.
789
790 <p>This corresponds to the definition of "fatal error" in section 1.2
791 of the W3C XML 1.0 Recommendation. For example, a parser would use
792 this callback to report the violation of a well-formedness
793 constraint.</p>
794
795 The application must assume that the document is unusable after the
796 parser has invoked this method, and should continue (if at all) only
797 for the sake of collecting addition error messages: in fact, SAX
798 parsers are free to stop reporting any other events once this method
799 has been invoked.</dd></dl></p>
800
801 <h3>Lexical Events</h3>
802
803 <p>This is an optional extension handler for SAX2 to provide lexical
804 information about an XML document, such as comments and CDATA section
805 boundaries; XML readers are not required to support this handler.</p>
806
807 <p>The events in the lexical handler apply to the entire document, not
808 just to the document element, and all lexical handler events must
809 appear between the content handler's <tt>start_document()</tt> and
810 <tt>end_document()</tt> events.</p>
811
812 <p>To set the LexicalHandler for an XML reader, set the Feature
813 "<tt>http://xml.org/sax/handlers/LexicalHandler</tt>" on the parser to
814 the object to receive lexical events. If the reader does not support
815 lexical events, it will throw a <tt>XML::SAX::Exception::NotRecognized</tt> or
816 a <tt>XML::SAX::Exception::NotSupported</tt> when you attempt to register the
817 handler.</p>
818
819 <p>
820 <dl><dt><b><tt class='function'>start_dtd</tt></b>(<var>dtd</var>)</dt>
821 <dd>
822 Report the start of DTD declarations, if any.
823
824 <p>Any declarations are assumed to be in the internal subset unless
825 otherwise indicated by a start_entity event.</p>
826
827 <p>Note that the <tt>start</tt>/<tt>end_dtd()</tt> events will appear
828 within the <tt>start</tt>/<tt>end_document()</tt> events from Content
829 Handler and before the first <tt>start_element()</tt> event.</p>
830
831 <p><var>dtd</var> is a hash with these properties:</p>
832
833 <blockquote>
834 <table>
835 <tr><td><b><tt>Name</tt></b></td>
836 <td>The document type name.</td></tr>
837 <tr><td><b><tt>PublicId</tt></b></td>
838 <td>The declared public identifier for the external DTD subset, or
839 <tt>undef</tt> if none was declared.</td></tr>
840 <tr><td><b><tt>SystemId</tt></b></td>
841 <td>The declared system identifier for the external DTD subset, or
842 <tt>undef</tt> if none was declared.</td></tr>
843 </table>
844 </blockquote></dd>
845 </dl></p>
846
847 <p>
848 <dl><dt><b><tt class='function'>end_dtd</tt></b>(<var>dtd</var>)</dt>
849 <dd>
850 Report the end of DTD declarations.
851
852 <p>No properties are defined for this event (<var>dtd</var> is
853 empty).</p></dd></dl></p>
854
855 <p>
856 <dl><dt><b><tt class='function'>start_entity</tt></b>(<var>entity</var>)</dt>
857 <dd>
858 Report the beginning of an entity in content.
859
860 <p><b>NOTE</b>: entity references in attribute values -- and the start
861 and end of the document entity -- are never reported.</p>
862
863 <p>The start and end of the external DTD subset are reported using the
864 pseudo-name "[dtd]". All other events must be properly nested within
865 start/end entity events.</p>
866
867 <p>Note that skipped entities will be reported through the
868 <tt>skipped_entity()</tt> event, which is part of the ContentHandler
869 interface.</p>
870
871 <p><var>entity</var> is a hash with these properties:</p>
872
873 <blockquote>
874 <table>
875 <tr><td><b><tt>Name</tt></b></td>
876 <td>The name of the entity. If it is a parameter entity, the
877 name will begin with '%'.</td></tr>
878 </table>
879 </blockquote></dd>
880 </dl></p>
881
882 <p>
883 <dl><dt><b><tt class='function'>end_entity</tt></b>(<var>entity</var>)</dt>
884 <dd>
885 Report the end of an entity.
886
887 <p><var>entity</var> is a hash with these properties:</p>
888
889 <blockquote>
890 <table>
891 <tr><td><b><tt>Name</tt></b></td>
892 <td>The name of the entity that is ending.</td></tr>
893 </table>
894 </blockquote></dd>
895 </dl></p>
896
897 <p>
898 <dl><dt><b><tt class='function'>start_cdata</tt></b>(<var>cdata</var>)</dt>
899 <dd>
900 Report the start of a CDATA section.
901
902 <p>The contents of the CDATA section will be reported through the
903 regular characters event.</p>
904
905 <p>No properties are defined for this event (<var>cdata</var> is
906 empty).</p></dd></dl></p>
907
908 <p>
909 <dl><dt><b><tt class='function'>end_cdata</tt></b>(<var>cdata</var>)</dt>
910 <dd>
911 Report the end of a CDATA section.
912
913 <p>No properties are defined for this event (<var>cdata</var> is
914 empty).</p></dd></dl></p>
915
916 <p>
917 <dl><dt><b><tt class='function'>comment</tt></b>(<var>comment</var>)</dt>
918 <dd>
919 Report an XML comment anywhere in the document.
920
921 <p>This callback will be used for comments inside or outside the
922 document element, including comments in the external DTD subset (if
923 read).</p>
924
925 <p><var>comment</var> is a hash with these properties:</p>
926
927 <blockquote>
928 <table>
929 <tr><td><b><tt>Data</tt></b></td>
930 <td>The comment characters.</td></tr>
931 </table>
932 </blockquote></dd>
933 </dl></p>
934
935 <h2><a name="Filters">SAX Filters</a></h2>
936
937 <p>An XML filter is like an XML event generator, except that it
938 obtains its events from another XML event generator rather than a
939 primary source like an XML document or database. Filters can modify a
940 stream of events as they pass on to the final application.</p>
941
942 <p>
943 <dl><dt><b><tt>Parent</tt></b></dt>
944 <dd>
945 The parent reader.
946
947 <p>This Feature allows the application to link the filter to a parent
948 event generator (which may be another filter).</p></dd></dl></p>
949
950 <p>
951 See the XML::SAX::Base module for more on filters. It is meant to be
952 used as a base class for filters and drivers, and makes them much
953 easier to implement.
954 </p>
955
956 <h2><a name="Java">Java Compatibility</a></h2>
957
958 The Perl SAX 2.0 binding differs from the Java binding in these ways:
959
960 <ul>
961
962 <li>Takes parameters to <tt>new()</tt>, to <tt>parse()</tt>, and to be
963 set directly in the object, instead of requiring set/get calls (see
964 below).</li>
965
966 <li>Allows a default <tt>Handler</tt> parameter to be used for all
967 handlers.</li>
968
969 <li>
970 No base classes are enforced. Instead, parsers dynamically
971 check the handlers for what methods they support. Note however that
972 using XML::SAX::Base as your base class for Drivers and Filters will
973 make your code a lot simpler, less error prone, and probably much more
974 correct with regard to this spec. Only reimplement that functionality
975 if you really need to.
976 </li>
977
978 <li>The Attribute, InputSource, and SAXException (XML::SAX::Exception)
979 classes are only described as hashes (see below).</li>
980
981 <li>Handlers are passed a hash (Node) containing properties as an
982 argument instead of positional arguments.</li>
983
984 <li><tt>parse()</tt> methods return the value returned by calling the
985 <tt>end_document()</tt> handler.</li>
986
987 <li>
988 Method names have been converted to lower-case with underscores.
989 Parameters are all mixed case with initial upper-case.
990 </li>
991 </ul>
992
993 <p>
994 If compatibility is a problem for you consider writing a Filter that
995 converts from this style to the one you want. It is likely that such
996 a Filter will be available from CPAN in the not distant future.
997 </p>
998
999 <!--
1000 <p>[FIXME: need to list package/class name equivalents for all
1001 hashes.]</p>
1002 -->
1003
1004 </body>
1005 </html>