1 | n/a | """ |
---|
2 | n/a | SAX driver for the pyexpat C module. This driver works with |
---|
3 | n/a | pyexpat.__version__ == '2.22'. |
---|
4 | n/a | """ |
---|
5 | n/a | |
---|
6 | n/a | version = "0.20" |
---|
7 | n/a | |
---|
8 | n/a | from xml.sax._exceptions import * |
---|
9 | n/a | from xml.sax.handler import feature_validation, feature_namespaces |
---|
10 | n/a | from xml.sax.handler import feature_namespace_prefixes |
---|
11 | n/a | from xml.sax.handler import feature_external_ges, feature_external_pes |
---|
12 | n/a | from xml.sax.handler import feature_string_interning |
---|
13 | n/a | from xml.sax.handler import property_xml_string, property_interning_dict |
---|
14 | n/a | |
---|
15 | n/a | # xml.parsers.expat does not raise ImportError in Jython |
---|
16 | n/a | import sys |
---|
17 | n/a | if sys.platform[:4] == "java": |
---|
18 | n/a | raise SAXReaderNotAvailable("expat not available in Java", None) |
---|
19 | n/a | del sys |
---|
20 | n/a | |
---|
21 | n/a | try: |
---|
22 | n/a | from xml.parsers import expat |
---|
23 | n/a | except ImportError: |
---|
24 | n/a | raise SAXReaderNotAvailable("expat not supported", None) |
---|
25 | n/a | else: |
---|
26 | n/a | if not hasattr(expat, "ParserCreate"): |
---|
27 | n/a | raise SAXReaderNotAvailable("expat not supported", None) |
---|
28 | n/a | from xml.sax import xmlreader, saxutils, handler |
---|
29 | n/a | |
---|
30 | n/a | AttributesImpl = xmlreader.AttributesImpl |
---|
31 | n/a | AttributesNSImpl = xmlreader.AttributesNSImpl |
---|
32 | n/a | |
---|
33 | n/a | # If we're using a sufficiently recent version of Python, we can use |
---|
34 | n/a | # weak references to avoid cycles between the parser and content |
---|
35 | n/a | # handler, otherwise we'll just have to pretend. |
---|
36 | n/a | try: |
---|
37 | n/a | import _weakref |
---|
38 | n/a | except ImportError: |
---|
39 | n/a | def _mkproxy(o): |
---|
40 | n/a | return o |
---|
41 | n/a | else: |
---|
42 | n/a | import weakref |
---|
43 | n/a | _mkproxy = weakref.proxy |
---|
44 | n/a | del weakref, _weakref |
---|
45 | n/a | |
---|
46 | n/a | class _ClosedParser: |
---|
47 | n/a | pass |
---|
48 | n/a | |
---|
49 | n/a | # --- ExpatLocator |
---|
50 | n/a | |
---|
51 | n/a | class ExpatLocator(xmlreader.Locator): |
---|
52 | n/a | """Locator for use with the ExpatParser class. |
---|
53 | n/a | |
---|
54 | n/a | This uses a weak reference to the parser object to avoid creating |
---|
55 | n/a | a circular reference between the parser and the content handler. |
---|
56 | n/a | """ |
---|
57 | n/a | def __init__(self, parser): |
---|
58 | n/a | self._ref = _mkproxy(parser) |
---|
59 | n/a | |
---|
60 | n/a | def getColumnNumber(self): |
---|
61 | n/a | parser = self._ref |
---|
62 | n/a | if parser._parser is None: |
---|
63 | n/a | return None |
---|
64 | n/a | return parser._parser.ErrorColumnNumber |
---|
65 | n/a | |
---|
66 | n/a | def getLineNumber(self): |
---|
67 | n/a | parser = self._ref |
---|
68 | n/a | if parser._parser is None: |
---|
69 | n/a | return 1 |
---|
70 | n/a | return parser._parser.ErrorLineNumber |
---|
71 | n/a | |
---|
72 | n/a | def getPublicId(self): |
---|
73 | n/a | parser = self._ref |
---|
74 | n/a | if parser is None: |
---|
75 | n/a | return None |
---|
76 | n/a | return parser._source.getPublicId() |
---|
77 | n/a | |
---|
78 | n/a | def getSystemId(self): |
---|
79 | n/a | parser = self._ref |
---|
80 | n/a | if parser is None: |
---|
81 | n/a | return None |
---|
82 | n/a | return parser._source.getSystemId() |
---|
83 | n/a | |
---|
84 | n/a | |
---|
85 | n/a | # --- ExpatParser |
---|
86 | n/a | |
---|
87 | n/a | class ExpatParser(xmlreader.IncrementalParser, xmlreader.Locator): |
---|
88 | n/a | """SAX driver for the pyexpat C module.""" |
---|
89 | n/a | |
---|
90 | n/a | def __init__(self, namespaceHandling=0, bufsize=2**16-20): |
---|
91 | n/a | xmlreader.IncrementalParser.__init__(self, bufsize) |
---|
92 | n/a | self._source = xmlreader.InputSource() |
---|
93 | n/a | self._parser = None |
---|
94 | n/a | self._namespaces = namespaceHandling |
---|
95 | n/a | self._lex_handler_prop = None |
---|
96 | n/a | self._parsing = 0 |
---|
97 | n/a | self._entity_stack = [] |
---|
98 | n/a | self._external_ges = 1 |
---|
99 | n/a | self._interning = None |
---|
100 | n/a | |
---|
101 | n/a | # XMLReader methods |
---|
102 | n/a | |
---|
103 | n/a | def parse(self, source): |
---|
104 | n/a | "Parse an XML document from a URL or an InputSource." |
---|
105 | n/a | source = saxutils.prepare_input_source(source) |
---|
106 | n/a | |
---|
107 | n/a | self._source = source |
---|
108 | n/a | self.reset() |
---|
109 | n/a | self._cont_handler.setDocumentLocator(ExpatLocator(self)) |
---|
110 | n/a | xmlreader.IncrementalParser.parse(self, source) |
---|
111 | n/a | |
---|
112 | n/a | def prepareParser(self, source): |
---|
113 | n/a | if source.getSystemId() is not None: |
---|
114 | n/a | self._parser.SetBase(source.getSystemId()) |
---|
115 | n/a | |
---|
116 | n/a | # Redefined setContentHandler to allow changing handlers during parsing |
---|
117 | n/a | |
---|
118 | n/a | def setContentHandler(self, handler): |
---|
119 | n/a | xmlreader.IncrementalParser.setContentHandler(self, handler) |
---|
120 | n/a | if self._parsing: |
---|
121 | n/a | self._reset_cont_handler() |
---|
122 | n/a | |
---|
123 | n/a | def getFeature(self, name): |
---|
124 | n/a | if name == feature_namespaces: |
---|
125 | n/a | return self._namespaces |
---|
126 | n/a | elif name == feature_string_interning: |
---|
127 | n/a | return self._interning is not None |
---|
128 | n/a | elif name in (feature_validation, feature_external_pes, |
---|
129 | n/a | feature_namespace_prefixes): |
---|
130 | n/a | return 0 |
---|
131 | n/a | elif name == feature_external_ges: |
---|
132 | n/a | return self._external_ges |
---|
133 | n/a | raise SAXNotRecognizedException("Feature '%s' not recognized" % name) |
---|
134 | n/a | |
---|
135 | n/a | def setFeature(self, name, state): |
---|
136 | n/a | if self._parsing: |
---|
137 | n/a | raise SAXNotSupportedException("Cannot set features while parsing") |
---|
138 | n/a | |
---|
139 | n/a | if name == feature_namespaces: |
---|
140 | n/a | self._namespaces = state |
---|
141 | n/a | elif name == feature_external_ges: |
---|
142 | n/a | self._external_ges = state |
---|
143 | n/a | elif name == feature_string_interning: |
---|
144 | n/a | if state: |
---|
145 | n/a | if self._interning is None: |
---|
146 | n/a | self._interning = {} |
---|
147 | n/a | else: |
---|
148 | n/a | self._interning = None |
---|
149 | n/a | elif name == feature_validation: |
---|
150 | n/a | if state: |
---|
151 | n/a | raise SAXNotSupportedException( |
---|
152 | n/a | "expat does not support validation") |
---|
153 | n/a | elif name == feature_external_pes: |
---|
154 | n/a | if state: |
---|
155 | n/a | raise SAXNotSupportedException( |
---|
156 | n/a | "expat does not read external parameter entities") |
---|
157 | n/a | elif name == feature_namespace_prefixes: |
---|
158 | n/a | if state: |
---|
159 | n/a | raise SAXNotSupportedException( |
---|
160 | n/a | "expat does not report namespace prefixes") |
---|
161 | n/a | else: |
---|
162 | n/a | raise SAXNotRecognizedException( |
---|
163 | n/a | "Feature '%s' not recognized" % name) |
---|
164 | n/a | |
---|
165 | n/a | def getProperty(self, name): |
---|
166 | n/a | if name == handler.property_lexical_handler: |
---|
167 | n/a | return self._lex_handler_prop |
---|
168 | n/a | elif name == property_interning_dict: |
---|
169 | n/a | return self._interning |
---|
170 | n/a | elif name == property_xml_string: |
---|
171 | n/a | if self._parser: |
---|
172 | n/a | if hasattr(self._parser, "GetInputContext"): |
---|
173 | n/a | return self._parser.GetInputContext() |
---|
174 | n/a | else: |
---|
175 | n/a | raise SAXNotRecognizedException( |
---|
176 | n/a | "This version of expat does not support getting" |
---|
177 | n/a | " the XML string") |
---|
178 | n/a | else: |
---|
179 | n/a | raise SAXNotSupportedException( |
---|
180 | n/a | "XML string cannot be returned when not parsing") |
---|
181 | n/a | raise SAXNotRecognizedException("Property '%s' not recognized" % name) |
---|
182 | n/a | |
---|
183 | n/a | def setProperty(self, name, value): |
---|
184 | n/a | if name == handler.property_lexical_handler: |
---|
185 | n/a | self._lex_handler_prop = value |
---|
186 | n/a | if self._parsing: |
---|
187 | n/a | self._reset_lex_handler_prop() |
---|
188 | n/a | elif name == property_interning_dict: |
---|
189 | n/a | self._interning = value |
---|
190 | n/a | elif name == property_xml_string: |
---|
191 | n/a | raise SAXNotSupportedException("Property '%s' cannot be set" % |
---|
192 | n/a | name) |
---|
193 | n/a | else: |
---|
194 | n/a | raise SAXNotRecognizedException("Property '%s' not recognized" % |
---|
195 | n/a | name) |
---|
196 | n/a | |
---|
197 | n/a | # IncrementalParser methods |
---|
198 | n/a | |
---|
199 | n/a | def feed(self, data, isFinal = 0): |
---|
200 | n/a | if not self._parsing: |
---|
201 | n/a | self.reset() |
---|
202 | n/a | self._parsing = 1 |
---|
203 | n/a | self._cont_handler.startDocument() |
---|
204 | n/a | |
---|
205 | n/a | try: |
---|
206 | n/a | # The isFinal parameter is internal to the expat reader. |
---|
207 | n/a | # If it is set to true, expat will check validity of the entire |
---|
208 | n/a | # document. When feeding chunks, they are not normally final - |
---|
209 | n/a | # except when invoked from close. |
---|
210 | n/a | self._parser.Parse(data, isFinal) |
---|
211 | n/a | except expat.error as e: |
---|
212 | n/a | exc = SAXParseException(expat.ErrorString(e.code), e, self) |
---|
213 | n/a | # FIXME: when to invoke error()? |
---|
214 | n/a | self._err_handler.fatalError(exc) |
---|
215 | n/a | |
---|
216 | n/a | def close(self): |
---|
217 | n/a | if (self._entity_stack or self._parser is None or |
---|
218 | n/a | isinstance(self._parser, _ClosedParser)): |
---|
219 | n/a | # If we are completing an external entity, do nothing here |
---|
220 | n/a | return |
---|
221 | n/a | try: |
---|
222 | n/a | self.feed("", isFinal = 1) |
---|
223 | n/a | self._cont_handler.endDocument() |
---|
224 | n/a | self._parsing = 0 |
---|
225 | n/a | # break cycle created by expat handlers pointing to our methods |
---|
226 | n/a | self._parser = None |
---|
227 | n/a | finally: |
---|
228 | n/a | self._parsing = 0 |
---|
229 | n/a | if self._parser is not None: |
---|
230 | n/a | # Keep ErrorColumnNumber and ErrorLineNumber after closing. |
---|
231 | n/a | parser = _ClosedParser() |
---|
232 | n/a | parser.ErrorColumnNumber = self._parser.ErrorColumnNumber |
---|
233 | n/a | parser.ErrorLineNumber = self._parser.ErrorLineNumber |
---|
234 | n/a | self._parser = parser |
---|
235 | n/a | try: |
---|
236 | n/a | file = self._source.getCharacterStream() |
---|
237 | n/a | if file is not None: |
---|
238 | n/a | file.close() |
---|
239 | n/a | finally: |
---|
240 | n/a | file = self._source.getByteStream() |
---|
241 | n/a | if file is not None: |
---|
242 | n/a | file.close() |
---|
243 | n/a | |
---|
244 | n/a | def _reset_cont_handler(self): |
---|
245 | n/a | self._parser.ProcessingInstructionHandler = \ |
---|
246 | n/a | self._cont_handler.processingInstruction |
---|
247 | n/a | self._parser.CharacterDataHandler = self._cont_handler.characters |
---|
248 | n/a | |
---|
249 | n/a | def _reset_lex_handler_prop(self): |
---|
250 | n/a | lex = self._lex_handler_prop |
---|
251 | n/a | parser = self._parser |
---|
252 | n/a | if lex is None: |
---|
253 | n/a | parser.CommentHandler = None |
---|
254 | n/a | parser.StartCdataSectionHandler = None |
---|
255 | n/a | parser.EndCdataSectionHandler = None |
---|
256 | n/a | parser.StartDoctypeDeclHandler = None |
---|
257 | n/a | parser.EndDoctypeDeclHandler = None |
---|
258 | n/a | else: |
---|
259 | n/a | parser.CommentHandler = lex.comment |
---|
260 | n/a | parser.StartCdataSectionHandler = lex.startCDATA |
---|
261 | n/a | parser.EndCdataSectionHandler = lex.endCDATA |
---|
262 | n/a | parser.StartDoctypeDeclHandler = self.start_doctype_decl |
---|
263 | n/a | parser.EndDoctypeDeclHandler = lex.endDTD |
---|
264 | n/a | |
---|
265 | n/a | def reset(self): |
---|
266 | n/a | if self._namespaces: |
---|
267 | n/a | self._parser = expat.ParserCreate(self._source.getEncoding(), " ", |
---|
268 | n/a | intern=self._interning) |
---|
269 | n/a | self._parser.namespace_prefixes = 1 |
---|
270 | n/a | self._parser.StartElementHandler = self.start_element_ns |
---|
271 | n/a | self._parser.EndElementHandler = self.end_element_ns |
---|
272 | n/a | else: |
---|
273 | n/a | self._parser = expat.ParserCreate(self._source.getEncoding(), |
---|
274 | n/a | intern = self._interning) |
---|
275 | n/a | self._parser.StartElementHandler = self.start_element |
---|
276 | n/a | self._parser.EndElementHandler = self.end_element |
---|
277 | n/a | |
---|
278 | n/a | self._reset_cont_handler() |
---|
279 | n/a | self._parser.UnparsedEntityDeclHandler = self.unparsed_entity_decl |
---|
280 | n/a | self._parser.NotationDeclHandler = self.notation_decl |
---|
281 | n/a | self._parser.StartNamespaceDeclHandler = self.start_namespace_decl |
---|
282 | n/a | self._parser.EndNamespaceDeclHandler = self.end_namespace_decl |
---|
283 | n/a | |
---|
284 | n/a | self._decl_handler_prop = None |
---|
285 | n/a | if self._lex_handler_prop: |
---|
286 | n/a | self._reset_lex_handler_prop() |
---|
287 | n/a | # self._parser.DefaultHandler = |
---|
288 | n/a | # self._parser.DefaultHandlerExpand = |
---|
289 | n/a | # self._parser.NotStandaloneHandler = |
---|
290 | n/a | self._parser.ExternalEntityRefHandler = self.external_entity_ref |
---|
291 | n/a | try: |
---|
292 | n/a | self._parser.SkippedEntityHandler = self.skipped_entity_handler |
---|
293 | n/a | except AttributeError: |
---|
294 | n/a | # This pyexpat does not support SkippedEntity |
---|
295 | n/a | pass |
---|
296 | n/a | self._parser.SetParamEntityParsing( |
---|
297 | n/a | expat.XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE) |
---|
298 | n/a | |
---|
299 | n/a | self._parsing = 0 |
---|
300 | n/a | self._entity_stack = [] |
---|
301 | n/a | |
---|
302 | n/a | # Locator methods |
---|
303 | n/a | |
---|
304 | n/a | def getColumnNumber(self): |
---|
305 | n/a | if self._parser is None: |
---|
306 | n/a | return None |
---|
307 | n/a | return self._parser.ErrorColumnNumber |
---|
308 | n/a | |
---|
309 | n/a | def getLineNumber(self): |
---|
310 | n/a | if self._parser is None: |
---|
311 | n/a | return 1 |
---|
312 | n/a | return self._parser.ErrorLineNumber |
---|
313 | n/a | |
---|
314 | n/a | def getPublicId(self): |
---|
315 | n/a | return self._source.getPublicId() |
---|
316 | n/a | |
---|
317 | n/a | def getSystemId(self): |
---|
318 | n/a | return self._source.getSystemId() |
---|
319 | n/a | |
---|
320 | n/a | # event handlers |
---|
321 | n/a | def start_element(self, name, attrs): |
---|
322 | n/a | self._cont_handler.startElement(name, AttributesImpl(attrs)) |
---|
323 | n/a | |
---|
324 | n/a | def end_element(self, name): |
---|
325 | n/a | self._cont_handler.endElement(name) |
---|
326 | n/a | |
---|
327 | n/a | def start_element_ns(self, name, attrs): |
---|
328 | n/a | pair = name.split() |
---|
329 | n/a | if len(pair) == 1: |
---|
330 | n/a | # no namespace |
---|
331 | n/a | pair = (None, name) |
---|
332 | n/a | elif len(pair) == 3: |
---|
333 | n/a | pair = pair[0], pair[1] |
---|
334 | n/a | else: |
---|
335 | n/a | # default namespace |
---|
336 | n/a | pair = tuple(pair) |
---|
337 | n/a | |
---|
338 | n/a | newattrs = {} |
---|
339 | n/a | qnames = {} |
---|
340 | n/a | for (aname, value) in attrs.items(): |
---|
341 | n/a | parts = aname.split() |
---|
342 | n/a | length = len(parts) |
---|
343 | n/a | if length == 1: |
---|
344 | n/a | # no namespace |
---|
345 | n/a | qname = aname |
---|
346 | n/a | apair = (None, aname) |
---|
347 | n/a | elif length == 3: |
---|
348 | n/a | qname = "%s:%s" % (parts[2], parts[1]) |
---|
349 | n/a | apair = parts[0], parts[1] |
---|
350 | n/a | else: |
---|
351 | n/a | # default namespace |
---|
352 | n/a | qname = parts[1] |
---|
353 | n/a | apair = tuple(parts) |
---|
354 | n/a | |
---|
355 | n/a | newattrs[apair] = value |
---|
356 | n/a | qnames[apair] = qname |
---|
357 | n/a | |
---|
358 | n/a | self._cont_handler.startElementNS(pair, None, |
---|
359 | n/a | AttributesNSImpl(newattrs, qnames)) |
---|
360 | n/a | |
---|
361 | n/a | def end_element_ns(self, name): |
---|
362 | n/a | pair = name.split() |
---|
363 | n/a | if len(pair) == 1: |
---|
364 | n/a | pair = (None, name) |
---|
365 | n/a | elif len(pair) == 3: |
---|
366 | n/a | pair = pair[0], pair[1] |
---|
367 | n/a | else: |
---|
368 | n/a | pair = tuple(pair) |
---|
369 | n/a | |
---|
370 | n/a | self._cont_handler.endElementNS(pair, None) |
---|
371 | n/a | |
---|
372 | n/a | # this is not used (call directly to ContentHandler) |
---|
373 | n/a | def processing_instruction(self, target, data): |
---|
374 | n/a | self._cont_handler.processingInstruction(target, data) |
---|
375 | n/a | |
---|
376 | n/a | # this is not used (call directly to ContentHandler) |
---|
377 | n/a | def character_data(self, data): |
---|
378 | n/a | self._cont_handler.characters(data) |
---|
379 | n/a | |
---|
380 | n/a | def start_namespace_decl(self, prefix, uri): |
---|
381 | n/a | self._cont_handler.startPrefixMapping(prefix, uri) |
---|
382 | n/a | |
---|
383 | n/a | def end_namespace_decl(self, prefix): |
---|
384 | n/a | self._cont_handler.endPrefixMapping(prefix) |
---|
385 | n/a | |
---|
386 | n/a | def start_doctype_decl(self, name, sysid, pubid, has_internal_subset): |
---|
387 | n/a | self._lex_handler_prop.startDTD(name, pubid, sysid) |
---|
388 | n/a | |
---|
389 | n/a | def unparsed_entity_decl(self, name, base, sysid, pubid, notation_name): |
---|
390 | n/a | self._dtd_handler.unparsedEntityDecl(name, pubid, sysid, notation_name) |
---|
391 | n/a | |
---|
392 | n/a | def notation_decl(self, name, base, sysid, pubid): |
---|
393 | n/a | self._dtd_handler.notationDecl(name, pubid, sysid) |
---|
394 | n/a | |
---|
395 | n/a | def external_entity_ref(self, context, base, sysid, pubid): |
---|
396 | n/a | if not self._external_ges: |
---|
397 | n/a | return 1 |
---|
398 | n/a | |
---|
399 | n/a | source = self._ent_handler.resolveEntity(pubid, sysid) |
---|
400 | n/a | source = saxutils.prepare_input_source(source, |
---|
401 | n/a | self._source.getSystemId() or |
---|
402 | n/a | "") |
---|
403 | n/a | |
---|
404 | n/a | self._entity_stack.append((self._parser, self._source)) |
---|
405 | n/a | self._parser = self._parser.ExternalEntityParserCreate(context) |
---|
406 | n/a | self._source = source |
---|
407 | n/a | |
---|
408 | n/a | try: |
---|
409 | n/a | xmlreader.IncrementalParser.parse(self, source) |
---|
410 | n/a | except: |
---|
411 | n/a | return 0 # FIXME: save error info here? |
---|
412 | n/a | |
---|
413 | n/a | (self._parser, self._source) = self._entity_stack[-1] |
---|
414 | n/a | del self._entity_stack[-1] |
---|
415 | n/a | return 1 |
---|
416 | n/a | |
---|
417 | n/a | def skipped_entity_handler(self, name, is_pe): |
---|
418 | n/a | if is_pe: |
---|
419 | n/a | # The SAX spec requires to report skipped PEs with a '%' |
---|
420 | n/a | name = '%'+name |
---|
421 | n/a | self._cont_handler.skippedEntity(name) |
---|
422 | n/a | |
---|
423 | n/a | # --- |
---|
424 | n/a | |
---|
425 | n/a | def create_parser(*args, **kwargs): |
---|
426 | n/a | return ExpatParser(*args, **kwargs) |
---|
427 | n/a | |
---|
428 | n/a | # --- |
---|
429 | n/a | |
---|
430 | n/a | if __name__ == "__main__": |
---|
431 | n/a | import xml.sax.saxutils |
---|
432 | n/a | p = create_parser() |
---|
433 | n/a | p.setContentHandler(xml.sax.saxutils.XMLGenerator()) |
---|
434 | n/a | p.setErrorHandler(xml.sax.ErrorHandler()) |
---|
435 | n/a | p.parse("http://www.ibiblio.org/xml/examples/shakespeare/hamlet.xml") |
---|