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