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