1 #!/usr/bin/env python 2 3 """ 4 DOM macros for virtual libxml2mod node methods and properties. 5 """ 6 7 import xml.dom 8 import libxml2mod 9 10 # NOTE: libxml2 seems to use UTF-8 throughout. 11 12 def from_unicode(s): 13 if isinstance(s, unicode): 14 return s.encode("utf-8") 15 else: 16 # The string might contain non-ASCII characters, thus upsetting libxml2 17 # as it encounters a non-UTF-8 string. 18 try: 19 unicode(s) 20 except UnicodeError: 21 raise TypeError, "Please use Unicode for non-ASCII data." 22 return s 23 24 def to_unicode(s): 25 if isinstance(s, str): 26 return unicode(s, encoding="utf-8") 27 else: 28 return s 29 30 def get_ns(ns): 31 out_ns = to_unicode(libxml2mod.xmlNodeGetContent(ns)) 32 # Detect "" and produce None as the empty namespace. 33 if out_ns: 34 return out_ns 35 else: 36 return None 37 38 def _get_prefix_and_localName(name): 39 t = name.split(":") 40 if len(t) == 1: 41 return None, name 42 elif len(t) == 2: 43 return t 44 else: 45 # NOTE: Should raise an exception. 46 return None, None 47 48 _nodeTypes = { 49 "attribute" : xml.dom.Node.ATTRIBUTE_NODE, 50 "comment" : xml.dom.Node.COMMENT_NODE, 51 "document_xml" : xml.dom.Node.DOCUMENT_NODE, 52 "doctype" : xml.dom.Node.DOCUMENT_TYPE_NODE, 53 "dtd" : xml.dom.Node.DOCUMENT_TYPE_NODE, # NOTE: Needs verifying. 54 "element" : xml.dom.Node.ELEMENT_NODE, 55 "entity" : xml.dom.Node.ENTITY_NODE, 56 "entity_ref" : xml.dom.Node.ENTITY_REFERENCE_NODE, 57 "notation" : xml.dom.Node.NOTATION_NODE, 58 "pi" : xml.dom.Node.PROCESSING_INSTRUCTION_NODE, 59 "text" : xml.dom.Node.TEXT_NODE 60 } 61 62 _reverseNodeTypes = {} 63 for label, value in _nodeTypes.items(): 64 _reverseNodeTypes[value] = label 65 66 def Node_ownerDocument(node): 67 return libxml2mod.doc(node) or node 68 69 def Node_nodeType(node): 70 return _nodeTypes[libxml2mod.type(node)] 71 72 def Node_childNodes(node): 73 74 # NOTE: Consider a generator instead. 75 76 child_nodes = [] 77 node = libxml2mod.children(node) 78 while node is not None: 79 # Remove doctypes. 80 if Node_nodeType(node) != xml.dom.Node.DOCUMENT_TYPE_NODE: 81 child_nodes.append(node) 82 node = libxml2mod.next(node) 83 return child_nodes 84 85 def Node_attributes(node): 86 attributes = {} 87 node = libxml2mod.properties(node) 88 while node is not None: 89 ns = libxml2mod.xmlNodeGetNs(node) 90 if ns is not None: 91 attributes[(get_ns(ns), libxml2mod.name(node))] = node 92 else: 93 attributes[(None, libxml2mod.name(node))] = node 94 node = libxml2mod.next(node) 95 return attributes 96 97 def Node_namespaceURI(node): 98 ns = libxml2mod.xmlNodeGetNs(node) 99 if ns is not None: 100 return get_ns(ns) 101 else: 102 return None 103 104 def Node_nodeValue(node): 105 return to_unicode(libxml2mod.xmlNodeGetContent(node)) 106 107 # NOTE: This is not properly exposed in the libxml2macro interface as the 108 # NOTE: writable form of nodeValue. 109 110 def Node_setNodeValue(node, value): 111 # NOTE: Cannot set attribute node values. 112 libxml2mod.xmlNodeSetContent(node, from_unicode(value)) 113 114 # NOTE: Verify this. 115 116 Node_data = Node_nodeValue 117 118 def Node_prefix(node): 119 ns = libxml2mod.xmlNodeGetNs(node) 120 if ns is not None: 121 return to_unicode(libxml2mod.name(ns)) 122 else: 123 return None 124 125 def Node_nodeName(node): 126 prefix = Node_prefix(node) 127 if prefix is not None: 128 return prefix + ":" + Node_localName(node) 129 else: 130 return Node_localName(node) 131 132 def Node_tagName(node): 133 if libxml2mod.type(node) == "element": 134 return Node_nodeName(node) 135 else: 136 return None 137 138 def Node_localName(node): 139 return to_unicode(libxml2mod.name(node)) 140 141 def Node_parentNode(node): 142 if libxml2mod.type(node) == "document_xml": 143 return None 144 else: 145 return libxml2mod.parent(node) 146 147 def Node_previousSibling(node): 148 if libxml2mod.prev(node) is not None: 149 return libxml2mod.prev(node) 150 else: 151 return None 152 153 def Node_nextSibling(node): 154 if libxml2mod.next(node) is not None: 155 return libxml2mod.next(node) 156 else: 157 return None 158 159 def Node_doctype(node): 160 return libxml2mod.xmlGetIntSubset(node) 161 162 def Node_hasAttributeNS(node, ns, localName): 163 return Node_getAttributeNS(node, ns, localName) is not None 164 165 def Node_hasAttribute(node, name): 166 return Node_getAttribute(node, name) is not None 167 168 def Node_getAttributeNS(node, ns, localName): 169 return to_unicode(libxml2mod.xmlGetNsProp(node, localName, ns)) 170 171 def Node_getAttribute(node, name): 172 return to_unicode(libxml2mod.xmlGetProp(node, name)) 173 174 def Node_getAttributeNodeNS(node, ns, localName): 175 # NOTE: Needs verifying. 176 return Node_attributes(node)[(ns, localName)] 177 178 def Node_getAttributeNode(node, name): 179 # NOTE: Needs verifying. 180 return Node_attributes(node)[(None, name)] 181 182 def Node_setAttributeNS(node, ns, name, value): 183 # NOTE: Need to convert from Unicode. 184 ns, name, value = map(from_unicode, [ns, name, value]) 185 186 prefix, localName = _get_prefix_and_localName(name) 187 188 # NOTE: Might need to be xmlSetNsProp. 189 if ns is not None and ns == libxml2mod.xmlNodeGetContent(libxml2mod.xmlNodeGetNs(node)): 190 libxml2mod.xmlNewNsProp(node, libxml2mod.xmlNodeGetNs(node), localName, value) 191 elif prefix is not None: 192 new_ns = libxml2mod.xmlNewNs(node, ns, prefix) 193 libxml2mod.xmlNewNsProp(node, new_ns, localName, value) 194 else: 195 # NOTE: Needs verifying: what should happen to the namespace? 196 # NOTE: This also catches the case where None is the element's 197 # NOTE: namespace and is also used for the attribute. 198 libxml2mod.xmlNewNsProp(node, None, localName, value) 199 200 def Node_setAttribute(node, name, value): 201 # NOTE: Need to convert from Unicode. 202 name, value = map(from_unicode, [name, value]) 203 204 libxml2mod.xmlSetProp(node, name, value) 205 206 def Node_setAttributeNodeNS(node, attr): 207 # NOTE: Not actually putting the node on the element. 208 Node_setAttributeNS(node, Node_namespaceURI(attr), Node_nodeName(attr), Node_nodeValue(attr)) 209 210 def Node_setAttributeNode(node, attr): 211 # NOTE: Not actually putting the node on the element. 212 Node_setAttribute(node, Node_nodeName(attr), Node_nodeValue(attr)) 213 214 def Node_removeAttributeNS(node, ns, localName): 215 attr = Node_getAttributeNodeNS(node, ns, localName) 216 libxml2mod.xmlUnsetNsProp(node, libxml2mod.xmlNodeGetNs(attr), libxml2mod.name(attr)) 217 218 def Node_removeAttribute(node, name): 219 name = from_unicode(name) 220 libxml2mod.xmlUnsetProp(node, name) 221 222 def Node_createElementNS(node, ns, name): 223 # NOTE: Need to convert from Unicode. 224 ns, name = map(from_unicode, [ns, name]) 225 226 prefix, localName = _get_prefix_and_localName(name) 227 new_node = libxml2mod.xmlNewNode(localName) 228 229 # If the namespace is not empty, set the declaration. 230 if ns is not None: 231 new_ns = libxml2mod.xmlNewNs(new_node, ns, prefix) 232 libxml2mod.xmlSetNs(new_node, new_ns) 233 # If the namespace is empty, set a "null" declaration. 234 else: 235 new_ns = libxml2mod.xmlNewNs(new_node, "", prefix) 236 libxml2mod.xmlSetNs(new_node, new_ns) 237 return new_node 238 239 def Node_createElement(node, name): 240 # NOTE: Need to convert from Unicode. 241 name = from_unicode(name) 242 243 new_node = libxml2mod.xmlNewNode(name) 244 return new_node 245 246 def Node_createAttributeNS(node, ns, name): 247 248 # NOTE: Need to convert from Unicode. 249 ns, name = map(from_unicode, [ns, name]) 250 251 prefix, localName = _get_prefix_and_localName(name) 252 # NOTE: Does it make sense to set the namespace if it is empty? 253 if ns is not None: 254 new_ns = libxml2mod.xmlNewNs(node, ns, prefix) 255 else: 256 new_ns = None 257 new_node = libxml2mod.xmlNewNsProp(node, new_ns, localName, None) 258 return new_node 259 260 def Node_createAttribute(node, name): 261 262 # NOTE: Need to convert from Unicode. 263 name = from_unicode(name) 264 265 # NOTE: xmlNewProp does not seem to work. 266 return Node_createAttributeNS(node, None, name) 267 268 def Node_createTextNode(node, value): 269 # NOTE: Need to convert from Unicode. 270 value = from_unicode(value) 271 272 return libxml2mod.xmlNewText(value) 273 274 def Node_createComment(node, value): 275 # NOTE: Need to convert from Unicode. 276 value = from_unicode(value) 277 278 return libxml2mod.xmlNewComment(value) 279 280 def Node_insertBefore(node, tmp, oldNode): 281 return libxml2mod.xmlAddPrevSibling(oldNode, tmp) 282 283 def Node_replaceChild(node, tmp, oldNode): 284 return libxml2mod.xmlReplaceNode(oldNode, tmp) 285 286 def Node_appendChild(node, tmp): 287 return libxml2mod.xmlAddChild(node, tmp) 288 289 def Node_removeChild(node, child): 290 libxml2mod.xmlUnlinkNode(child) 291 292 def Node_importNode(node, other, deep): 293 if Node_nodeType(other) == xml.dom.Node.ELEMENT_NODE: 294 imported_element = Node_createElementNS(node, Node_namespaceURI(other), Node_tagName(other)) 295 for attr in Node_attributes(other).values(): 296 Node_setAttributeNS(imported_element, Node_namespaceURI(attr), Node_nodeName(attr), Node_nodeValue(attr)) 297 298 if deep: 299 for child in Node_childNodes(other): 300 imported_child = Node_importNode(node, child, deep) 301 if imported_child: 302 Node_appendChild(imported_element, imported_child) 303 304 return imported_element 305 306 elif Node_nodeType(other) == xml.dom.Node.TEXT_NODE: 307 return Node_createTextNode(node, Node_nodeValue(other)) 308 309 elif Node_nodeType(other) == xml.dom.Node.COMMENT_NODE: 310 return Node_createComment(node, Node_data(other)) 311 312 raise ValueError, "Node type '%s' (%d) not supported." % (other, Node_nodeType(other)) 313 314 def Node_importNode_DOM(node, other, deep): 315 if other.nodeType == xml.dom.Node.ELEMENT_NODE: 316 imported_element = Node_createElementNS(node, other.namespaceURI, other.tagName) 317 for attr in other.attributes.values(): 318 Node_setAttributeNS(imported_element, attr.namespaceURI, attr.nodeName, attr.nodeValue) 319 320 if deep: 321 for child in other.childNodes: 322 imported_child = Node_importNode_DOM(node, child, deep) 323 if imported_child: 324 Node_appendChild(imported_element, imported_child) 325 326 return imported_element 327 328 elif other.nodeType == xml.dom.Node.TEXT_NODE: 329 return Node_createTextNode(node, other.nodeValue) 330 331 elif other.nodeType == xml.dom.Node.COMMENT_NODE: 332 return Node_createComment(node, other.data) 333 334 raise ValueError, "Node type '%s' (%d) not supported." % (_reverseNodeTypes[other.nodeType], other.nodeType) 335 336 def Node_xpath(node, expr, variables=None, namespaces=None): 337 # NOTE: Need to convert from Unicode. 338 expr = from_unicode(expr) 339 340 context = libxml2mod.xmlXPathNewContext(Node_ownerDocument(node)) 341 libxml2mod.xmlXPathSetContextNode(context, node) 342 # NOTE: Discover namespaces from the node. 343 # NOTE: Work out how to specify paths without having to use prefixes on 344 # NOTE: names all the time. 345 for prefix, ns in (namespaces or {}).items(): 346 libxml2mod.xmlXPathRegisterNs(context, prefix, ns) 347 # NOTE: No such functions are exposed in current versions of libxml2. 348 #for (prefix, ns), value in (variables or {}).items(): 349 # # NOTE: Need to convert from Unicode. 350 # value = from_unicode(value) 351 # libxml2mod.xmlXPathRegisterVariableNS(context, prefix, ns, value) 352 result = libxml2mod.xmlXPathEval(expr, context) 353 libxml2mod.xmlXPathFreeContext(context) 354 return result 355 356 # Utility functions. 357 358 def createDocument(namespaceURI, localName, doctype): 359 # NOTE: Fixed to use version 1.0 only. 360 d = libxml2mod.xmlNewDoc("1.0") 361 if localName is not None: 362 # NOTE: Verify that this is really what should occur. 363 if namespaceURI is not None: 364 root = Node_createElementNS(d, namespaceURI, localName) 365 else: 366 root = Node_createElement(d, localName) 367 Node_appendChild(d, root) 368 if doctype is not None: 369 libxml2mod.xmlCreateIntSubset(d, doctype.localName, doctype.publicId, doctype.systemId) 370 return d 371 372 def parse(stream_or_string, html=0): 373 if hasattr(stream_or_string, "read"): 374 stream = stream_or_string 375 return parseString(stream.read(), html) 376 else: 377 return parseFile(stream_or_string, html) 378 379 def parseFile(s, html=0): 380 # NOTE: Switching off validation and remote DTD resolution. 381 if not html: 382 context = libxml2mod.xmlCreateFileParserCtxt(s) 383 libxml2mod.xmlParserSetPedantic(context, 0) 384 libxml2mod.xmlParserSetValidate(context, 0) 385 libxml2mod.xmlCtxtUseOptions(context, XML_PARSE_NOERROR | XML_PARSE_NOWARNING | XML_PARSE_NONET) 386 libxml2mod.xmlParseDocument(context) 387 return libxml2mod.xmlParserGetDoc(context) 388 else: 389 return libxml2mod.htmlReadFile(s, None, HTML_PARSE_NOERROR | HTML_PARSE_NOWARNING | HTML_PARSE_NONET) 390 391 def parseString(s, html=0): 392 # NOTE: Switching off validation and remote DTD resolution. 393 if not html: 394 context = libxml2mod.xmlCreateMemoryParserCtxt(s, len(s)) 395 libxml2mod.xmlParserSetPedantic(context, 0) 396 libxml2mod.xmlParserSetValidate(context, 0) 397 libxml2mod.xmlCtxtUseOptions(context, XML_PARSE_NOERROR | XML_PARSE_NOWARNING | XML_PARSE_NONET) 398 libxml2mod.xmlParseDocument(context) 399 return libxml2mod.xmlParserGetDoc(context) 400 else: 401 # NOTE: URL given as None. 402 html_url = None 403 return libxml2mod.htmlReadMemory(s, len(s), html_url, None, 404 HTML_PARSE_NOERROR | HTML_PARSE_NOWARNING | HTML_PARSE_NONET) 405 406 def parseURI(uri, html=0): 407 # NOTE: Switching off validation and remote DTD resolution. 408 if not html: 409 context = libxml2mod.xmlCreateURLParserCtxt(uri, 0) 410 libxml2mod.xmlParserSetPedantic(context, 0) 411 libxml2mod.xmlParserSetValidate(context, 0) 412 libxml2mod.xmlCtxtUseOptions(context, XML_PARSE_NOERROR | XML_PARSE_NOWARNING | XML_PARSE_NONET) 413 libxml2mod.xmlParseDocument(context) 414 return libxml2mod.xmlParserGetDoc(context) 415 else: 416 raise NotImplementedError, "parseURI does not yet support HTML" 417 418 def toString(node, encoding=None, prettyprint=0): 419 return libxml2mod.serializeNode(node, encoding, prettyprint) 420 421 def toStream(node, stream, encoding=None, prettyprint=0): 422 stream.write(toString(node, encoding, prettyprint)) 423 424 def toFile(node, f, encoding=None, prettyprint=0): 425 libxml2mod.saveNodeTo(node, f, encoding, prettyprint) 426 427 # libxml2mod constants. 428 429 HTML_PARSE_NOERROR = 32 430 HTML_PARSE_NOWARNING = 64 431 HTML_PARSE_NONET = 2048 432 XML_PARSE_NOERROR = 32 433 XML_PARSE_NOWARNING = 64 434 XML_PARSE_NONET = 2048 435 436 # vim: tabstop=4 expandtab shiftwidth=4