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