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 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +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: |
| 23 | """Basic interface for SAX error handlers. If you create an object |
| 24 | that implements this interface, then register the object with your |
| 25 | Parser, the parser will call the methods in your object to report |
| 26 | all warnings and errors. There are three levels of errors |
| 27 | available: warnings, (possibly) recoverable errors, and |
| 28 | unrecoverable errors. All methods take a SAXParseException as the |
| 29 | only parameter.""" |
| 30 | |
| 31 | def error(self, exception): |
Skip Montanaro | a2dccfb | 2000-07-06 02:55:41 +0000 | [diff] [blame] | 32 | "Handle a recoverable error." |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 33 | raise exception |
| 34 | |
| 35 | def fatalError(self, exception): |
Skip Montanaro | a2dccfb | 2000-07-06 02:55:41 +0000 | [diff] [blame] | 36 | "Handle a non-recoverable error." |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 37 | raise exception |
| 38 | |
| 39 | def warning(self, exception): |
Skip Montanaro | a2dccfb | 2000-07-06 02:55:41 +0000 | [diff] [blame] | 40 | "Handle a warning." |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 41 | print exception |
| 42 | |
| 43 | # ===== CONTENTHANDLER ===== |
| 44 | |
| 45 | class ContentHandler: |
| 46 | """Interface for receiving logical document content events. |
| 47 | |
| 48 | This is the main callback interface in SAX, and the one most |
| 49 | important to applications. The order of events in this interface |
| 50 | mirrors the order of the information in the document.""" |
| 51 | |
| 52 | def __init__(self): |
| 53 | self._locator = None |
| 54 | |
| 55 | def setDocumentLocator(self, locator): |
| 56 | """Called by the parser to give the application a locator for |
| 57 | locating the origin of document events. |
| 58 | |
| 59 | SAX parsers are strongly encouraged (though not absolutely |
| 60 | required) to supply a locator: if it does so, it must supply |
| 61 | the locator to the application by invoking this method before |
| 62 | invoking any of the other methods in the DocumentHandler |
| 63 | interface. |
| 64 | |
| 65 | The locator allows the application to determine the end |
| 66 | position of any document-related event, even if the parser is |
| 67 | not reporting an error. Typically, the application will use |
| 68 | this information for reporting its own errors (such as |
| 69 | character content that does not match an application's |
| 70 | business rules). The information returned by the locator is |
| 71 | probably not sufficient for use with a search engine. |
| 72 | |
| 73 | Note that the locator will return correct information only |
| 74 | during the invocation of the events in this interface. The |
| 75 | application should not attempt to use it at any other time.""" |
| 76 | self._locator = locator |
| 77 | |
| 78 | def startDocument(self): |
| 79 | """Receive notification of the beginning of a document. |
| 80 | |
| 81 | The SAX parser will invoke this method only once, before any |
| 82 | other methods in this interface or in DTDHandler (except for |
| 83 | setDocumentLocator).""" |
| 84 | |
| 85 | def endDocument(self): |
| 86 | """Receive notification of the end of a document. |
| 87 | |
| 88 | The SAX parser will invoke this method only once, and it will |
| 89 | be the last method invoked during the parse. The parser shall |
| 90 | not invoke this method until it has either abandoned parsing |
| 91 | (because of an unrecoverable error) or reached the end of |
| 92 | input.""" |
| 93 | |
| 94 | def startPrefixMapping(self, prefix, uri): |
| 95 | """Begin the scope of a prefix-URI Namespace mapping. |
| 96 | |
| 97 | The information from this event is not necessary for normal |
| 98 | Namespace processing: the SAX XML reader will automatically |
| 99 | replace prefixes for element and attribute names when the |
| 100 | http://xml.org/sax/features/namespaces feature is true (the |
| 101 | default). |
| 102 | |
| 103 | There are cases, however, when applications need to use |
| 104 | prefixes in character data or in attribute values, where they |
| 105 | cannot safely be expanded automatically; the |
| 106 | start/endPrefixMapping event supplies the information to the |
| 107 | application to expand prefixes in those contexts itself, if |
| 108 | necessary. |
| 109 | |
| 110 | Note that start/endPrefixMapping events are not guaranteed to |
| 111 | be properly nested relative to each-other: all |
| 112 | startPrefixMapping events will occur before the corresponding |
| 113 | startElement event, and all endPrefixMapping events will occur |
| 114 | after the corresponding endElement event, but their order is |
| 115 | not guaranteed.""" |
| 116 | |
| 117 | def endPrefixMapping(self, prefix): |
| 118 | """End the scope of a prefix-URI mapping. |
| 119 | |
| 120 | See startPrefixMapping for details. This event will always |
| 121 | occur after the corresponding endElement event, but the order |
| 122 | of endPrefixMapping events is not otherwise guaranteed.""" |
| 123 | |
| 124 | def startElement(self, name, attrs): |
Lars Gustäbel | b4d6bb09 | 2000-09-21 08:18:55 +0000 | [diff] [blame] | 125 | """Signals the start of an element in non-namespace mode. |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 126 | |
Lars Gustäbel | b4d6bb09 | 2000-09-21 08:18:55 +0000 | [diff] [blame] | 127 | The name parameter contains the raw XML 1.0 name of the |
| 128 | element type as a string and the attrs parameter holds an |
| 129 | instance of the Attributes class containing the attributes of |
| 130 | the element.""" |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 131 | |
Lars Gustäbel | b4d6bb09 | 2000-09-21 08:18:55 +0000 | [diff] [blame] | 132 | def endElement(self, name): |
| 133 | """Signals the end of an element in non-namespace mode. |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 134 | |
| 135 | The name parameter contains the name of the element type, just |
| 136 | as with the startElement event.""" |
| 137 | |
Lars Gustäbel | b4d6bb09 | 2000-09-21 08:18:55 +0000 | [diff] [blame] | 138 | def startElementNS(self, name, qname, attrs): |
| 139 | """Signals the start of an element in namespace mode. |
| 140 | |
| 141 | The name parameter contains the name of the element type as a |
| 142 | (uri, localname) tuple, the qname parameter the raw XML 1.0 |
| 143 | name used in the source document, and the attrs parameter |
| 144 | holds an instance of the Attributes class containing the |
| 145 | attributes of the element.""" |
| 146 | |
| 147 | def endElementNS(self, name, qname): |
| 148 | """Signals the end of an element in namespace mode. |
| 149 | |
| 150 | The name parameter contains the name of the element type, just |
| 151 | as with the startElementNS event.""" |
| 152 | |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 153 | def characters(self, content): |
| 154 | """Receive notification of character data. |
| 155 | |
| 156 | The Parser will call this method to report each chunk of |
| 157 | character data. SAX parsers may return all contiguous |
| 158 | character data in a single chunk, or they may split it into |
| 159 | several chunks; however, all of the characters in any single |
| 160 | event must come from the same external entity so that the |
| 161 | Locator provides useful information.""" |
| 162 | |
Lars Gustäbel | 358f4da | 2000-09-24 11:06:27 +0000 | [diff] [blame] | 163 | def ignorableWhitespace(self, whitespace): |
Fred Drake | 45cd9de | 2000-06-29 19:34:54 +0000 | [diff] [blame] | 164 | """Receive notification of ignorable whitespace in element content. |
| 165 | |
| 166 | Validating Parsers must use this method to report each chunk |
| 167 | of ignorable whitespace (see the W3C XML 1.0 recommendation, |
| 168 | section 2.10): non-validating parsers may also use this method |
| 169 | if they are capable of parsing and using content models. |
| 170 | |
| 171 | SAX parsers may return all contiguous whitespace in a single |
| 172 | chunk, or they may split it into several chunks; however, all |
| 173 | of the characters in any single event must come from the same |
| 174 | external entity, so that the Locator provides useful |
| 175 | information. |
| 176 | |
| 177 | The application must not attempt to read from the array |
| 178 | outside of the specified range.""" |
| 179 | |
| 180 | def processingInstruction(self, target, data): |
| 181 | """Receive notification of a processing instruction. |
| 182 | |
| 183 | The Parser will invoke this method once for each processing |
| 184 | instruction found: note that processing instructions may occur |
| 185 | before or after the main document element. |
| 186 | |
| 187 | A SAX parser should never report an XML declaration (XML 1.0, |
| 188 | section 2.8) or a text declaration (XML 1.0, section 4.3.1) |
| 189 | using this method.""" |
| 190 | |
| 191 | def skippedEntity(self, name): |
| 192 | """Receive notification of a skipped entity. |
| 193 | |
| 194 | The Parser will invoke this method once for each entity |
| 195 | skipped. Non-validating processors may skip entities if they |
| 196 | have not seen the declarations (because, for example, the |
| 197 | entity was declared in an external DTD subset). All processors |
| 198 | may skip external entities, depending on the values of the |
| 199 | http://xml.org/sax/features/external-general-entities and the |
| 200 | http://xml.org/sax/features/external-parameter-entities |
| 201 | properties.""" |
| 202 | |
| 203 | #============================================================================ |
| 204 | # |
| 205 | # CORE FEATURES |
| 206 | # |
| 207 | #============================================================================ |
| 208 | |
| 209 | feature_namespaces = "http://xml.org/sax/features/namespaces" |
| 210 | # true: Perform Namespace processing (default). |
| 211 | # false: Optionally do not perform Namespace processing |
| 212 | # (implies namespace-prefixes). |
| 213 | # access: (parsing) read-only; (not parsing) read/write |
| 214 | |
| 215 | feature_namespace_prefixes = "http://xml.org/sax/features/namespace-prefixes" |
| 216 | # true: Report the original prefixed names and attributes used for Namespace |
| 217 | # declarations. |
| 218 | # false: Do not report attributes used for Namespace declarations, and |
| 219 | # optionally do not report original prefixed names (default). |
| 220 | # access: (parsing) read-only; (not parsing) read/write |
| 221 | |
| 222 | feature_string_interning = "http://xml.org/sax/features/string-interning" |
| 223 | # true: All element names, prefixes, attribute names, Namespace URIs, and |
| 224 | # local names are interned using the built-in intern function. |
| 225 | # false: Names are not necessarily interned, although they may be (default). |
| 226 | # access: (parsing) read-only; (not parsing) read/write |
| 227 | |
| 228 | feature_validation = "http://xml.org/sax/features/validation" |
| 229 | # true: Report all validation errors (implies external-general-entities and |
| 230 | # external-parameter-entities). |
| 231 | # false: Do not report validation errors. |
| 232 | # access: (parsing) read-only; (not parsing) read/write |
| 233 | |
| 234 | feature_external_ges = "http://xml.org/sax/features/external-general-entities" |
| 235 | # true: Include all external general (text) entities. |
| 236 | # false: Do not include external general entities. |
| 237 | # access: (parsing) read-only; (not parsing) read/write |
| 238 | |
| 239 | feature_external_pes = "http://xml.org/sax/features/external-parameter-entities" |
| 240 | # true: Include all external parameter entities, including the external |
| 241 | # DTD subset. |
| 242 | # false: Do not include any external parameter entities, even the external |
| 243 | # DTD subset. |
| 244 | # access: (parsing) read-only; (not parsing) read/write |
| 245 | |
| 246 | all_features = [feature_namespaces, |
| 247 | feature_namespace_prefixes, |
| 248 | feature_string_interning, |
| 249 | feature_validation, |
| 250 | feature_external_ges, |
| 251 | feature_external_pes] |
| 252 | |
| 253 | |
| 254 | #============================================================================ |
| 255 | # |
| 256 | # CORE PROPERTIES |
| 257 | # |
| 258 | #============================================================================ |
| 259 | |
| 260 | property_lexical_handler = "http://xml.org/sax/properties/lexical-handler" |
| 261 | # data type: xml.sax.sax2lib.LexicalHandler |
| 262 | # description: An optional extension handler for lexical events like comments. |
| 263 | # access: read/write |
| 264 | |
| 265 | property_declaration_handler = "http://xml.org/sax/properties/declaration-handler" |
| 266 | # data type: xml.sax.sax2lib.DeclHandler |
| 267 | # description: An optional extension handler for DTD-related events other |
| 268 | # than notations and unparsed entities. |
| 269 | # access: read/write |
| 270 | |
| 271 | property_dom_node = "http://xml.org/sax/properties/dom-node" |
| 272 | # data type: org.w3c.dom.Node |
| 273 | # description: When parsing, the current DOM node being visited if this is |
| 274 | # a DOM iterator; when not parsing, the root DOM node for |
| 275 | # iteration. |
| 276 | # access: (parsing) read-only; (not parsing) read/write |
| 277 | |
| 278 | property_xml_string = "http://xml.org/sax/properties/xml-string" |
| 279 | # data type: String |
| 280 | # description: The literal string of characters that was the source for |
| 281 | # the current event. |
| 282 | # access: read-only |
| 283 | |
| 284 | all_properties = [property_lexical_handler, |
| 285 | property_dom_node, |
| 286 | property_declaration_handler, |
| 287 | property_xml_string] |