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