paulb@46 | 1 | #!/usr/bin/env python |
paulb@46 | 2 | |
paulb@46 | 3 | """ |
paulb@55 | 4 | DOM macros for virtual libxml2mod node methods and properties. |
paulb@188 | 5 | |
paulb@227 | 6 | Copyright (C) 2003, 2004, 2005, 2006, 2007 Paul Boddie <paul@boddie.org.uk> |
paulb@188 | 7 | |
paulb@293 | 8 | This program is free software; you can redistribute it and/or modify it under |
paulb@293 | 9 | the terms of the GNU Lesser General Public License as published by the Free |
paulb@293 | 10 | Software Foundation; either version 3 of the License, or (at your option) any |
paulb@293 | 11 | later version. |
paulb@188 | 12 | |
paulb@293 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paulb@293 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paulb@293 | 15 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
paulb@293 | 16 | details. |
paulb@188 | 17 | |
paulb@293 | 18 | You should have received a copy of the GNU Lesser General Public License along |
paulb@293 | 19 | with this program. If not, see <http://www.gnu.org/licenses/>. |
paulb@46 | 20 | """ |
paulb@46 | 21 | |
paulb@46 | 22 | import xml.dom |
paulb@214 | 23 | |
paulb@214 | 24 | # Try the conventional import first. |
paulb@214 | 25 | |
paulb@214 | 26 | try: |
paulb@214 | 27 | import libxml2mod |
paulb@214 | 28 | except ImportError: |
paulb@214 | 29 | from libxmlmods import libxml2mod |
paulb@46 | 30 | |
paulb@46 | 31 | # NOTE: libxml2 seems to use UTF-8 throughout. |
paulb@287 | 32 | # NOTE: Implement: http://www.w3.org/TR/2006/REC-xml-20060816/#AVNormalize |
paulb@46 | 33 | |
paulb@46 | 34 | def from_unicode(s): |
paulb@119 | 35 | if isinstance(s, unicode): |
paulb@46 | 36 | return s.encode("utf-8") |
paulb@46 | 37 | else: |
paulb@161 | 38 | # The string might contain non-ASCII characters, thus upsetting libxml2 |
paulb@161 | 39 | # as it encounters a non-UTF-8 string. |
paulb@161 | 40 | try: |
paulb@161 | 41 | unicode(s) |
paulb@161 | 42 | except UnicodeError: |
paulb@161 | 43 | raise TypeError, "Please use Unicode for non-ASCII data." |
paulb@46 | 44 | return s |
paulb@46 | 45 | |
paulb@46 | 46 | def to_unicode(s): |
paulb@119 | 47 | if isinstance(s, str): |
paulb@46 | 48 | return unicode(s, encoding="utf-8") |
paulb@46 | 49 | else: |
paulb@46 | 50 | return s |
paulb@46 | 51 | |
paulb@170 | 52 | def get_ns(ns): |
paulb@170 | 53 | out_ns = to_unicode(libxml2mod.xmlNodeGetContent(ns)) |
paulb@170 | 54 | # Detect "" and produce None as the empty namespace. |
paulb@170 | 55 | if out_ns: |
paulb@170 | 56 | return out_ns |
paulb@170 | 57 | else: |
paulb@170 | 58 | return None |
paulb@170 | 59 | |
paulb@46 | 60 | def _get_prefix_and_localName(name): |
paulb@46 | 61 | t = name.split(":") |
paulb@46 | 62 | if len(t) == 1: |
paulb@46 | 63 | return None, name |
paulb@46 | 64 | elif len(t) == 2: |
paulb@46 | 65 | return t |
paulb@46 | 66 | else: |
paulb@46 | 67 | # NOTE: Should raise an exception. |
paulb@46 | 68 | return None, None |
paulb@46 | 69 | |
paulb@196 | 70 | def _find_namespace_for_prefix(node, prefix): |
paulb@196 | 71 | |
paulb@196 | 72 | "Find the namespace definition node in the given 'node' for 'prefix'." |
paulb@196 | 73 | |
paulb@196 | 74 | current = libxml2mod.xmlNodeGetNsDefs(node) |
paulb@196 | 75 | while current is not None: |
paulb@196 | 76 | if libxml2mod.name(current) == prefix: |
paulb@196 | 77 | return current |
paulb@196 | 78 | current = libxml2mod.next(current) |
paulb@196 | 79 | return None |
paulb@196 | 80 | |
paulb@179 | 81 | def _find_namespace(node, ns, prefix): |
paulb@196 | 82 | |
paulb@196 | 83 | """ |
paulb@196 | 84 | Find the namespace definition node in the given 'node' for the given 'ns' |
paulb@196 | 85 | and 'prefix'. |
paulb@196 | 86 | """ |
paulb@196 | 87 | |
paulb@179 | 88 | new_ns = None |
paulb@179 | 89 | current = libxml2mod.xmlNodeGetNsDefs(node) |
paulb@179 | 90 | while current is not None: |
paulb@179 | 91 | if _check_namespace(current, ns, prefix): |
paulb@179 | 92 | new_ns = current |
paulb@179 | 93 | break |
paulb@179 | 94 | current = libxml2mod.next(current) |
paulb@179 | 95 | if new_ns is None: |
paulb@179 | 96 | node_ns = libxml2mod.xmlNodeGetNs(node) |
paulb@179 | 97 | if node_ns is not None and _check_namespace(node_ns, ns, prefix): |
paulb@179 | 98 | new_ns = node_ns |
paulb@179 | 99 | return new_ns |
paulb@179 | 100 | |
paulb@179 | 101 | def _check_namespace(current, ns, prefix): |
paulb@196 | 102 | |
paulb@196 | 103 | "Check the 'current' namespace definition node against 'ns' and 'prefix'." |
paulb@196 | 104 | |
paulb@196 | 105 | current_ns = get_ns(current) |
paulb@179 | 106 | current_prefix = libxml2mod.name(current) |
paulb@192 | 107 | if ns == current_ns and (prefix is None or prefix == current_prefix): |
paulb@179 | 108 | return 1 |
paulb@179 | 109 | else: |
paulb@179 | 110 | return 0 |
paulb@179 | 111 | |
paulb@179 | 112 | def _make_namespace(node, ns, prefix, set_default=0): |
paulb@196 | 113 | |
paulb@196 | 114 | """ |
paulb@196 | 115 | Make a new namespace definition node within the given 'node' for 'ns', |
paulb@196 | 116 | 'prefix', setting the default namespace on 'node' when 'prefix' is None and |
paulb@196 | 117 | 'set_default' is set to a true value (unlike the default value for that |
paulb@196 | 118 | parameter). |
paulb@196 | 119 | """ |
paulb@196 | 120 | |
paulb@179 | 121 | if prefix is not None or set_default: |
paulb@179 | 122 | new_ns = libxml2mod.xmlNewNs(node, ns, prefix) |
paulb@179 | 123 | else: |
paulb@179 | 124 | new_ns = None |
paulb@179 | 125 | return new_ns |
paulb@179 | 126 | |
paulb@192 | 127 | def _get_invented_prefix(node, ns): |
paulb@192 | 128 | current = libxml2mod.xmlNodeGetNsDefs(node) |
paulb@192 | 129 | prefixes = [] |
paulb@192 | 130 | while current is not None: |
paulb@192 | 131 | current_prefix = libxml2mod.name(current) |
paulb@192 | 132 | prefixes.append(current_prefix) |
paulb@192 | 133 | current = libxml2mod.next(current) |
paulb@192 | 134 | i = 0 |
paulb@192 | 135 | while 1: |
paulb@192 | 136 | prefix = "NS%d" % i |
paulb@192 | 137 | if prefix not in prefixes: |
paulb@192 | 138 | return prefix |
paulb@192 | 139 | i += 1 |
paulb@192 | 140 | |
paulb@46 | 141 | _nodeTypes = { |
paulb@46 | 142 | "attribute" : xml.dom.Node.ATTRIBUTE_NODE, |
paulb@243 | 143 | "cdata" : xml.dom.Node.CDATA_SECTION_NODE, |
paulb@46 | 144 | "comment" : xml.dom.Node.COMMENT_NODE, |
paulb@46 | 145 | "document_xml" : xml.dom.Node.DOCUMENT_NODE, |
paulb@203 | 146 | "document_html" : xml.dom.Node.DOCUMENT_NODE, |
paulb@46 | 147 | "doctype" : xml.dom.Node.DOCUMENT_TYPE_NODE, |
paulb@46 | 148 | "dtd" : xml.dom.Node.DOCUMENT_TYPE_NODE, # NOTE: Needs verifying. |
paulb@46 | 149 | "element" : xml.dom.Node.ELEMENT_NODE, |
paulb@46 | 150 | "entity" : xml.dom.Node.ENTITY_NODE, |
paulb@46 | 151 | "entity_ref" : xml.dom.Node.ENTITY_REFERENCE_NODE, |
paulb@46 | 152 | "notation" : xml.dom.Node.NOTATION_NODE, |
paulb@46 | 153 | "pi" : xml.dom.Node.PROCESSING_INSTRUCTION_NODE, |
paulb@46 | 154 | "text" : xml.dom.Node.TEXT_NODE |
paulb@46 | 155 | } |
paulb@46 | 156 | |
paulb@134 | 157 | _reverseNodeTypes = {} |
paulb@134 | 158 | for label, value in _nodeTypes.items(): |
paulb@134 | 159 | _reverseNodeTypes[value] = label |
paulb@134 | 160 | |
paulb@277 | 161 | def Node_equals(node, other): |
paulb@277 | 162 | return libxml2mod.xmlXPathCmpNodes(node, other) == 0 |
paulb@277 | 163 | |
paulb@46 | 164 | def Node_ownerDocument(node): |
paulb@229 | 165 | return libxml2mod.doc(node) |
paulb@46 | 166 | |
paulb@46 | 167 | def Node_nodeType(node): |
paulb@55 | 168 | return _nodeTypes[libxml2mod.type(node)] |
paulb@46 | 169 | |
paulb@46 | 170 | def Node_childNodes(node): |
paulb@46 | 171 | |
paulb@46 | 172 | # NOTE: Consider a generator instead. |
paulb@46 | 173 | |
paulb@46 | 174 | child_nodes = [] |
paulb@55 | 175 | node = libxml2mod.children(node) |
paulb@46 | 176 | while node is not None: |
paulb@155 | 177 | # Remove doctypes. |
paulb@155 | 178 | if Node_nodeType(node) != xml.dom.Node.DOCUMENT_TYPE_NODE: |
paulb@155 | 179 | child_nodes.append(node) |
paulb@55 | 180 | node = libxml2mod.next(node) |
paulb@46 | 181 | return child_nodes |
paulb@46 | 182 | |
paulb@46 | 183 | def Node_attributes(node): |
paulb@46 | 184 | attributes = {} |
paulb@196 | 185 | |
paulb@196 | 186 | # Include normal attributes. |
paulb@196 | 187 | |
paulb@196 | 188 | current = libxml2mod.properties(node) |
paulb@196 | 189 | while current is not None: |
paulb@196 | 190 | ns = libxml2mod.xmlNodeGetNs(current) |
paulb@46 | 191 | if ns is not None: |
paulb@196 | 192 | attributes[(get_ns(ns), libxml2mod.name(current))] = current |
paulb@46 | 193 | else: |
paulb@196 | 194 | attributes[(None, libxml2mod.name(current))] = current |
paulb@196 | 195 | current = libxml2mod.next(current) |
paulb@196 | 196 | |
paulb@196 | 197 | # Include xmlns attributes. |
paulb@196 | 198 | |
paulb@196 | 199 | #current = libxml2mod.xmlNodeGetNsDefs(node) |
paulb@196 | 200 | #while current is not None: |
paulb@196 | 201 | # ns = get_ns(current) |
paulb@196 | 202 | # prefix = libxml2mod.name(current) |
paulb@196 | 203 | # attributes[(xml.dom.XMLNS_NAMESPACE, "xmlns:" + prefix)] = ns # NOTE: Need a real node here. |
paulb@196 | 204 | # current = libxml2mod.next(current) |
paulb@196 | 205 | |
paulb@46 | 206 | return attributes |
paulb@46 | 207 | |
paulb@46 | 208 | def Node_namespaceURI(node): |
paulb@55 | 209 | ns = libxml2mod.xmlNodeGetNs(node) |
paulb@46 | 210 | if ns is not None: |
paulb@170 | 211 | return get_ns(ns) |
paulb@46 | 212 | else: |
paulb@46 | 213 | return None |
paulb@46 | 214 | |
paulb@46 | 215 | def Node_nodeValue(node): |
paulb@55 | 216 | return to_unicode(libxml2mod.xmlNodeGetContent(node)) |
paulb@46 | 217 | |
paulb@87 | 218 | # NOTE: This is not properly exposed in the libxml2macro interface as the |
paulb@87 | 219 | # NOTE: writable form of nodeValue. |
paulb@87 | 220 | |
paulb@87 | 221 | def Node_setNodeValue(node, value): |
paulb@87 | 222 | # NOTE: Cannot set attribute node values. |
paulb@87 | 223 | libxml2mod.xmlNodeSetContent(node, from_unicode(value)) |
paulb@87 | 224 | |
paulb@237 | 225 | # NOTE: Verify this. The data attribute should only really exist for text, |
paulb@237 | 226 | # NOTE: character data, processing instructions and comments. |
paulb@73 | 227 | |
paulb@73 | 228 | Node_data = Node_nodeValue |
paulb@73 | 229 | |
paulb@237 | 230 | Node_textContent = Node_nodeValue |
paulb@237 | 231 | |
paulb@46 | 232 | def Node_prefix(node): |
paulb@55 | 233 | ns = libxml2mod.xmlNodeGetNs(node) |
paulb@46 | 234 | if ns is not None: |
paulb@55 | 235 | return to_unicode(libxml2mod.name(ns)) |
paulb@46 | 236 | else: |
paulb@46 | 237 | return None |
paulb@46 | 238 | |
paulb@46 | 239 | def Node_nodeName(node): |
paulb@46 | 240 | prefix = Node_prefix(node) |
paulb@46 | 241 | if prefix is not None: |
paulb@46 | 242 | return prefix + ":" + Node_localName(node) |
paulb@46 | 243 | else: |
paulb@46 | 244 | return Node_localName(node) |
paulb@46 | 245 | |
paulb@46 | 246 | def Node_tagName(node): |
paulb@55 | 247 | if libxml2mod.type(node) == "element": |
paulb@46 | 248 | return Node_nodeName(node) |
paulb@46 | 249 | else: |
paulb@46 | 250 | return None |
paulb@46 | 251 | |
paulb@46 | 252 | def Node_localName(node): |
paulb@55 | 253 | return to_unicode(libxml2mod.name(node)) |
paulb@46 | 254 | |
paulb@46 | 255 | def Node_parentNode(node): |
paulb@55 | 256 | if libxml2mod.type(node) == "document_xml": |
paulb@46 | 257 | return None |
paulb@46 | 258 | else: |
paulb@55 | 259 | return libxml2mod.parent(node) |
paulb@46 | 260 | |
paulb@46 | 261 | def Node_previousSibling(node): |
paulb@55 | 262 | if libxml2mod.prev(node) is not None: |
paulb@55 | 263 | return libxml2mod.prev(node) |
paulb@46 | 264 | else: |
paulb@46 | 265 | return None |
paulb@46 | 266 | |
paulb@46 | 267 | def Node_nextSibling(node): |
paulb@55 | 268 | if libxml2mod.next(node) is not None: |
paulb@55 | 269 | return libxml2mod.next(node) |
paulb@46 | 270 | else: |
paulb@46 | 271 | return None |
paulb@46 | 272 | |
paulb@155 | 273 | def Node_doctype(node): |
paulb@155 | 274 | return libxml2mod.xmlGetIntSubset(node) |
paulb@155 | 275 | |
paulb@46 | 276 | def Node_hasAttributeNS(node, ns, localName): |
paulb@196 | 277 | return Node_getAttributeNS(node, ns, localName) is not None or \ |
paulb@196 | 278 | _find_namespace(node, ns, localName) is not None |
paulb@46 | 279 | |
paulb@46 | 280 | def Node_hasAttribute(node, name): |
paulb@92 | 281 | return Node_getAttribute(node, name) is not None |
paulb@46 | 282 | |
paulb@46 | 283 | def Node_getAttributeNS(node, ns, localName): |
paulb@196 | 284 | if ns == xml.dom.XMLNS_NAMESPACE: |
paulb@196 | 285 | ns_def = _find_namespace_for_prefix(node, localName) |
paulb@196 | 286 | if ns_def is not None: |
paulb@196 | 287 | return get_ns(ns_def) |
paulb@196 | 288 | else: |
paulb@196 | 289 | return None |
paulb@196 | 290 | else: |
paulb@196 | 291 | return to_unicode(libxml2mod.xmlGetNsProp(node, localName, ns)) |
paulb@46 | 292 | |
paulb@46 | 293 | def Node_getAttribute(node, name): |
paulb@55 | 294 | return to_unicode(libxml2mod.xmlGetProp(node, name)) |
paulb@46 | 295 | |
paulb@46 | 296 | def Node_getAttributeNodeNS(node, ns, localName): |
paulb@55 | 297 | # NOTE: Needs verifying. |
paulb@87 | 298 | return Node_attributes(node)[(ns, localName)] |
paulb@46 | 299 | |
paulb@46 | 300 | def Node_getAttributeNode(node, name): |
paulb@46 | 301 | # NOTE: Needs verifying. |
paulb@87 | 302 | return Node_attributes(node)[(None, name)] |
paulb@46 | 303 | |
paulb@46 | 304 | def Node_setAttributeNS(node, ns, name, value): |
paulb@46 | 305 | ns, name, value = map(from_unicode, [ns, name, value]) |
paulb@46 | 306 | prefix, localName = _get_prefix_and_localName(name) |
paulb@184 | 307 | |
paulb@184 | 308 | # Detect setting of xmlns:localName=value, looking for cases where |
paulb@184 | 309 | # x:attr=value have caused the definition of xmlns:x=y (as a declaration |
paulb@184 | 310 | # with prefix=x, ns=y). |
paulb@184 | 311 | if prefix == "xmlns" and ns == xml.dom.XMLNS_NAMESPACE: |
paulb@184 | 312 | if _find_namespace(node, value, localName): |
paulb@179 | 313 | return |
paulb@184 | 314 | new_ns = _make_namespace(node, value, localName, set_default=0) |
paulb@184 | 315 | # For non-xmlns attributes, we find or make a namespace declaration and then |
paulb@184 | 316 | # set an attribute. |
paulb@184 | 317 | elif ns is not None: |
paulb@192 | 318 | # Look for a suitable namespace. |
paulb@179 | 319 | new_ns = _find_namespace(node, ns, prefix) |
paulb@192 | 320 | # Create a declaration if no suitable one was found. |
paulb@179 | 321 | if new_ns is None: |
paulb@192 | 322 | # Invent a prefix for unprefixed attributes with namespaces. |
paulb@192 | 323 | if prefix is None: |
paulb@192 | 324 | prefix = _get_invented_prefix(node, ns) |
paulb@179 | 325 | new_ns = _make_namespace(node, ns, prefix, set_default=0) |
paulb@179 | 326 | libxml2mod.xmlSetNsProp(node, new_ns, localName, value) |
paulb@46 | 327 | else: |
paulb@46 | 328 | # NOTE: Needs verifying: what should happen to the namespace? |
paulb@46 | 329 | # NOTE: This also catches the case where None is the element's |
paulb@46 | 330 | # NOTE: namespace and is also used for the attribute. |
paulb@179 | 331 | libxml2mod.xmlSetNsProp(node, None, localName, value) |
paulb@46 | 332 | |
paulb@46 | 333 | def Node_setAttribute(node, name, value): |
paulb@46 | 334 | name, value = map(from_unicode, [name, value]) |
paulb@46 | 335 | |
paulb@55 | 336 | libxml2mod.xmlSetProp(node, name, value) |
paulb@55 | 337 | |
paulb@87 | 338 | def Node_setAttributeNodeNS(node, attr): |
paulb@87 | 339 | # NOTE: Not actually putting the node on the element. |
paulb@87 | 340 | Node_setAttributeNS(node, Node_namespaceURI(attr), Node_nodeName(attr), Node_nodeValue(attr)) |
paulb@55 | 341 | |
paulb@87 | 342 | def Node_setAttributeNode(node, attr): |
paulb@46 | 343 | # NOTE: Not actually putting the node on the element. |
paulb@87 | 344 | Node_setAttribute(node, Node_nodeName(attr), Node_nodeValue(attr)) |
paulb@46 | 345 | |
paulb@92 | 346 | def Node_removeAttributeNS(node, ns, localName): |
paulb@92 | 347 | attr = Node_getAttributeNodeNS(node, ns, localName) |
paulb@92 | 348 | libxml2mod.xmlUnsetNsProp(node, libxml2mod.xmlNodeGetNs(attr), libxml2mod.name(attr)) |
paulb@92 | 349 | |
paulb@92 | 350 | def Node_removeAttribute(node, name): |
paulb@92 | 351 | name = from_unicode(name) |
paulb@92 | 352 | libxml2mod.xmlUnsetProp(node, name) |
paulb@92 | 353 | |
paulb@46 | 354 | def Node_createElementNS(node, ns, name): |
paulb@46 | 355 | ns, name = map(from_unicode, [ns, name]) |
paulb@46 | 356 | |
paulb@46 | 357 | prefix, localName = _get_prefix_and_localName(name) |
paulb@55 | 358 | new_node = libxml2mod.xmlNewNode(localName) |
paulb@166 | 359 | |
paulb@166 | 360 | # If the namespace is not empty, set the declaration. |
paulb@46 | 361 | if ns is not None: |
paulb@179 | 362 | new_ns = _find_namespace(new_node, ns, prefix) |
paulb@179 | 363 | if new_ns is None: |
paulb@179 | 364 | new_ns = _make_namespace(new_node, ns, prefix, set_default=1) |
paulb@55 | 365 | libxml2mod.xmlSetNs(new_node, new_ns) |
paulb@166 | 366 | # If the namespace is empty, set a "null" declaration. |
paulb@177 | 367 | elif prefix is not None: |
paulb@179 | 368 | new_ns = _find_namespace(new_node, "", prefix) |
paulb@179 | 369 | if new_ns is None: |
paulb@179 | 370 | new_ns = _make_namespace(new_node, "", prefix) |
paulb@177 | 371 | libxml2mod.xmlSetNs(new_node, new_ns) |
paulb@166 | 372 | else: |
paulb@174 | 373 | libxml2mod.xmlSetNs(new_node, None) |
paulb@174 | 374 | Node_setAttribute(new_node, "xmlns", "") |
paulb@46 | 375 | return new_node |
paulb@46 | 376 | |
paulb@46 | 377 | def Node_createElement(node, name): |
paulb@46 | 378 | name = from_unicode(name) |
paulb@46 | 379 | |
paulb@55 | 380 | new_node = libxml2mod.xmlNewNode(name) |
paulb@46 | 381 | return new_node |
paulb@46 | 382 | |
paulb@46 | 383 | def Node_createAttributeNS(node, ns, name): |
paulb@46 | 384 | ns, name = map(from_unicode, [ns, name]) |
paulb@46 | 385 | |
paulb@46 | 386 | prefix, localName = _get_prefix_and_localName(name) |
paulb@46 | 387 | # NOTE: Does it make sense to set the namespace if it is empty? |
paulb@46 | 388 | if ns is not None: |
paulb@179 | 389 | new_ns = _find_namespace(node, ns, prefix) |
paulb@179 | 390 | if new_ns is None: |
paulb@179 | 391 | new_ns = _make_namespace(node, ns, prefix, set_default=0) |
paulb@46 | 392 | else: |
paulb@46 | 393 | new_ns = None |
paulb@89 | 394 | new_node = libxml2mod.xmlNewNsProp(node, new_ns, localName, None) |
paulb@87 | 395 | return new_node |
paulb@46 | 396 | |
paulb@46 | 397 | def Node_createAttribute(node, name): |
paulb@46 | 398 | name = from_unicode(name) |
paulb@46 | 399 | |
paulb@87 | 400 | # NOTE: xmlNewProp does not seem to work. |
paulb@87 | 401 | return Node_createAttributeNS(node, None, name) |
paulb@46 | 402 | |
paulb@46 | 403 | def Node_createTextNode(node, value): |
paulb@46 | 404 | value = from_unicode(value) |
paulb@46 | 405 | |
paulb@55 | 406 | return libxml2mod.xmlNewText(value) |
paulb@46 | 407 | |
paulb@46 | 408 | def Node_createComment(node, value): |
paulb@46 | 409 | value = from_unicode(value) |
paulb@46 | 410 | |
paulb@55 | 411 | return libxml2mod.xmlNewComment(value) |
paulb@46 | 412 | |
paulb@251 | 413 | def Node_createCDATASection(node, value): |
paulb@251 | 414 | value = from_unicode(value) |
paulb@251 | 415 | |
paulb@251 | 416 | return libxml2mod.xmlNewCDataBlock(Node_ownerDocument(node), value, len(value)) |
paulb@251 | 417 | |
paulb@46 | 418 | def Node_insertBefore(node, tmp, oldNode): |
paulb@87 | 419 | return libxml2mod.xmlAddPrevSibling(oldNode, tmp) |
paulb@46 | 420 | |
paulb@46 | 421 | def Node_replaceChild(node, tmp, oldNode): |
paulb@87 | 422 | return libxml2mod.xmlReplaceNode(oldNode, tmp) |
paulb@46 | 423 | |
paulb@46 | 424 | def Node_appendChild(node, tmp): |
paulb@55 | 425 | return libxml2mod.xmlAddChild(node, tmp) |
paulb@46 | 426 | |
paulb@46 | 427 | def Node_removeChild(node, child): |
paulb@85 | 428 | libxml2mod.xmlUnlinkNode(child) |
paulb@46 | 429 | |
paulb@73 | 430 | def Node_importNode(node, other, deep): |
paulb@73 | 431 | if Node_nodeType(other) == xml.dom.Node.ELEMENT_NODE: |
paulb@73 | 432 | imported_element = Node_createElementNS(node, Node_namespaceURI(other), Node_tagName(other)) |
paulb@73 | 433 | for attr in Node_attributes(other).values(): |
paulb@73 | 434 | Node_setAttributeNS(imported_element, Node_namespaceURI(attr), Node_nodeName(attr), Node_nodeValue(attr)) |
paulb@73 | 435 | |
paulb@73 | 436 | if deep: |
paulb@73 | 437 | for child in Node_childNodes(other): |
paulb@73 | 438 | imported_child = Node_importNode(node, child, deep) |
paulb@73 | 439 | if imported_child: |
paulb@73 | 440 | Node_appendChild(imported_element, imported_child) |
paulb@73 | 441 | |
paulb@73 | 442 | return imported_element |
paulb@73 | 443 | |
paulb@73 | 444 | elif Node_nodeType(other) == xml.dom.Node.TEXT_NODE: |
paulb@73 | 445 | return Node_createTextNode(node, Node_nodeValue(other)) |
paulb@73 | 446 | |
paulb@73 | 447 | elif Node_nodeType(other) == xml.dom.Node.COMMENT_NODE: |
paulb@73 | 448 | return Node_createComment(node, Node_data(other)) |
paulb@73 | 449 | |
paulb@251 | 450 | elif Node_nodeType(other) == xml.dom.Node.CDATA_SECTION_NODE: |
paulb@251 | 451 | return Node_createCDATASection(node, Node_data(other)) |
paulb@251 | 452 | |
paulb@227 | 453 | raise xml.dom.DOMException(xml.dom.NOT_SUPPORTED_ERR, "Node type '%s' (%d) not supported." % (other, Node_nodeType(other))) |
paulb@73 | 454 | |
paulb@73 | 455 | def Node_importNode_DOM(node, other, deep): |
paulb@73 | 456 | if other.nodeType == xml.dom.Node.ELEMENT_NODE: |
paulb@73 | 457 | imported_element = Node_createElementNS(node, other.namespaceURI, other.tagName) |
paulb@73 | 458 | for attr in other.attributes.values(): |
paulb@73 | 459 | Node_setAttributeNS(imported_element, attr.namespaceURI, attr.nodeName, attr.nodeValue) |
paulb@73 | 460 | |
paulb@73 | 461 | if deep: |
paulb@73 | 462 | for child in other.childNodes: |
paulb@73 | 463 | imported_child = Node_importNode_DOM(node, child, deep) |
paulb@73 | 464 | if imported_child: |
paulb@73 | 465 | Node_appendChild(imported_element, imported_child) |
paulb@73 | 466 | |
paulb@73 | 467 | return imported_element |
paulb@73 | 468 | |
paulb@73 | 469 | elif other.nodeType == xml.dom.Node.TEXT_NODE: |
paulb@73 | 470 | return Node_createTextNode(node, other.nodeValue) |
paulb@73 | 471 | |
paulb@73 | 472 | elif other.nodeType == xml.dom.Node.COMMENT_NODE: |
paulb@73 | 473 | return Node_createComment(node, other.data) |
paulb@73 | 474 | |
paulb@251 | 475 | elif other.nodeType == xml.dom.Node.CDATA_SECTION_NODE: |
paulb@251 | 476 | return Node_createCDATASection(node, other.data) |
paulb@251 | 477 | |
paulb@227 | 478 | raise xml.dom.DOMException(xml.dom.NOT_SUPPORTED_ERR, "Node type '%s' (%d) not supported." % (_reverseNodeTypes[other.nodeType], other.nodeType)) |
paulb@73 | 479 | |
paulb@46 | 480 | def Node_xpath(node, expr, variables=None, namespaces=None): |
paulb@149 | 481 | expr = from_unicode(expr) |
paulb@149 | 482 | |
paulb@230 | 483 | context = libxml2mod.xmlXPathNewContext(Node_ownerDocument(node) or node) |
paulb@82 | 484 | libxml2mod.xmlXPathSetContextNode(context, node) |
paulb@46 | 485 | # NOTE: Discover namespaces from the node. |
paulb@97 | 486 | # NOTE: Work out how to specify paths without having to use prefixes on |
paulb@97 | 487 | # NOTE: names all the time. |
paulb@46 | 488 | for prefix, ns in (namespaces or {}).items(): |
paulb@55 | 489 | libxml2mod.xmlXPathRegisterNs(context, prefix, ns) |
paulb@97 | 490 | # NOTE: No such functions are exposed in current versions of libxml2. |
paulb@97 | 491 | #for (prefix, ns), value in (variables or {}).items(): |
paulb@149 | 492 | # value = from_unicode(value) |
paulb@97 | 493 | # libxml2mod.xmlXPathRegisterVariableNS(context, prefix, ns, value) |
paulb@77 | 494 | result = libxml2mod.xmlXPathEval(expr, context) |
paulb@77 | 495 | libxml2mod.xmlXPathFreeContext(context) |
paulb@77 | 496 | return result |
paulb@46 | 497 | |
paulb@269 | 498 | # Exceptions. |
paulb@269 | 499 | |
paulb@269 | 500 | class LSException(Exception): |
paulb@269 | 501 | |
paulb@269 | 502 | "DOM Level 3 Load/Save exception." |
paulb@269 | 503 | |
paulb@269 | 504 | PARSE_ERR = 81 |
paulb@269 | 505 | SERIALIZE_ERR = 82 |
paulb@269 | 506 | |
paulb@46 | 507 | # Utility functions. |
paulb@46 | 508 | |
paulb@46 | 509 | def createDocument(namespaceURI, localName, doctype): |
paulb@46 | 510 | # NOTE: Fixed to use version 1.0 only. |
paulb@55 | 511 | d = libxml2mod.xmlNewDoc("1.0") |
paulb@46 | 512 | if localName is not None: |
paulb@174 | 513 | # NOTE: Verify that this is always what should occur. |
paulb@174 | 514 | root = Node_createElementNS(d, namespaceURI, localName) |
paulb@46 | 515 | Node_appendChild(d, root) |
paulb@155 | 516 | if doctype is not None: |
paulb@155 | 517 | libxml2mod.xmlCreateIntSubset(d, doctype.localName, doctype.publicId, doctype.systemId) |
paulb@46 | 518 | return d |
paulb@46 | 519 | |
paulb@269 | 520 | def parse(stream_or_string, html=0, htmlencoding=None, unfinished=0): |
paulb@46 | 521 | if hasattr(stream_or_string, "read"): |
paulb@46 | 522 | stream = stream_or_string |
paulb@269 | 523 | return parseString(stream.read(), html=html, htmlencoding=htmlencoding, unfinished=unfinished) |
paulb@46 | 524 | else: |
paulb@269 | 525 | return parseFile(stream_or_string, html=html, htmlencoding=htmlencoding, unfinished=unfinished) |
paulb@46 | 526 | |
paulb@269 | 527 | def parseFile(s, html=0, htmlencoding=None, unfinished=0): |
paulb@46 | 528 | # NOTE: Switching off validation and remote DTD resolution. |
paulb@103 | 529 | if not html: |
paulb@103 | 530 | context = libxml2mod.xmlCreateFileParserCtxt(s) |
paulb@273 | 531 | Parser_configure(context) |
paulb@273 | 532 | Parser_parse(context) |
paulb@273 | 533 | doc = Parser_document(context) |
paulb@273 | 534 | if unfinished or Parser_well_formed(context): |
paulb@269 | 535 | return doc |
paulb@269 | 536 | else: |
paulb@269 | 537 | raise LSException(LSException.PARSE_ERR) |
paulb@103 | 538 | else: |
paulb@246 | 539 | return libxml2mod.htmlReadFile(s, htmlencoding, HTML_PARSE_NOERROR | HTML_PARSE_NOWARNING | HTML_PARSE_NONET) |
paulb@46 | 540 | |
paulb@269 | 541 | def parseString(s, html=0, htmlencoding=None, unfinished=0): |
paulb@46 | 542 | # NOTE: Switching off validation and remote DTD resolution. |
paulb@103 | 543 | if not html: |
paulb@103 | 544 | context = libxml2mod.xmlCreateMemoryParserCtxt(s, len(s)) |
paulb@273 | 545 | Parser_configure(context) |
paulb@273 | 546 | Parser_parse(context) |
paulb@273 | 547 | doc = Parser_document(context) |
paulb@273 | 548 | if unfinished or Parser_well_formed(context): |
paulb@269 | 549 | return doc |
paulb@269 | 550 | else: |
paulb@269 | 551 | raise LSException(LSException.PARSE_ERR) |
paulb@103 | 552 | else: |
paulb@103 | 553 | # NOTE: URL given as None. |
paulb@103 | 554 | html_url = None |
paulb@246 | 555 | return libxml2mod.htmlReadMemory(s, len(s), html_url, htmlencoding, |
paulb@103 | 556 | HTML_PARSE_NOERROR | HTML_PARSE_NOWARNING | HTML_PARSE_NONET) |
paulb@46 | 557 | |
paulb@269 | 558 | def parseURI(uri, html=0, htmlencoding=None, unfinished=0): |
paulb@103 | 559 | # NOTE: Switching off validation and remote DTD resolution. |
paulb@103 | 560 | if not html: |
paulb@116 | 561 | context = libxml2mod.xmlCreateURLParserCtxt(uri, 0) |
paulb@273 | 562 | Parser_configure(context) |
paulb@273 | 563 | Parser_parse(context) |
paulb@273 | 564 | doc = Parser_document(context) |
paulb@273 | 565 | if unfinished or Parser_well_formed(context): |
paulb@269 | 566 | return doc |
paulb@269 | 567 | else: |
paulb@269 | 568 | raise LSException(LSException.PARSE_ERR) |
paulb@103 | 569 | else: |
paulb@127 | 570 | raise NotImplementedError, "parseURI does not yet support HTML" |
paulb@55 | 571 | |
paulb@120 | 572 | def toString(node, encoding=None, prettyprint=0): |
paulb@120 | 573 | return libxml2mod.serializeNode(node, encoding, prettyprint) |
paulb@73 | 574 | |
paulb@120 | 575 | def toStream(node, stream, encoding=None, prettyprint=0): |
paulb@120 | 576 | stream.write(toString(node, encoding, prettyprint)) |
paulb@73 | 577 | |
paulb@120 | 578 | def toFile(node, f, encoding=None, prettyprint=0): |
paulb@120 | 579 | libxml2mod.saveNodeTo(node, f, encoding, prettyprint) |
paulb@46 | 580 | |
paulb@273 | 581 | # libxml2mod constants and helper functions. |
paulb@103 | 582 | |
paulb@103 | 583 | HTML_PARSE_NOERROR = 32 |
paulb@103 | 584 | HTML_PARSE_NOWARNING = 64 |
paulb@103 | 585 | HTML_PARSE_NONET = 2048 |
paulb@103 | 586 | XML_PARSE_NOERROR = 32 |
paulb@103 | 587 | XML_PARSE_NOWARNING = 64 |
paulb@103 | 588 | XML_PARSE_NONET = 2048 |
paulb@103 | 589 | |
paulb@273 | 590 | def Parser_push(): |
paulb@273 | 591 | return libxml2mod.xmlCreatePushParser(None, "", 0, None) |
paulb@273 | 592 | |
paulb@273 | 593 | def Parser_configure(context): |
paulb@273 | 594 | libxml2mod.xmlParserSetPedantic(context, 0) |
paulb@273 | 595 | libxml2mod.xmlParserSetValidate(context, 0) |
paulb@273 | 596 | libxml2mod.xmlCtxtUseOptions(context, XML_PARSE_NOERROR | XML_PARSE_NOWARNING | XML_PARSE_NONET) |
paulb@273 | 597 | |
paulb@273 | 598 | def Parser_feed(context, s): |
paulb@273 | 599 | libxml2mod.xmlParseChunk(context, s, len(s), 1) |
paulb@273 | 600 | |
paulb@273 | 601 | def Parser_well_formed(context): |
paulb@273 | 602 | return libxml2mod.xmlParserGetWellFormed(context) |
paulb@273 | 603 | |
paulb@273 | 604 | def Parser_document(context): |
paulb@273 | 605 | return libxml2mod.xmlParserGetDoc(context) |
paulb@273 | 606 | |
paulb@273 | 607 | def Parser_parse(context): |
paulb@273 | 608 | libxml2mod.xmlParseDocument(context) |
paulb@273 | 609 | |
paulb@46 | 610 | # vim: tabstop=4 expandtab shiftwidth=4 |