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