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