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