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@46 | 5 | """ |
paulb@46 | 6 | |
paulb@46 | 7 | import xml.dom |
paulb@55 | 8 | import libxml2mod |
paulb@46 | 9 | |
paulb@46 | 10 | # NOTE: libxml2 seems to use UTF-8 throughout. |
paulb@46 | 11 | |
paulb@46 | 12 | def from_unicode(s): |
paulb@46 | 13 | if type(s) == type(u""): |
paulb@46 | 14 | return s.encode("utf-8") |
paulb@46 | 15 | else: |
paulb@46 | 16 | return s |
paulb@46 | 17 | |
paulb@46 | 18 | def to_unicode(s): |
paulb@46 | 19 | if type(s) == type(""): |
paulb@46 | 20 | return unicode(s, encoding="utf-8") |
paulb@46 | 21 | else: |
paulb@46 | 22 | return s |
paulb@46 | 23 | |
paulb@46 | 24 | def _get_prefix_and_localName(name): |
paulb@46 | 25 | t = name.split(":") |
paulb@46 | 26 | if len(t) == 1: |
paulb@46 | 27 | return None, name |
paulb@46 | 28 | elif len(t) == 2: |
paulb@46 | 29 | return t |
paulb@46 | 30 | else: |
paulb@46 | 31 | # NOTE: Should raise an exception. |
paulb@46 | 32 | return None, None |
paulb@46 | 33 | |
paulb@46 | 34 | _nodeTypes = { |
paulb@46 | 35 | "attribute" : xml.dom.Node.ATTRIBUTE_NODE, |
paulb@46 | 36 | "comment" : xml.dom.Node.COMMENT_NODE, |
paulb@46 | 37 | "document_xml" : xml.dom.Node.DOCUMENT_NODE, |
paulb@46 | 38 | "doctype" : xml.dom.Node.DOCUMENT_TYPE_NODE, |
paulb@46 | 39 | "dtd" : xml.dom.Node.DOCUMENT_TYPE_NODE, # NOTE: Needs verifying. |
paulb@46 | 40 | "element" : xml.dom.Node.ELEMENT_NODE, |
paulb@46 | 41 | "entity" : xml.dom.Node.ENTITY_NODE, |
paulb@46 | 42 | "entity_ref" : xml.dom.Node.ENTITY_REFERENCE_NODE, |
paulb@46 | 43 | "notation" : xml.dom.Node.NOTATION_NODE, |
paulb@46 | 44 | "pi" : xml.dom.Node.PROCESSING_INSTRUCTION_NODE, |
paulb@46 | 45 | "text" : xml.dom.Node.TEXT_NODE |
paulb@46 | 46 | } |
paulb@46 | 47 | |
paulb@46 | 48 | def Node_ownerDocument(node): |
paulb@62 | 49 | return libxml2mod.doc(node) or node |
paulb@46 | 50 | |
paulb@46 | 51 | def Node_nodeType(node): |
paulb@69 | 52 | global _nodeTypes |
paulb@55 | 53 | return _nodeTypes[libxml2mod.type(node)] |
paulb@46 | 54 | |
paulb@46 | 55 | def Node_childNodes(node): |
paulb@46 | 56 | |
paulb@46 | 57 | # NOTE: Consider a generator instead. |
paulb@46 | 58 | |
paulb@46 | 59 | child_nodes = [] |
paulb@55 | 60 | node = libxml2mod.children(node) |
paulb@46 | 61 | while node is not None: |
paulb@46 | 62 | child_nodes.append(node) |
paulb@55 | 63 | node = libxml2mod.next(node) |
paulb@46 | 64 | return child_nodes |
paulb@46 | 65 | |
paulb@46 | 66 | def Node_attributes(node): |
paulb@46 | 67 | attributes = {} |
paulb@55 | 68 | node = libxml2mod.properties(node) |
paulb@46 | 69 | while node is not None: |
paulb@55 | 70 | ns = libxml2mod.xmlNodeGetNs(node) |
paulb@46 | 71 | if ns is not None: |
paulb@55 | 72 | attributes[(libxml2mod.xmlNodeGetContent(ns), libxml2mod.name(node))] = node |
paulb@46 | 73 | else: |
paulb@55 | 74 | attributes[(None, libxml2mod.name(node))] = node |
paulb@55 | 75 | node = libxml2mod.next(node) |
paulb@46 | 76 | return attributes |
paulb@46 | 77 | |
paulb@46 | 78 | def Node_namespaceURI(node): |
paulb@55 | 79 | ns = libxml2mod.xmlNodeGetNs(node) |
paulb@46 | 80 | if ns is not None: |
paulb@55 | 81 | return to_unicode(libxml2mod.xmlNodeGetContent(ns)) |
paulb@46 | 82 | else: |
paulb@46 | 83 | return None |
paulb@46 | 84 | |
paulb@46 | 85 | def Node_nodeValue(node): |
paulb@55 | 86 | return to_unicode(libxml2mod.xmlNodeGetContent(node)) |
paulb@46 | 87 | |
paulb@87 | 88 | # NOTE: This is not properly exposed in the libxml2macro interface as the |
paulb@87 | 89 | # NOTE: writable form of nodeValue. |
paulb@87 | 90 | |
paulb@87 | 91 | def Node_setNodeValue(node, value): |
paulb@87 | 92 | # NOTE: Cannot set attribute node values. |
paulb@87 | 93 | libxml2mod.xmlNodeSetContent(node, from_unicode(value)) |
paulb@87 | 94 | |
paulb@73 | 95 | # NOTE: Verify this. |
paulb@73 | 96 | |
paulb@73 | 97 | Node_data = Node_nodeValue |
paulb@73 | 98 | |
paulb@46 | 99 | def Node_prefix(node): |
paulb@55 | 100 | ns = libxml2mod.xmlNodeGetNs(node) |
paulb@46 | 101 | if ns is not None: |
paulb@55 | 102 | return to_unicode(libxml2mod.name(ns)) |
paulb@46 | 103 | else: |
paulb@46 | 104 | return None |
paulb@46 | 105 | |
paulb@46 | 106 | def Node_nodeName(node): |
paulb@46 | 107 | prefix = Node_prefix(node) |
paulb@46 | 108 | if prefix is not None: |
paulb@46 | 109 | return prefix + ":" + Node_localName(node) |
paulb@46 | 110 | else: |
paulb@46 | 111 | return Node_localName(node) |
paulb@46 | 112 | |
paulb@46 | 113 | def Node_tagName(node): |
paulb@55 | 114 | if libxml2mod.type(node) == "element": |
paulb@46 | 115 | return Node_nodeName(node) |
paulb@46 | 116 | else: |
paulb@46 | 117 | return None |
paulb@46 | 118 | |
paulb@46 | 119 | def Node_localName(node): |
paulb@55 | 120 | return to_unicode(libxml2mod.name(node)) |
paulb@46 | 121 | |
paulb@46 | 122 | def Node_parentNode(node): |
paulb@55 | 123 | if libxml2mod.type(node) == "document_xml": |
paulb@46 | 124 | return None |
paulb@46 | 125 | else: |
paulb@55 | 126 | return libxml2mod.parent(node) |
paulb@46 | 127 | |
paulb@46 | 128 | def Node_previousSibling(node): |
paulb@55 | 129 | if libxml2mod.prev(node) is not None: |
paulb@55 | 130 | return libxml2mod.prev(node) |
paulb@46 | 131 | else: |
paulb@46 | 132 | return None |
paulb@46 | 133 | |
paulb@46 | 134 | def Node_nextSibling(node): |
paulb@55 | 135 | if libxml2mod.next(node) is not None: |
paulb@55 | 136 | return libxml2mod.next(node) |
paulb@46 | 137 | else: |
paulb@46 | 138 | return None |
paulb@46 | 139 | |
paulb@46 | 140 | def Node_hasAttributeNS(node, ns, localName): |
paulb@46 | 141 | return Node_getAttributeNS(ns, localName) is not None |
paulb@46 | 142 | |
paulb@46 | 143 | def Node_hasAttribute(node, name): |
paulb@46 | 144 | return Node_getAttribute(name) is not None |
paulb@46 | 145 | |
paulb@46 | 146 | def Node_getAttributeNS(node, ns, localName): |
paulb@55 | 147 | return to_unicode(libxml2mod.xmlGetNsProp(node, localName, ns)) |
paulb@46 | 148 | |
paulb@46 | 149 | def Node_getAttribute(node, name): |
paulb@55 | 150 | return to_unicode(libxml2mod.xmlGetProp(node, name)) |
paulb@46 | 151 | |
paulb@46 | 152 | def Node_getAttributeNodeNS(node, ns, localName): |
paulb@55 | 153 | # NOTE: Needs verifying. |
paulb@87 | 154 | return Node_attributes(node)[(ns, localName)] |
paulb@46 | 155 | |
paulb@46 | 156 | def Node_getAttributeNode(node, name): |
paulb@46 | 157 | # NOTE: Needs verifying. |
paulb@87 | 158 | return Node_attributes(node)[(None, name)] |
paulb@46 | 159 | |
paulb@46 | 160 | def Node_setAttributeNS(node, ns, name, value): |
paulb@46 | 161 | # NOTE: Need to convert from Unicode. |
paulb@46 | 162 | ns, name, value = map(from_unicode, [ns, name, value]) |
paulb@46 | 163 | |
paulb@46 | 164 | prefix, localName = _get_prefix_and_localName(name) |
paulb@55 | 165 | |
paulb@55 | 166 | # NOTE: Might need to be xmlSetNsProp. |
paulb@89 | 167 | if ns is not None and ns == libxml2mod.xmlNodeGetContent(libxml2mod.xmlNodeGetNs(node)): |
paulb@55 | 168 | libxml2mod.xmlNewNsProp(node, libxml2mod.xmlNodeGetNs(node), localName, value) |
paulb@89 | 169 | elif prefix is not None: |
paulb@89 | 170 | new_ns = libxml2mod.xmlNewNs(node, ns, prefix) |
paulb@89 | 171 | libxml2mod.xmlNewNsProp(node, new_ns, localName, value) |
paulb@46 | 172 | else: |
paulb@46 | 173 | # NOTE: Needs verifying: what should happen to the namespace? |
paulb@46 | 174 | # NOTE: This also catches the case where None is the element's |
paulb@46 | 175 | # NOTE: namespace and is also used for the attribute. |
paulb@55 | 176 | libxml2mod.xmlNewNsProp(node, None, localName, value) |
paulb@46 | 177 | |
paulb@46 | 178 | def Node_setAttribute(node, name, value): |
paulb@46 | 179 | # NOTE: Need to convert from Unicode. |
paulb@46 | 180 | name, value = map(from_unicode, [name, value]) |
paulb@46 | 181 | |
paulb@55 | 182 | libxml2mod.xmlSetProp(node, name, value) |
paulb@55 | 183 | |
paulb@87 | 184 | def Node_setAttributeNodeNS(node, attr): |
paulb@87 | 185 | # NOTE: Not actually putting the node on the element. |
paulb@87 | 186 | Node_setAttributeNS(node, Node_namespaceURI(attr), Node_nodeName(attr), Node_nodeValue(attr)) |
paulb@55 | 187 | |
paulb@87 | 188 | def Node_setAttributeNode(node, attr): |
paulb@46 | 189 | # NOTE: Not actually putting the node on the element. |
paulb@87 | 190 | Node_setAttribute(node, Node_nodeName(attr), Node_nodeValue(attr)) |
paulb@46 | 191 | |
paulb@46 | 192 | def Node_createElementNS(node, ns, name): |
paulb@46 | 193 | # NOTE: Need to convert from Unicode. |
paulb@46 | 194 | ns, name = map(from_unicode, [ns, name]) |
paulb@46 | 195 | |
paulb@46 | 196 | prefix, localName = _get_prefix_and_localName(name) |
paulb@55 | 197 | new_node = libxml2mod.xmlNewNode(localName) |
paulb@46 | 198 | # NOTE: Does it make sense to set the namespace if it is empty? |
paulb@46 | 199 | if ns is not None: |
paulb@55 | 200 | new_ns = libxml2mod.xmlNewNs(new_node, ns, prefix) |
paulb@55 | 201 | libxml2mod.xmlSetNs(new_node, new_ns) |
paulb@46 | 202 | return new_node |
paulb@46 | 203 | |
paulb@46 | 204 | def Node_createElement(node, name): |
paulb@46 | 205 | # NOTE: Need to convert from Unicode. |
paulb@46 | 206 | name = from_unicode(name) |
paulb@46 | 207 | |
paulb@55 | 208 | new_node = libxml2mod.xmlNewNode(name) |
paulb@46 | 209 | return new_node |
paulb@46 | 210 | |
paulb@46 | 211 | def Node_createAttributeNS(node, ns, name): |
paulb@87 | 212 | |
paulb@46 | 213 | # NOTE: Need to convert from Unicode. |
paulb@46 | 214 | ns, name = map(from_unicode, [ns, name]) |
paulb@46 | 215 | |
paulb@46 | 216 | prefix, localName = _get_prefix_and_localName(name) |
paulb@46 | 217 | # NOTE: Does it make sense to set the namespace if it is empty? |
paulb@46 | 218 | if ns is not None: |
paulb@87 | 219 | new_ns = libxml2mod.xmlNewNs(node, ns, prefix) |
paulb@46 | 220 | else: |
paulb@46 | 221 | new_ns = None |
paulb@89 | 222 | new_node = libxml2mod.xmlNewNsProp(node, new_ns, localName, None) |
paulb@87 | 223 | return new_node |
paulb@46 | 224 | |
paulb@46 | 225 | def Node_createAttribute(node, name): |
paulb@87 | 226 | |
paulb@46 | 227 | # NOTE: Need to convert from Unicode. |
paulb@46 | 228 | name = from_unicode(name) |
paulb@46 | 229 | |
paulb@87 | 230 | # NOTE: xmlNewProp does not seem to work. |
paulb@87 | 231 | return Node_createAttributeNS(node, None, name) |
paulb@46 | 232 | |
paulb@46 | 233 | def Node_createTextNode(node, value): |
paulb@46 | 234 | # NOTE: Need to convert from Unicode. |
paulb@46 | 235 | value = from_unicode(value) |
paulb@46 | 236 | |
paulb@55 | 237 | return libxml2mod.xmlNewText(value) |
paulb@46 | 238 | |
paulb@46 | 239 | def Node_createComment(node, value): |
paulb@46 | 240 | # NOTE: Need to convert from Unicode. |
paulb@46 | 241 | value = from_unicode(value) |
paulb@46 | 242 | |
paulb@55 | 243 | return libxml2mod.xmlNewComment(value) |
paulb@46 | 244 | |
paulb@46 | 245 | def Node_insertBefore(node, tmp, oldNode): |
paulb@87 | 246 | return libxml2mod.xmlAddPrevSibling(oldNode, tmp) |
paulb@46 | 247 | |
paulb@46 | 248 | def Node_replaceChild(node, tmp, oldNode): |
paulb@87 | 249 | return libxml2mod.xmlReplaceNode(oldNode, tmp) |
paulb@46 | 250 | |
paulb@46 | 251 | def Node_appendChild(node, tmp): |
paulb@55 | 252 | return libxml2mod.xmlAddChild(node, tmp) |
paulb@46 | 253 | |
paulb@46 | 254 | def Node_removeChild(node, child): |
paulb@85 | 255 | libxml2mod.xmlUnlinkNode(child) |
paulb@46 | 256 | |
paulb@73 | 257 | def Node_importNode(node, other, deep): |
paulb@73 | 258 | if Node_nodeType(other) == xml.dom.Node.ELEMENT_NODE: |
paulb@73 | 259 | imported_element = Node_createElementNS(node, Node_namespaceURI(other), Node_tagName(other)) |
paulb@73 | 260 | for attr in Node_attributes(other).values(): |
paulb@73 | 261 | Node_setAttributeNS(imported_element, Node_namespaceURI(attr), Node_nodeName(attr), Node_nodeValue(attr)) |
paulb@73 | 262 | |
paulb@73 | 263 | if deep: |
paulb@73 | 264 | for child in Node_childNodes(other): |
paulb@73 | 265 | imported_child = Node_importNode(node, child, deep) |
paulb@73 | 266 | if imported_child: |
paulb@73 | 267 | Node_appendChild(imported_element, imported_child) |
paulb@73 | 268 | |
paulb@73 | 269 | return imported_element |
paulb@73 | 270 | |
paulb@73 | 271 | elif Node_nodeType(other) == xml.dom.Node.TEXT_NODE: |
paulb@73 | 272 | return Node_createTextNode(node, Node_nodeValue(other)) |
paulb@73 | 273 | |
paulb@73 | 274 | elif Node_nodeType(other) == xml.dom.Node.COMMENT_NODE: |
paulb@73 | 275 | return Node_createComment(node, Node_data(other)) |
paulb@73 | 276 | |
paulb@73 | 277 | raise ValueError, Node_nodeType(other) |
paulb@73 | 278 | |
paulb@73 | 279 | def Node_importNode_DOM(node, other, deep): |
paulb@73 | 280 | if other.nodeType == xml.dom.Node.ELEMENT_NODE: |
paulb@73 | 281 | imported_element = Node_createElementNS(node, other.namespaceURI, other.tagName) |
paulb@73 | 282 | for attr in other.attributes.values(): |
paulb@73 | 283 | Node_setAttributeNS(imported_element, attr.namespaceURI, attr.nodeName, attr.nodeValue) |
paulb@73 | 284 | |
paulb@73 | 285 | if deep: |
paulb@73 | 286 | for child in other.childNodes: |
paulb@73 | 287 | imported_child = Node_importNode_DOM(node, child, deep) |
paulb@73 | 288 | if imported_child: |
paulb@73 | 289 | Node_appendChild(imported_element, imported_child) |
paulb@73 | 290 | |
paulb@73 | 291 | return imported_element |
paulb@73 | 292 | |
paulb@73 | 293 | elif other.nodeType == xml.dom.Node.TEXT_NODE: |
paulb@73 | 294 | return Node_createTextNode(node, other.nodeValue) |
paulb@73 | 295 | |
paulb@73 | 296 | elif other.nodeType == xml.dom.Node.COMMENT_NODE: |
paulb@73 | 297 | return Node_createComment(node, other.data) |
paulb@73 | 298 | |
paulb@73 | 299 | raise ValueError, other.nodeType |
paulb@73 | 300 | |
paulb@46 | 301 | def Node_xpath(node, expr, variables=None, namespaces=None): |
paulb@55 | 302 | context = libxml2mod.xmlXPathNewContext(Node_ownerDocument(node)) |
paulb@82 | 303 | libxml2mod.xmlXPathSetContextNode(context, node) |
paulb@46 | 304 | # NOTE: Discover namespaces from the node. |
paulb@46 | 305 | for prefix, ns in (namespaces or {}).items(): |
paulb@55 | 306 | libxml2mod.xmlXPathRegisterNs(context, prefix, ns) |
paulb@77 | 307 | result = libxml2mod.xmlXPathEval(expr, context) |
paulb@77 | 308 | libxml2mod.xmlXPathFreeContext(context) |
paulb@77 | 309 | return result |
paulb@46 | 310 | |
paulb@46 | 311 | # Utility functions. |
paulb@46 | 312 | |
paulb@46 | 313 | def createDocumentType(localName, publicId, systemId): |
paulb@46 | 314 | return None |
paulb@46 | 315 | |
paulb@46 | 316 | def createDocument(namespaceURI, localName, doctype): |
paulb@46 | 317 | # NOTE: Fixed to use version 1.0 only. |
paulb@55 | 318 | d = libxml2mod.xmlNewDoc("1.0") |
paulb@46 | 319 | if localName is not None: |
paulb@46 | 320 | root = Node_createElementNS(d, namespaceURI, localName) |
paulb@46 | 321 | Node_appendChild(d, root) |
paulb@46 | 322 | return d |
paulb@46 | 323 | |
paulb@46 | 324 | def parse(stream_or_string): |
paulb@46 | 325 | if hasattr(stream_or_string, "read"): |
paulb@46 | 326 | stream = stream_or_string |
paulb@46 | 327 | return parseString(stream.read()) |
paulb@46 | 328 | else: |
paulb@46 | 329 | return parseFile(stream_or_string) |
paulb@46 | 330 | |
paulb@46 | 331 | def parseFile(s): |
paulb@46 | 332 | # NOTE: Switching off validation and remote DTD resolution. |
paulb@55 | 333 | context = libxml2mod.xmlCreateFileParserCtxt(s) |
paulb@55 | 334 | libxml2mod.xmlParserSetValidate(context, 0) |
paulb@55 | 335 | libxml2mod.xmlCtxtUseOptions(context, 0) |
paulb@55 | 336 | libxml2mod.xmlParseDocument(context) |
paulb@55 | 337 | return libxml2mod.xmlParserGetDoc(context) |
paulb@46 | 338 | |
paulb@46 | 339 | def parseString(s): |
paulb@46 | 340 | # NOTE: Switching off validation and remote DTD resolution. |
paulb@55 | 341 | context = libxml2mod.xmlCreateMemoryParserCtxt(s, len(s)) |
paulb@55 | 342 | libxml2mod.xmlParserSetValidate(context, 0) |
paulb@55 | 343 | libxml2mod.xmlCtxtUseOptions(context, 0) |
paulb@55 | 344 | libxml2mod.xmlParseDocument(context) |
paulb@55 | 345 | return libxml2mod.xmlParserGetDoc(context) |
paulb@46 | 346 | |
paulb@46 | 347 | def parseURI(uri): |
paulb@55 | 348 | context = libxml2mod.xmlCreateURLParserCtxt(url) |
paulb@55 | 349 | libxml2mod.xmlParserSetValidate(context, 0) |
paulb@55 | 350 | libxml2mod.xmlCtxtUseOptions(context, 0) |
paulb@55 | 351 | libxml2mod.xmlParseDocument(context) |
paulb@55 | 352 | return libxml2mod.xmlParserGetDoc(context) |
paulb@55 | 353 | |
paulb@73 | 354 | def toString(node, encoding=None): |
paulb@73 | 355 | return libxml2mod.serializeNode(node, encoding, 0) |
paulb@73 | 356 | |
paulb@73 | 357 | def toStream(node, stream, encoding=None): |
paulb@73 | 358 | stream.write(toString(node, encoding)) |
paulb@73 | 359 | |
paulb@73 | 360 | def toFile(node, f, encoding=None): |
paulb@73 | 361 | libxml2mod.saveNodeTo(node, f, encoding, 0) |
paulb@46 | 362 | |
paulb@46 | 363 | # vim: tabstop=4 expandtab shiftwidth=4 |