1 | n/a | import xml.sax |
---|
2 | n/a | import xml.sax.handler |
---|
3 | n/a | |
---|
4 | n/a | START_ELEMENT = "START_ELEMENT" |
---|
5 | n/a | END_ELEMENT = "END_ELEMENT" |
---|
6 | n/a | COMMENT = "COMMENT" |
---|
7 | n/a | START_DOCUMENT = "START_DOCUMENT" |
---|
8 | n/a | END_DOCUMENT = "END_DOCUMENT" |
---|
9 | n/a | PROCESSING_INSTRUCTION = "PROCESSING_INSTRUCTION" |
---|
10 | n/a | IGNORABLE_WHITESPACE = "IGNORABLE_WHITESPACE" |
---|
11 | n/a | CHARACTERS = "CHARACTERS" |
---|
12 | n/a | |
---|
13 | n/a | class PullDOM(xml.sax.ContentHandler): |
---|
14 | n/a | _locator = None |
---|
15 | n/a | document = None |
---|
16 | n/a | |
---|
17 | n/a | def __init__(self, documentFactory=None): |
---|
18 | n/a | from xml.dom import XML_NAMESPACE |
---|
19 | n/a | self.documentFactory = documentFactory |
---|
20 | n/a | self.firstEvent = [None, None] |
---|
21 | n/a | self.lastEvent = self.firstEvent |
---|
22 | n/a | self.elementStack = [] |
---|
23 | n/a | self.push = self.elementStack.append |
---|
24 | n/a | try: |
---|
25 | n/a | self.pop = self.elementStack.pop |
---|
26 | n/a | except AttributeError: |
---|
27 | n/a | # use class' pop instead |
---|
28 | n/a | pass |
---|
29 | n/a | self._ns_contexts = [{XML_NAMESPACE:'xml'}] # contains uri -> prefix dicts |
---|
30 | n/a | self._current_context = self._ns_contexts[-1] |
---|
31 | n/a | self.pending_events = [] |
---|
32 | n/a | |
---|
33 | n/a | def pop(self): |
---|
34 | n/a | result = self.elementStack[-1] |
---|
35 | n/a | del self.elementStack[-1] |
---|
36 | n/a | return result |
---|
37 | n/a | |
---|
38 | n/a | def setDocumentLocator(self, locator): |
---|
39 | n/a | self._locator = locator |
---|
40 | n/a | |
---|
41 | n/a | def startPrefixMapping(self, prefix, uri): |
---|
42 | n/a | if not hasattr(self, '_xmlns_attrs'): |
---|
43 | n/a | self._xmlns_attrs = [] |
---|
44 | n/a | self._xmlns_attrs.append((prefix or 'xmlns', uri)) |
---|
45 | n/a | self._ns_contexts.append(self._current_context.copy()) |
---|
46 | n/a | self._current_context[uri] = prefix or None |
---|
47 | n/a | |
---|
48 | n/a | def endPrefixMapping(self, prefix): |
---|
49 | n/a | self._current_context = self._ns_contexts.pop() |
---|
50 | n/a | |
---|
51 | n/a | def startElementNS(self, name, tagName , attrs): |
---|
52 | n/a | # Retrieve xml namespace declaration attributes. |
---|
53 | n/a | xmlns_uri = 'http://www.w3.org/2000/xmlns/' |
---|
54 | n/a | xmlns_attrs = getattr(self, '_xmlns_attrs', None) |
---|
55 | n/a | if xmlns_attrs is not None: |
---|
56 | n/a | for aname, value in xmlns_attrs: |
---|
57 | n/a | attrs._attrs[(xmlns_uri, aname)] = value |
---|
58 | n/a | self._xmlns_attrs = [] |
---|
59 | n/a | uri, localname = name |
---|
60 | n/a | if uri: |
---|
61 | n/a | # When using namespaces, the reader may or may not |
---|
62 | n/a | # provide us with the original name. If not, create |
---|
63 | n/a | # *a* valid tagName from the current context. |
---|
64 | n/a | if tagName is None: |
---|
65 | n/a | prefix = self._current_context[uri] |
---|
66 | n/a | if prefix: |
---|
67 | n/a | tagName = prefix + ":" + localname |
---|
68 | n/a | else: |
---|
69 | n/a | tagName = localname |
---|
70 | n/a | if self.document: |
---|
71 | n/a | node = self.document.createElementNS(uri, tagName) |
---|
72 | n/a | else: |
---|
73 | n/a | node = self.buildDocument(uri, tagName) |
---|
74 | n/a | else: |
---|
75 | n/a | # When the tagname is not prefixed, it just appears as |
---|
76 | n/a | # localname |
---|
77 | n/a | if self.document: |
---|
78 | n/a | node = self.document.createElement(localname) |
---|
79 | n/a | else: |
---|
80 | n/a | node = self.buildDocument(None, localname) |
---|
81 | n/a | |
---|
82 | n/a | for aname,value in attrs.items(): |
---|
83 | n/a | a_uri, a_localname = aname |
---|
84 | n/a | if a_uri == xmlns_uri: |
---|
85 | n/a | if a_localname == 'xmlns': |
---|
86 | n/a | qname = a_localname |
---|
87 | n/a | else: |
---|
88 | n/a | qname = 'xmlns:' + a_localname |
---|
89 | n/a | attr = self.document.createAttributeNS(a_uri, qname) |
---|
90 | n/a | node.setAttributeNodeNS(attr) |
---|
91 | n/a | elif a_uri: |
---|
92 | n/a | prefix = self._current_context[a_uri] |
---|
93 | n/a | if prefix: |
---|
94 | n/a | qname = prefix + ":" + a_localname |
---|
95 | n/a | else: |
---|
96 | n/a | qname = a_localname |
---|
97 | n/a | attr = self.document.createAttributeNS(a_uri, qname) |
---|
98 | n/a | node.setAttributeNodeNS(attr) |
---|
99 | n/a | else: |
---|
100 | n/a | attr = self.document.createAttribute(a_localname) |
---|
101 | n/a | node.setAttributeNode(attr) |
---|
102 | n/a | attr.value = value |
---|
103 | n/a | |
---|
104 | n/a | self.lastEvent[1] = [(START_ELEMENT, node), None] |
---|
105 | n/a | self.lastEvent = self.lastEvent[1] |
---|
106 | n/a | self.push(node) |
---|
107 | n/a | |
---|
108 | n/a | def endElementNS(self, name, tagName): |
---|
109 | n/a | self.lastEvent[1] = [(END_ELEMENT, self.pop()), None] |
---|
110 | n/a | self.lastEvent = self.lastEvent[1] |
---|
111 | n/a | |
---|
112 | n/a | def startElement(self, name, attrs): |
---|
113 | n/a | if self.document: |
---|
114 | n/a | node = self.document.createElement(name) |
---|
115 | n/a | else: |
---|
116 | n/a | node = self.buildDocument(None, name) |
---|
117 | n/a | |
---|
118 | n/a | for aname,value in attrs.items(): |
---|
119 | n/a | attr = self.document.createAttribute(aname) |
---|
120 | n/a | attr.value = value |
---|
121 | n/a | node.setAttributeNode(attr) |
---|
122 | n/a | |
---|
123 | n/a | self.lastEvent[1] = [(START_ELEMENT, node), None] |
---|
124 | n/a | self.lastEvent = self.lastEvent[1] |
---|
125 | n/a | self.push(node) |
---|
126 | n/a | |
---|
127 | n/a | def endElement(self, name): |
---|
128 | n/a | self.lastEvent[1] = [(END_ELEMENT, self.pop()), None] |
---|
129 | n/a | self.lastEvent = self.lastEvent[1] |
---|
130 | n/a | |
---|
131 | n/a | def comment(self, s): |
---|
132 | n/a | if self.document: |
---|
133 | n/a | node = self.document.createComment(s) |
---|
134 | n/a | self.lastEvent[1] = [(COMMENT, node), None] |
---|
135 | n/a | self.lastEvent = self.lastEvent[1] |
---|
136 | n/a | else: |
---|
137 | n/a | event = [(COMMENT, s), None] |
---|
138 | n/a | self.pending_events.append(event) |
---|
139 | n/a | |
---|
140 | n/a | def processingInstruction(self, target, data): |
---|
141 | n/a | if self.document: |
---|
142 | n/a | node = self.document.createProcessingInstruction(target, data) |
---|
143 | n/a | self.lastEvent[1] = [(PROCESSING_INSTRUCTION, node), None] |
---|
144 | n/a | self.lastEvent = self.lastEvent[1] |
---|
145 | n/a | else: |
---|
146 | n/a | event = [(PROCESSING_INSTRUCTION, target, data), None] |
---|
147 | n/a | self.pending_events.append(event) |
---|
148 | n/a | |
---|
149 | n/a | def ignorableWhitespace(self, chars): |
---|
150 | n/a | node = self.document.createTextNode(chars) |
---|
151 | n/a | self.lastEvent[1] = [(IGNORABLE_WHITESPACE, node), None] |
---|
152 | n/a | self.lastEvent = self.lastEvent[1] |
---|
153 | n/a | |
---|
154 | n/a | def characters(self, chars): |
---|
155 | n/a | node = self.document.createTextNode(chars) |
---|
156 | n/a | self.lastEvent[1] = [(CHARACTERS, node), None] |
---|
157 | n/a | self.lastEvent = self.lastEvent[1] |
---|
158 | n/a | |
---|
159 | n/a | def startDocument(self): |
---|
160 | n/a | if self.documentFactory is None: |
---|
161 | n/a | import xml.dom.minidom |
---|
162 | n/a | self.documentFactory = xml.dom.minidom.Document.implementation |
---|
163 | n/a | |
---|
164 | n/a | def buildDocument(self, uri, tagname): |
---|
165 | n/a | # Can't do that in startDocument, since we need the tagname |
---|
166 | n/a | # XXX: obtain DocumentType |
---|
167 | n/a | node = self.documentFactory.createDocument(uri, tagname, None) |
---|
168 | n/a | self.document = node |
---|
169 | n/a | self.lastEvent[1] = [(START_DOCUMENT, node), None] |
---|
170 | n/a | self.lastEvent = self.lastEvent[1] |
---|
171 | n/a | self.push(node) |
---|
172 | n/a | # Put everything we have seen so far into the document |
---|
173 | n/a | for e in self.pending_events: |
---|
174 | n/a | if e[0][0] == PROCESSING_INSTRUCTION: |
---|
175 | n/a | _,target,data = e[0] |
---|
176 | n/a | n = self.document.createProcessingInstruction(target, data) |
---|
177 | n/a | e[0] = (PROCESSING_INSTRUCTION, n) |
---|
178 | n/a | elif e[0][0] == COMMENT: |
---|
179 | n/a | n = self.document.createComment(e[0][1]) |
---|
180 | n/a | e[0] = (COMMENT, n) |
---|
181 | n/a | else: |
---|
182 | n/a | raise AssertionError("Unknown pending event ",e[0][0]) |
---|
183 | n/a | self.lastEvent[1] = e |
---|
184 | n/a | self.lastEvent = e |
---|
185 | n/a | self.pending_events = None |
---|
186 | n/a | return node.firstChild |
---|
187 | n/a | |
---|
188 | n/a | def endDocument(self): |
---|
189 | n/a | self.lastEvent[1] = [(END_DOCUMENT, self.document), None] |
---|
190 | n/a | self.pop() |
---|
191 | n/a | |
---|
192 | n/a | def clear(self): |
---|
193 | n/a | "clear(): Explicitly release parsing structures" |
---|
194 | n/a | self.document = None |
---|
195 | n/a | |
---|
196 | n/a | class ErrorHandler: |
---|
197 | n/a | def warning(self, exception): |
---|
198 | n/a | print(exception) |
---|
199 | n/a | def error(self, exception): |
---|
200 | n/a | raise exception |
---|
201 | n/a | def fatalError(self, exception): |
---|
202 | n/a | raise exception |
---|
203 | n/a | |
---|
204 | n/a | class DOMEventStream: |
---|
205 | n/a | def __init__(self, stream, parser, bufsize): |
---|
206 | n/a | self.stream = stream |
---|
207 | n/a | self.parser = parser |
---|
208 | n/a | self.bufsize = bufsize |
---|
209 | n/a | if not hasattr(self.parser, 'feed'): |
---|
210 | n/a | self.getEvent = self._slurp |
---|
211 | n/a | self.reset() |
---|
212 | n/a | |
---|
213 | n/a | def reset(self): |
---|
214 | n/a | self.pulldom = PullDOM() |
---|
215 | n/a | # This content handler relies on namespace support |
---|
216 | n/a | self.parser.setFeature(xml.sax.handler.feature_namespaces, 1) |
---|
217 | n/a | self.parser.setContentHandler(self.pulldom) |
---|
218 | n/a | |
---|
219 | n/a | def __getitem__(self, pos): |
---|
220 | n/a | rc = self.getEvent() |
---|
221 | n/a | if rc: |
---|
222 | n/a | return rc |
---|
223 | n/a | raise IndexError |
---|
224 | n/a | |
---|
225 | n/a | def __next__(self): |
---|
226 | n/a | rc = self.getEvent() |
---|
227 | n/a | if rc: |
---|
228 | n/a | return rc |
---|
229 | n/a | raise StopIteration |
---|
230 | n/a | |
---|
231 | n/a | def __iter__(self): |
---|
232 | n/a | return self |
---|
233 | n/a | |
---|
234 | n/a | def expandNode(self, node): |
---|
235 | n/a | event = self.getEvent() |
---|
236 | n/a | parents = [node] |
---|
237 | n/a | while event: |
---|
238 | n/a | token, cur_node = event |
---|
239 | n/a | if cur_node is node: |
---|
240 | n/a | return |
---|
241 | n/a | if token != END_ELEMENT: |
---|
242 | n/a | parents[-1].appendChild(cur_node) |
---|
243 | n/a | if token == START_ELEMENT: |
---|
244 | n/a | parents.append(cur_node) |
---|
245 | n/a | elif token == END_ELEMENT: |
---|
246 | n/a | del parents[-1] |
---|
247 | n/a | event = self.getEvent() |
---|
248 | n/a | |
---|
249 | n/a | def getEvent(self): |
---|
250 | n/a | # use IncrementalParser interface, so we get the desired |
---|
251 | n/a | # pull effect |
---|
252 | n/a | if not self.pulldom.firstEvent[1]: |
---|
253 | n/a | self.pulldom.lastEvent = self.pulldom.firstEvent |
---|
254 | n/a | while not self.pulldom.firstEvent[1]: |
---|
255 | n/a | buf = self.stream.read(self.bufsize) |
---|
256 | n/a | if not buf: |
---|
257 | n/a | self.parser.close() |
---|
258 | n/a | return None |
---|
259 | n/a | self.parser.feed(buf) |
---|
260 | n/a | rc = self.pulldom.firstEvent[1][0] |
---|
261 | n/a | self.pulldom.firstEvent[1] = self.pulldom.firstEvent[1][1] |
---|
262 | n/a | return rc |
---|
263 | n/a | |
---|
264 | n/a | def _slurp(self): |
---|
265 | n/a | """ Fallback replacement for getEvent() using the |
---|
266 | n/a | standard SAX2 interface, which means we slurp the |
---|
267 | n/a | SAX events into memory (no performance gain, but |
---|
268 | n/a | we are compatible to all SAX parsers). |
---|
269 | n/a | """ |
---|
270 | n/a | self.parser.parse(self.stream) |
---|
271 | n/a | self.getEvent = self._emit |
---|
272 | n/a | return self._emit() |
---|
273 | n/a | |
---|
274 | n/a | def _emit(self): |
---|
275 | n/a | """ Fallback replacement for getEvent() that emits |
---|
276 | n/a | the events that _slurp() read previously. |
---|
277 | n/a | """ |
---|
278 | n/a | rc = self.pulldom.firstEvent[1][0] |
---|
279 | n/a | self.pulldom.firstEvent[1] = self.pulldom.firstEvent[1][1] |
---|
280 | n/a | return rc |
---|
281 | n/a | |
---|
282 | n/a | def clear(self): |
---|
283 | n/a | """clear(): Explicitly release parsing objects""" |
---|
284 | n/a | self.pulldom.clear() |
---|
285 | n/a | del self.pulldom |
---|
286 | n/a | self.parser = None |
---|
287 | n/a | self.stream = None |
---|
288 | n/a | |
---|
289 | n/a | class SAX2DOM(PullDOM): |
---|
290 | n/a | |
---|
291 | n/a | def startElementNS(self, name, tagName , attrs): |
---|
292 | n/a | PullDOM.startElementNS(self, name, tagName, attrs) |
---|
293 | n/a | curNode = self.elementStack[-1] |
---|
294 | n/a | parentNode = self.elementStack[-2] |
---|
295 | n/a | parentNode.appendChild(curNode) |
---|
296 | n/a | |
---|
297 | n/a | def startElement(self, name, attrs): |
---|
298 | n/a | PullDOM.startElement(self, name, attrs) |
---|
299 | n/a | curNode = self.elementStack[-1] |
---|
300 | n/a | parentNode = self.elementStack[-2] |
---|
301 | n/a | parentNode.appendChild(curNode) |
---|
302 | n/a | |
---|
303 | n/a | def processingInstruction(self, target, data): |
---|
304 | n/a | PullDOM.processingInstruction(self, target, data) |
---|
305 | n/a | node = self.lastEvent[0][1] |
---|
306 | n/a | parentNode = self.elementStack[-1] |
---|
307 | n/a | parentNode.appendChild(node) |
---|
308 | n/a | |
---|
309 | n/a | def ignorableWhitespace(self, chars): |
---|
310 | n/a | PullDOM.ignorableWhitespace(self, chars) |
---|
311 | n/a | node = self.lastEvent[0][1] |
---|
312 | n/a | parentNode = self.elementStack[-1] |
---|
313 | n/a | parentNode.appendChild(node) |
---|
314 | n/a | |
---|
315 | n/a | def characters(self, chars): |
---|
316 | n/a | PullDOM.characters(self, chars) |
---|
317 | n/a | node = self.lastEvent[0][1] |
---|
318 | n/a | parentNode = self.elementStack[-1] |
---|
319 | n/a | parentNode.appendChild(node) |
---|
320 | n/a | |
---|
321 | n/a | |
---|
322 | n/a | default_bufsize = (2 ** 14) - 20 |
---|
323 | n/a | |
---|
324 | n/a | def parse(stream_or_string, parser=None, bufsize=None): |
---|
325 | n/a | if bufsize is None: |
---|
326 | n/a | bufsize = default_bufsize |
---|
327 | n/a | if isinstance(stream_or_string, str): |
---|
328 | n/a | stream = open(stream_or_string, 'rb') |
---|
329 | n/a | else: |
---|
330 | n/a | stream = stream_or_string |
---|
331 | n/a | if not parser: |
---|
332 | n/a | parser = xml.sax.make_parser() |
---|
333 | n/a | return DOMEventStream(stream, parser, bufsize) |
---|
334 | n/a | |
---|
335 | n/a | def parseString(string, parser=None): |
---|
336 | n/a | from io import StringIO |
---|
337 | n/a | |
---|
338 | n/a | bufsize = len(string) |
---|
339 | n/a | buf = StringIO(string) |
---|
340 | n/a | if not parser: |
---|
341 | n/a | parser = xml.sax.make_parser() |
---|
342 | n/a | return DOMEventStream(buf, parser, bufsize) |
---|