1 | n/a | """Simple implementation of the Level 1 DOM. |
---|
2 | n/a | |
---|
3 | n/a | Namespaces and other minor Level 2 features are also supported. |
---|
4 | n/a | |
---|
5 | n/a | parse("foo.xml") |
---|
6 | n/a | |
---|
7 | n/a | parseString("<foo><bar/></foo>") |
---|
8 | n/a | |
---|
9 | n/a | Todo: |
---|
10 | n/a | ===== |
---|
11 | n/a | * convenience methods for getting elements and text. |
---|
12 | n/a | * more testing |
---|
13 | n/a | * bring some of the writer and linearizer code into conformance with this |
---|
14 | n/a | interface |
---|
15 | n/a | * SAX 2 namespaces |
---|
16 | n/a | """ |
---|
17 | n/a | |
---|
18 | n/a | import io |
---|
19 | n/a | import xml.dom |
---|
20 | n/a | |
---|
21 | n/a | from xml.dom import EMPTY_NAMESPACE, EMPTY_PREFIX, XMLNS_NAMESPACE, domreg |
---|
22 | n/a | from xml.dom.minicompat import * |
---|
23 | n/a | from xml.dom.xmlbuilder import DOMImplementationLS, DocumentLS |
---|
24 | n/a | |
---|
25 | n/a | # This is used by the ID-cache invalidation checks; the list isn't |
---|
26 | n/a | # actually complete, since the nodes being checked will never be the |
---|
27 | n/a | # DOCUMENT_NODE or DOCUMENT_FRAGMENT_NODE. (The node being checked is |
---|
28 | n/a | # the node being added or removed, not the node being modified.) |
---|
29 | n/a | # |
---|
30 | n/a | _nodeTypes_with_children = (xml.dom.Node.ELEMENT_NODE, |
---|
31 | n/a | xml.dom.Node.ENTITY_REFERENCE_NODE) |
---|
32 | n/a | |
---|
33 | n/a | |
---|
34 | n/a | class Node(xml.dom.Node): |
---|
35 | n/a | namespaceURI = None # this is non-null only for elements and attributes |
---|
36 | n/a | parentNode = None |
---|
37 | n/a | ownerDocument = None |
---|
38 | n/a | nextSibling = None |
---|
39 | n/a | previousSibling = None |
---|
40 | n/a | |
---|
41 | n/a | prefix = EMPTY_PREFIX # non-null only for NS elements and attributes |
---|
42 | n/a | |
---|
43 | n/a | def __bool__(self): |
---|
44 | n/a | return True |
---|
45 | n/a | |
---|
46 | n/a | def toxml(self, encoding=None): |
---|
47 | n/a | return self.toprettyxml("", "", encoding) |
---|
48 | n/a | |
---|
49 | n/a | def toprettyxml(self, indent="\t", newl="\n", encoding=None): |
---|
50 | n/a | if encoding is None: |
---|
51 | n/a | writer = io.StringIO() |
---|
52 | n/a | else: |
---|
53 | n/a | writer = io.TextIOWrapper(io.BytesIO(), |
---|
54 | n/a | encoding=encoding, |
---|
55 | n/a | errors="xmlcharrefreplace", |
---|
56 | n/a | newline='\n') |
---|
57 | n/a | if self.nodeType == Node.DOCUMENT_NODE: |
---|
58 | n/a | # Can pass encoding only to document, to put it into XML header |
---|
59 | n/a | self.writexml(writer, "", indent, newl, encoding) |
---|
60 | n/a | else: |
---|
61 | n/a | self.writexml(writer, "", indent, newl) |
---|
62 | n/a | if encoding is None: |
---|
63 | n/a | return writer.getvalue() |
---|
64 | n/a | else: |
---|
65 | n/a | return writer.detach().getvalue() |
---|
66 | n/a | |
---|
67 | n/a | def hasChildNodes(self): |
---|
68 | n/a | return bool(self.childNodes) |
---|
69 | n/a | |
---|
70 | n/a | def _get_childNodes(self): |
---|
71 | n/a | return self.childNodes |
---|
72 | n/a | |
---|
73 | n/a | def _get_firstChild(self): |
---|
74 | n/a | if self.childNodes: |
---|
75 | n/a | return self.childNodes[0] |
---|
76 | n/a | |
---|
77 | n/a | def _get_lastChild(self): |
---|
78 | n/a | if self.childNodes: |
---|
79 | n/a | return self.childNodes[-1] |
---|
80 | n/a | |
---|
81 | n/a | def insertBefore(self, newChild, refChild): |
---|
82 | n/a | if newChild.nodeType == self.DOCUMENT_FRAGMENT_NODE: |
---|
83 | n/a | for c in tuple(newChild.childNodes): |
---|
84 | n/a | self.insertBefore(c, refChild) |
---|
85 | n/a | ### The DOM does not clearly specify what to return in this case |
---|
86 | n/a | return newChild |
---|
87 | n/a | if newChild.nodeType not in self._child_node_types: |
---|
88 | n/a | raise xml.dom.HierarchyRequestErr( |
---|
89 | n/a | "%s cannot be child of %s" % (repr(newChild), repr(self))) |
---|
90 | n/a | if newChild.parentNode is not None: |
---|
91 | n/a | newChild.parentNode.removeChild(newChild) |
---|
92 | n/a | if refChild is None: |
---|
93 | n/a | self.appendChild(newChild) |
---|
94 | n/a | else: |
---|
95 | n/a | try: |
---|
96 | n/a | index = self.childNodes.index(refChild) |
---|
97 | n/a | except ValueError: |
---|
98 | n/a | raise xml.dom.NotFoundErr() |
---|
99 | n/a | if newChild.nodeType in _nodeTypes_with_children: |
---|
100 | n/a | _clear_id_cache(self) |
---|
101 | n/a | self.childNodes.insert(index, newChild) |
---|
102 | n/a | newChild.nextSibling = refChild |
---|
103 | n/a | refChild.previousSibling = newChild |
---|
104 | n/a | if index: |
---|
105 | n/a | node = self.childNodes[index-1] |
---|
106 | n/a | node.nextSibling = newChild |
---|
107 | n/a | newChild.previousSibling = node |
---|
108 | n/a | else: |
---|
109 | n/a | newChild.previousSibling = None |
---|
110 | n/a | newChild.parentNode = self |
---|
111 | n/a | return newChild |
---|
112 | n/a | |
---|
113 | n/a | def appendChild(self, node): |
---|
114 | n/a | if node.nodeType == self.DOCUMENT_FRAGMENT_NODE: |
---|
115 | n/a | for c in tuple(node.childNodes): |
---|
116 | n/a | self.appendChild(c) |
---|
117 | n/a | ### The DOM does not clearly specify what to return in this case |
---|
118 | n/a | return node |
---|
119 | n/a | if node.nodeType not in self._child_node_types: |
---|
120 | n/a | raise xml.dom.HierarchyRequestErr( |
---|
121 | n/a | "%s cannot be child of %s" % (repr(node), repr(self))) |
---|
122 | n/a | elif node.nodeType in _nodeTypes_with_children: |
---|
123 | n/a | _clear_id_cache(self) |
---|
124 | n/a | if node.parentNode is not None: |
---|
125 | n/a | node.parentNode.removeChild(node) |
---|
126 | n/a | _append_child(self, node) |
---|
127 | n/a | node.nextSibling = None |
---|
128 | n/a | return node |
---|
129 | n/a | |
---|
130 | n/a | def replaceChild(self, newChild, oldChild): |
---|
131 | n/a | if newChild.nodeType == self.DOCUMENT_FRAGMENT_NODE: |
---|
132 | n/a | refChild = oldChild.nextSibling |
---|
133 | n/a | self.removeChild(oldChild) |
---|
134 | n/a | return self.insertBefore(newChild, refChild) |
---|
135 | n/a | if newChild.nodeType not in self._child_node_types: |
---|
136 | n/a | raise xml.dom.HierarchyRequestErr( |
---|
137 | n/a | "%s cannot be child of %s" % (repr(newChild), repr(self))) |
---|
138 | n/a | if newChild is oldChild: |
---|
139 | n/a | return |
---|
140 | n/a | if newChild.parentNode is not None: |
---|
141 | n/a | newChild.parentNode.removeChild(newChild) |
---|
142 | n/a | try: |
---|
143 | n/a | index = self.childNodes.index(oldChild) |
---|
144 | n/a | except ValueError: |
---|
145 | n/a | raise xml.dom.NotFoundErr() |
---|
146 | n/a | self.childNodes[index] = newChild |
---|
147 | n/a | newChild.parentNode = self |
---|
148 | n/a | oldChild.parentNode = None |
---|
149 | n/a | if (newChild.nodeType in _nodeTypes_with_children |
---|
150 | n/a | or oldChild.nodeType in _nodeTypes_with_children): |
---|
151 | n/a | _clear_id_cache(self) |
---|
152 | n/a | newChild.nextSibling = oldChild.nextSibling |
---|
153 | n/a | newChild.previousSibling = oldChild.previousSibling |
---|
154 | n/a | oldChild.nextSibling = None |
---|
155 | n/a | oldChild.previousSibling = None |
---|
156 | n/a | if newChild.previousSibling: |
---|
157 | n/a | newChild.previousSibling.nextSibling = newChild |
---|
158 | n/a | if newChild.nextSibling: |
---|
159 | n/a | newChild.nextSibling.previousSibling = newChild |
---|
160 | n/a | return oldChild |
---|
161 | n/a | |
---|
162 | n/a | def removeChild(self, oldChild): |
---|
163 | n/a | try: |
---|
164 | n/a | self.childNodes.remove(oldChild) |
---|
165 | n/a | except ValueError: |
---|
166 | n/a | raise xml.dom.NotFoundErr() |
---|
167 | n/a | if oldChild.nextSibling is not None: |
---|
168 | n/a | oldChild.nextSibling.previousSibling = oldChild.previousSibling |
---|
169 | n/a | if oldChild.previousSibling is not None: |
---|
170 | n/a | oldChild.previousSibling.nextSibling = oldChild.nextSibling |
---|
171 | n/a | oldChild.nextSibling = oldChild.previousSibling = None |
---|
172 | n/a | if oldChild.nodeType in _nodeTypes_with_children: |
---|
173 | n/a | _clear_id_cache(self) |
---|
174 | n/a | |
---|
175 | n/a | oldChild.parentNode = None |
---|
176 | n/a | return oldChild |
---|
177 | n/a | |
---|
178 | n/a | def normalize(self): |
---|
179 | n/a | L = [] |
---|
180 | n/a | for child in self.childNodes: |
---|
181 | n/a | if child.nodeType == Node.TEXT_NODE: |
---|
182 | n/a | if not child.data: |
---|
183 | n/a | # empty text node; discard |
---|
184 | n/a | if L: |
---|
185 | n/a | L[-1].nextSibling = child.nextSibling |
---|
186 | n/a | if child.nextSibling: |
---|
187 | n/a | child.nextSibling.previousSibling = child.previousSibling |
---|
188 | n/a | child.unlink() |
---|
189 | n/a | elif L and L[-1].nodeType == child.nodeType: |
---|
190 | n/a | # collapse text node |
---|
191 | n/a | node = L[-1] |
---|
192 | n/a | node.data = node.data + child.data |
---|
193 | n/a | node.nextSibling = child.nextSibling |
---|
194 | n/a | if child.nextSibling: |
---|
195 | n/a | child.nextSibling.previousSibling = node |
---|
196 | n/a | child.unlink() |
---|
197 | n/a | else: |
---|
198 | n/a | L.append(child) |
---|
199 | n/a | else: |
---|
200 | n/a | L.append(child) |
---|
201 | n/a | if child.nodeType == Node.ELEMENT_NODE: |
---|
202 | n/a | child.normalize() |
---|
203 | n/a | self.childNodes[:] = L |
---|
204 | n/a | |
---|
205 | n/a | def cloneNode(self, deep): |
---|
206 | n/a | return _clone_node(self, deep, self.ownerDocument or self) |
---|
207 | n/a | |
---|
208 | n/a | def isSupported(self, feature, version): |
---|
209 | n/a | return self.ownerDocument.implementation.hasFeature(feature, version) |
---|
210 | n/a | |
---|
211 | n/a | def _get_localName(self): |
---|
212 | n/a | # Overridden in Element and Attr where localName can be Non-Null |
---|
213 | n/a | return None |
---|
214 | n/a | |
---|
215 | n/a | # Node interfaces from Level 3 (WD 9 April 2002) |
---|
216 | n/a | |
---|
217 | n/a | def isSameNode(self, other): |
---|
218 | n/a | return self is other |
---|
219 | n/a | |
---|
220 | n/a | def getInterface(self, feature): |
---|
221 | n/a | if self.isSupported(feature, None): |
---|
222 | n/a | return self |
---|
223 | n/a | else: |
---|
224 | n/a | return None |
---|
225 | n/a | |
---|
226 | n/a | # The "user data" functions use a dictionary that is only present |
---|
227 | n/a | # if some user data has been set, so be careful not to assume it |
---|
228 | n/a | # exists. |
---|
229 | n/a | |
---|
230 | n/a | def getUserData(self, key): |
---|
231 | n/a | try: |
---|
232 | n/a | return self._user_data[key][0] |
---|
233 | n/a | except (AttributeError, KeyError): |
---|
234 | n/a | return None |
---|
235 | n/a | |
---|
236 | n/a | def setUserData(self, key, data, handler): |
---|
237 | n/a | old = None |
---|
238 | n/a | try: |
---|
239 | n/a | d = self._user_data |
---|
240 | n/a | except AttributeError: |
---|
241 | n/a | d = {} |
---|
242 | n/a | self._user_data = d |
---|
243 | n/a | if key in d: |
---|
244 | n/a | old = d[key][0] |
---|
245 | n/a | if data is None: |
---|
246 | n/a | # ignore handlers passed for None |
---|
247 | n/a | handler = None |
---|
248 | n/a | if old is not None: |
---|
249 | n/a | del d[key] |
---|
250 | n/a | else: |
---|
251 | n/a | d[key] = (data, handler) |
---|
252 | n/a | return old |
---|
253 | n/a | |
---|
254 | n/a | def _call_user_data_handler(self, operation, src, dst): |
---|
255 | n/a | if hasattr(self, "_user_data"): |
---|
256 | n/a | for key, (data, handler) in list(self._user_data.items()): |
---|
257 | n/a | if handler is not None: |
---|
258 | n/a | handler.handle(operation, key, data, src, dst) |
---|
259 | n/a | |
---|
260 | n/a | # minidom-specific API: |
---|
261 | n/a | |
---|
262 | n/a | def unlink(self): |
---|
263 | n/a | self.parentNode = self.ownerDocument = None |
---|
264 | n/a | if self.childNodes: |
---|
265 | n/a | for child in self.childNodes: |
---|
266 | n/a | child.unlink() |
---|
267 | n/a | self.childNodes = NodeList() |
---|
268 | n/a | self.previousSibling = None |
---|
269 | n/a | self.nextSibling = None |
---|
270 | n/a | |
---|
271 | n/a | # A Node is its own context manager, to ensure that an unlink() call occurs. |
---|
272 | n/a | # This is similar to how a file object works. |
---|
273 | n/a | def __enter__(self): |
---|
274 | n/a | return self |
---|
275 | n/a | |
---|
276 | n/a | def __exit__(self, et, ev, tb): |
---|
277 | n/a | self.unlink() |
---|
278 | n/a | |
---|
279 | n/a | defproperty(Node, "firstChild", doc="First child node, or None.") |
---|
280 | n/a | defproperty(Node, "lastChild", doc="Last child node, or None.") |
---|
281 | n/a | defproperty(Node, "localName", doc="Namespace-local name of this node.") |
---|
282 | n/a | |
---|
283 | n/a | |
---|
284 | n/a | def _append_child(self, node): |
---|
285 | n/a | # fast path with less checks; usable by DOM builders if careful |
---|
286 | n/a | childNodes = self.childNodes |
---|
287 | n/a | if childNodes: |
---|
288 | n/a | last = childNodes[-1] |
---|
289 | n/a | node.previousSibling = last |
---|
290 | n/a | last.nextSibling = node |
---|
291 | n/a | childNodes.append(node) |
---|
292 | n/a | node.parentNode = self |
---|
293 | n/a | |
---|
294 | n/a | def _in_document(node): |
---|
295 | n/a | # return True iff node is part of a document tree |
---|
296 | n/a | while node is not None: |
---|
297 | n/a | if node.nodeType == Node.DOCUMENT_NODE: |
---|
298 | n/a | return True |
---|
299 | n/a | node = node.parentNode |
---|
300 | n/a | return False |
---|
301 | n/a | |
---|
302 | n/a | def _write_data(writer, data): |
---|
303 | n/a | "Writes datachars to writer." |
---|
304 | n/a | if data: |
---|
305 | n/a | data = data.replace("&", "&").replace("<", "<"). \ |
---|
306 | n/a | replace("\"", """).replace(">", ">") |
---|
307 | n/a | writer.write(data) |
---|
308 | n/a | |
---|
309 | n/a | def _get_elements_by_tagName_helper(parent, name, rc): |
---|
310 | n/a | for node in parent.childNodes: |
---|
311 | n/a | if node.nodeType == Node.ELEMENT_NODE and \ |
---|
312 | n/a | (name == "*" or node.tagName == name): |
---|
313 | n/a | rc.append(node) |
---|
314 | n/a | _get_elements_by_tagName_helper(node, name, rc) |
---|
315 | n/a | return rc |
---|
316 | n/a | |
---|
317 | n/a | def _get_elements_by_tagName_ns_helper(parent, nsURI, localName, rc): |
---|
318 | n/a | for node in parent.childNodes: |
---|
319 | n/a | if node.nodeType == Node.ELEMENT_NODE: |
---|
320 | n/a | if ((localName == "*" or node.localName == localName) and |
---|
321 | n/a | (nsURI == "*" or node.namespaceURI == nsURI)): |
---|
322 | n/a | rc.append(node) |
---|
323 | n/a | _get_elements_by_tagName_ns_helper(node, nsURI, localName, rc) |
---|
324 | n/a | return rc |
---|
325 | n/a | |
---|
326 | n/a | class DocumentFragment(Node): |
---|
327 | n/a | nodeType = Node.DOCUMENT_FRAGMENT_NODE |
---|
328 | n/a | nodeName = "#document-fragment" |
---|
329 | n/a | nodeValue = None |
---|
330 | n/a | attributes = None |
---|
331 | n/a | parentNode = None |
---|
332 | n/a | _child_node_types = (Node.ELEMENT_NODE, |
---|
333 | n/a | Node.TEXT_NODE, |
---|
334 | n/a | Node.CDATA_SECTION_NODE, |
---|
335 | n/a | Node.ENTITY_REFERENCE_NODE, |
---|
336 | n/a | Node.PROCESSING_INSTRUCTION_NODE, |
---|
337 | n/a | Node.COMMENT_NODE, |
---|
338 | n/a | Node.NOTATION_NODE) |
---|
339 | n/a | |
---|
340 | n/a | def __init__(self): |
---|
341 | n/a | self.childNodes = NodeList() |
---|
342 | n/a | |
---|
343 | n/a | |
---|
344 | n/a | class Attr(Node): |
---|
345 | n/a | __slots__=('_name', '_value', 'namespaceURI', |
---|
346 | n/a | '_prefix', 'childNodes', '_localName', 'ownerDocument', 'ownerElement') |
---|
347 | n/a | nodeType = Node.ATTRIBUTE_NODE |
---|
348 | n/a | attributes = None |
---|
349 | n/a | specified = False |
---|
350 | n/a | _is_id = False |
---|
351 | n/a | |
---|
352 | n/a | _child_node_types = (Node.TEXT_NODE, Node.ENTITY_REFERENCE_NODE) |
---|
353 | n/a | |
---|
354 | n/a | def __init__(self, qName, namespaceURI=EMPTY_NAMESPACE, localName=None, |
---|
355 | n/a | prefix=None): |
---|
356 | n/a | self.ownerElement = None |
---|
357 | n/a | self._name = qName |
---|
358 | n/a | self.namespaceURI = namespaceURI |
---|
359 | n/a | self._prefix = prefix |
---|
360 | n/a | self.childNodes = NodeList() |
---|
361 | n/a | |
---|
362 | n/a | # Add the single child node that represents the value of the attr |
---|
363 | n/a | self.childNodes.append(Text()) |
---|
364 | n/a | |
---|
365 | n/a | # nodeValue and value are set elsewhere |
---|
366 | n/a | |
---|
367 | n/a | def _get_localName(self): |
---|
368 | n/a | try: |
---|
369 | n/a | return self._localName |
---|
370 | n/a | except AttributeError: |
---|
371 | n/a | return self.nodeName.split(":", 1)[-1] |
---|
372 | n/a | |
---|
373 | n/a | def _get_specified(self): |
---|
374 | n/a | return self.specified |
---|
375 | n/a | |
---|
376 | n/a | def _get_name(self): |
---|
377 | n/a | return self._name |
---|
378 | n/a | |
---|
379 | n/a | def _set_name(self, value): |
---|
380 | n/a | self._name = value |
---|
381 | n/a | if self.ownerElement is not None: |
---|
382 | n/a | _clear_id_cache(self.ownerElement) |
---|
383 | n/a | |
---|
384 | n/a | nodeName = name = property(_get_name, _set_name) |
---|
385 | n/a | |
---|
386 | n/a | def _get_value(self): |
---|
387 | n/a | return self._value |
---|
388 | n/a | |
---|
389 | n/a | def _set_value(self, value): |
---|
390 | n/a | self._value = value |
---|
391 | n/a | self.childNodes[0].data = value |
---|
392 | n/a | if self.ownerElement is not None: |
---|
393 | n/a | _clear_id_cache(self.ownerElement) |
---|
394 | n/a | self.childNodes[0].data = value |
---|
395 | n/a | |
---|
396 | n/a | nodeValue = value = property(_get_value, _set_value) |
---|
397 | n/a | |
---|
398 | n/a | def _get_prefix(self): |
---|
399 | n/a | return self._prefix |
---|
400 | n/a | |
---|
401 | n/a | def _set_prefix(self, prefix): |
---|
402 | n/a | nsuri = self.namespaceURI |
---|
403 | n/a | if prefix == "xmlns": |
---|
404 | n/a | if nsuri and nsuri != XMLNS_NAMESPACE: |
---|
405 | n/a | raise xml.dom.NamespaceErr( |
---|
406 | n/a | "illegal use of 'xmlns' prefix for the wrong namespace") |
---|
407 | n/a | self._prefix = prefix |
---|
408 | n/a | if prefix is None: |
---|
409 | n/a | newName = self.localName |
---|
410 | n/a | else: |
---|
411 | n/a | newName = "%s:%s" % (prefix, self.localName) |
---|
412 | n/a | if self.ownerElement: |
---|
413 | n/a | _clear_id_cache(self.ownerElement) |
---|
414 | n/a | self.name = newName |
---|
415 | n/a | |
---|
416 | n/a | prefix = property(_get_prefix, _set_prefix) |
---|
417 | n/a | |
---|
418 | n/a | def unlink(self): |
---|
419 | n/a | # This implementation does not call the base implementation |
---|
420 | n/a | # since most of that is not needed, and the expense of the |
---|
421 | n/a | # method call is not warranted. We duplicate the removal of |
---|
422 | n/a | # children, but that's all we needed from the base class. |
---|
423 | n/a | elem = self.ownerElement |
---|
424 | n/a | if elem is not None: |
---|
425 | n/a | del elem._attrs[self.nodeName] |
---|
426 | n/a | del elem._attrsNS[(self.namespaceURI, self.localName)] |
---|
427 | n/a | if self._is_id: |
---|
428 | n/a | self._is_id = False |
---|
429 | n/a | elem._magic_id_nodes -= 1 |
---|
430 | n/a | self.ownerDocument._magic_id_count -= 1 |
---|
431 | n/a | for child in self.childNodes: |
---|
432 | n/a | child.unlink() |
---|
433 | n/a | del self.childNodes[:] |
---|
434 | n/a | |
---|
435 | n/a | def _get_isId(self): |
---|
436 | n/a | if self._is_id: |
---|
437 | n/a | return True |
---|
438 | n/a | doc = self.ownerDocument |
---|
439 | n/a | elem = self.ownerElement |
---|
440 | n/a | if doc is None or elem is None: |
---|
441 | n/a | return False |
---|
442 | n/a | |
---|
443 | n/a | info = doc._get_elem_info(elem) |
---|
444 | n/a | if info is None: |
---|
445 | n/a | return False |
---|
446 | n/a | if self.namespaceURI: |
---|
447 | n/a | return info.isIdNS(self.namespaceURI, self.localName) |
---|
448 | n/a | else: |
---|
449 | n/a | return info.isId(self.nodeName) |
---|
450 | n/a | |
---|
451 | n/a | def _get_schemaType(self): |
---|
452 | n/a | doc = self.ownerDocument |
---|
453 | n/a | elem = self.ownerElement |
---|
454 | n/a | if doc is None or elem is None: |
---|
455 | n/a | return _no_type |
---|
456 | n/a | |
---|
457 | n/a | info = doc._get_elem_info(elem) |
---|
458 | n/a | if info is None: |
---|
459 | n/a | return _no_type |
---|
460 | n/a | if self.namespaceURI: |
---|
461 | n/a | return info.getAttributeTypeNS(self.namespaceURI, self.localName) |
---|
462 | n/a | else: |
---|
463 | n/a | return info.getAttributeType(self.nodeName) |
---|
464 | n/a | |
---|
465 | n/a | defproperty(Attr, "isId", doc="True if this attribute is an ID.") |
---|
466 | n/a | defproperty(Attr, "localName", doc="Namespace-local name of this attribute.") |
---|
467 | n/a | defproperty(Attr, "schemaType", doc="Schema type for this attribute.") |
---|
468 | n/a | |
---|
469 | n/a | |
---|
470 | n/a | class NamedNodeMap(object): |
---|
471 | n/a | """The attribute list is a transient interface to the underlying |
---|
472 | n/a | dictionaries. Mutations here will change the underlying element's |
---|
473 | n/a | dictionary. |
---|
474 | n/a | |
---|
475 | n/a | Ordering is imposed artificially and does not reflect the order of |
---|
476 | n/a | attributes as found in an input document. |
---|
477 | n/a | """ |
---|
478 | n/a | |
---|
479 | n/a | __slots__ = ('_attrs', '_attrsNS', '_ownerElement') |
---|
480 | n/a | |
---|
481 | n/a | def __init__(self, attrs, attrsNS, ownerElement): |
---|
482 | n/a | self._attrs = attrs |
---|
483 | n/a | self._attrsNS = attrsNS |
---|
484 | n/a | self._ownerElement = ownerElement |
---|
485 | n/a | |
---|
486 | n/a | def _get_length(self): |
---|
487 | n/a | return len(self._attrs) |
---|
488 | n/a | |
---|
489 | n/a | def item(self, index): |
---|
490 | n/a | try: |
---|
491 | n/a | return self[list(self._attrs.keys())[index]] |
---|
492 | n/a | except IndexError: |
---|
493 | n/a | return None |
---|
494 | n/a | |
---|
495 | n/a | def items(self): |
---|
496 | n/a | L = [] |
---|
497 | n/a | for node in self._attrs.values(): |
---|
498 | n/a | L.append((node.nodeName, node.value)) |
---|
499 | n/a | return L |
---|
500 | n/a | |
---|
501 | n/a | def itemsNS(self): |
---|
502 | n/a | L = [] |
---|
503 | n/a | for node in self._attrs.values(): |
---|
504 | n/a | L.append(((node.namespaceURI, node.localName), node.value)) |
---|
505 | n/a | return L |
---|
506 | n/a | |
---|
507 | n/a | def __contains__(self, key): |
---|
508 | n/a | if isinstance(key, str): |
---|
509 | n/a | return key in self._attrs |
---|
510 | n/a | else: |
---|
511 | n/a | return key in self._attrsNS |
---|
512 | n/a | |
---|
513 | n/a | def keys(self): |
---|
514 | n/a | return self._attrs.keys() |
---|
515 | n/a | |
---|
516 | n/a | def keysNS(self): |
---|
517 | n/a | return self._attrsNS.keys() |
---|
518 | n/a | |
---|
519 | n/a | def values(self): |
---|
520 | n/a | return self._attrs.values() |
---|
521 | n/a | |
---|
522 | n/a | def get(self, name, value=None): |
---|
523 | n/a | return self._attrs.get(name, value) |
---|
524 | n/a | |
---|
525 | n/a | __len__ = _get_length |
---|
526 | n/a | |
---|
527 | n/a | def _cmp(self, other): |
---|
528 | n/a | if self._attrs is getattr(other, "_attrs", None): |
---|
529 | n/a | return 0 |
---|
530 | n/a | else: |
---|
531 | n/a | return (id(self) > id(other)) - (id(self) < id(other)) |
---|
532 | n/a | |
---|
533 | n/a | def __eq__(self, other): |
---|
534 | n/a | return self._cmp(other) == 0 |
---|
535 | n/a | |
---|
536 | n/a | def __ge__(self, other): |
---|
537 | n/a | return self._cmp(other) >= 0 |
---|
538 | n/a | |
---|
539 | n/a | def __gt__(self, other): |
---|
540 | n/a | return self._cmp(other) > 0 |
---|
541 | n/a | |
---|
542 | n/a | def __le__(self, other): |
---|
543 | n/a | return self._cmp(other) <= 0 |
---|
544 | n/a | |
---|
545 | n/a | def __lt__(self, other): |
---|
546 | n/a | return self._cmp(other) < 0 |
---|
547 | n/a | |
---|
548 | n/a | def __getitem__(self, attname_or_tuple): |
---|
549 | n/a | if isinstance(attname_or_tuple, tuple): |
---|
550 | n/a | return self._attrsNS[attname_or_tuple] |
---|
551 | n/a | else: |
---|
552 | n/a | return self._attrs[attname_or_tuple] |
---|
553 | n/a | |
---|
554 | n/a | # same as set |
---|
555 | n/a | def __setitem__(self, attname, value): |
---|
556 | n/a | if isinstance(value, str): |
---|
557 | n/a | try: |
---|
558 | n/a | node = self._attrs[attname] |
---|
559 | n/a | except KeyError: |
---|
560 | n/a | node = Attr(attname) |
---|
561 | n/a | node.ownerDocument = self._ownerElement.ownerDocument |
---|
562 | n/a | self.setNamedItem(node) |
---|
563 | n/a | node.value = value |
---|
564 | n/a | else: |
---|
565 | n/a | if not isinstance(value, Attr): |
---|
566 | n/a | raise TypeError("value must be a string or Attr object") |
---|
567 | n/a | node = value |
---|
568 | n/a | self.setNamedItem(node) |
---|
569 | n/a | |
---|
570 | n/a | def getNamedItem(self, name): |
---|
571 | n/a | try: |
---|
572 | n/a | return self._attrs[name] |
---|
573 | n/a | except KeyError: |
---|
574 | n/a | return None |
---|
575 | n/a | |
---|
576 | n/a | def getNamedItemNS(self, namespaceURI, localName): |
---|
577 | n/a | try: |
---|
578 | n/a | return self._attrsNS[(namespaceURI, localName)] |
---|
579 | n/a | except KeyError: |
---|
580 | n/a | return None |
---|
581 | n/a | |
---|
582 | n/a | def removeNamedItem(self, name): |
---|
583 | n/a | n = self.getNamedItem(name) |
---|
584 | n/a | if n is not None: |
---|
585 | n/a | _clear_id_cache(self._ownerElement) |
---|
586 | n/a | del self._attrs[n.nodeName] |
---|
587 | n/a | del self._attrsNS[(n.namespaceURI, n.localName)] |
---|
588 | n/a | if hasattr(n, 'ownerElement'): |
---|
589 | n/a | n.ownerElement = None |
---|
590 | n/a | return n |
---|
591 | n/a | else: |
---|
592 | n/a | raise xml.dom.NotFoundErr() |
---|
593 | n/a | |
---|
594 | n/a | def removeNamedItemNS(self, namespaceURI, localName): |
---|
595 | n/a | n = self.getNamedItemNS(namespaceURI, localName) |
---|
596 | n/a | if n is not None: |
---|
597 | n/a | _clear_id_cache(self._ownerElement) |
---|
598 | n/a | del self._attrsNS[(n.namespaceURI, n.localName)] |
---|
599 | n/a | del self._attrs[n.nodeName] |
---|
600 | n/a | if hasattr(n, 'ownerElement'): |
---|
601 | n/a | n.ownerElement = None |
---|
602 | n/a | return n |
---|
603 | n/a | else: |
---|
604 | n/a | raise xml.dom.NotFoundErr() |
---|
605 | n/a | |
---|
606 | n/a | def setNamedItem(self, node): |
---|
607 | n/a | if not isinstance(node, Attr): |
---|
608 | n/a | raise xml.dom.HierarchyRequestErr( |
---|
609 | n/a | "%s cannot be child of %s" % (repr(node), repr(self))) |
---|
610 | n/a | old = self._attrs.get(node.name) |
---|
611 | n/a | if old: |
---|
612 | n/a | old.unlink() |
---|
613 | n/a | self._attrs[node.name] = node |
---|
614 | n/a | self._attrsNS[(node.namespaceURI, node.localName)] = node |
---|
615 | n/a | node.ownerElement = self._ownerElement |
---|
616 | n/a | _clear_id_cache(node.ownerElement) |
---|
617 | n/a | return old |
---|
618 | n/a | |
---|
619 | n/a | def setNamedItemNS(self, node): |
---|
620 | n/a | return self.setNamedItem(node) |
---|
621 | n/a | |
---|
622 | n/a | def __delitem__(self, attname_or_tuple): |
---|
623 | n/a | node = self[attname_or_tuple] |
---|
624 | n/a | _clear_id_cache(node.ownerElement) |
---|
625 | n/a | node.unlink() |
---|
626 | n/a | |
---|
627 | n/a | def __getstate__(self): |
---|
628 | n/a | return self._attrs, self._attrsNS, self._ownerElement |
---|
629 | n/a | |
---|
630 | n/a | def __setstate__(self, state): |
---|
631 | n/a | self._attrs, self._attrsNS, self._ownerElement = state |
---|
632 | n/a | |
---|
633 | n/a | defproperty(NamedNodeMap, "length", |
---|
634 | n/a | doc="Number of nodes in the NamedNodeMap.") |
---|
635 | n/a | |
---|
636 | n/a | AttributeList = NamedNodeMap |
---|
637 | n/a | |
---|
638 | n/a | |
---|
639 | n/a | class TypeInfo(object): |
---|
640 | n/a | __slots__ = 'namespace', 'name' |
---|
641 | n/a | |
---|
642 | n/a | def __init__(self, namespace, name): |
---|
643 | n/a | self.namespace = namespace |
---|
644 | n/a | self.name = name |
---|
645 | n/a | |
---|
646 | n/a | def __repr__(self): |
---|
647 | n/a | if self.namespace: |
---|
648 | n/a | return "<%s %r (from %r)>" % (self.__class__.__name__, self.name, |
---|
649 | n/a | self.namespace) |
---|
650 | n/a | else: |
---|
651 | n/a | return "<%s %r>" % (self.__class__.__name__, self.name) |
---|
652 | n/a | |
---|
653 | n/a | def _get_name(self): |
---|
654 | n/a | return self.name |
---|
655 | n/a | |
---|
656 | n/a | def _get_namespace(self): |
---|
657 | n/a | return self.namespace |
---|
658 | n/a | |
---|
659 | n/a | _no_type = TypeInfo(None, None) |
---|
660 | n/a | |
---|
661 | n/a | class Element(Node): |
---|
662 | n/a | __slots__=('ownerDocument', 'parentNode', 'tagName', 'nodeName', 'prefix', |
---|
663 | n/a | 'namespaceURI', '_localName', 'childNodes', '_attrs', '_attrsNS', |
---|
664 | n/a | 'nextSibling', 'previousSibling') |
---|
665 | n/a | nodeType = Node.ELEMENT_NODE |
---|
666 | n/a | nodeValue = None |
---|
667 | n/a | schemaType = _no_type |
---|
668 | n/a | |
---|
669 | n/a | _magic_id_nodes = 0 |
---|
670 | n/a | |
---|
671 | n/a | _child_node_types = (Node.ELEMENT_NODE, |
---|
672 | n/a | Node.PROCESSING_INSTRUCTION_NODE, |
---|
673 | n/a | Node.COMMENT_NODE, |
---|
674 | n/a | Node.TEXT_NODE, |
---|
675 | n/a | Node.CDATA_SECTION_NODE, |
---|
676 | n/a | Node.ENTITY_REFERENCE_NODE) |
---|
677 | n/a | |
---|
678 | n/a | def __init__(self, tagName, namespaceURI=EMPTY_NAMESPACE, prefix=None, |
---|
679 | n/a | localName=None): |
---|
680 | n/a | self.parentNode = None |
---|
681 | n/a | self.tagName = self.nodeName = tagName |
---|
682 | n/a | self.prefix = prefix |
---|
683 | n/a | self.namespaceURI = namespaceURI |
---|
684 | n/a | self.childNodes = NodeList() |
---|
685 | n/a | self.nextSibling = self.previousSibling = None |
---|
686 | n/a | |
---|
687 | n/a | # Attribute dictionaries are lazily created |
---|
688 | n/a | # attributes are double-indexed: |
---|
689 | n/a | # tagName -> Attribute |
---|
690 | n/a | # URI,localName -> Attribute |
---|
691 | n/a | # in the future: consider lazy generation |
---|
692 | n/a | # of attribute objects this is too tricky |
---|
693 | n/a | # for now because of headaches with |
---|
694 | n/a | # namespaces. |
---|
695 | n/a | self._attrs = None |
---|
696 | n/a | self._attrsNS = None |
---|
697 | n/a | |
---|
698 | n/a | def _ensure_attributes(self): |
---|
699 | n/a | if self._attrs is None: |
---|
700 | n/a | self._attrs = {} |
---|
701 | n/a | self._attrsNS = {} |
---|
702 | n/a | |
---|
703 | n/a | def _get_localName(self): |
---|
704 | n/a | try: |
---|
705 | n/a | return self._localName |
---|
706 | n/a | except AttributeError: |
---|
707 | n/a | return self.tagName.split(":", 1)[-1] |
---|
708 | n/a | |
---|
709 | n/a | def _get_tagName(self): |
---|
710 | n/a | return self.tagName |
---|
711 | n/a | |
---|
712 | n/a | def unlink(self): |
---|
713 | n/a | if self._attrs is not None: |
---|
714 | n/a | for attr in list(self._attrs.values()): |
---|
715 | n/a | attr.unlink() |
---|
716 | n/a | self._attrs = None |
---|
717 | n/a | self._attrsNS = None |
---|
718 | n/a | Node.unlink(self) |
---|
719 | n/a | |
---|
720 | n/a | def getAttribute(self, attname): |
---|
721 | n/a | if self._attrs is None: |
---|
722 | n/a | return "" |
---|
723 | n/a | try: |
---|
724 | n/a | return self._attrs[attname].value |
---|
725 | n/a | except KeyError: |
---|
726 | n/a | return "" |
---|
727 | n/a | |
---|
728 | n/a | def getAttributeNS(self, namespaceURI, localName): |
---|
729 | n/a | if self._attrsNS is None: |
---|
730 | n/a | return "" |
---|
731 | n/a | try: |
---|
732 | n/a | return self._attrsNS[(namespaceURI, localName)].value |
---|
733 | n/a | except KeyError: |
---|
734 | n/a | return "" |
---|
735 | n/a | |
---|
736 | n/a | def setAttribute(self, attname, value): |
---|
737 | n/a | attr = self.getAttributeNode(attname) |
---|
738 | n/a | if attr is None: |
---|
739 | n/a | attr = Attr(attname) |
---|
740 | n/a | attr.value = value # also sets nodeValue |
---|
741 | n/a | attr.ownerDocument = self.ownerDocument |
---|
742 | n/a | self.setAttributeNode(attr) |
---|
743 | n/a | elif value != attr.value: |
---|
744 | n/a | attr.value = value |
---|
745 | n/a | if attr.isId: |
---|
746 | n/a | _clear_id_cache(self) |
---|
747 | n/a | |
---|
748 | n/a | def setAttributeNS(self, namespaceURI, qualifiedName, value): |
---|
749 | n/a | prefix, localname = _nssplit(qualifiedName) |
---|
750 | n/a | attr = self.getAttributeNodeNS(namespaceURI, localname) |
---|
751 | n/a | if attr is None: |
---|
752 | n/a | attr = Attr(qualifiedName, namespaceURI, localname, prefix) |
---|
753 | n/a | attr.value = value |
---|
754 | n/a | attr.ownerDocument = self.ownerDocument |
---|
755 | n/a | self.setAttributeNode(attr) |
---|
756 | n/a | else: |
---|
757 | n/a | if value != attr.value: |
---|
758 | n/a | attr.value = value |
---|
759 | n/a | if attr.isId: |
---|
760 | n/a | _clear_id_cache(self) |
---|
761 | n/a | if attr.prefix != prefix: |
---|
762 | n/a | attr.prefix = prefix |
---|
763 | n/a | attr.nodeName = qualifiedName |
---|
764 | n/a | |
---|
765 | n/a | def getAttributeNode(self, attrname): |
---|
766 | n/a | if self._attrs is None: |
---|
767 | n/a | return None |
---|
768 | n/a | return self._attrs.get(attrname) |
---|
769 | n/a | |
---|
770 | n/a | def getAttributeNodeNS(self, namespaceURI, localName): |
---|
771 | n/a | if self._attrsNS is None: |
---|
772 | n/a | return None |
---|
773 | n/a | return self._attrsNS.get((namespaceURI, localName)) |
---|
774 | n/a | |
---|
775 | n/a | def setAttributeNode(self, attr): |
---|
776 | n/a | if attr.ownerElement not in (None, self): |
---|
777 | n/a | raise xml.dom.InuseAttributeErr("attribute node already owned") |
---|
778 | n/a | self._ensure_attributes() |
---|
779 | n/a | old1 = self._attrs.get(attr.name, None) |
---|
780 | n/a | if old1 is not None: |
---|
781 | n/a | self.removeAttributeNode(old1) |
---|
782 | n/a | old2 = self._attrsNS.get((attr.namespaceURI, attr.localName), None) |
---|
783 | n/a | if old2 is not None and old2 is not old1: |
---|
784 | n/a | self.removeAttributeNode(old2) |
---|
785 | n/a | _set_attribute_node(self, attr) |
---|
786 | n/a | |
---|
787 | n/a | if old1 is not attr: |
---|
788 | n/a | # It might have already been part of this node, in which case |
---|
789 | n/a | # it doesn't represent a change, and should not be returned. |
---|
790 | n/a | return old1 |
---|
791 | n/a | if old2 is not attr: |
---|
792 | n/a | return old2 |
---|
793 | n/a | |
---|
794 | n/a | setAttributeNodeNS = setAttributeNode |
---|
795 | n/a | |
---|
796 | n/a | def removeAttribute(self, name): |
---|
797 | n/a | if self._attrsNS is None: |
---|
798 | n/a | raise xml.dom.NotFoundErr() |
---|
799 | n/a | try: |
---|
800 | n/a | attr = self._attrs[name] |
---|
801 | n/a | except KeyError: |
---|
802 | n/a | raise xml.dom.NotFoundErr() |
---|
803 | n/a | self.removeAttributeNode(attr) |
---|
804 | n/a | |
---|
805 | n/a | def removeAttributeNS(self, namespaceURI, localName): |
---|
806 | n/a | if self._attrsNS is None: |
---|
807 | n/a | raise xml.dom.NotFoundErr() |
---|
808 | n/a | try: |
---|
809 | n/a | attr = self._attrsNS[(namespaceURI, localName)] |
---|
810 | n/a | except KeyError: |
---|
811 | n/a | raise xml.dom.NotFoundErr() |
---|
812 | n/a | self.removeAttributeNode(attr) |
---|
813 | n/a | |
---|
814 | n/a | def removeAttributeNode(self, node): |
---|
815 | n/a | if node is None: |
---|
816 | n/a | raise xml.dom.NotFoundErr() |
---|
817 | n/a | try: |
---|
818 | n/a | self._attrs[node.name] |
---|
819 | n/a | except KeyError: |
---|
820 | n/a | raise xml.dom.NotFoundErr() |
---|
821 | n/a | _clear_id_cache(self) |
---|
822 | n/a | node.unlink() |
---|
823 | n/a | # Restore this since the node is still useful and otherwise |
---|
824 | n/a | # unlinked |
---|
825 | n/a | node.ownerDocument = self.ownerDocument |
---|
826 | n/a | |
---|
827 | n/a | removeAttributeNodeNS = removeAttributeNode |
---|
828 | n/a | |
---|
829 | n/a | def hasAttribute(self, name): |
---|
830 | n/a | if self._attrs is None: |
---|
831 | n/a | return False |
---|
832 | n/a | return name in self._attrs |
---|
833 | n/a | |
---|
834 | n/a | def hasAttributeNS(self, namespaceURI, localName): |
---|
835 | n/a | if self._attrsNS is None: |
---|
836 | n/a | return False |
---|
837 | n/a | return (namespaceURI, localName) in self._attrsNS |
---|
838 | n/a | |
---|
839 | n/a | def getElementsByTagName(self, name): |
---|
840 | n/a | return _get_elements_by_tagName_helper(self, name, NodeList()) |
---|
841 | n/a | |
---|
842 | n/a | def getElementsByTagNameNS(self, namespaceURI, localName): |
---|
843 | n/a | return _get_elements_by_tagName_ns_helper( |
---|
844 | n/a | self, namespaceURI, localName, NodeList()) |
---|
845 | n/a | |
---|
846 | n/a | def __repr__(self): |
---|
847 | n/a | return "<DOM Element: %s at %#x>" % (self.tagName, id(self)) |
---|
848 | n/a | |
---|
849 | n/a | def writexml(self, writer, indent="", addindent="", newl=""): |
---|
850 | n/a | # indent = current indentation |
---|
851 | n/a | # addindent = indentation to add to higher levels |
---|
852 | n/a | # newl = newline string |
---|
853 | n/a | writer.write(indent+"<" + self.tagName) |
---|
854 | n/a | |
---|
855 | n/a | attrs = self._get_attributes() |
---|
856 | n/a | a_names = sorted(attrs.keys()) |
---|
857 | n/a | |
---|
858 | n/a | for a_name in a_names: |
---|
859 | n/a | writer.write(" %s=\"" % a_name) |
---|
860 | n/a | _write_data(writer, attrs[a_name].value) |
---|
861 | n/a | writer.write("\"") |
---|
862 | n/a | if self.childNodes: |
---|
863 | n/a | writer.write(">") |
---|
864 | n/a | if (len(self.childNodes) == 1 and |
---|
865 | n/a | self.childNodes[0].nodeType == Node.TEXT_NODE): |
---|
866 | n/a | self.childNodes[0].writexml(writer, '', '', '') |
---|
867 | n/a | else: |
---|
868 | n/a | writer.write(newl) |
---|
869 | n/a | for node in self.childNodes: |
---|
870 | n/a | node.writexml(writer, indent+addindent, addindent, newl) |
---|
871 | n/a | writer.write(indent) |
---|
872 | n/a | writer.write("</%s>%s" % (self.tagName, newl)) |
---|
873 | n/a | else: |
---|
874 | n/a | writer.write("/>%s"%(newl)) |
---|
875 | n/a | |
---|
876 | n/a | def _get_attributes(self): |
---|
877 | n/a | self._ensure_attributes() |
---|
878 | n/a | return NamedNodeMap(self._attrs, self._attrsNS, self) |
---|
879 | n/a | |
---|
880 | n/a | def hasAttributes(self): |
---|
881 | n/a | if self._attrs: |
---|
882 | n/a | return True |
---|
883 | n/a | else: |
---|
884 | n/a | return False |
---|
885 | n/a | |
---|
886 | n/a | # DOM Level 3 attributes, based on the 22 Oct 2002 draft |
---|
887 | n/a | |
---|
888 | n/a | def setIdAttribute(self, name): |
---|
889 | n/a | idAttr = self.getAttributeNode(name) |
---|
890 | n/a | self.setIdAttributeNode(idAttr) |
---|
891 | n/a | |
---|
892 | n/a | def setIdAttributeNS(self, namespaceURI, localName): |
---|
893 | n/a | idAttr = self.getAttributeNodeNS(namespaceURI, localName) |
---|
894 | n/a | self.setIdAttributeNode(idAttr) |
---|
895 | n/a | |
---|
896 | n/a | def setIdAttributeNode(self, idAttr): |
---|
897 | n/a | if idAttr is None or not self.isSameNode(idAttr.ownerElement): |
---|
898 | n/a | raise xml.dom.NotFoundErr() |
---|
899 | n/a | if _get_containing_entref(self) is not None: |
---|
900 | n/a | raise xml.dom.NoModificationAllowedErr() |
---|
901 | n/a | if not idAttr._is_id: |
---|
902 | n/a | idAttr._is_id = True |
---|
903 | n/a | self._magic_id_nodes += 1 |
---|
904 | n/a | self.ownerDocument._magic_id_count += 1 |
---|
905 | n/a | _clear_id_cache(self) |
---|
906 | n/a | |
---|
907 | n/a | defproperty(Element, "attributes", |
---|
908 | n/a | doc="NamedNodeMap of attributes on the element.") |
---|
909 | n/a | defproperty(Element, "localName", |
---|
910 | n/a | doc="Namespace-local name of this element.") |
---|
911 | n/a | |
---|
912 | n/a | |
---|
913 | n/a | def _set_attribute_node(element, attr): |
---|
914 | n/a | _clear_id_cache(element) |
---|
915 | n/a | element._ensure_attributes() |
---|
916 | n/a | element._attrs[attr.name] = attr |
---|
917 | n/a | element._attrsNS[(attr.namespaceURI, attr.localName)] = attr |
---|
918 | n/a | |
---|
919 | n/a | # This creates a circular reference, but Element.unlink() |
---|
920 | n/a | # breaks the cycle since the references to the attribute |
---|
921 | n/a | # dictionaries are tossed. |
---|
922 | n/a | attr.ownerElement = element |
---|
923 | n/a | |
---|
924 | n/a | class Childless: |
---|
925 | n/a | """Mixin that makes childless-ness easy to implement and avoids |
---|
926 | n/a | the complexity of the Node methods that deal with children. |
---|
927 | n/a | """ |
---|
928 | n/a | __slots__ = () |
---|
929 | n/a | |
---|
930 | n/a | attributes = None |
---|
931 | n/a | childNodes = EmptyNodeList() |
---|
932 | n/a | firstChild = None |
---|
933 | n/a | lastChild = None |
---|
934 | n/a | |
---|
935 | n/a | def _get_firstChild(self): |
---|
936 | n/a | return None |
---|
937 | n/a | |
---|
938 | n/a | def _get_lastChild(self): |
---|
939 | n/a | return None |
---|
940 | n/a | |
---|
941 | n/a | def appendChild(self, node): |
---|
942 | n/a | raise xml.dom.HierarchyRequestErr( |
---|
943 | n/a | self.nodeName + " nodes cannot have children") |
---|
944 | n/a | |
---|
945 | n/a | def hasChildNodes(self): |
---|
946 | n/a | return False |
---|
947 | n/a | |
---|
948 | n/a | def insertBefore(self, newChild, refChild): |
---|
949 | n/a | raise xml.dom.HierarchyRequestErr( |
---|
950 | n/a | self.nodeName + " nodes do not have children") |
---|
951 | n/a | |
---|
952 | n/a | def removeChild(self, oldChild): |
---|
953 | n/a | raise xml.dom.NotFoundErr( |
---|
954 | n/a | self.nodeName + " nodes do not have children") |
---|
955 | n/a | |
---|
956 | n/a | def normalize(self): |
---|
957 | n/a | # For childless nodes, normalize() has nothing to do. |
---|
958 | n/a | pass |
---|
959 | n/a | |
---|
960 | n/a | def replaceChild(self, newChild, oldChild): |
---|
961 | n/a | raise xml.dom.HierarchyRequestErr( |
---|
962 | n/a | self.nodeName + " nodes do not have children") |
---|
963 | n/a | |
---|
964 | n/a | |
---|
965 | n/a | class ProcessingInstruction(Childless, Node): |
---|
966 | n/a | nodeType = Node.PROCESSING_INSTRUCTION_NODE |
---|
967 | n/a | __slots__ = ('target', 'data') |
---|
968 | n/a | |
---|
969 | n/a | def __init__(self, target, data): |
---|
970 | n/a | self.target = target |
---|
971 | n/a | self.data = data |
---|
972 | n/a | |
---|
973 | n/a | # nodeValue is an alias for data |
---|
974 | n/a | def _get_nodeValue(self): |
---|
975 | n/a | return self.data |
---|
976 | n/a | def _set_nodeValue(self, value): |
---|
977 | n/a | self.data = value |
---|
978 | n/a | nodeValue = property(_get_nodeValue, _set_nodeValue) |
---|
979 | n/a | |
---|
980 | n/a | # nodeName is an alias for target |
---|
981 | n/a | def _get_nodeName(self): |
---|
982 | n/a | return self.target |
---|
983 | n/a | def _set_nodeName(self, value): |
---|
984 | n/a | self.target = value |
---|
985 | n/a | nodeName = property(_get_nodeName, _set_nodeName) |
---|
986 | n/a | |
---|
987 | n/a | def writexml(self, writer, indent="", addindent="", newl=""): |
---|
988 | n/a | writer.write("%s<?%s %s?>%s" % (indent,self.target, self.data, newl)) |
---|
989 | n/a | |
---|
990 | n/a | |
---|
991 | n/a | class CharacterData(Childless, Node): |
---|
992 | n/a | __slots__=('_data', 'ownerDocument','parentNode', 'previousSibling', 'nextSibling') |
---|
993 | n/a | |
---|
994 | n/a | def __init__(self): |
---|
995 | n/a | self.ownerDocument = self.parentNode = None |
---|
996 | n/a | self.previousSibling = self.nextSibling = None |
---|
997 | n/a | self._data = '' |
---|
998 | n/a | Node.__init__(self) |
---|
999 | n/a | |
---|
1000 | n/a | def _get_length(self): |
---|
1001 | n/a | return len(self.data) |
---|
1002 | n/a | __len__ = _get_length |
---|
1003 | n/a | |
---|
1004 | n/a | def _get_data(self): |
---|
1005 | n/a | return self._data |
---|
1006 | n/a | def _set_data(self, data): |
---|
1007 | n/a | self._data = data |
---|
1008 | n/a | |
---|
1009 | n/a | data = nodeValue = property(_get_data, _set_data) |
---|
1010 | n/a | |
---|
1011 | n/a | def __repr__(self): |
---|
1012 | n/a | data = self.data |
---|
1013 | n/a | if len(data) > 10: |
---|
1014 | n/a | dotdotdot = "..." |
---|
1015 | n/a | else: |
---|
1016 | n/a | dotdotdot = "" |
---|
1017 | n/a | return '<DOM %s node "%r%s">' % ( |
---|
1018 | n/a | self.__class__.__name__, data[0:10], dotdotdot) |
---|
1019 | n/a | |
---|
1020 | n/a | def substringData(self, offset, count): |
---|
1021 | n/a | if offset < 0: |
---|
1022 | n/a | raise xml.dom.IndexSizeErr("offset cannot be negative") |
---|
1023 | n/a | if offset >= len(self.data): |
---|
1024 | n/a | raise xml.dom.IndexSizeErr("offset cannot be beyond end of data") |
---|
1025 | n/a | if count < 0: |
---|
1026 | n/a | raise xml.dom.IndexSizeErr("count cannot be negative") |
---|
1027 | n/a | return self.data[offset:offset+count] |
---|
1028 | n/a | |
---|
1029 | n/a | def appendData(self, arg): |
---|
1030 | n/a | self.data = self.data + arg |
---|
1031 | n/a | |
---|
1032 | n/a | def insertData(self, offset, arg): |
---|
1033 | n/a | if offset < 0: |
---|
1034 | n/a | raise xml.dom.IndexSizeErr("offset cannot be negative") |
---|
1035 | n/a | if offset >= len(self.data): |
---|
1036 | n/a | raise xml.dom.IndexSizeErr("offset cannot be beyond end of data") |
---|
1037 | n/a | if arg: |
---|
1038 | n/a | self.data = "%s%s%s" % ( |
---|
1039 | n/a | self.data[:offset], arg, self.data[offset:]) |
---|
1040 | n/a | |
---|
1041 | n/a | def deleteData(self, offset, count): |
---|
1042 | n/a | if offset < 0: |
---|
1043 | n/a | raise xml.dom.IndexSizeErr("offset cannot be negative") |
---|
1044 | n/a | if offset >= len(self.data): |
---|
1045 | n/a | raise xml.dom.IndexSizeErr("offset cannot be beyond end of data") |
---|
1046 | n/a | if count < 0: |
---|
1047 | n/a | raise xml.dom.IndexSizeErr("count cannot be negative") |
---|
1048 | n/a | if count: |
---|
1049 | n/a | self.data = self.data[:offset] + self.data[offset+count:] |
---|
1050 | n/a | |
---|
1051 | n/a | def replaceData(self, offset, count, arg): |
---|
1052 | n/a | if offset < 0: |
---|
1053 | n/a | raise xml.dom.IndexSizeErr("offset cannot be negative") |
---|
1054 | n/a | if offset >= len(self.data): |
---|
1055 | n/a | raise xml.dom.IndexSizeErr("offset cannot be beyond end of data") |
---|
1056 | n/a | if count < 0: |
---|
1057 | n/a | raise xml.dom.IndexSizeErr("count cannot be negative") |
---|
1058 | n/a | if count: |
---|
1059 | n/a | self.data = "%s%s%s" % ( |
---|
1060 | n/a | self.data[:offset], arg, self.data[offset+count:]) |
---|
1061 | n/a | |
---|
1062 | n/a | defproperty(CharacterData, "length", doc="Length of the string data.") |
---|
1063 | n/a | |
---|
1064 | n/a | |
---|
1065 | n/a | class Text(CharacterData): |
---|
1066 | n/a | __slots__ = () |
---|
1067 | n/a | |
---|
1068 | n/a | nodeType = Node.TEXT_NODE |
---|
1069 | n/a | nodeName = "#text" |
---|
1070 | n/a | attributes = None |
---|
1071 | n/a | |
---|
1072 | n/a | def splitText(self, offset): |
---|
1073 | n/a | if offset < 0 or offset > len(self.data): |
---|
1074 | n/a | raise xml.dom.IndexSizeErr("illegal offset value") |
---|
1075 | n/a | newText = self.__class__() |
---|
1076 | n/a | newText.data = self.data[offset:] |
---|
1077 | n/a | newText.ownerDocument = self.ownerDocument |
---|
1078 | n/a | next = self.nextSibling |
---|
1079 | n/a | if self.parentNode and self in self.parentNode.childNodes: |
---|
1080 | n/a | if next is None: |
---|
1081 | n/a | self.parentNode.appendChild(newText) |
---|
1082 | n/a | else: |
---|
1083 | n/a | self.parentNode.insertBefore(newText, next) |
---|
1084 | n/a | self.data = self.data[:offset] |
---|
1085 | n/a | return newText |
---|
1086 | n/a | |
---|
1087 | n/a | def writexml(self, writer, indent="", addindent="", newl=""): |
---|
1088 | n/a | _write_data(writer, "%s%s%s" % (indent, self.data, newl)) |
---|
1089 | n/a | |
---|
1090 | n/a | # DOM Level 3 (WD 9 April 2002) |
---|
1091 | n/a | |
---|
1092 | n/a | def _get_wholeText(self): |
---|
1093 | n/a | L = [self.data] |
---|
1094 | n/a | n = self.previousSibling |
---|
1095 | n/a | while n is not None: |
---|
1096 | n/a | if n.nodeType in (Node.TEXT_NODE, Node.CDATA_SECTION_NODE): |
---|
1097 | n/a | L.insert(0, n.data) |
---|
1098 | n/a | n = n.previousSibling |
---|
1099 | n/a | else: |
---|
1100 | n/a | break |
---|
1101 | n/a | n = self.nextSibling |
---|
1102 | n/a | while n is not None: |
---|
1103 | n/a | if n.nodeType in (Node.TEXT_NODE, Node.CDATA_SECTION_NODE): |
---|
1104 | n/a | L.append(n.data) |
---|
1105 | n/a | n = n.nextSibling |
---|
1106 | n/a | else: |
---|
1107 | n/a | break |
---|
1108 | n/a | return ''.join(L) |
---|
1109 | n/a | |
---|
1110 | n/a | def replaceWholeText(self, content): |
---|
1111 | n/a | # XXX This needs to be seriously changed if minidom ever |
---|
1112 | n/a | # supports EntityReference nodes. |
---|
1113 | n/a | parent = self.parentNode |
---|
1114 | n/a | n = self.previousSibling |
---|
1115 | n/a | while n is not None: |
---|
1116 | n/a | if n.nodeType in (Node.TEXT_NODE, Node.CDATA_SECTION_NODE): |
---|
1117 | n/a | next = n.previousSibling |
---|
1118 | n/a | parent.removeChild(n) |
---|
1119 | n/a | n = next |
---|
1120 | n/a | else: |
---|
1121 | n/a | break |
---|
1122 | n/a | n = self.nextSibling |
---|
1123 | n/a | if not content: |
---|
1124 | n/a | parent.removeChild(self) |
---|
1125 | n/a | while n is not None: |
---|
1126 | n/a | if n.nodeType in (Node.TEXT_NODE, Node.CDATA_SECTION_NODE): |
---|
1127 | n/a | next = n.nextSibling |
---|
1128 | n/a | parent.removeChild(n) |
---|
1129 | n/a | n = next |
---|
1130 | n/a | else: |
---|
1131 | n/a | break |
---|
1132 | n/a | if content: |
---|
1133 | n/a | self.data = content |
---|
1134 | n/a | return self |
---|
1135 | n/a | else: |
---|
1136 | n/a | return None |
---|
1137 | n/a | |
---|
1138 | n/a | def _get_isWhitespaceInElementContent(self): |
---|
1139 | n/a | if self.data.strip(): |
---|
1140 | n/a | return False |
---|
1141 | n/a | elem = _get_containing_element(self) |
---|
1142 | n/a | if elem is None: |
---|
1143 | n/a | return False |
---|
1144 | n/a | info = self.ownerDocument._get_elem_info(elem) |
---|
1145 | n/a | if info is None: |
---|
1146 | n/a | return False |
---|
1147 | n/a | else: |
---|
1148 | n/a | return info.isElementContent() |
---|
1149 | n/a | |
---|
1150 | n/a | defproperty(Text, "isWhitespaceInElementContent", |
---|
1151 | n/a | doc="True iff this text node contains only whitespace" |
---|
1152 | n/a | " and is in element content.") |
---|
1153 | n/a | defproperty(Text, "wholeText", |
---|
1154 | n/a | doc="The text of all logically-adjacent text nodes.") |
---|
1155 | n/a | |
---|
1156 | n/a | |
---|
1157 | n/a | def _get_containing_element(node): |
---|
1158 | n/a | c = node.parentNode |
---|
1159 | n/a | while c is not None: |
---|
1160 | n/a | if c.nodeType == Node.ELEMENT_NODE: |
---|
1161 | n/a | return c |
---|
1162 | n/a | c = c.parentNode |
---|
1163 | n/a | return None |
---|
1164 | n/a | |
---|
1165 | n/a | def _get_containing_entref(node): |
---|
1166 | n/a | c = node.parentNode |
---|
1167 | n/a | while c is not None: |
---|
1168 | n/a | if c.nodeType == Node.ENTITY_REFERENCE_NODE: |
---|
1169 | n/a | return c |
---|
1170 | n/a | c = c.parentNode |
---|
1171 | n/a | return None |
---|
1172 | n/a | |
---|
1173 | n/a | |
---|
1174 | n/a | class Comment(CharacterData): |
---|
1175 | n/a | nodeType = Node.COMMENT_NODE |
---|
1176 | n/a | nodeName = "#comment" |
---|
1177 | n/a | |
---|
1178 | n/a | def __init__(self, data): |
---|
1179 | n/a | CharacterData.__init__(self) |
---|
1180 | n/a | self._data = data |
---|
1181 | n/a | |
---|
1182 | n/a | def writexml(self, writer, indent="", addindent="", newl=""): |
---|
1183 | n/a | if "--" in self.data: |
---|
1184 | n/a | raise ValueError("'--' is not allowed in a comment node") |
---|
1185 | n/a | writer.write("%s<!--%s-->%s" % (indent, self.data, newl)) |
---|
1186 | n/a | |
---|
1187 | n/a | |
---|
1188 | n/a | class CDATASection(Text): |
---|
1189 | n/a | __slots__ = () |
---|
1190 | n/a | |
---|
1191 | n/a | nodeType = Node.CDATA_SECTION_NODE |
---|
1192 | n/a | nodeName = "#cdata-section" |
---|
1193 | n/a | |
---|
1194 | n/a | def writexml(self, writer, indent="", addindent="", newl=""): |
---|
1195 | n/a | if self.data.find("]]>") >= 0: |
---|
1196 | n/a | raise ValueError("']]>' not allowed in a CDATA section") |
---|
1197 | n/a | writer.write("<![CDATA[%s]]>" % self.data) |
---|
1198 | n/a | |
---|
1199 | n/a | |
---|
1200 | n/a | class ReadOnlySequentialNamedNodeMap(object): |
---|
1201 | n/a | __slots__ = '_seq', |
---|
1202 | n/a | |
---|
1203 | n/a | def __init__(self, seq=()): |
---|
1204 | n/a | # seq should be a list or tuple |
---|
1205 | n/a | self._seq = seq |
---|
1206 | n/a | |
---|
1207 | n/a | def __len__(self): |
---|
1208 | n/a | return len(self._seq) |
---|
1209 | n/a | |
---|
1210 | n/a | def _get_length(self): |
---|
1211 | n/a | return len(self._seq) |
---|
1212 | n/a | |
---|
1213 | n/a | def getNamedItem(self, name): |
---|
1214 | n/a | for n in self._seq: |
---|
1215 | n/a | if n.nodeName == name: |
---|
1216 | n/a | return n |
---|
1217 | n/a | |
---|
1218 | n/a | def getNamedItemNS(self, namespaceURI, localName): |
---|
1219 | n/a | for n in self._seq: |
---|
1220 | n/a | if n.namespaceURI == namespaceURI and n.localName == localName: |
---|
1221 | n/a | return n |
---|
1222 | n/a | |
---|
1223 | n/a | def __getitem__(self, name_or_tuple): |
---|
1224 | n/a | if isinstance(name_or_tuple, tuple): |
---|
1225 | n/a | node = self.getNamedItemNS(*name_or_tuple) |
---|
1226 | n/a | else: |
---|
1227 | n/a | node = self.getNamedItem(name_or_tuple) |
---|
1228 | n/a | if node is None: |
---|
1229 | n/a | raise KeyError(name_or_tuple) |
---|
1230 | n/a | return node |
---|
1231 | n/a | |
---|
1232 | n/a | def item(self, index): |
---|
1233 | n/a | if index < 0: |
---|
1234 | n/a | return None |
---|
1235 | n/a | try: |
---|
1236 | n/a | return self._seq[index] |
---|
1237 | n/a | except IndexError: |
---|
1238 | n/a | return None |
---|
1239 | n/a | |
---|
1240 | n/a | def removeNamedItem(self, name): |
---|
1241 | n/a | raise xml.dom.NoModificationAllowedErr( |
---|
1242 | n/a | "NamedNodeMap instance is read-only") |
---|
1243 | n/a | |
---|
1244 | n/a | def removeNamedItemNS(self, namespaceURI, localName): |
---|
1245 | n/a | raise xml.dom.NoModificationAllowedErr( |
---|
1246 | n/a | "NamedNodeMap instance is read-only") |
---|
1247 | n/a | |
---|
1248 | n/a | def setNamedItem(self, node): |
---|
1249 | n/a | raise xml.dom.NoModificationAllowedErr( |
---|
1250 | n/a | "NamedNodeMap instance is read-only") |
---|
1251 | n/a | |
---|
1252 | n/a | def setNamedItemNS(self, node): |
---|
1253 | n/a | raise xml.dom.NoModificationAllowedErr( |
---|
1254 | n/a | "NamedNodeMap instance is read-only") |
---|
1255 | n/a | |
---|
1256 | n/a | def __getstate__(self): |
---|
1257 | n/a | return [self._seq] |
---|
1258 | n/a | |
---|
1259 | n/a | def __setstate__(self, state): |
---|
1260 | n/a | self._seq = state[0] |
---|
1261 | n/a | |
---|
1262 | n/a | defproperty(ReadOnlySequentialNamedNodeMap, "length", |
---|
1263 | n/a | doc="Number of entries in the NamedNodeMap.") |
---|
1264 | n/a | |
---|
1265 | n/a | |
---|
1266 | n/a | class Identified: |
---|
1267 | n/a | """Mix-in class that supports the publicId and systemId attributes.""" |
---|
1268 | n/a | |
---|
1269 | n/a | __slots__ = 'publicId', 'systemId' |
---|
1270 | n/a | |
---|
1271 | n/a | def _identified_mixin_init(self, publicId, systemId): |
---|
1272 | n/a | self.publicId = publicId |
---|
1273 | n/a | self.systemId = systemId |
---|
1274 | n/a | |
---|
1275 | n/a | def _get_publicId(self): |
---|
1276 | n/a | return self.publicId |
---|
1277 | n/a | |
---|
1278 | n/a | def _get_systemId(self): |
---|
1279 | n/a | return self.systemId |
---|
1280 | n/a | |
---|
1281 | n/a | class DocumentType(Identified, Childless, Node): |
---|
1282 | n/a | nodeType = Node.DOCUMENT_TYPE_NODE |
---|
1283 | n/a | nodeValue = None |
---|
1284 | n/a | name = None |
---|
1285 | n/a | publicId = None |
---|
1286 | n/a | systemId = None |
---|
1287 | n/a | internalSubset = None |
---|
1288 | n/a | |
---|
1289 | n/a | def __init__(self, qualifiedName): |
---|
1290 | n/a | self.entities = ReadOnlySequentialNamedNodeMap() |
---|
1291 | n/a | self.notations = ReadOnlySequentialNamedNodeMap() |
---|
1292 | n/a | if qualifiedName: |
---|
1293 | n/a | prefix, localname = _nssplit(qualifiedName) |
---|
1294 | n/a | self.name = localname |
---|
1295 | n/a | self.nodeName = self.name |
---|
1296 | n/a | |
---|
1297 | n/a | def _get_internalSubset(self): |
---|
1298 | n/a | return self.internalSubset |
---|
1299 | n/a | |
---|
1300 | n/a | def cloneNode(self, deep): |
---|
1301 | n/a | if self.ownerDocument is None: |
---|
1302 | n/a | # it's ok |
---|
1303 | n/a | clone = DocumentType(None) |
---|
1304 | n/a | clone.name = self.name |
---|
1305 | n/a | clone.nodeName = self.name |
---|
1306 | n/a | operation = xml.dom.UserDataHandler.NODE_CLONED |
---|
1307 | n/a | if deep: |
---|
1308 | n/a | clone.entities._seq = [] |
---|
1309 | n/a | clone.notations._seq = [] |
---|
1310 | n/a | for n in self.notations._seq: |
---|
1311 | n/a | notation = Notation(n.nodeName, n.publicId, n.systemId) |
---|
1312 | n/a | clone.notations._seq.append(notation) |
---|
1313 | n/a | n._call_user_data_handler(operation, n, notation) |
---|
1314 | n/a | for e in self.entities._seq: |
---|
1315 | n/a | entity = Entity(e.nodeName, e.publicId, e.systemId, |
---|
1316 | n/a | e.notationName) |
---|
1317 | n/a | entity.actualEncoding = e.actualEncoding |
---|
1318 | n/a | entity.encoding = e.encoding |
---|
1319 | n/a | entity.version = e.version |
---|
1320 | n/a | clone.entities._seq.append(entity) |
---|
1321 | n/a | e._call_user_data_handler(operation, n, entity) |
---|
1322 | n/a | self._call_user_data_handler(operation, self, clone) |
---|
1323 | n/a | return clone |
---|
1324 | n/a | else: |
---|
1325 | n/a | return None |
---|
1326 | n/a | |
---|
1327 | n/a | def writexml(self, writer, indent="", addindent="", newl=""): |
---|
1328 | n/a | writer.write("<!DOCTYPE ") |
---|
1329 | n/a | writer.write(self.name) |
---|
1330 | n/a | if self.publicId: |
---|
1331 | n/a | writer.write("%s PUBLIC '%s'%s '%s'" |
---|
1332 | n/a | % (newl, self.publicId, newl, self.systemId)) |
---|
1333 | n/a | elif self.systemId: |
---|
1334 | n/a | writer.write("%s SYSTEM '%s'" % (newl, self.systemId)) |
---|
1335 | n/a | if self.internalSubset is not None: |
---|
1336 | n/a | writer.write(" [") |
---|
1337 | n/a | writer.write(self.internalSubset) |
---|
1338 | n/a | writer.write("]") |
---|
1339 | n/a | writer.write(">"+newl) |
---|
1340 | n/a | |
---|
1341 | n/a | class Entity(Identified, Node): |
---|
1342 | n/a | attributes = None |
---|
1343 | n/a | nodeType = Node.ENTITY_NODE |
---|
1344 | n/a | nodeValue = None |
---|
1345 | n/a | |
---|
1346 | n/a | actualEncoding = None |
---|
1347 | n/a | encoding = None |
---|
1348 | n/a | version = None |
---|
1349 | n/a | |
---|
1350 | n/a | def __init__(self, name, publicId, systemId, notation): |
---|
1351 | n/a | self.nodeName = name |
---|
1352 | n/a | self.notationName = notation |
---|
1353 | n/a | self.childNodes = NodeList() |
---|
1354 | n/a | self._identified_mixin_init(publicId, systemId) |
---|
1355 | n/a | |
---|
1356 | n/a | def _get_actualEncoding(self): |
---|
1357 | n/a | return self.actualEncoding |
---|
1358 | n/a | |
---|
1359 | n/a | def _get_encoding(self): |
---|
1360 | n/a | return self.encoding |
---|
1361 | n/a | |
---|
1362 | n/a | def _get_version(self): |
---|
1363 | n/a | return self.version |
---|
1364 | n/a | |
---|
1365 | n/a | def appendChild(self, newChild): |
---|
1366 | n/a | raise xml.dom.HierarchyRequestErr( |
---|
1367 | n/a | "cannot append children to an entity node") |
---|
1368 | n/a | |
---|
1369 | n/a | def insertBefore(self, newChild, refChild): |
---|
1370 | n/a | raise xml.dom.HierarchyRequestErr( |
---|
1371 | n/a | "cannot insert children below an entity node") |
---|
1372 | n/a | |
---|
1373 | n/a | def removeChild(self, oldChild): |
---|
1374 | n/a | raise xml.dom.HierarchyRequestErr( |
---|
1375 | n/a | "cannot remove children from an entity node") |
---|
1376 | n/a | |
---|
1377 | n/a | def replaceChild(self, newChild, oldChild): |
---|
1378 | n/a | raise xml.dom.HierarchyRequestErr( |
---|
1379 | n/a | "cannot replace children of an entity node") |
---|
1380 | n/a | |
---|
1381 | n/a | class Notation(Identified, Childless, Node): |
---|
1382 | n/a | nodeType = Node.NOTATION_NODE |
---|
1383 | n/a | nodeValue = None |
---|
1384 | n/a | |
---|
1385 | n/a | def __init__(self, name, publicId, systemId): |
---|
1386 | n/a | self.nodeName = name |
---|
1387 | n/a | self._identified_mixin_init(publicId, systemId) |
---|
1388 | n/a | |
---|
1389 | n/a | |
---|
1390 | n/a | class DOMImplementation(DOMImplementationLS): |
---|
1391 | n/a | _features = [("core", "1.0"), |
---|
1392 | n/a | ("core", "2.0"), |
---|
1393 | n/a | ("core", None), |
---|
1394 | n/a | ("xml", "1.0"), |
---|
1395 | n/a | ("xml", "2.0"), |
---|
1396 | n/a | ("xml", None), |
---|
1397 | n/a | ("ls-load", "3.0"), |
---|
1398 | n/a | ("ls-load", None), |
---|
1399 | n/a | ] |
---|
1400 | n/a | |
---|
1401 | n/a | def hasFeature(self, feature, version): |
---|
1402 | n/a | if version == "": |
---|
1403 | n/a | version = None |
---|
1404 | n/a | return (feature.lower(), version) in self._features |
---|
1405 | n/a | |
---|
1406 | n/a | def createDocument(self, namespaceURI, qualifiedName, doctype): |
---|
1407 | n/a | if doctype and doctype.parentNode is not None: |
---|
1408 | n/a | raise xml.dom.WrongDocumentErr( |
---|
1409 | n/a | "doctype object owned by another DOM tree") |
---|
1410 | n/a | doc = self._create_document() |
---|
1411 | n/a | |
---|
1412 | n/a | add_root_element = not (namespaceURI is None |
---|
1413 | n/a | and qualifiedName is None |
---|
1414 | n/a | and doctype is None) |
---|
1415 | n/a | |
---|
1416 | n/a | if not qualifiedName and add_root_element: |
---|
1417 | n/a | # The spec is unclear what to raise here; SyntaxErr |
---|
1418 | n/a | # would be the other obvious candidate. Since Xerces raises |
---|
1419 | n/a | # InvalidCharacterErr, and since SyntaxErr is not listed |
---|
1420 | n/a | # for createDocument, that seems to be the better choice. |
---|
1421 | n/a | # XXX: need to check for illegal characters here and in |
---|
1422 | n/a | # createElement. |
---|
1423 | n/a | |
---|
1424 | n/a | # DOM Level III clears this up when talking about the return value |
---|
1425 | n/a | # of this function. If namespaceURI, qName and DocType are |
---|
1426 | n/a | # Null the document is returned without a document element |
---|
1427 | n/a | # Otherwise if doctype or namespaceURI are not None |
---|
1428 | n/a | # Then we go back to the above problem |
---|
1429 | n/a | raise xml.dom.InvalidCharacterErr("Element with no name") |
---|
1430 | n/a | |
---|
1431 | n/a | if add_root_element: |
---|
1432 | n/a | prefix, localname = _nssplit(qualifiedName) |
---|
1433 | n/a | if prefix == "xml" \ |
---|
1434 | n/a | and namespaceURI != "http://www.w3.org/XML/1998/namespace": |
---|
1435 | n/a | raise xml.dom.NamespaceErr("illegal use of 'xml' prefix") |
---|
1436 | n/a | if prefix and not namespaceURI: |
---|
1437 | n/a | raise xml.dom.NamespaceErr( |
---|
1438 | n/a | "illegal use of prefix without namespaces") |
---|
1439 | n/a | element = doc.createElementNS(namespaceURI, qualifiedName) |
---|
1440 | n/a | if doctype: |
---|
1441 | n/a | doc.appendChild(doctype) |
---|
1442 | n/a | doc.appendChild(element) |
---|
1443 | n/a | |
---|
1444 | n/a | if doctype: |
---|
1445 | n/a | doctype.parentNode = doctype.ownerDocument = doc |
---|
1446 | n/a | |
---|
1447 | n/a | doc.doctype = doctype |
---|
1448 | n/a | doc.implementation = self |
---|
1449 | n/a | return doc |
---|
1450 | n/a | |
---|
1451 | n/a | def createDocumentType(self, qualifiedName, publicId, systemId): |
---|
1452 | n/a | doctype = DocumentType(qualifiedName) |
---|
1453 | n/a | doctype.publicId = publicId |
---|
1454 | n/a | doctype.systemId = systemId |
---|
1455 | n/a | return doctype |
---|
1456 | n/a | |
---|
1457 | n/a | # DOM Level 3 (WD 9 April 2002) |
---|
1458 | n/a | |
---|
1459 | n/a | def getInterface(self, feature): |
---|
1460 | n/a | if self.hasFeature(feature, None): |
---|
1461 | n/a | return self |
---|
1462 | n/a | else: |
---|
1463 | n/a | return None |
---|
1464 | n/a | |
---|
1465 | n/a | # internal |
---|
1466 | n/a | def _create_document(self): |
---|
1467 | n/a | return Document() |
---|
1468 | n/a | |
---|
1469 | n/a | class ElementInfo(object): |
---|
1470 | n/a | """Object that represents content-model information for an element. |
---|
1471 | n/a | |
---|
1472 | n/a | This implementation is not expected to be used in practice; DOM |
---|
1473 | n/a | builders should provide implementations which do the right thing |
---|
1474 | n/a | using information available to it. |
---|
1475 | n/a | |
---|
1476 | n/a | """ |
---|
1477 | n/a | |
---|
1478 | n/a | __slots__ = 'tagName', |
---|
1479 | n/a | |
---|
1480 | n/a | def __init__(self, name): |
---|
1481 | n/a | self.tagName = name |
---|
1482 | n/a | |
---|
1483 | n/a | def getAttributeType(self, aname): |
---|
1484 | n/a | return _no_type |
---|
1485 | n/a | |
---|
1486 | n/a | def getAttributeTypeNS(self, namespaceURI, localName): |
---|
1487 | n/a | return _no_type |
---|
1488 | n/a | |
---|
1489 | n/a | def isElementContent(self): |
---|
1490 | n/a | return False |
---|
1491 | n/a | |
---|
1492 | n/a | def isEmpty(self): |
---|
1493 | n/a | """Returns true iff this element is declared to have an EMPTY |
---|
1494 | n/a | content model.""" |
---|
1495 | n/a | return False |
---|
1496 | n/a | |
---|
1497 | n/a | def isId(self, aname): |
---|
1498 | n/a | """Returns true iff the named attribute is a DTD-style ID.""" |
---|
1499 | n/a | return False |
---|
1500 | n/a | |
---|
1501 | n/a | def isIdNS(self, namespaceURI, localName): |
---|
1502 | n/a | """Returns true iff the identified attribute is a DTD-style ID.""" |
---|
1503 | n/a | return False |
---|
1504 | n/a | |
---|
1505 | n/a | def __getstate__(self): |
---|
1506 | n/a | return self.tagName |
---|
1507 | n/a | |
---|
1508 | n/a | def __setstate__(self, state): |
---|
1509 | n/a | self.tagName = state |
---|
1510 | n/a | |
---|
1511 | n/a | def _clear_id_cache(node): |
---|
1512 | n/a | if node.nodeType == Node.DOCUMENT_NODE: |
---|
1513 | n/a | node._id_cache.clear() |
---|
1514 | n/a | node._id_search_stack = None |
---|
1515 | n/a | elif _in_document(node): |
---|
1516 | n/a | node.ownerDocument._id_cache.clear() |
---|
1517 | n/a | node.ownerDocument._id_search_stack= None |
---|
1518 | n/a | |
---|
1519 | n/a | class Document(Node, DocumentLS): |
---|
1520 | n/a | __slots__ = ('_elem_info', 'doctype', |
---|
1521 | n/a | '_id_search_stack', 'childNodes', '_id_cache') |
---|
1522 | n/a | _child_node_types = (Node.ELEMENT_NODE, Node.PROCESSING_INSTRUCTION_NODE, |
---|
1523 | n/a | Node.COMMENT_NODE, Node.DOCUMENT_TYPE_NODE) |
---|
1524 | n/a | |
---|
1525 | n/a | implementation = DOMImplementation() |
---|
1526 | n/a | nodeType = Node.DOCUMENT_NODE |
---|
1527 | n/a | nodeName = "#document" |
---|
1528 | n/a | nodeValue = None |
---|
1529 | n/a | attributes = None |
---|
1530 | n/a | parentNode = None |
---|
1531 | n/a | previousSibling = nextSibling = None |
---|
1532 | n/a | |
---|
1533 | n/a | |
---|
1534 | n/a | # Document attributes from Level 3 (WD 9 April 2002) |
---|
1535 | n/a | |
---|
1536 | n/a | actualEncoding = None |
---|
1537 | n/a | encoding = None |
---|
1538 | n/a | standalone = None |
---|
1539 | n/a | version = None |
---|
1540 | n/a | strictErrorChecking = False |
---|
1541 | n/a | errorHandler = None |
---|
1542 | n/a | documentURI = None |
---|
1543 | n/a | |
---|
1544 | n/a | _magic_id_count = 0 |
---|
1545 | n/a | |
---|
1546 | n/a | def __init__(self): |
---|
1547 | n/a | self.doctype = None |
---|
1548 | n/a | self.childNodes = NodeList() |
---|
1549 | n/a | # mapping of (namespaceURI, localName) -> ElementInfo |
---|
1550 | n/a | # and tagName -> ElementInfo |
---|
1551 | n/a | self._elem_info = {} |
---|
1552 | n/a | self._id_cache = {} |
---|
1553 | n/a | self._id_search_stack = None |
---|
1554 | n/a | |
---|
1555 | n/a | def _get_elem_info(self, element): |
---|
1556 | n/a | if element.namespaceURI: |
---|
1557 | n/a | key = element.namespaceURI, element.localName |
---|
1558 | n/a | else: |
---|
1559 | n/a | key = element.tagName |
---|
1560 | n/a | return self._elem_info.get(key) |
---|
1561 | n/a | |
---|
1562 | n/a | def _get_actualEncoding(self): |
---|
1563 | n/a | return self.actualEncoding |
---|
1564 | n/a | |
---|
1565 | n/a | def _get_doctype(self): |
---|
1566 | n/a | return self.doctype |
---|
1567 | n/a | |
---|
1568 | n/a | def _get_documentURI(self): |
---|
1569 | n/a | return self.documentURI |
---|
1570 | n/a | |
---|
1571 | n/a | def _get_encoding(self): |
---|
1572 | n/a | return self.encoding |
---|
1573 | n/a | |
---|
1574 | n/a | def _get_errorHandler(self): |
---|
1575 | n/a | return self.errorHandler |
---|
1576 | n/a | |
---|
1577 | n/a | def _get_standalone(self): |
---|
1578 | n/a | return self.standalone |
---|
1579 | n/a | |
---|
1580 | n/a | def _get_strictErrorChecking(self): |
---|
1581 | n/a | return self.strictErrorChecking |
---|
1582 | n/a | |
---|
1583 | n/a | def _get_version(self): |
---|
1584 | n/a | return self.version |
---|
1585 | n/a | |
---|
1586 | n/a | def appendChild(self, node): |
---|
1587 | n/a | if node.nodeType not in self._child_node_types: |
---|
1588 | n/a | raise xml.dom.HierarchyRequestErr( |
---|
1589 | n/a | "%s cannot be child of %s" % (repr(node), repr(self))) |
---|
1590 | n/a | if node.parentNode is not None: |
---|
1591 | n/a | # This needs to be done before the next test since this |
---|
1592 | n/a | # may *be* the document element, in which case it should |
---|
1593 | n/a | # end up re-ordered to the end. |
---|
1594 | n/a | node.parentNode.removeChild(node) |
---|
1595 | n/a | |
---|
1596 | n/a | if node.nodeType == Node.ELEMENT_NODE \ |
---|
1597 | n/a | and self._get_documentElement(): |
---|
1598 | n/a | raise xml.dom.HierarchyRequestErr( |
---|
1599 | n/a | "two document elements disallowed") |
---|
1600 | n/a | return Node.appendChild(self, node) |
---|
1601 | n/a | |
---|
1602 | n/a | def removeChild(self, oldChild): |
---|
1603 | n/a | try: |
---|
1604 | n/a | self.childNodes.remove(oldChild) |
---|
1605 | n/a | except ValueError: |
---|
1606 | n/a | raise xml.dom.NotFoundErr() |
---|
1607 | n/a | oldChild.nextSibling = oldChild.previousSibling = None |
---|
1608 | n/a | oldChild.parentNode = None |
---|
1609 | n/a | if self.documentElement is oldChild: |
---|
1610 | n/a | self.documentElement = None |
---|
1611 | n/a | |
---|
1612 | n/a | return oldChild |
---|
1613 | n/a | |
---|
1614 | n/a | def _get_documentElement(self): |
---|
1615 | n/a | for node in self.childNodes: |
---|
1616 | n/a | if node.nodeType == Node.ELEMENT_NODE: |
---|
1617 | n/a | return node |
---|
1618 | n/a | |
---|
1619 | n/a | def unlink(self): |
---|
1620 | n/a | if self.doctype is not None: |
---|
1621 | n/a | self.doctype.unlink() |
---|
1622 | n/a | self.doctype = None |
---|
1623 | n/a | Node.unlink(self) |
---|
1624 | n/a | |
---|
1625 | n/a | def cloneNode(self, deep): |
---|
1626 | n/a | if not deep: |
---|
1627 | n/a | return None |
---|
1628 | n/a | clone = self.implementation.createDocument(None, None, None) |
---|
1629 | n/a | clone.encoding = self.encoding |
---|
1630 | n/a | clone.standalone = self.standalone |
---|
1631 | n/a | clone.version = self.version |
---|
1632 | n/a | for n in self.childNodes: |
---|
1633 | n/a | childclone = _clone_node(n, deep, clone) |
---|
1634 | n/a | assert childclone.ownerDocument.isSameNode(clone) |
---|
1635 | n/a | clone.childNodes.append(childclone) |
---|
1636 | n/a | if childclone.nodeType == Node.DOCUMENT_NODE: |
---|
1637 | n/a | assert clone.documentElement is None |
---|
1638 | n/a | elif childclone.nodeType == Node.DOCUMENT_TYPE_NODE: |
---|
1639 | n/a | assert clone.doctype is None |
---|
1640 | n/a | clone.doctype = childclone |
---|
1641 | n/a | childclone.parentNode = clone |
---|
1642 | n/a | self._call_user_data_handler(xml.dom.UserDataHandler.NODE_CLONED, |
---|
1643 | n/a | self, clone) |
---|
1644 | n/a | return clone |
---|
1645 | n/a | |
---|
1646 | n/a | def createDocumentFragment(self): |
---|
1647 | n/a | d = DocumentFragment() |
---|
1648 | n/a | d.ownerDocument = self |
---|
1649 | n/a | return d |
---|
1650 | n/a | |
---|
1651 | n/a | def createElement(self, tagName): |
---|
1652 | n/a | e = Element(tagName) |
---|
1653 | n/a | e.ownerDocument = self |
---|
1654 | n/a | return e |
---|
1655 | n/a | |
---|
1656 | n/a | def createTextNode(self, data): |
---|
1657 | n/a | if not isinstance(data, str): |
---|
1658 | n/a | raise TypeError("node contents must be a string") |
---|
1659 | n/a | t = Text() |
---|
1660 | n/a | t.data = data |
---|
1661 | n/a | t.ownerDocument = self |
---|
1662 | n/a | return t |
---|
1663 | n/a | |
---|
1664 | n/a | def createCDATASection(self, data): |
---|
1665 | n/a | if not isinstance(data, str): |
---|
1666 | n/a | raise TypeError("node contents must be a string") |
---|
1667 | n/a | c = CDATASection() |
---|
1668 | n/a | c.data = data |
---|
1669 | n/a | c.ownerDocument = self |
---|
1670 | n/a | return c |
---|
1671 | n/a | |
---|
1672 | n/a | def createComment(self, data): |
---|
1673 | n/a | c = Comment(data) |
---|
1674 | n/a | c.ownerDocument = self |
---|
1675 | n/a | return c |
---|
1676 | n/a | |
---|
1677 | n/a | def createProcessingInstruction(self, target, data): |
---|
1678 | n/a | p = ProcessingInstruction(target, data) |
---|
1679 | n/a | p.ownerDocument = self |
---|
1680 | n/a | return p |
---|
1681 | n/a | |
---|
1682 | n/a | def createAttribute(self, qName): |
---|
1683 | n/a | a = Attr(qName) |
---|
1684 | n/a | a.ownerDocument = self |
---|
1685 | n/a | a.value = "" |
---|
1686 | n/a | return a |
---|
1687 | n/a | |
---|
1688 | n/a | def createElementNS(self, namespaceURI, qualifiedName): |
---|
1689 | n/a | prefix, localName = _nssplit(qualifiedName) |
---|
1690 | n/a | e = Element(qualifiedName, namespaceURI, prefix) |
---|
1691 | n/a | e.ownerDocument = self |
---|
1692 | n/a | return e |
---|
1693 | n/a | |
---|
1694 | n/a | def createAttributeNS(self, namespaceURI, qualifiedName): |
---|
1695 | n/a | prefix, localName = _nssplit(qualifiedName) |
---|
1696 | n/a | a = Attr(qualifiedName, namespaceURI, localName, prefix) |
---|
1697 | n/a | a.ownerDocument = self |
---|
1698 | n/a | a.value = "" |
---|
1699 | n/a | return a |
---|
1700 | n/a | |
---|
1701 | n/a | # A couple of implementation-specific helpers to create node types |
---|
1702 | n/a | # not supported by the W3C DOM specs: |
---|
1703 | n/a | |
---|
1704 | n/a | def _create_entity(self, name, publicId, systemId, notationName): |
---|
1705 | n/a | e = Entity(name, publicId, systemId, notationName) |
---|
1706 | n/a | e.ownerDocument = self |
---|
1707 | n/a | return e |
---|
1708 | n/a | |
---|
1709 | n/a | def _create_notation(self, name, publicId, systemId): |
---|
1710 | n/a | n = Notation(name, publicId, systemId) |
---|
1711 | n/a | n.ownerDocument = self |
---|
1712 | n/a | return n |
---|
1713 | n/a | |
---|
1714 | n/a | def getElementById(self, id): |
---|
1715 | n/a | if id in self._id_cache: |
---|
1716 | n/a | return self._id_cache[id] |
---|
1717 | n/a | if not (self._elem_info or self._magic_id_count): |
---|
1718 | n/a | return None |
---|
1719 | n/a | |
---|
1720 | n/a | stack = self._id_search_stack |
---|
1721 | n/a | if stack is None: |
---|
1722 | n/a | # we never searched before, or the cache has been cleared |
---|
1723 | n/a | stack = [self.documentElement] |
---|
1724 | n/a | self._id_search_stack = stack |
---|
1725 | n/a | elif not stack: |
---|
1726 | n/a | # Previous search was completed and cache is still valid; |
---|
1727 | n/a | # no matching node. |
---|
1728 | n/a | return None |
---|
1729 | n/a | |
---|
1730 | n/a | result = None |
---|
1731 | n/a | while stack: |
---|
1732 | n/a | node = stack.pop() |
---|
1733 | n/a | # add child elements to stack for continued searching |
---|
1734 | n/a | stack.extend([child for child in node.childNodes |
---|
1735 | n/a | if child.nodeType in _nodeTypes_with_children]) |
---|
1736 | n/a | # check this node |
---|
1737 | n/a | info = self._get_elem_info(node) |
---|
1738 | n/a | if info: |
---|
1739 | n/a | # We have to process all ID attributes before |
---|
1740 | n/a | # returning in order to get all the attributes set to |
---|
1741 | n/a | # be IDs using Element.setIdAttribute*(). |
---|
1742 | n/a | for attr in node.attributes.values(): |
---|
1743 | n/a | if attr.namespaceURI: |
---|
1744 | n/a | if info.isIdNS(attr.namespaceURI, attr.localName): |
---|
1745 | n/a | self._id_cache[attr.value] = node |
---|
1746 | n/a | if attr.value == id: |
---|
1747 | n/a | result = node |
---|
1748 | n/a | elif not node._magic_id_nodes: |
---|
1749 | n/a | break |
---|
1750 | n/a | elif info.isId(attr.name): |
---|
1751 | n/a | self._id_cache[attr.value] = node |
---|
1752 | n/a | if attr.value == id: |
---|
1753 | n/a | result = node |
---|
1754 | n/a | elif not node._magic_id_nodes: |
---|
1755 | n/a | break |
---|
1756 | n/a | elif attr._is_id: |
---|
1757 | n/a | self._id_cache[attr.value] = node |
---|
1758 | n/a | if attr.value == id: |
---|
1759 | n/a | result = node |
---|
1760 | n/a | elif node._magic_id_nodes == 1: |
---|
1761 | n/a | break |
---|
1762 | n/a | elif node._magic_id_nodes: |
---|
1763 | n/a | for attr in node.attributes.values(): |
---|
1764 | n/a | if attr._is_id: |
---|
1765 | n/a | self._id_cache[attr.value] = node |
---|
1766 | n/a | if attr.value == id: |
---|
1767 | n/a | result = node |
---|
1768 | n/a | if result is not None: |
---|
1769 | n/a | break |
---|
1770 | n/a | return result |
---|
1771 | n/a | |
---|
1772 | n/a | def getElementsByTagName(self, name): |
---|
1773 | n/a | return _get_elements_by_tagName_helper(self, name, NodeList()) |
---|
1774 | n/a | |
---|
1775 | n/a | def getElementsByTagNameNS(self, namespaceURI, localName): |
---|
1776 | n/a | return _get_elements_by_tagName_ns_helper( |
---|
1777 | n/a | self, namespaceURI, localName, NodeList()) |
---|
1778 | n/a | |
---|
1779 | n/a | def isSupported(self, feature, version): |
---|
1780 | n/a | return self.implementation.hasFeature(feature, version) |
---|
1781 | n/a | |
---|
1782 | n/a | def importNode(self, node, deep): |
---|
1783 | n/a | if node.nodeType == Node.DOCUMENT_NODE: |
---|
1784 | n/a | raise xml.dom.NotSupportedErr("cannot import document nodes") |
---|
1785 | n/a | elif node.nodeType == Node.DOCUMENT_TYPE_NODE: |
---|
1786 | n/a | raise xml.dom.NotSupportedErr("cannot import document type nodes") |
---|
1787 | n/a | return _clone_node(node, deep, self) |
---|
1788 | n/a | |
---|
1789 | n/a | def writexml(self, writer, indent="", addindent="", newl="", encoding=None): |
---|
1790 | n/a | if encoding is None: |
---|
1791 | n/a | writer.write('<?xml version="1.0" ?>'+newl) |
---|
1792 | n/a | else: |
---|
1793 | n/a | writer.write('<?xml version="1.0" encoding="%s"?>%s' % ( |
---|
1794 | n/a | encoding, newl)) |
---|
1795 | n/a | for node in self.childNodes: |
---|
1796 | n/a | node.writexml(writer, indent, addindent, newl) |
---|
1797 | n/a | |
---|
1798 | n/a | # DOM Level 3 (WD 9 April 2002) |
---|
1799 | n/a | |
---|
1800 | n/a | def renameNode(self, n, namespaceURI, name): |
---|
1801 | n/a | if n.ownerDocument is not self: |
---|
1802 | n/a | raise xml.dom.WrongDocumentErr( |
---|
1803 | n/a | "cannot rename nodes from other documents;\n" |
---|
1804 | n/a | "expected %s,\nfound %s" % (self, n.ownerDocument)) |
---|
1805 | n/a | if n.nodeType not in (Node.ELEMENT_NODE, Node.ATTRIBUTE_NODE): |
---|
1806 | n/a | raise xml.dom.NotSupportedErr( |
---|
1807 | n/a | "renameNode() only applies to element and attribute nodes") |
---|
1808 | n/a | if namespaceURI != EMPTY_NAMESPACE: |
---|
1809 | n/a | if ':' in name: |
---|
1810 | n/a | prefix, localName = name.split(':', 1) |
---|
1811 | n/a | if ( prefix == "xmlns" |
---|
1812 | n/a | and namespaceURI != xml.dom.XMLNS_NAMESPACE): |
---|
1813 | n/a | raise xml.dom.NamespaceErr( |
---|
1814 | n/a | "illegal use of 'xmlns' prefix") |
---|
1815 | n/a | else: |
---|
1816 | n/a | if ( name == "xmlns" |
---|
1817 | n/a | and namespaceURI != xml.dom.XMLNS_NAMESPACE |
---|
1818 | n/a | and n.nodeType == Node.ATTRIBUTE_NODE): |
---|
1819 | n/a | raise xml.dom.NamespaceErr( |
---|
1820 | n/a | "illegal use of the 'xmlns' attribute") |
---|
1821 | n/a | prefix = None |
---|
1822 | n/a | localName = name |
---|
1823 | n/a | else: |
---|
1824 | n/a | prefix = None |
---|
1825 | n/a | localName = None |
---|
1826 | n/a | if n.nodeType == Node.ATTRIBUTE_NODE: |
---|
1827 | n/a | element = n.ownerElement |
---|
1828 | n/a | if element is not None: |
---|
1829 | n/a | is_id = n._is_id |
---|
1830 | n/a | element.removeAttributeNode(n) |
---|
1831 | n/a | else: |
---|
1832 | n/a | element = None |
---|
1833 | n/a | n.prefix = prefix |
---|
1834 | n/a | n._localName = localName |
---|
1835 | n/a | n.namespaceURI = namespaceURI |
---|
1836 | n/a | n.nodeName = name |
---|
1837 | n/a | if n.nodeType == Node.ELEMENT_NODE: |
---|
1838 | n/a | n.tagName = name |
---|
1839 | n/a | else: |
---|
1840 | n/a | # attribute node |
---|
1841 | n/a | n.name = name |
---|
1842 | n/a | if element is not None: |
---|
1843 | n/a | element.setAttributeNode(n) |
---|
1844 | n/a | if is_id: |
---|
1845 | n/a | element.setIdAttributeNode(n) |
---|
1846 | n/a | # It's not clear from a semantic perspective whether we should |
---|
1847 | n/a | # call the user data handlers for the NODE_RENAMED event since |
---|
1848 | n/a | # we're re-using the existing node. The draft spec has been |
---|
1849 | n/a | # interpreted as meaning "no, don't call the handler unless a |
---|
1850 | n/a | # new node is created." |
---|
1851 | n/a | return n |
---|
1852 | n/a | |
---|
1853 | n/a | defproperty(Document, "documentElement", |
---|
1854 | n/a | doc="Top-level element of this document.") |
---|
1855 | n/a | |
---|
1856 | n/a | |
---|
1857 | n/a | def _clone_node(node, deep, newOwnerDocument): |
---|
1858 | n/a | """ |
---|
1859 | n/a | Clone a node and give it the new owner document. |
---|
1860 | n/a | Called by Node.cloneNode and Document.importNode |
---|
1861 | n/a | """ |
---|
1862 | n/a | if node.ownerDocument.isSameNode(newOwnerDocument): |
---|
1863 | n/a | operation = xml.dom.UserDataHandler.NODE_CLONED |
---|
1864 | n/a | else: |
---|
1865 | n/a | operation = xml.dom.UserDataHandler.NODE_IMPORTED |
---|
1866 | n/a | if node.nodeType == Node.ELEMENT_NODE: |
---|
1867 | n/a | clone = newOwnerDocument.createElementNS(node.namespaceURI, |
---|
1868 | n/a | node.nodeName) |
---|
1869 | n/a | for attr in node.attributes.values(): |
---|
1870 | n/a | clone.setAttributeNS(attr.namespaceURI, attr.nodeName, attr.value) |
---|
1871 | n/a | a = clone.getAttributeNodeNS(attr.namespaceURI, attr.localName) |
---|
1872 | n/a | a.specified = attr.specified |
---|
1873 | n/a | |
---|
1874 | n/a | if deep: |
---|
1875 | n/a | for child in node.childNodes: |
---|
1876 | n/a | c = _clone_node(child, deep, newOwnerDocument) |
---|
1877 | n/a | clone.appendChild(c) |
---|
1878 | n/a | |
---|
1879 | n/a | elif node.nodeType == Node.DOCUMENT_FRAGMENT_NODE: |
---|
1880 | n/a | clone = newOwnerDocument.createDocumentFragment() |
---|
1881 | n/a | if deep: |
---|
1882 | n/a | for child in node.childNodes: |
---|
1883 | n/a | c = _clone_node(child, deep, newOwnerDocument) |
---|
1884 | n/a | clone.appendChild(c) |
---|
1885 | n/a | |
---|
1886 | n/a | elif node.nodeType == Node.TEXT_NODE: |
---|
1887 | n/a | clone = newOwnerDocument.createTextNode(node.data) |
---|
1888 | n/a | elif node.nodeType == Node.CDATA_SECTION_NODE: |
---|
1889 | n/a | clone = newOwnerDocument.createCDATASection(node.data) |
---|
1890 | n/a | elif node.nodeType == Node.PROCESSING_INSTRUCTION_NODE: |
---|
1891 | n/a | clone = newOwnerDocument.createProcessingInstruction(node.target, |
---|
1892 | n/a | node.data) |
---|
1893 | n/a | elif node.nodeType == Node.COMMENT_NODE: |
---|
1894 | n/a | clone = newOwnerDocument.createComment(node.data) |
---|
1895 | n/a | elif node.nodeType == Node.ATTRIBUTE_NODE: |
---|
1896 | n/a | clone = newOwnerDocument.createAttributeNS(node.namespaceURI, |
---|
1897 | n/a | node.nodeName) |
---|
1898 | n/a | clone.specified = True |
---|
1899 | n/a | clone.value = node.value |
---|
1900 | n/a | elif node.nodeType == Node.DOCUMENT_TYPE_NODE: |
---|
1901 | n/a | assert node.ownerDocument is not newOwnerDocument |
---|
1902 | n/a | operation = xml.dom.UserDataHandler.NODE_IMPORTED |
---|
1903 | n/a | clone = newOwnerDocument.implementation.createDocumentType( |
---|
1904 | n/a | node.name, node.publicId, node.systemId) |
---|
1905 | n/a | clone.ownerDocument = newOwnerDocument |
---|
1906 | n/a | if deep: |
---|
1907 | n/a | clone.entities._seq = [] |
---|
1908 | n/a | clone.notations._seq = [] |
---|
1909 | n/a | for n in node.notations._seq: |
---|
1910 | n/a | notation = Notation(n.nodeName, n.publicId, n.systemId) |
---|
1911 | n/a | notation.ownerDocument = newOwnerDocument |
---|
1912 | n/a | clone.notations._seq.append(notation) |
---|
1913 | n/a | if hasattr(n, '_call_user_data_handler'): |
---|
1914 | n/a | n._call_user_data_handler(operation, n, notation) |
---|
1915 | n/a | for e in node.entities._seq: |
---|
1916 | n/a | entity = Entity(e.nodeName, e.publicId, e.systemId, |
---|
1917 | n/a | e.notationName) |
---|
1918 | n/a | entity.actualEncoding = e.actualEncoding |
---|
1919 | n/a | entity.encoding = e.encoding |
---|
1920 | n/a | entity.version = e.version |
---|
1921 | n/a | entity.ownerDocument = newOwnerDocument |
---|
1922 | n/a | clone.entities._seq.append(entity) |
---|
1923 | n/a | if hasattr(e, '_call_user_data_handler'): |
---|
1924 | n/a | e._call_user_data_handler(operation, n, entity) |
---|
1925 | n/a | else: |
---|
1926 | n/a | # Note the cloning of Document and DocumentType nodes is |
---|
1927 | n/a | # implementation specific. minidom handles those cases |
---|
1928 | n/a | # directly in the cloneNode() methods. |
---|
1929 | n/a | raise xml.dom.NotSupportedErr("Cannot clone node %s" % repr(node)) |
---|
1930 | n/a | |
---|
1931 | n/a | # Check for _call_user_data_handler() since this could conceivably |
---|
1932 | n/a | # used with other DOM implementations (one of the FourThought |
---|
1933 | n/a | # DOMs, perhaps?). |
---|
1934 | n/a | if hasattr(node, '_call_user_data_handler'): |
---|
1935 | n/a | node._call_user_data_handler(operation, node, clone) |
---|
1936 | n/a | return clone |
---|
1937 | n/a | |
---|
1938 | n/a | |
---|
1939 | n/a | def _nssplit(qualifiedName): |
---|
1940 | n/a | fields = qualifiedName.split(':', 1) |
---|
1941 | n/a | if len(fields) == 2: |
---|
1942 | n/a | return fields |
---|
1943 | n/a | else: |
---|
1944 | n/a | return (None, fields[0]) |
---|
1945 | n/a | |
---|
1946 | n/a | |
---|
1947 | n/a | def _do_pulldom_parse(func, args, kwargs): |
---|
1948 | n/a | events = func(*args, **kwargs) |
---|
1949 | n/a | toktype, rootNode = events.getEvent() |
---|
1950 | n/a | events.expandNode(rootNode) |
---|
1951 | n/a | events.clear() |
---|
1952 | n/a | return rootNode |
---|
1953 | n/a | |
---|
1954 | n/a | def parse(file, parser=None, bufsize=None): |
---|
1955 | n/a | """Parse a file into a DOM by filename or file object.""" |
---|
1956 | n/a | if parser is None and not bufsize: |
---|
1957 | n/a | from xml.dom import expatbuilder |
---|
1958 | n/a | return expatbuilder.parse(file) |
---|
1959 | n/a | else: |
---|
1960 | n/a | from xml.dom import pulldom |
---|
1961 | n/a | return _do_pulldom_parse(pulldom.parse, (file,), |
---|
1962 | n/a | {'parser': parser, 'bufsize': bufsize}) |
---|
1963 | n/a | |
---|
1964 | n/a | def parseString(string, parser=None): |
---|
1965 | n/a | """Parse a file into a DOM from a string.""" |
---|
1966 | n/a | if parser is None: |
---|
1967 | n/a | from xml.dom import expatbuilder |
---|
1968 | n/a | return expatbuilder.parseString(string) |
---|
1969 | n/a | else: |
---|
1970 | n/a | from xml.dom import pulldom |
---|
1971 | n/a | return _do_pulldom_parse(pulldom.parseString, (string,), |
---|
1972 | n/a | {'parser': parser}) |
---|
1973 | n/a | |
---|
1974 | n/a | def getDOMImplementation(features=None): |
---|
1975 | n/a | if features: |
---|
1976 | n/a | if isinstance(features, str): |
---|
1977 | n/a | features = domreg._parse_feature_string(features) |
---|
1978 | n/a | for f, v in features: |
---|
1979 | n/a | if not Document.implementation.hasFeature(f, v): |
---|
1980 | n/a | return None |
---|
1981 | n/a | return Document.implementation |
---|