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 ns is not None and ns == libxml2mod.xmlNodeGetContent(libxml2mod.xmlNodeGetNs(node)): 168 libxml2mod.xmlNewNsProp(node, libxml2mod.xmlNodeGetNs(node), localName, value) 169 elif prefix is not None: 170 new_ns = libxml2mod.xmlNewNs(node, ns, prefix) 171 libxml2mod.xmlNewNsProp(node, new_ns, localName, value) 172 else: 173 # NOTE: Needs verifying: what should happen to the namespace? 174 # NOTE: This also catches the case where None is the element's 175 # NOTE: namespace and is also used for the attribute. 176 libxml2mod.xmlNewNsProp(node, None, localName, value) 177 178 def Node_setAttribute(node, name, value): 179 # NOTE: Need to convert from Unicode. 180 name, value = map(from_unicode, [name, value]) 181 182 libxml2mod.xmlSetProp(node, name, value) 183 184 def Node_setAttributeNodeNS(node, attr): 185 # NOTE: Not actually putting the node on the element. 186 Node_setAttributeNS(node, Node_namespaceURI(attr), Node_nodeName(attr), Node_nodeValue(attr)) 187 188 def Node_setAttributeNode(node, attr): 189 # NOTE: Not actually putting the node on the element. 190 Node_setAttribute(node, Node_nodeName(attr), Node_nodeValue(attr)) 191 192 def Node_createElementNS(node, ns, name): 193 # NOTE: Need to convert from Unicode. 194 ns, name = map(from_unicode, [ns, name]) 195 196 prefix, localName = _get_prefix_and_localName(name) 197 new_node = libxml2mod.xmlNewNode(localName) 198 # NOTE: Does it make sense to set the namespace if it is empty? 199 if ns is not None: 200 new_ns = libxml2mod.xmlNewNs(new_node, ns, prefix) 201 libxml2mod.xmlSetNs(new_node, new_ns) 202 return new_node 203 204 def Node_createElement(node, name): 205 # NOTE: Need to convert from Unicode. 206 name = from_unicode(name) 207 208 new_node = libxml2mod.xmlNewNode(name) 209 return new_node 210 211 def Node_createAttributeNS(node, ns, name): 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, localName, None) 223 return new_node 224 225 def Node_createAttribute(node, name): 226 227 # NOTE: Need to convert from Unicode. 228 name = from_unicode(name) 229 230 # NOTE: xmlNewProp does not seem to work. 231 return Node_createAttributeNS(node, None, name) 232 233 def Node_createTextNode(node, value): 234 # NOTE: Need to convert from Unicode. 235 value = from_unicode(value) 236 237 return libxml2mod.xmlNewText(value) 238 239 def Node_createComment(node, value): 240 # NOTE: Need to convert from Unicode. 241 value = from_unicode(value) 242 243 return libxml2mod.xmlNewComment(value) 244 245 def Node_insertBefore(node, tmp, oldNode): 246 return libxml2mod.xmlAddPrevSibling(oldNode, tmp) 247 248 def Node_replaceChild(node, tmp, oldNode): 249 return libxml2mod.xmlReplaceNode(oldNode, tmp) 250 251 def Node_appendChild(node, tmp): 252 return libxml2mod.xmlAddChild(node, tmp) 253 254 def Node_removeChild(node, child): 255 libxml2mod.xmlUnlinkNode(child) 256 257 def Node_importNode(node, other, deep): 258 if Node_nodeType(other) == xml.dom.Node.ELEMENT_NODE: 259 imported_element = Node_createElementNS(node, Node_namespaceURI(other), Node_tagName(other)) 260 for attr in Node_attributes(other).values(): 261 Node_setAttributeNS(imported_element, Node_namespaceURI(attr), Node_nodeName(attr), Node_nodeValue(attr)) 262 263 if deep: 264 for child in Node_childNodes(other): 265 imported_child = Node_importNode(node, child, deep) 266 if imported_child: 267 Node_appendChild(imported_element, imported_child) 268 269 return imported_element 270 271 elif Node_nodeType(other) == xml.dom.Node.TEXT_NODE: 272 return Node_createTextNode(node, Node_nodeValue(other)) 273 274 elif Node_nodeType(other) == xml.dom.Node.COMMENT_NODE: 275 return Node_createComment(node, Node_data(other)) 276 277 raise ValueError, Node_nodeType(other) 278 279 def Node_importNode_DOM(node, other, deep): 280 if other.nodeType == xml.dom.Node.ELEMENT_NODE: 281 imported_element = Node_createElementNS(node, other.namespaceURI, other.tagName) 282 for attr in other.attributes.values(): 283 Node_setAttributeNS(imported_element, attr.namespaceURI, attr.nodeName, attr.nodeValue) 284 285 if deep: 286 for child in other.childNodes: 287 imported_child = Node_importNode_DOM(node, child, deep) 288 if imported_child: 289 Node_appendChild(imported_element, imported_child) 290 291 return imported_element 292 293 elif other.nodeType == xml.dom.Node.TEXT_NODE: 294 return Node_createTextNode(node, other.nodeValue) 295 296 elif other.nodeType == xml.dom.Node.COMMENT_NODE: 297 return Node_createComment(node, other.data) 298 299 raise ValueError, other.nodeType 300 301 def Node_xpath(node, expr, variables=None, namespaces=None): 302 context = libxml2mod.xmlXPathNewContext(Node_ownerDocument(node)) 303 libxml2mod.xmlXPathSetContextNode(context, node) 304 # NOTE: Discover namespaces from the node. 305 for prefix, ns in (namespaces or {}).items(): 306 libxml2mod.xmlXPathRegisterNs(context, prefix, ns) 307 result = libxml2mod.xmlXPathEval(expr, context) 308 libxml2mod.xmlXPathFreeContext(context) 309 return result 310 311 # Utility functions. 312 313 def createDocumentType(localName, publicId, systemId): 314 return None 315 316 def createDocument(namespaceURI, localName, doctype): 317 # NOTE: Fixed to use version 1.0 only. 318 d = libxml2mod.xmlNewDoc("1.0") 319 if localName is not None: 320 root = Node_createElementNS(d, namespaceURI, localName) 321 Node_appendChild(d, root) 322 return d 323 324 def parse(stream_or_string): 325 if hasattr(stream_or_string, "read"): 326 stream = stream_or_string 327 return parseString(stream.read()) 328 else: 329 return parseFile(stream_or_string) 330 331 def parseFile(s): 332 # NOTE: Switching off validation and remote DTD resolution. 333 context = libxml2mod.xmlCreateFileParserCtxt(s) 334 libxml2mod.xmlParserSetValidate(context, 0) 335 libxml2mod.xmlCtxtUseOptions(context, 0) 336 libxml2mod.xmlParseDocument(context) 337 return libxml2mod.xmlParserGetDoc(context) 338 339 def parseString(s): 340 # NOTE: Switching off validation and remote DTD resolution. 341 context = libxml2mod.xmlCreateMemoryParserCtxt(s, len(s)) 342 libxml2mod.xmlParserSetValidate(context, 0) 343 libxml2mod.xmlCtxtUseOptions(context, 0) 344 libxml2mod.xmlParseDocument(context) 345 return libxml2mod.xmlParserGetDoc(context) 346 347 def parseURI(uri): 348 context = libxml2mod.xmlCreateURLParserCtxt(url) 349 libxml2mod.xmlParserSetValidate(context, 0) 350 libxml2mod.xmlCtxtUseOptions(context, 0) 351 libxml2mod.xmlParseDocument(context) 352 return libxml2mod.xmlParserGetDoc(context) 353 354 def toString(node, encoding=None): 355 return libxml2mod.serializeNode(node, encoding, 0) 356 357 def toStream(node, stream, encoding=None): 358 stream.write(toString(node, encoding)) 359 360 def toFile(node, f, encoding=None): 361 libxml2mod.saveNodeTo(node, f, encoding, 0) 362 363 # vim: tabstop=4 expandtab shiftwidth=4