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