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