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