annotate Lib/xml/dom/pulldom.py @ 58765:2ba7677bfa95 legacy-trunk

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