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