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@188 | 8 | This library is free software; you can redistribute it and/or |
paulb@188 | 9 | modify it under the terms of the GNU Lesser General Public |
paulb@188 | 10 | License as published by the Free Software Foundation; either |
paulb@188 | 11 | version 2.1 of the License, or (at your option) any later version. |
paulb@188 | 12 | |
paulb@188 | 13 | This library is distributed in the hope that it will be useful, |
paulb@188 | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paulb@188 | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
paulb@188 | 16 | Lesser General Public License for more details. |
paulb@188 | 17 | |
paulb@188 | 18 | You should have received a copy of the GNU Lesser General Public |
paulb@188 | 19 | License along with this library; if not, write to the Free Software |
paulb@188 | 20 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paulb@46 | 21 | """ |
paulb@46 | 22 | |
paulb@46 | 23 | import xml.dom |
paulb@214 | 24 | |
paulb@214 | 25 | # Try the conventional import first. |
paulb@214 | 26 | |
paulb@214 | 27 | try: |
paulb@214 | 28 | import libxml2mod |
paulb@214 | 29 | except ImportError: |
paulb@214 | 30 | from libxmlmods import libxml2mod |
paulb@46 | 31 | |
paulb@46 | 32 | # NOTE: libxml2 seems to use UTF-8 throughout. |
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@227 | 52 | def check_document(node, tmp): |
paulb@229 | 53 | if Node_ownerDocument(tmp) is not None and Node_ownerDocument(node) is not Node_ownerDocument(tmp): |
paulb@227 | 54 | raise xml.dom.DOMException(xml.dom.WRONG_DOCUMENT_ERR) |
paulb@227 | 55 | |
paulb@170 | 56 | def get_ns(ns): |
paulb@170 | 57 | out_ns = to_unicode(libxml2mod.xmlNodeGetContent(ns)) |
paulb@170 | 58 | # Detect "" and produce None as the empty namespace. |
paulb@170 | 59 | if out_ns: |
paulb@170 | 60 | return out_ns |
paulb@170 | 61 | else: |
paulb@170 | 62 | return None |
paulb@170 | 63 | |
paulb@46 | 64 | def _get_prefix_and_localName(name): |
paulb@46 | 65 | t = name.split(":") |
paulb@46 | 66 | if len(t) == 1: |
paulb@46 | 67 | return None, name |
paulb@46 | 68 | elif len(t) == 2: |
paulb@46 | 69 | return t |
paulb@46 | 70 | else: |
paulb@46 | 71 | # NOTE: Should raise an exception. |
paulb@46 | 72 | return None, None |
paulb@46 | 73 | |
paulb@196 | 74 | def _find_namespace_for_prefix(node, prefix): |
paulb@196 | 75 | |
paulb@196 | 76 | "Find the namespace definition node in the given 'node' for 'prefix'." |
paulb@196 | 77 | |
paulb@196 | 78 | current = libxml2mod.xmlNodeGetNsDefs(node) |
paulb@196 | 79 | while current is not None: |
paulb@196 | 80 | if libxml2mod.name(current) == prefix: |
paulb@196 | 81 | return current |
paulb@196 | 82 | current = libxml2mod.next(current) |
paulb@196 | 83 | return None |
paulb@196 | 84 | |
paulb@179 | 85 | def _find_namespace(node, ns, prefix): |
paulb@196 | 86 | |
paulb@196 | 87 | """ |
paulb@196 | 88 | Find the namespace definition node in the given 'node' for the given 'ns' |
paulb@196 | 89 | and 'prefix'. |
paulb@196 | 90 | """ |
paulb@196 | 91 | |
paulb@179 | 92 | new_ns = None |
paulb@179 | 93 | current = libxml2mod.xmlNodeGetNsDefs(node) |
paulb@179 | 94 | while current is not None: |
paulb@179 | 95 | if _check_namespace(current, ns, prefix): |
paulb@179 | 96 | new_ns = current |
paulb@179 | 97 | break |
paulb@179 | 98 | current = libxml2mod.next(current) |
paulb@179 | 99 | if new_ns is None: |
paulb@179 | 100 | node_ns = libxml2mod.xmlNodeGetNs(node) |
paulb@179 | 101 | if node_ns is not None and _check_namespace(node_ns, ns, prefix): |
paulb@179 | 102 | new_ns = node_ns |
paulb@179 | 103 | return new_ns |
paulb@179 | 104 | |
paulb@179 | 105 | def _check_namespace(current, ns, prefix): |
paulb@196 | 106 | |
paulb@196 | 107 | "Check the 'current' namespace definition node against 'ns' and 'prefix'." |
paulb@196 | 108 | |
paulb@196 | 109 | current_ns = get_ns(current) |
paulb@179 | 110 | current_prefix = libxml2mod.name(current) |
paulb@192 | 111 | if ns == current_ns and (prefix is None or prefix == current_prefix): |
paulb@179 | 112 | return 1 |
paulb@179 | 113 | else: |
paulb@179 | 114 | return 0 |
paulb@179 | 115 | |
paulb@179 | 116 | def _make_namespace(node, ns, prefix, set_default=0): |
paulb@196 | 117 | |
paulb@196 | 118 | """ |
paulb@196 | 119 | Make a new namespace definition node within the given 'node' for 'ns', |
paulb@196 | 120 | 'prefix', setting the default namespace on 'node' when 'prefix' is None and |
paulb@196 | 121 | 'set_default' is set to a true value (unlike the default value for that |
paulb@196 | 122 | parameter). |
paulb@196 | 123 | """ |
paulb@196 | 124 | |
paulb@179 | 125 | if prefix is not None or set_default: |
paulb@179 | 126 | new_ns = libxml2mod.xmlNewNs(node, ns, prefix) |
paulb@179 | 127 | else: |
paulb@179 | 128 | new_ns = None |
paulb@179 | 129 | return new_ns |
paulb@179 | 130 | |
paulb@192 | 131 | def _get_invented_prefix(node, ns): |
paulb@192 | 132 | current = libxml2mod.xmlNodeGetNsDefs(node) |
paulb@192 | 133 | prefixes = [] |
paulb@192 | 134 | while current is not None: |
paulb@192 | 135 | current_prefix = libxml2mod.name(current) |
paulb@192 | 136 | prefixes.append(current_prefix) |
paulb@192 | 137 | current = libxml2mod.next(current) |
paulb@192 | 138 | i = 0 |
paulb@192 | 139 | while 1: |
paulb@192 | 140 | prefix = "NS%d" % i |
paulb@192 | 141 | if prefix not in prefixes: |
paulb@192 | 142 | return prefix |
paulb@192 | 143 | i += 1 |
paulb@192 | 144 | |
paulb@46 | 145 | _nodeTypes = { |
paulb@46 | 146 | "attribute" : xml.dom.Node.ATTRIBUTE_NODE, |
paulb@46 | 147 | "comment" : xml.dom.Node.COMMENT_NODE, |
paulb@46 | 148 | "document_xml" : xml.dom.Node.DOCUMENT_NODE, |
paulb@203 | 149 | "document_html" : xml.dom.Node.DOCUMENT_NODE, |
paulb@46 | 150 | "doctype" : xml.dom.Node.DOCUMENT_TYPE_NODE, |
paulb@46 | 151 | "dtd" : xml.dom.Node.DOCUMENT_TYPE_NODE, # NOTE: Needs verifying. |
paulb@46 | 152 | "element" : xml.dom.Node.ELEMENT_NODE, |
paulb@46 | 153 | "entity" : xml.dom.Node.ENTITY_NODE, |
paulb@46 | 154 | "entity_ref" : xml.dom.Node.ENTITY_REFERENCE_NODE, |
paulb@46 | 155 | "notation" : xml.dom.Node.NOTATION_NODE, |
paulb@46 | 156 | "pi" : xml.dom.Node.PROCESSING_INSTRUCTION_NODE, |
paulb@46 | 157 | "text" : xml.dom.Node.TEXT_NODE |
paulb@46 | 158 | } |
paulb@46 | 159 | |
paulb@134 | 160 | _reverseNodeTypes = {} |
paulb@134 | 161 | for label, value in _nodeTypes.items(): |
paulb@134 | 162 | _reverseNodeTypes[value] = label |
paulb@134 | 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@73 | 225 | # NOTE: Verify this. |
paulb@73 | 226 | |
paulb@73 | 227 | Node_data = Node_nodeValue |
paulb@73 | 228 | |
paulb@46 | 229 | def Node_prefix(node): |
paulb@55 | 230 | ns = libxml2mod.xmlNodeGetNs(node) |
paulb@46 | 231 | if ns is not None: |
paulb@55 | 232 | return to_unicode(libxml2mod.name(ns)) |
paulb@46 | 233 | else: |
paulb@46 | 234 | return None |
paulb@46 | 235 | |
paulb@46 | 236 | def Node_nodeName(node): |
paulb@46 | 237 | prefix = Node_prefix(node) |
paulb@46 | 238 | if prefix is not None: |
paulb@46 | 239 | return prefix + ":" + Node_localName(node) |
paulb@46 | 240 | else: |
paulb@46 | 241 | return Node_localName(node) |
paulb@46 | 242 | |
paulb@46 | 243 | def Node_tagName(node): |
paulb@55 | 244 | if libxml2mod.type(node) == "element": |
paulb@46 | 245 | return Node_nodeName(node) |
paulb@46 | 246 | else: |
paulb@46 | 247 | return None |
paulb@46 | 248 | |
paulb@46 | 249 | def Node_localName(node): |
paulb@55 | 250 | return to_unicode(libxml2mod.name(node)) |
paulb@46 | 251 | |
paulb@46 | 252 | def Node_parentNode(node): |
paulb@55 | 253 | if libxml2mod.type(node) == "document_xml": |
paulb@46 | 254 | return None |
paulb@46 | 255 | else: |
paulb@55 | 256 | return libxml2mod.parent(node) |
paulb@46 | 257 | |
paulb@46 | 258 | def Node_previousSibling(node): |
paulb@55 | 259 | if libxml2mod.prev(node) is not None: |
paulb@55 | 260 | return libxml2mod.prev(node) |
paulb@46 | 261 | else: |
paulb@46 | 262 | return None |
paulb@46 | 263 | |
paulb@46 | 264 | def Node_nextSibling(node): |
paulb@55 | 265 | if libxml2mod.next(node) is not None: |
paulb@55 | 266 | return libxml2mod.next(node) |
paulb@46 | 267 | else: |
paulb@46 | 268 | return None |
paulb@46 | 269 | |
paulb@155 | 270 | def Node_doctype(node): |
paulb@155 | 271 | return libxml2mod.xmlGetIntSubset(node) |
paulb@155 | 272 | |
paulb@46 | 273 | def Node_hasAttributeNS(node, ns, localName): |
paulb@196 | 274 | return Node_getAttributeNS(node, ns, localName) is not None or \ |
paulb@196 | 275 | _find_namespace(node, ns, localName) is not None |
paulb@46 | 276 | |
paulb@46 | 277 | def Node_hasAttribute(node, name): |
paulb@92 | 278 | return Node_getAttribute(node, name) is not None |
paulb@46 | 279 | |
paulb@46 | 280 | def Node_getAttributeNS(node, ns, localName): |
paulb@196 | 281 | if ns == xml.dom.XMLNS_NAMESPACE: |
paulb@196 | 282 | ns_def = _find_namespace_for_prefix(node, localName) |
paulb@196 | 283 | if ns_def is not None: |
paulb@196 | 284 | return get_ns(ns_def) |
paulb@196 | 285 | else: |
paulb@196 | 286 | return None |
paulb@196 | 287 | else: |
paulb@196 | 288 | return to_unicode(libxml2mod.xmlGetNsProp(node, localName, ns)) |
paulb@46 | 289 | |
paulb@46 | 290 | def Node_getAttribute(node, name): |
paulb@55 | 291 | return to_unicode(libxml2mod.xmlGetProp(node, name)) |
paulb@46 | 292 | |
paulb@46 | 293 | def Node_getAttributeNodeNS(node, ns, localName): |
paulb@55 | 294 | # NOTE: Needs verifying. |
paulb@87 | 295 | return Node_attributes(node)[(ns, localName)] |
paulb@46 | 296 | |
paulb@46 | 297 | def Node_getAttributeNode(node, name): |
paulb@46 | 298 | # NOTE: Needs verifying. |
paulb@87 | 299 | return Node_attributes(node)[(None, name)] |
paulb@46 | 300 | |
paulb@46 | 301 | def Node_setAttributeNS(node, ns, name, value): |
paulb@46 | 302 | ns, name, value = map(from_unicode, [ns, name, value]) |
paulb@46 | 303 | prefix, localName = _get_prefix_and_localName(name) |
paulb@184 | 304 | |
paulb@184 | 305 | # Detect setting of xmlns:localName=value, looking for cases where |
paulb@184 | 306 | # x:attr=value have caused the definition of xmlns:x=y (as a declaration |
paulb@184 | 307 | # with prefix=x, ns=y). |
paulb@184 | 308 | if prefix == "xmlns" and ns == xml.dom.XMLNS_NAMESPACE: |
paulb@184 | 309 | if _find_namespace(node, value, localName): |
paulb@179 | 310 | return |
paulb@184 | 311 | new_ns = _make_namespace(node, value, localName, set_default=0) |
paulb@184 | 312 | # For non-xmlns attributes, we find or make a namespace declaration and then |
paulb@184 | 313 | # set an attribute. |
paulb@184 | 314 | elif ns is not None: |
paulb@192 | 315 | # Look for a suitable namespace. |
paulb@179 | 316 | new_ns = _find_namespace(node, ns, prefix) |
paulb@192 | 317 | # Create a declaration if no suitable one was found. |
paulb@179 | 318 | if new_ns is None: |
paulb@192 | 319 | # Invent a prefix for unprefixed attributes with namespaces. |
paulb@192 | 320 | if prefix is None: |
paulb@192 | 321 | prefix = _get_invented_prefix(node, ns) |
paulb@179 | 322 | new_ns = _make_namespace(node, ns, prefix, set_default=0) |
paulb@179 | 323 | libxml2mod.xmlSetNsProp(node, new_ns, localName, value) |
paulb@46 | 324 | else: |
paulb@46 | 325 | # NOTE: Needs verifying: what should happen to the namespace? |
paulb@46 | 326 | # NOTE: This also catches the case where None is the element's |
paulb@46 | 327 | # NOTE: namespace and is also used for the attribute. |
paulb@179 | 328 | libxml2mod.xmlSetNsProp(node, None, localName, value) |
paulb@46 | 329 | |
paulb@46 | 330 | def Node_setAttribute(node, name, value): |
paulb@46 | 331 | name, value = map(from_unicode, [name, value]) |
paulb@46 | 332 | |
paulb@55 | 333 | libxml2mod.xmlSetProp(node, name, value) |
paulb@55 | 334 | |
paulb@87 | 335 | def Node_setAttributeNodeNS(node, attr): |
paulb@87 | 336 | # NOTE: Not actually putting the node on the element. |
paulb@87 | 337 | Node_setAttributeNS(node, Node_namespaceURI(attr), Node_nodeName(attr), Node_nodeValue(attr)) |
paulb@55 | 338 | |
paulb@87 | 339 | def Node_setAttributeNode(node, attr): |
paulb@46 | 340 | # NOTE: Not actually putting the node on the element. |
paulb@87 | 341 | Node_setAttribute(node, Node_nodeName(attr), Node_nodeValue(attr)) |
paulb@46 | 342 | |
paulb@92 | 343 | def Node_removeAttributeNS(node, ns, localName): |
paulb@92 | 344 | attr = Node_getAttributeNodeNS(node, ns, localName) |
paulb@92 | 345 | libxml2mod.xmlUnsetNsProp(node, libxml2mod.xmlNodeGetNs(attr), libxml2mod.name(attr)) |
paulb@92 | 346 | |
paulb@92 | 347 | def Node_removeAttribute(node, name): |
paulb@92 | 348 | name = from_unicode(name) |
paulb@92 | 349 | libxml2mod.xmlUnsetProp(node, name) |
paulb@92 | 350 | |
paulb@46 | 351 | def Node_createElementNS(node, ns, name): |
paulb@46 | 352 | ns, name = map(from_unicode, [ns, name]) |
paulb@46 | 353 | |
paulb@46 | 354 | prefix, localName = _get_prefix_and_localName(name) |
paulb@55 | 355 | new_node = libxml2mod.xmlNewNode(localName) |
paulb@166 | 356 | |
paulb@166 | 357 | # If the namespace is not empty, set the declaration. |
paulb@46 | 358 | if ns is not None: |
paulb@179 | 359 | new_ns = _find_namespace(new_node, ns, prefix) |
paulb@179 | 360 | if new_ns is None: |
paulb@179 | 361 | new_ns = _make_namespace(new_node, ns, prefix, set_default=1) |
paulb@55 | 362 | libxml2mod.xmlSetNs(new_node, new_ns) |
paulb@166 | 363 | # If the namespace is empty, set a "null" declaration. |
paulb@177 | 364 | elif prefix is not None: |
paulb@179 | 365 | new_ns = _find_namespace(new_node, "", prefix) |
paulb@179 | 366 | if new_ns is None: |
paulb@179 | 367 | new_ns = _make_namespace(new_node, "", prefix) |
paulb@177 | 368 | libxml2mod.xmlSetNs(new_node, new_ns) |
paulb@166 | 369 | else: |
paulb@174 | 370 | libxml2mod.xmlSetNs(new_node, None) |
paulb@174 | 371 | Node_setAttribute(new_node, "xmlns", "") |
paulb@46 | 372 | return new_node |
paulb@46 | 373 | |
paulb@46 | 374 | def Node_createElement(node, name): |
paulb@46 | 375 | name = from_unicode(name) |
paulb@46 | 376 | |
paulb@55 | 377 | new_node = libxml2mod.xmlNewNode(name) |
paulb@46 | 378 | return new_node |
paulb@46 | 379 | |
paulb@46 | 380 | def Node_createAttributeNS(node, ns, name): |
paulb@46 | 381 | ns, name = map(from_unicode, [ns, name]) |
paulb@46 | 382 | |
paulb@46 | 383 | prefix, localName = _get_prefix_and_localName(name) |
paulb@46 | 384 | # NOTE: Does it make sense to set the namespace if it is empty? |
paulb@46 | 385 | if ns is not None: |
paulb@179 | 386 | new_ns = _find_namespace(node, ns, prefix) |
paulb@179 | 387 | if new_ns is None: |
paulb@179 | 388 | new_ns = _make_namespace(node, ns, prefix, set_default=0) |
paulb@46 | 389 | else: |
paulb@46 | 390 | new_ns = None |
paulb@89 | 391 | new_node = libxml2mod.xmlNewNsProp(node, new_ns, localName, None) |
paulb@87 | 392 | return new_node |
paulb@46 | 393 | |
paulb@46 | 394 | def Node_createAttribute(node, name): |
paulb@46 | 395 | name = from_unicode(name) |
paulb@46 | 396 | |
paulb@87 | 397 | # NOTE: xmlNewProp does not seem to work. |
paulb@87 | 398 | return Node_createAttributeNS(node, None, name) |
paulb@46 | 399 | |
paulb@46 | 400 | def Node_createTextNode(node, value): |
paulb@46 | 401 | value = from_unicode(value) |
paulb@46 | 402 | |
paulb@55 | 403 | return libxml2mod.xmlNewText(value) |
paulb@46 | 404 | |
paulb@46 | 405 | def Node_createComment(node, value): |
paulb@46 | 406 | value = from_unicode(value) |
paulb@46 | 407 | |
paulb@55 | 408 | return libxml2mod.xmlNewComment(value) |
paulb@46 | 409 | |
paulb@46 | 410 | def Node_insertBefore(node, tmp, oldNode): |
paulb@227 | 411 | check_document(node, tmp) |
paulb@87 | 412 | return libxml2mod.xmlAddPrevSibling(oldNode, tmp) |
paulb@46 | 413 | |
paulb@46 | 414 | def Node_replaceChild(node, tmp, oldNode): |
paulb@227 | 415 | check_document(node, tmp) |
paulb@87 | 416 | return libxml2mod.xmlReplaceNode(oldNode, tmp) |
paulb@46 | 417 | |
paulb@46 | 418 | def Node_appendChild(node, tmp): |
paulb@227 | 419 | check_document(node, tmp) |
paulb@55 | 420 | return libxml2mod.xmlAddChild(node, tmp) |
paulb@46 | 421 | |
paulb@46 | 422 | def Node_removeChild(node, child): |
paulb@85 | 423 | libxml2mod.xmlUnlinkNode(child) |
paulb@46 | 424 | |
paulb@73 | 425 | def Node_importNode(node, other, deep): |
paulb@73 | 426 | if Node_nodeType(other) == xml.dom.Node.ELEMENT_NODE: |
paulb@73 | 427 | imported_element = Node_createElementNS(node, Node_namespaceURI(other), Node_tagName(other)) |
paulb@73 | 428 | for attr in Node_attributes(other).values(): |
paulb@73 | 429 | Node_setAttributeNS(imported_element, Node_namespaceURI(attr), Node_nodeName(attr), Node_nodeValue(attr)) |
paulb@73 | 430 | |
paulb@73 | 431 | if deep: |
paulb@73 | 432 | for child in Node_childNodes(other): |
paulb@73 | 433 | imported_child = Node_importNode(node, child, deep) |
paulb@73 | 434 | if imported_child: |
paulb@73 | 435 | Node_appendChild(imported_element, imported_child) |
paulb@73 | 436 | |
paulb@73 | 437 | return imported_element |
paulb@73 | 438 | |
paulb@73 | 439 | elif Node_nodeType(other) == xml.dom.Node.TEXT_NODE: |
paulb@73 | 440 | return Node_createTextNode(node, Node_nodeValue(other)) |
paulb@73 | 441 | |
paulb@73 | 442 | elif Node_nodeType(other) == xml.dom.Node.COMMENT_NODE: |
paulb@73 | 443 | return Node_createComment(node, Node_data(other)) |
paulb@73 | 444 | |
paulb@227 | 445 | raise xml.dom.DOMException(xml.dom.NOT_SUPPORTED_ERR, "Node type '%s' (%d) not supported." % (other, Node_nodeType(other))) |
paulb@73 | 446 | |
paulb@73 | 447 | def Node_importNode_DOM(node, other, deep): |
paulb@73 | 448 | if other.nodeType == xml.dom.Node.ELEMENT_NODE: |
paulb@73 | 449 | imported_element = Node_createElementNS(node, other.namespaceURI, other.tagName) |
paulb@73 | 450 | for attr in other.attributes.values(): |
paulb@73 | 451 | Node_setAttributeNS(imported_element, attr.namespaceURI, attr.nodeName, attr.nodeValue) |
paulb@73 | 452 | |
paulb@73 | 453 | if deep: |
paulb@73 | 454 | for child in other.childNodes: |
paulb@73 | 455 | imported_child = Node_importNode_DOM(node, child, deep) |
paulb@73 | 456 | if imported_child: |
paulb@73 | 457 | Node_appendChild(imported_element, imported_child) |
paulb@73 | 458 | |
paulb@73 | 459 | return imported_element |
paulb@73 | 460 | |
paulb@73 | 461 | elif other.nodeType == xml.dom.Node.TEXT_NODE: |
paulb@73 | 462 | return Node_createTextNode(node, other.nodeValue) |
paulb@73 | 463 | |
paulb@73 | 464 | elif other.nodeType == xml.dom.Node.COMMENT_NODE: |
paulb@73 | 465 | return Node_createComment(node, other.data) |
paulb@73 | 466 | |
paulb@227 | 467 | raise xml.dom.DOMException(xml.dom.NOT_SUPPORTED_ERR, "Node type '%s' (%d) not supported." % (_reverseNodeTypes[other.nodeType], other.nodeType)) |
paulb@73 | 468 | |
paulb@46 | 469 | def Node_xpath(node, expr, variables=None, namespaces=None): |
paulb@149 | 470 | expr = from_unicode(expr) |
paulb@149 | 471 | |
paulb@230 | 472 | context = libxml2mod.xmlXPathNewContext(Node_ownerDocument(node) or node) |
paulb@82 | 473 | libxml2mod.xmlXPathSetContextNode(context, node) |
paulb@46 | 474 | # NOTE: Discover namespaces from the node. |
paulb@97 | 475 | # NOTE: Work out how to specify paths without having to use prefixes on |
paulb@97 | 476 | # NOTE: names all the time. |
paulb@46 | 477 | for prefix, ns in (namespaces or {}).items(): |
paulb@55 | 478 | libxml2mod.xmlXPathRegisterNs(context, prefix, ns) |
paulb@97 | 479 | # NOTE: No such functions are exposed in current versions of libxml2. |
paulb@97 | 480 | #for (prefix, ns), value in (variables or {}).items(): |
paulb@149 | 481 | # value = from_unicode(value) |
paulb@97 | 482 | # libxml2mod.xmlXPathRegisterVariableNS(context, prefix, ns, value) |
paulb@77 | 483 | result = libxml2mod.xmlXPathEval(expr, context) |
paulb@77 | 484 | libxml2mod.xmlXPathFreeContext(context) |
paulb@77 | 485 | return result |
paulb@46 | 486 | |
paulb@46 | 487 | # Utility functions. |
paulb@46 | 488 | |
paulb@46 | 489 | def createDocument(namespaceURI, localName, doctype): |
paulb@46 | 490 | # NOTE: Fixed to use version 1.0 only. |
paulb@55 | 491 | d = libxml2mod.xmlNewDoc("1.0") |
paulb@46 | 492 | if localName is not None: |
paulb@174 | 493 | # NOTE: Verify that this is always what should occur. |
paulb@174 | 494 | root = Node_createElementNS(d, namespaceURI, localName) |
paulb@46 | 495 | Node_appendChild(d, root) |
paulb@155 | 496 | if doctype is not None: |
paulb@155 | 497 | libxml2mod.xmlCreateIntSubset(d, doctype.localName, doctype.publicId, doctype.systemId) |
paulb@46 | 498 | return d |
paulb@46 | 499 | |
paulb@103 | 500 | def parse(stream_or_string, html=0): |
paulb@46 | 501 | if hasattr(stream_or_string, "read"): |
paulb@46 | 502 | stream = stream_or_string |
paulb@103 | 503 | return parseString(stream.read(), html) |
paulb@46 | 504 | else: |
paulb@103 | 505 | return parseFile(stream_or_string, html) |
paulb@46 | 506 | |
paulb@103 | 507 | def parseFile(s, html=0): |
paulb@46 | 508 | # NOTE: Switching off validation and remote DTD resolution. |
paulb@103 | 509 | if not html: |
paulb@103 | 510 | context = libxml2mod.xmlCreateFileParserCtxt(s) |
paulb@106 | 511 | libxml2mod.xmlParserSetPedantic(context, 0) |
paulb@103 | 512 | libxml2mod.xmlParserSetValidate(context, 0) |
paulb@103 | 513 | libxml2mod.xmlCtxtUseOptions(context, XML_PARSE_NOERROR | XML_PARSE_NOWARNING | XML_PARSE_NONET) |
paulb@103 | 514 | libxml2mod.xmlParseDocument(context) |
paulb@103 | 515 | return libxml2mod.xmlParserGetDoc(context) |
paulb@103 | 516 | else: |
paulb@103 | 517 | return libxml2mod.htmlReadFile(s, None, HTML_PARSE_NOERROR | HTML_PARSE_NOWARNING | HTML_PARSE_NONET) |
paulb@46 | 518 | |
paulb@103 | 519 | def parseString(s, html=0): |
paulb@46 | 520 | # NOTE: Switching off validation and remote DTD resolution. |
paulb@103 | 521 | if not html: |
paulb@103 | 522 | context = libxml2mod.xmlCreateMemoryParserCtxt(s, len(s)) |
paulb@106 | 523 | libxml2mod.xmlParserSetPedantic(context, 0) |
paulb@103 | 524 | libxml2mod.xmlParserSetValidate(context, 0) |
paulb@103 | 525 | libxml2mod.xmlCtxtUseOptions(context, XML_PARSE_NOERROR | XML_PARSE_NOWARNING | XML_PARSE_NONET) |
paulb@103 | 526 | libxml2mod.xmlParseDocument(context) |
paulb@103 | 527 | return libxml2mod.xmlParserGetDoc(context) |
paulb@103 | 528 | else: |
paulb@103 | 529 | # NOTE: URL given as None. |
paulb@103 | 530 | html_url = None |
paulb@103 | 531 | return libxml2mod.htmlReadMemory(s, len(s), html_url, None, |
paulb@103 | 532 | HTML_PARSE_NOERROR | HTML_PARSE_NOWARNING | HTML_PARSE_NONET) |
paulb@46 | 533 | |
paulb@103 | 534 | def parseURI(uri, html=0): |
paulb@103 | 535 | # NOTE: Switching off validation and remote DTD resolution. |
paulb@103 | 536 | if not html: |
paulb@116 | 537 | context = libxml2mod.xmlCreateURLParserCtxt(uri, 0) |
paulb@106 | 538 | libxml2mod.xmlParserSetPedantic(context, 0) |
paulb@103 | 539 | libxml2mod.xmlParserSetValidate(context, 0) |
paulb@103 | 540 | libxml2mod.xmlCtxtUseOptions(context, XML_PARSE_NOERROR | XML_PARSE_NOWARNING | XML_PARSE_NONET) |
paulb@103 | 541 | libxml2mod.xmlParseDocument(context) |
paulb@103 | 542 | return libxml2mod.xmlParserGetDoc(context) |
paulb@103 | 543 | else: |
paulb@127 | 544 | raise NotImplementedError, "parseURI does not yet support HTML" |
paulb@55 | 545 | |
paulb@120 | 546 | def toString(node, encoding=None, prettyprint=0): |
paulb@120 | 547 | return libxml2mod.serializeNode(node, encoding, prettyprint) |
paulb@73 | 548 | |
paulb@120 | 549 | def toStream(node, stream, encoding=None, prettyprint=0): |
paulb@120 | 550 | stream.write(toString(node, encoding, prettyprint)) |
paulb@73 | 551 | |
paulb@120 | 552 | def toFile(node, f, encoding=None, prettyprint=0): |
paulb@120 | 553 | libxml2mod.saveNodeTo(node, f, encoding, prettyprint) |
paulb@46 | 554 | |
paulb@103 | 555 | # libxml2mod constants. |
paulb@103 | 556 | |
paulb@103 | 557 | HTML_PARSE_NOERROR = 32 |
paulb@103 | 558 | HTML_PARSE_NOWARNING = 64 |
paulb@103 | 559 | HTML_PARSE_NONET = 2048 |
paulb@103 | 560 | XML_PARSE_NOERROR = 32 |
paulb@103 | 561 | XML_PARSE_NOWARNING = 64 |
paulb@103 | 562 | XML_PARSE_NONET = 2048 |
paulb@103 | 563 | |
paulb@46 | 564 | # vim: tabstop=4 expandtab shiftwidth=4 |