Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 1 | """ |
| 2 | This module contains the core classes of version 2.0 of SAX for Python. |
| 3 | This file provides only default classes with absolutely minimum |
| 4 | functionality, from which drivers and applications can be subclassed. |
| 5 | |
| 6 | Many of these classes are empty and are included only as documentation |
| 7 | of the interfaces. |
| 8 | |
| 9 | $Id$ |
| 10 | """ |
| 11 | |
| 12 | version = '2.0beta' |
Lars Gustäbel | b4d6bb09 | 2000-09-21 08:18:55 +0000 | [diff] [blame] | 13 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 14 | #============================================================================ |
| 15 | # |
| 16 | # HANDLER INTERFACES |
| 17 | # |
| 18 | #============================================================================ |
Lars Gustäbel | b4d6bb09 | 2000-09-21 08:18:55 +0000 | [diff] [blame] | 19 | |
Lars Gustäbel | e292a24 | 2000-09-24 20:19:45 +0000 | [diff] [blame] | 20 | # ===== ERRORHANDLER ===== |
Lars Gustäbel | b4d6bb09 | 2000-09-21 08:18:55 +0000 | [diff] [blame] | 21 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 22 | class ErrorHandler: |
Lars Gustäbel | bb75713 | 2000-09-24 20:38:18 +0000 | [diff] [blame] | 23 | """Basic interface for SAX error handlers. |
| 24 | |
| 25 | If you create an object that implements this interface, then |
| 26 | register the object with your XMLReader, the parser will call the |
| 27 | methods in your object to report all warnings and errors. There |
| 28 | are three levels of errors available: warnings, (possibly) |
| 29 | recoverable errors, and unrecoverable errors. All methods take a |
| 30 | SAXParseException as the only parameter.""" |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 31 | |
| 32 | def error(self, exception): |
Skip Montanaro | a2dccfb | 2000-07-06 02:55:41 +0000 | [diff] [blame] | 33 | "Handle a recoverable error." |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 34 | raise exception |
| 35 | |
| 36 | def fatalError(self, exception): |
Skip Montanaro | a2dccfb | 2000-07-06 02:55:41 +0000 | [diff] [blame] | 37 | "Handle a non-recoverable error." |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 38 | raise exception |
| 39 | |
| 40 | def warning(self, exception): |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 41 | "Handle a warning." |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 42 | print exception |
| 43 | |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 44 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 45 | # ===== CONTENTHANDLER ===== |
| 46 | |
| 47 | class ContentHandler: |
| 48 | """Interface for receiving logical document content events. |
| 49 | |
| 50 | This is the main callback interface in SAX, and the one most |
| 51 | important to applications. The order of events in this interface |
| 52 | mirrors the order of the information in the document.""" |
| 53 | |
| 54 | def __init__(self): |
| 55 | self._locator = None |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 56 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 57 | def setDocumentLocator(self, locator): |
| 58 | """Called by the parser to give the application a locator for |
| 59 | locating the origin of document events. |
| 60 | |
| 61 | SAX parsers are strongly encouraged (though not absolutely |
| 62 | required) to supply a locator: if it does so, it must supply |
| 63 | the locator to the application by invoking this method before |
| 64 | invoking any of the other methods in the DocumentHandler |
| 65 | interface. |
| 66 | |
| 67 | The locator allows the application to determine the end |
| 68 | position of any document-related event, even if the parser is |
| 69 | not reporting an error. Typically, the application will use |
| 70 | this information for reporting its own errors (such as |
| 71 | character content that does not match an application's |
| 72 | business rules). The information returned by the locator is |
| 73 | probably not sufficient for use with a search engine. |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 74 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 75 | Note that the locator will return correct information only |
| 76 | during the invocation of the events in this interface. The |
| 77 | application should not attempt to use it at any other time.""" |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 78 | self._locator = locator |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 79 | |
| 80 | def startDocument(self): |
| 81 | """Receive notification of the beginning of a document. |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 82 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 83 | The SAX parser will invoke this method only once, before any |
| 84 | other methods in this interface or in DTDHandler (except for |
| 85 | setDocumentLocator).""" |
| 86 | |
| 87 | def endDocument(self): |
| 88 | """Receive notification of the end of a document. |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 89 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 90 | The SAX parser will invoke this method only once, and it will |
| 91 | be the last method invoked during the parse. The parser shall |
| 92 | not invoke this method until it has either abandoned parsing |
| 93 | (because of an unrecoverable error) or reached the end of |
| 94 | input.""" |
| 95 | |
| 96 | def startPrefixMapping(self, prefix, uri): |
| 97 | """Begin the scope of a prefix-URI Namespace mapping. |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 98 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 99 | The information from this event is not necessary for normal |
| 100 | Namespace processing: the SAX XML reader will automatically |
| 101 | replace prefixes for element and attribute names when the |
| 102 | http://xml.org/sax/features/namespaces feature is true (the |
| 103 | default). |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 104 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 105 | There are cases, however, when applications need to use |
| 106 | prefixes in character data or in attribute values, where they |
| 107 | cannot safely be expanded automatically; the |
| 108 | start/endPrefixMapping event supplies the information to the |
| 109 | application to expand prefixes in those contexts itself, if |
| 110 | necessary. |
| 111 | |
| 112 | Note that start/endPrefixMapping events are not guaranteed to |
| 113 | be properly nested relative to each-other: all |
| 114 | startPrefixMapping events will occur before the corresponding |
| 115 | startElement event, and all endPrefixMapping events will occur |
| 116 | after the corresponding endElement event, but their order is |
| 117 | not guaranteed.""" |
| 118 | |
| 119 | def endPrefixMapping(self, prefix): |
| 120 | """End the scope of a prefix-URI mapping. |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 121 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 122 | See startPrefixMapping for details. This event will always |
| 123 | occur after the corresponding endElement event, but the order |
| 124 | of endPrefixMapping events is not otherwise guaranteed.""" |
| 125 | |
| 126 | def startElement(self, name, attrs): |
Lars Gustäbel | b4d6bb09 | 2000-09-21 08:18:55 +0000 | [diff] [blame] | 127 | """Signals the start of an element in non-namespace mode. |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 128 | |
Lars Gustäbel | b4d6bb09 | 2000-09-21 08:18:55 +0000 | [diff] [blame] | 129 | The name parameter contains the raw XML 1.0 name of the |
| 130 | element type as a string and the attrs parameter holds an |
| 131 | instance of the Attributes class containing the attributes of |
| 132 | the element.""" |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 133 | |
Lars Gustäbel | b4d6bb09 | 2000-09-21 08:18:55 +0000 | [diff] [blame] | 134 | def endElement(self, name): |
| 135 | """Signals the end of an element in non-namespace mode. |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 136 | |
| 137 | The name parameter contains the name of the element type, just |
| 138 | as with the startElement event.""" |
| 139 | |
Lars Gustäbel | b4d6bb09 | 2000-09-21 08:18:55 +0000 | [diff] [blame] | 140 | def startElementNS(self, name, qname, attrs): |
| 141 | """Signals the start of an element in namespace mode. |
| 142 | |
| 143 | The name parameter contains the name of the element type as a |
| 144 | (uri, localname) tuple, the qname parameter the raw XML 1.0 |
| 145 | name used in the source document, and the attrs parameter |
| 146 | holds an instance of the Attributes class containing the |
Martin v. Löwis | 2ebfd09 | 2002-06-30 07:38:50 +0000 | [diff] [blame] | 147 | attributes of the element. |
| 148 | |
| 149 | The uri part of the name tuple is None for elements which have |
| 150 | no namespace.""" |
Lars Gustäbel | b4d6bb09 | 2000-09-21 08:18:55 +0000 | [diff] [blame] | 151 | |
| 152 | def endElementNS(self, name, qname): |
| 153 | """Signals the end of an element in namespace mode. |
| 154 | |
| 155 | The name parameter contains the name of the element type, just |
| 156 | as with the startElementNS event.""" |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 157 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 158 | def characters(self, content): |
| 159 | """Receive notification of character data. |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 160 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 161 | The Parser will call this method to report each chunk of |
| 162 | character data. SAX parsers may return all contiguous |
| 163 | character data in a single chunk, or they may split it into |
| 164 | several chunks; however, all of the characters in any single |
| 165 | event must come from the same external entity so that the |
| 166 | Locator provides useful information.""" |
| 167 | |
Lars Gustäbel | 358f4da | 2000-09-24 11:06:27 +0000 | [diff] [blame] | 168 | def ignorableWhitespace(self, whitespace): |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 169 | """Receive notification of ignorable whitespace in element content. |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 170 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 171 | Validating Parsers must use this method to report each chunk |
| 172 | of ignorable whitespace (see the W3C XML 1.0 recommendation, |
| 173 | section 2.10): non-validating parsers may also use this method |
| 174 | if they are capable of parsing and using content models. |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 175 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 176 | SAX parsers may return all contiguous whitespace in a single |
| 177 | chunk, or they may split it into several chunks; however, all |
| 178 | of the characters in any single event must come from the same |
| 179 | external entity, so that the Locator provides useful |
| 180 | information. |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 181 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 182 | The application must not attempt to read from the array |
| 183 | outside of the specified range.""" |
| 184 | |
| 185 | def processingInstruction(self, target, data): |
| 186 | """Receive notification of a processing instruction. |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 187 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 188 | The Parser will invoke this method once for each processing |
| 189 | instruction found: note that processing instructions may occur |
| 190 | before or after the main document element. |
| 191 | |
| 192 | A SAX parser should never report an XML declaration (XML 1.0, |
| 193 | section 2.8) or a text declaration (XML 1.0, section 4.3.1) |
| 194 | using this method.""" |
| 195 | |
| 196 | def skippedEntity(self, name): |
| 197 | """Receive notification of a skipped entity. |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 198 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 199 | The Parser will invoke this method once for each entity |
| 200 | skipped. Non-validating processors may skip entities if they |
| 201 | have not seen the declarations (because, for example, the |
| 202 | entity was declared in an external DTD subset). All processors |
| 203 | may skip external entities, depending on the values of the |
| 204 | http://xml.org/sax/features/external-general-entities and the |
| 205 | http://xml.org/sax/features/external-parameter-entities |
| 206 | properties.""" |
Lars Gustäbel | e292a24 | 2000-09-24 20:19:45 +0000 | [diff] [blame] | 207 | |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 208 | |
Lars Gustäbel | e292a24 | 2000-09-24 20:19:45 +0000 | [diff] [blame] | 209 | # ===== DTDHandler ===== |
| 210 | |
| 211 | class DTDHandler: |
| 212 | """Handle DTD events. |
| 213 | |
| 214 | This interface specifies only those DTD events required for basic |
| 215 | parsing (unparsed entities and attributes).""" |
| 216 | |
| 217 | def notationDecl(self, name, publicId, systemId): |
Thomas Wouters | 84da8aa | 2000-09-25 00:11:37 +0000 | [diff] [blame] | 218 | "Handle a notation declaration event." |
Lars Gustäbel | e292a24 | 2000-09-24 20:19:45 +0000 | [diff] [blame] | 219 | |
| 220 | def unparsedEntityDecl(self, name, publicId, systemId, ndata): |
Thomas Wouters | 84da8aa | 2000-09-25 00:11:37 +0000 | [diff] [blame] | 221 | "Handle an unparsed entity declaration event." |
Lars Gustäbel | e292a24 | 2000-09-24 20:19:45 +0000 | [diff] [blame] | 222 | |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 223 | |
Lars Gustäbel | e292a24 | 2000-09-24 20:19:45 +0000 | [diff] [blame] | 224 | # ===== ENTITYRESOLVER ===== |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 225 | |
Lars Gustäbel | e292a24 | 2000-09-24 20:19:45 +0000 | [diff] [blame] | 226 | class EntityResolver: |
| 227 | """Basic interface for resolving entities. If you create an object |
| 228 | implementing this interface, then register the object with your |
| 229 | Parser, the parser will call the method in your object to |
| 230 | resolve all external entities. Note that DefaultHandler implements |
| 231 | this interface with the default behaviour.""" |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 232 | |
Lars Gustäbel | e292a24 | 2000-09-24 20:19:45 +0000 | [diff] [blame] | 233 | def resolveEntity(self, publicId, systemId): |
Thomas Wouters | 84da8aa | 2000-09-25 00:11:37 +0000 | [diff] [blame] | 234 | """Resolve the system identifier of an entity and return either |
Lars Gustäbel | e292a24 | 2000-09-24 20:19:45 +0000 | [diff] [blame] | 235 | the system identifier to read from as a string, or an InputSource |
| 236 | to read from.""" |
Thomas Wouters | 84da8aa | 2000-09-25 00:11:37 +0000 | [diff] [blame] | 237 | return systemId |
Lars Gustäbel | e292a24 | 2000-09-24 20:19:45 +0000 | [diff] [blame] | 238 | |
| 239 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 240 | #============================================================================ |
| 241 | # |
| 242 | # CORE FEATURES |
| 243 | # |
| 244 | #============================================================================ |
| 245 | |
| 246 | feature_namespaces = "http://xml.org/sax/features/namespaces" |
| 247 | # true: Perform Namespace processing (default). |
| 248 | # false: Optionally do not perform Namespace processing |
| 249 | # (implies namespace-prefixes). |
| 250 | # access: (parsing) read-only; (not parsing) read/write |
| 251 | |
| 252 | feature_namespace_prefixes = "http://xml.org/sax/features/namespace-prefixes" |
| 253 | # true: Report the original prefixed names and attributes used for Namespace |
| 254 | # declarations. |
| 255 | # false: Do not report attributes used for Namespace declarations, and |
| 256 | # optionally do not report original prefixed names (default). |
| 257 | # access: (parsing) read-only; (not parsing) read/write |
| 258 | |
| 259 | feature_string_interning = "http://xml.org/sax/features/string-interning" |
| 260 | # true: All element names, prefixes, attribute names, Namespace URIs, and |
| 261 | # local names are interned using the built-in intern function. |
| 262 | # false: Names are not necessarily interned, although they may be (default). |
| 263 | # access: (parsing) read-only; (not parsing) read/write |
| 264 | |
| 265 | feature_validation = "http://xml.org/sax/features/validation" |
| 266 | # true: Report all validation errors (implies external-general-entities and |
| 267 | # external-parameter-entities). |
| 268 | # false: Do not report validation errors. |
| 269 | # access: (parsing) read-only; (not parsing) read/write |
| 270 | |
| 271 | feature_external_ges = "http://xml.org/sax/features/external-general-entities" |
| 272 | # true: Include all external general (text) entities. |
| 273 | # false: Do not include external general entities. |
| 274 | # access: (parsing) read-only; (not parsing) read/write |
| 275 | |
| 276 | feature_external_pes = "http://xml.org/sax/features/external-parameter-entities" |
| 277 | # true: Include all external parameter entities, including the external |
| 278 | # DTD subset. |
| 279 | # false: Do not include any external parameter entities, even the external |
| 280 | # DTD subset. |
| 281 | # access: (parsing) read-only; (not parsing) read/write |
| 282 | |
| 283 | all_features = [feature_namespaces, |
| 284 | feature_namespace_prefixes, |
| 285 | feature_string_interning, |
| 286 | feature_validation, |
| 287 | feature_external_ges, |
| 288 | feature_external_pes] |
| 289 | |
| 290 | |
| 291 | #============================================================================ |
| 292 | # |
| 293 | # CORE PROPERTIES |
| 294 | # |
| 295 | #============================================================================ |
| 296 | |
| 297 | property_lexical_handler = "http://xml.org/sax/properties/lexical-handler" |
| 298 | # data type: xml.sax.sax2lib.LexicalHandler |
| 299 | # description: An optional extension handler for lexical events like comments. |
| 300 | # access: read/write |
| 301 | |
| 302 | property_declaration_handler = "http://xml.org/sax/properties/declaration-handler" |
| 303 | # data type: xml.sax.sax2lib.DeclHandler |
| 304 | # description: An optional extension handler for DTD-related events other |
| 305 | # than notations and unparsed entities. |
| 306 | # access: read/write |
| 307 | |
| 308 | property_dom_node = "http://xml.org/sax/properties/dom-node" |
| 309 | # data type: org.w3c.dom.Node |
| 310 | # description: When parsing, the current DOM node being visited if this is |
| 311 | # a DOM iterator; when not parsing, the root DOM node for |
| 312 | # iteration. |
| 313 | # access: (parsing) read-only; (not parsing) read/write |
| 314 | |
| 315 | property_xml_string = "http://xml.org/sax/properties/xml-string" |
| 316 | # data type: String |
| 317 | # description: The literal string of characters that was the source for |
| 318 | # the current event. |
| 319 | # access: read-only |
| 320 | |
Martin v. Löwis | 2ebfd09 | 2002-06-30 07:38:50 +0000 | [diff] [blame] | 321 | property_encoding = "http://www.python.org/sax/properties/encoding" |
| 322 | # data type: String |
| 323 | # description: The name of the encoding to assume for input data. |
| 324 | # access: write: set the encoding, e.g. established by a higher-level |
| 325 | # protocol. May change during parsing (e.g. after |
| 326 | # processing a META tag) |
| 327 | # read: return the current encoding (possibly established through |
| 328 | # auto-detection. |
| 329 | # initial value: UTF-8 |
| 330 | # |
| 331 | |
| 332 | property_interning_dict = "http://www.python.org/sax/properties/interning-dict" |
| 333 | # data type: Dictionary |
| 334 | # description: The dictionary used to intern common strings in the document |
| 335 | # access: write: Request that the parser uses a specific dictionary, to |
| 336 | # allow interning across different documents |
| 337 | # read: return the current interning dictionary, or None |
| 338 | # |
| 339 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 340 | all_properties = [property_lexical_handler, |
| 341 | property_dom_node, |
| 342 | property_declaration_handler, |
Martin v. Löwis | 2ebfd09 | 2002-06-30 07:38:50 +0000 | [diff] [blame] | 343 | property_xml_string, |
| 344 | property_encoding, |
| 345 | property_interning_dict] |