1.1 --- a/libxml2dom/macrolib/macrolib.py Mon Jan 24 00:48:28 2005 +0000
1.2 +++ b/libxml2dom/macrolib/macrolib.py Mon Jan 24 02:15:27 2005 +0000
1.3 @@ -1,17 +1,18 @@
1.4 #!/usr/bin/env python
1.5
1.6 """
1.7 -DOM macros for virtual libxml2 node methods and properties.
1.8 +DOM macros for virtual libxml2mod node methods and properties.
1.9 """
1.10
1.11 import xml.dom
1.12 -import libxml2
1.13 +import libxml2mod
1.14
1.15 class TemporaryNode:
1.16 def __init__(self, ns, name):
1.17 self._ns = ns
1.18 self.name = name
1.19 self.type = "attribute"
1.20 + self.nodeValue = None
1.21
1.22 def ns(self):
1.23 return self._ns
1.24 @@ -55,58 +56,49 @@
1.25 }
1.26
1.27 def Node_ownerDocument(node):
1.28 - return node.doc
1.29 + return libxml2mod.doc(node)
1.30
1.31 def Node_nodeType(node):
1.32 global _nodesTypes
1.33 - return _nodeTypes[node.type]
1.34 + return _nodeTypes[libxml2mod.type(node)]
1.35
1.36 def Node_childNodes(node):
1.37
1.38 # NOTE: Consider a generator instead.
1.39
1.40 child_nodes = []
1.41 - node = node.children
1.42 + node = libxml2mod.children(node)
1.43 while node is not None:
1.44 child_nodes.append(node)
1.45 - node = node.next
1.46 + node = libxml2mod.next(node)
1.47 return child_nodes
1.48
1.49 def Node_attributes(node):
1.50 attributes = {}
1.51 - node = node.properties
1.52 + node = libxml2mod.properties(node)
1.53 while node is not None:
1.54 - ns = _getNs(node)
1.55 + ns = libxml2mod.xmlNodeGetNs(node)
1.56 if ns is not None:
1.57 - attributes[(ns.content, node.name)] = node
1.58 + attributes[(libxml2mod.xmlNodeGetContent(ns), libxml2mod.name(node))] = node
1.59 else:
1.60 - attributes[(None, node.name)] = node
1.61 - node = node.next
1.62 + attributes[(None, libxml2mod.name(node))] = node
1.63 + node = libxml2mod.next(node)
1.64 return attributes
1.65
1.66 -def _getNs(node):
1.67 -
1.68 - "Internal namespace information retrieval."
1.69 -
1.70 - try:
1.71 - return node.ns()
1.72 - except libxml2.treeError:
1.73 - return None
1.74 -
1.75 def Node_namespaceURI(node):
1.76 - ns = _getNs(node)
1.77 + ns = libxml2mod.xmlNodeGetNs(node)
1.78 if ns is not None:
1.79 - return to_unicode(ns.content)
1.80 + return to_unicode(libxml2mod.xmlNodeGetContent(ns))
1.81 else:
1.82 return None
1.83
1.84 def Node_nodeValue(node):
1.85 - return to_unicode(node.content)
1.86 + return to_unicode(libxml2mod.xmlNodeGetContent(node))
1.87
1.88 def Node_prefix(node):
1.89 - ns = _getNs(node)
1.90 + ns = libxml2mod.xmlNodeGetNs(node)
1.91 if ns is not None:
1.92 - return to_unicode(ns.name)
1.93 + return to_unicode(libxml2mod.name(ns))
1.94 else:
1.95 return None
1.96
1.97 @@ -118,29 +110,29 @@
1.98 return Node_localName(node)
1.99
1.100 def Node_tagName(node):
1.101 - if node.type == "element":
1.102 + if libxml2mod.type(node) == "element":
1.103 return Node_nodeName(node)
1.104 else:
1.105 return None
1.106
1.107 def Node_localName(node):
1.108 - return to_unicode(node.name)
1.109 + return to_unicode(libxml2mod.name(node))
1.110
1.111 def Node_parentNode(node):
1.112 - if node.type == "document_xml":
1.113 + if libxml2mod.type(node) == "document_xml":
1.114 return None
1.115 else:
1.116 - return node.parent
1.117 + return libxml2mod.parent(node)
1.118
1.119 def Node_previousSibling(node):
1.120 - if node.prev is not None:
1.121 - return node.prev
1.122 + if libxml2mod.prev(node) is not None:
1.123 + return libxml2mod.prev(node)
1.124 else:
1.125 return None
1.126
1.127 def Node_nextSibling(node):
1.128 - if node.next is not None:
1.129 - return node.next
1.130 + if libxml2mod.next(node) is not None:
1.131 + return libxml2mod.next(node)
1.132 else:
1.133 return None
1.134
1.135 @@ -151,64 +143,77 @@
1.136 return Node_getAttribute(name) is not None
1.137
1.138 def Node_getAttributeNS(node, ns, localName):
1.139 - return to_unicode(node.nsProp(localName, ns))
1.140 + return to_unicode(libxml2mod.xmlGetNsProp(node, localName, ns))
1.141
1.142 def Node_getAttribute(node, name):
1.143 - return to_unicode(node.prop(name))
1.144 + return to_unicode(libxml2mod.xmlGetProp(node, name))
1.145
1.146 def Node_getAttributeNodeNS(node, ns, localName):
1.147 - return node.nsProp(localName, ns)
1.148 + # NOTE: Needs verifying.
1.149 + return libxml2mod.xmlGetNsProp(node, localName, ns)
1.150
1.151 def Node_getAttributeNode(node, name):
1.152 # NOTE: Needs verifying.
1.153 - return node.prop(name)
1.154 + return libxml2mod.xmlGetProp(node, name)
1.155
1.156 def Node_setAttributeNS(node, ns, name, value):
1.157 # NOTE: Need to convert from Unicode.
1.158 ns, name, value = map(from_unicode, [ns, name, value])
1.159
1.160 prefix, localName = _get_prefix_and_localName(name)
1.161 +
1.162 + # NOTE: Might need to be xmlSetNsProp.
1.163 if prefix is not None:
1.164 - node.setNsProp(node.newNs(ns, prefix), localName, value)
1.165 - elif ns is not None and ns == node.ns().content:
1.166 - node.setNsProp(node.ns(), localName, value)
1.167 + libxml2mod.xmlNewNsProp(node, libxml2mod.xmlNewNs(node, ns, prefix), localName, value)
1.168 + elif ns is not None and ns == libxml2mod.xmlNodeGetContent(libxml2mod.xmlNodeGetNs(node)):
1.169 + libxml2mod.xmlNewNsProp(node, libxml2mod.xmlNodeGetNs(node), localName, value)
1.170 else:
1.171 # NOTE: Needs verifying: what should happen to the namespace?
1.172 # NOTE: This also catches the case where None is the element's
1.173 # NOTE: namespace and is also used for the attribute.
1.174 - node.setNsProp(None, localName, value)
1.175 + libxml2mod.xmlNewNsProp(node, None, localName, value)
1.176
1.177 def Node_setAttribute(node, name, value):
1.178 # NOTE: Need to convert from Unicode.
1.179 name, value = map(from_unicode, [name, value])
1.180
1.181 - node.setProp(name, value)
1.182 + libxml2mod.xmlSetProp(node, name, value)
1.183 +
1.184 +def _add_node(node, tmp):
1.185 + if tmp.ns is not None:
1.186 + child = libxml2mod.xmlNewNsProp(node, None, Node_localName(tmp), None)
1.187 + ns = libxml2mod.xmlNewNs(child, Node_namespaceURI(tmp), Node_prefix(tmp))
1.188 + libxml2mod.xmlNodeSetNs(child, ns)
1.189 + else:
1.190 + child = libxml2mod.xmlNewProp(node, libxml2mod.name(tmp))
1.191 +
1.192 + return child
1.193
1.194 def Node_setAttributeNodeNS(node, ns, name, attr):
1.195 # NOTE: Not actually putting the node on the element.
1.196 - Node_setAttributeNS(node, ns, name, Node_nodeValue(attr))
1.197 + Node_setAttributeNS(node, ns, name, attr.nodeValue) # Node_nodeValue(attr)
1.198
1.199 def Node_setAttributeNode(node, name, attr):
1.200 # NOTE: Not actually putting the node on the element.
1.201 - Node_setAttribute(node, name, Node_nodeValue(attr))
1.202 + Node_setAttribute(node, name, attr.nodeValue) # Node_nodeValue(attr)
1.203
1.204 def Node_createElementNS(node, ns, name):
1.205 # NOTE: Need to convert from Unicode.
1.206 ns, name = map(from_unicode, [ns, name])
1.207
1.208 prefix, localName = _get_prefix_and_localName(name)
1.209 - new_node = libxml2.newNode(localName)
1.210 + new_node = libxml2mod.xmlNewNode(localName)
1.211 # NOTE: Does it make sense to set the namespace if it is empty?
1.212 if ns is not None:
1.213 - new_ns = new_node.newNs(ns, prefix)
1.214 - new_node.setNs(new_ns)
1.215 + new_ns = libxml2mod.xmlNewNs(new_node, ns, prefix)
1.216 + libxml2mod.xmlSetNs(new_node, new_ns)
1.217 return new_node
1.218
1.219 def Node_createElement(node, name):
1.220 # NOTE: Need to convert from Unicode.
1.221 name = from_unicode(name)
1.222
1.223 - new_node = libxml2.newNode(name)
1.224 + new_node = libxml2mod.xmlNewNode(name)
1.225 return new_node
1.226
1.227 def Node_createAttributeNS(node, ns, name):
1.228 @@ -218,7 +223,7 @@
1.229 prefix, localName = _get_prefix_and_localName(name)
1.230 # NOTE: Does it make sense to set the namespace if it is empty?
1.231 if ns is not None:
1.232 - new_ns = new_node.newNs(ns, prefix)
1.233 + new_ns = libxml2mod.xmlNewNs(new_node, ns, prefix)
1.234 else:
1.235 new_ns = None
1.236 return TemporaryNode(new_ns, localName)
1.237 @@ -233,50 +238,40 @@
1.238 # NOTE: Need to convert from Unicode.
1.239 value = from_unicode(value)
1.240
1.241 - return libxml2.newText(value)
1.242 + return libxml2mod.xmlNewText(value)
1.243
1.244 def Node_createComment(node, value):
1.245 # NOTE: Need to convert from Unicode.
1.246 value = from_unicode(value)
1.247
1.248 - return libxml2.newComment(value)
1.249 -
1.250 -def _add_node(node, tmp):
1.251 - if tmp.ns is not None:
1.252 - child = node.newNsProp(None, Node_localName(tmp), None)
1.253 - ns = child.newNs(Node_namespaceURI(tmp), Node_prefix(tmp))
1.254 - child.setNs(ns)
1.255 - else:
1.256 - child = node.newProp(None, tmp.name, None)
1.257 -
1.258 - return child
1.259 + return libxml2mod.xmlNewComment(value)
1.260
1.261 def Node_insertBefore(node, tmp, oldNode):
1.262 if not isinstance(tmp, TemporaryNode):
1.263 - return oldNode.addPrevSibling(tmp)
1.264 + return libxml2mod.xmlAddPrevSibling(oldNode, tmp)
1.265 else:
1.266 return None
1.267
1.268 def Node_replaceChild(node, tmp, oldNode):
1.269 if not isinstance(tmp, TemporaryNode):
1.270 - return oldNode.replaceNode(tmp)
1.271 + return libxml2mod.xmlReplaceNode(oldNode, tmp)
1.272 else:
1.273 return None
1.274
1.275 def Node_appendChild(node, tmp):
1.276 - return node.addChild(tmp)
1.277 + return libxml2mod.xmlAddChild(node, tmp)
1.278
1.279 def Node_removeChild(node, child):
1.280 - child.unlinkNode()
1.281 + libxml2mod.unlinkNode(child)
1.282
1.283 def Node_xpath(node, expr, variables=None, namespaces=None):
1.284 - context = Node_ownerDocument(node).xpathNewContext()
1.285 - context.setContextNode(node)
1.286 + context = libxml2mod.xmlXPathNewContext(Node_ownerDocument(node))
1.287 + libxml2mod.xmlXPathSetContextNode(context, node)
1.288 # NOTE: Discover namespaces from the node.
1.289 for prefix, ns in (namespaces or {}).items():
1.290 - context.xpathRegisterNs(prefix, ns)
1.291 + libxml2mod.xmlXPathRegisterNs(context, prefix, ns)
1.292 # NOTE: May need to tidy up the context.
1.293 - return context.xpathEval(expr)
1.294 + return libxml2mod.xmlXPathEval(context, expr)
1.295
1.296 # Utility functions.
1.297
1.298 @@ -285,7 +280,7 @@
1.299
1.300 def createDocument(namespaceURI, localName, doctype):
1.301 # NOTE: Fixed to use version 1.0 only.
1.302 - d = libxml2.newDoc("1.0")
1.303 + d = libxml2mod.xmlNewDoc("1.0")
1.304 if localName is not None:
1.305 root = Node_createElementNS(d, namespaceURI, localName)
1.306 Node_appendChild(d, root)
1.307 @@ -300,21 +295,28 @@
1.308
1.309 def parseFile(s):
1.310 # NOTE: Switching off validation and remote DTD resolution.
1.311 - context = libxml2.createFileParserCtxt(s)
1.312 - context.validate(0)
1.313 - context.ctxtUseOptions(0)
1.314 - context.parseDocument()
1.315 - return context.doc()
1.316 + context = libxml2mod.xmlCreateFileParserCtxt(s)
1.317 + libxml2mod.xmlParserSetValidate(context, 0)
1.318 + libxml2mod.xmlCtxtUseOptions(context, 0)
1.319 + libxml2mod.xmlParseDocument(context)
1.320 + return libxml2mod.xmlParserGetDoc(context)
1.321
1.322 def parseString(s):
1.323 # NOTE: Switching off validation and remote DTD resolution.
1.324 - context = libxml2.createMemoryParserCtxt(s, len(s))
1.325 - context.validate(0)
1.326 - context.ctxtUseOptions(0)
1.327 - context.parseDocument()
1.328 - return context.doc()
1.329 + context = libxml2mod.xmlCreateMemoryParserCtxt(s, len(s))
1.330 + libxml2mod.xmlParserSetValidate(context, 0)
1.331 + libxml2mod.xmlCtxtUseOptions(context, 0)
1.332 + libxml2mod.xmlParseDocument(context)
1.333 + return libxml2mod.xmlParserGetDoc(context)
1.334
1.335 def parseURI(uri):
1.336 - return libxml2.parseURI(uri)
1.337 + context = libxml2mod.xmlCreateURLParserCtxt(url)
1.338 + libxml2mod.xmlParserSetValidate(context, 0)
1.339 + libxml2mod.xmlCtxtUseOptions(context, 0)
1.340 + libxml2mod.xmlParseDocument(context)
1.341 + return libxml2mod.xmlParserGetDoc(context)
1.342 +
1.343 +def toFile(doc, s):
1.344 + libxml2mod.xmlSaveFile(s, doc)
1.345
1.346 # vim: tabstop=4 expandtab shiftwidth=4