Skip to content

Commit

Permalink
Import simplexml updates from gajim, thanks mainly to asterix, thorst…
Browse files Browse the repository at this point in the history
…enp and dwd
  • Loading branch information
normanr committed Mar 3, 2009
1 parent 98b8022 commit 8a6ff2f
Showing 1 changed file with 149 additions and 60 deletions.
209 changes: 149 additions & 60 deletions xmpp/simplexml.py
Expand Up @@ -21,18 +21,19 @@

def XMLescape(txt):
"""Returns provided string with symbols & < > " replaced by their respective XML entities."""
return txt.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;").replace('"', "&quot;")
# replace also FORM FEED and ESC, because they are not valid XML chars
return txt.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;").replace('"', "&quot;").replace(u'\x0C', "").replace(u'\x1B', "")

ENCODING='utf-8'
def ustr(what):
"""Converts object "what" to unicode string using it's own __str__ method if accessible or unicode method otherwise."""
if type(what) == type(u''): return what
if isinstance(what, unicode): return what
try: r=what.__str__()
except AttributeError: r=str(what)
if type(r)<>type(u''): return unicode(r,ENCODING)
if not isinstance(r, unicode): return unicode(r,ENCODING)
return r

class Node:
class Node(object):
""" Node class describes syntax of separate XML Node. It have a constructor that permits node creation
from set of "namespace name", attributes and payload of text strings and other nodes.
It does not natively support building node from text string and uses NodeBuilder class for that purpose.
Expand All @@ -48,56 +49,90 @@ class Node:
replication (and using replication only to move upwards on the classes tree).
"""
FORCE_NODE_RECREATION=0
def __init__(self, tag=None, attrs={}, payload=[], parent=None, node=None):
def __init__(self, tag=None, attrs={}, payload=[], parent=None, nsp=None, node_built=False, node=None):
""" Takes "tag" argument as the name of node (prepended by namespace, if needed and separated from it
by a space), attrs dictionary as the set of arguments, payload list as the set of textual strings
and child nodes that this node carries within itself and "parent" argument that is another node
that this one will be the child of. Also the __init__ can be provided with "node" argument that is
that this one will be the child of. Also the __init__ can be provided with "node" argument that is
either a text string containing exactly one node or another Node instance to begin with. If both
"node" and other arguments is provided then the node initially created as replica of "node"
provided and then modified to be compliant with other arguments."""
if node:
if self.FORCE_NODE_RECREATION and type(node)==type(self): node=str(node)
if type(node)<>type(self): node=NodeBuilder(node,self)
if self.FORCE_NODE_RECREATION and isinstance(node, Node):
node=str(node)
if not isinstance(node, Node):
node=NodeBuilder(node,self)
node_built = True
else:
self.name,self.namespace,self.attrs,self.data,self.kids,self.parent = node.name,node.namespace,{},[],[],node.parent
self.name,self.namespace,self.attrs,self.data,self.kids,self.parent,self.nsd = node.name,node.namespace,{},[],[],node.parent,{}
for key in node.attrs.keys(): self.attrs[key]=node.attrs[key]
for data in node.data: self.data.append(data)
for kid in node.kids: self.kids.append(kid)
else: self.name,self.namespace,self.attrs,self.data,self.kids,self.parent = 'tag','',{},[],[],None

if tag: self.namespace, self.name = ([self.namespace]+tag.split())[-2:]
if parent: self.parent = parent
if self.parent and not self.namespace: self.namespace=self.parent.namespace
for attr in attrs.keys():
for k,v in node.nsd.items(): self.nsd[k] = v
else: self.name,self.namespace,self.attrs,self.data,self.kids,self.parent,self.nsd = 'tag','',{},[],[],None,{}
if parent:
self.parent = parent
self.nsp_cache = {}
if nsp:
for k,v in nsp.items(): self.nsp_cache[k] = v
for attr,val in attrs.items():
if attr == 'xmlns':
self.nsd[u''] = val
elif attr.startswith('xmlns:'):
self.nsd[attr[6:]] = val
self.attrs[attr]=attrs[attr]
if type(payload) in (type(''),type(u'')): payload=[payload]
if tag:
if node_built:
pfx,self.name = (['']+tag.split(':'))[-2:]
self.namespace = self.lookup_nsp(pfx)
else:
if ' ' in tag:
self.namespace,self.name = tag.split()
else:
self.name = tag
if isinstance(payload, basestring): payload=[payload]
for i in payload:
if type(i)==type(self): self.addChild(node=i)
else: self.addData(i)
if isinstance(i, Node): self.addChild(node=i)
else: self.data.append(ustr(i))

def lookup_nsp(self,pfx=''):
ns = self.nsd.get(pfx,None)
if ns is None:
ns = self.nsp_cache.get(pfx,None)
if ns is None:
if self.parent:
ns = self.parent.lookup_nsp(pfx)
self.nsp_cache[pfx] = ns
else:
return 'http://www.gajim.org/xmlns/undeclared'
return ns

def __str__(self,fancy=0):
""" Method used to dump node into textual representation.
if "fancy" argument is set to True produces indented output for readability."""
s = (fancy-1) * 2 * ' ' + "<" + self.name
if self.namespace:
if not self.parent or self.parent.namespace!=self.namespace:
s = s + ' xmlns="%s"'%self.namespace
if 'xmlns' not in self.attrs:
s = s + ' xmlns="%s"'%self.namespace
for key in self.attrs.keys():
val = ustr(self.attrs[key])
s = s + ' %s="%s"' % ( key, XMLescape(val) )
s = s + ">"
cnt = 0
cnt = 0
if self.kids:
if fancy: s = s + "\n"
for a in self.kids:
if not fancy and (len(self.data)-1)>=cnt: s=s+XMLescape(self.data[cnt])
elif (len(self.data)-1)>=cnt: s=s+XMLescape(self.data[cnt].strip())
if a: s = s + a.__str__(fancy and fancy+1)
if isinstance(a, Node):
s = s + a.__str__(fancy and fancy+1)
elif a:
s = s + a.__str__()
cnt=cnt+1
if not fancy and (len(self.data)-1) >= cnt: s = s + XMLescape(self.data[cnt])
elif (len(self.data)-1) >= cnt: s = s + XMLescape(self.data[cnt].strip())
if not self.kids and s[-1:]=='>':
if not self.kids and s.endswith('>'):
s=s[:-1]+' />'
if fancy: s = s + "\n"
else:
Expand All @@ -121,13 +156,14 @@ def getCDATA(self):
def addChild(self, name=None, attrs={}, payload=[], namespace=None, node=None):
""" If "node" argument is provided, adds it as child node. Else creates new node from
the other arguments' values and adds it as well."""
if attrs.has_key('xmlns'):
if 'xmlns' in attrs:
raise AttributeError("Use namespace=x instead of attrs={'xmlns':x}")
if namespace: name=namespace+' '+name
if node:
newnode=node
node.parent = self
else: newnode=Node(tag=name, parent=self, attrs=attrs, payload=payload)
if namespace:
newnode.setNamespace(namespace)
self.kids.append(newnode)
self.data.append(u'')
return newnode
Expand All @@ -144,7 +180,7 @@ def delAttr(self, key):
def delChild(self, node, attrs={}):
""" Deletes the "node" from the node's childs list, if "node" is an instance.
Else deletes the first node that have specified name and (optionally) attributes. """
if type(node)<>type(self): node=self.getTag(node,attrs)
if not isinstance(node, Node): node=self.getTag(node,attrs)
self.kids[self.kids.index(node)]=None
return node
def getAttrs(self):
Expand Down Expand Up @@ -196,13 +232,26 @@ def getTags(self, name, attrs={}, namespace=None, one=0):
nodes=[]
for node in self.kids:
if not node: continue
if namespace and namespace<>node.getNamespace(): continue
if namespace and namespace!=node.getNamespace(): continue
if node.getName() == name:
for key in attrs.keys():
if not node.attrs.has_key(key) or node.attrs[key]<>attrs[key]: break
if key not in node.attrs or node.attrs[key]!=attrs[key]: break
else: nodes.append(node)
if one and nodes: return nodes[0]
if not one: return nodes

def iterTags(self, name, attrs={}, namespace=None):
""" Iterate over all children using specified arguments as filter. """
for node in self.kids:
if not node: continue
if namespace is not None and namespace!=node.getNamespace(): continue
if node.getName() == name:
for key in attrs.keys():
if key not in node.attrs or \
node.attrs[key]!=attrs[key]: break
else:
yield node

def setAttr(self, key, val):
""" Sets attribute "key" with the value "val". """
self.attrs[key]=val
Expand All @@ -215,14 +264,14 @@ def setName(self,val):
def setNamespace(self, namespace):
""" Changes the node namespace. """
self.namespace=namespace
def setParent(self, node):
""" Sets node's parent to "node". WARNING: do not checks if the parent already present
def setParent(self, node):
""" Sets node's parent to "node". WARNING: do not checks if the parent already present
and not removes the node from the list of childs of previous parent. """
self.parent = node
def setPayload(self,payload,add=0):
""" Sets node payload according to the list specified. WARNING: completely replaces all node's
previous content. If you wish just to add child or CDATA - use addData or addChild methods. """
if type(payload) in (type(''),type(u'')): payload=[payload]
if isinstance(payload, basestring): payload=[payload]
if add: self.kids+=payload
else: self.kids=payload
def setTag(self, name, attrs={}, namespace=None):
Expand All @@ -243,7 +292,7 @@ def setTagData(self,tag,val,attrs={}):
except: self.addChild(tag,attrs,payload=[ustr(val)])
def has_attr(self,key):
""" Checks if node have attribute "key"."""
return self.attrs.has_key(key)
return key in self.attrs
def __getitem__(self,item):
""" Returns node's attribute "item" value. """
return self.getAttr(item)
Expand Down Expand Up @@ -283,7 +332,7 @@ def __setattr__(self,attr,val):
class NodeBuilder:
""" Builds a Node class minidom from data parsed to it. This class used for two purposes:
1. Creation an XML Node from a textual representation. F.e. reading a config file. See an XML2Node method.
2. Handling an incoming XML stream. This is done by mangling
2. Handling an incoming XML stream. This is done by mangling
the __dispatch_depth parameter and redefining the dispatch method.
You do not need to use this class directly if you do not designing your own XML handler."""
def __init__(self,data=None,initial_node=None):
Expand All @@ -294,95 +343,135 @@ def __init__(self,data=None,initial_node=None):
"data" (if provided) feeded to parser immidiatedly after instance init.
"""
self.DEBUG(DBG_NODEBUILDER, "Preparing to handle incoming XML stream.", 'start')
self._parser = xml.parsers.expat.ParserCreate(namespace_separator=' ')
self._parser = xml.parsers.expat.ParserCreate()
self._parser.StartElementHandler = self.starttag
self._parser.EndElementHandler = self.endtag
self._parser.CharacterDataHandler = self.handle_data
self._parser.CharacterDataHandler = self.handle_cdata
self._parser.StartNamespaceDeclHandler = self.handle_namespace_start
self._parser.buffer_text = True
self.Parse = self._parser.Parse

self.__depth = 0
self.__last_depth = 0
self.__max_depth = 0
self._dispatch_depth = 1
self._document_attrs = None
self._document_nsp = None
self._mini_dom=initial_node
self.last_is_data = 1
self._ptr=None
self.namespaces={"http://www.w3.org/XML/1998/namespace":'xml:'}
self.xmlns="http://www.w3.org/XML/1998/namespace"
self.data_buffer = None
self.streamError = ''
if data:
self._parser.Parse(data,1)

if data: self._parser.Parse(data,1)
def check_data_buffer(self):
if self.data_buffer:
self._ptr.data.append(''.join(self.data_buffer))
del self.data_buffer[:]
self.data_buffer = None

def destroy(self):
""" Method used to allow class instance to be garbage-collected. """
self.check_data_buffer()
self._parser.StartElementHandler = None
self._parser.EndElementHandler = None
self._parser.CharacterDataHandler = None
self._parser.StartNamespaceDeclHandler = None

def starttag(self, tag, attrs):
"""XML Parser callback. Used internally"""
attlist=attrs.keys() #
for attr in attlist: # FIXME: Crude hack. And it also slows down the whole library considerably.
sp=attr.rfind(" ") #
if sp==-1: continue #
ns=attr[:sp] #
attrs[self.namespaces[ns]+attr[sp+1:]]=attrs[attr]
del attrs[attr] #
self.__depth += 1
self.check_data_buffer()
self._inc_depth()
self.DEBUG(DBG_NODEBUILDER, "DEPTH -> %i , tag -> %s, attrs -> %s" % (self.__depth, tag, `attrs`), 'down')
if self.__depth == self._dispatch_depth:
if not self._mini_dom : self._mini_dom = Node(tag=tag, attrs=attrs)
else: Node.__init__(self._mini_dom,tag=tag, attrs=attrs)
if not self._mini_dom :
self._mini_dom = Node(tag=tag, attrs=attrs, nsp = self._document_nsp, node_built=True)
else:
Node.__init__(self._mini_dom,tag=tag, attrs=attrs, nsp = self._document_nsp, node_built=True)
self._ptr = self._mini_dom
elif self.__depth > self._dispatch_depth:
self._ptr.kids.append(Node(tag=tag,parent=self._ptr,attrs=attrs))
self._ptr.kids.append(Node(tag=tag,parent=self._ptr,attrs=attrs, node_built=True))
self._ptr = self._ptr.kids[-1]
if self.__depth == 1:
self._document_attrs = attrs
ns, name = (['']+tag.split())[-2:]
self.stream_header_received(ns, name, attrs)
if not self.last_is_data and self._ptr.parent: self._ptr.parent.data.append('')
self._document_attrs = {}
self._document_nsp = {}
nsp, name = (['']+tag.split(':'))[-2:]
for attr,val in attrs.items():
if attr == 'xmlns':
self._document_nsp[u''] = val
elif attr.startswith('xmlns:'):
self._document_nsp[attr[6:]] = val
else:
self._document_attrs[attr] = val
ns = self._document_nsp.get(nsp, 'http://www.gajim.org/xmlns/undeclared-root')
try:
self.stream_header_received(ns, name, attrs)
except ValueError, e:
self._document_attrs = None
raise ValueError(str(e))
if not self.last_is_data and self._ptr.parent:
self._ptr.parent.data.append('')
self.last_is_data = 0

def endtag(self, tag ):
"""XML Parser callback. Used internally"""
self.DEBUG(DBG_NODEBUILDER, "DEPTH -> %i , tag -> %s" % (self.__depth, tag), 'up')
self.check_data_buffer()
if self.__depth == self._dispatch_depth:
if self._mini_dom.getName() == 'error':
self.streamError = self._mini_dom.getChildren()[0].getName()
self.dispatch(self._mini_dom)
elif self.__depth > self._dispatch_depth:
self._ptr = self._ptr.parent
else:
self.DEBUG(DBG_NODEBUILDER, "Got higher than dispatch level. Stream terminated?", 'stop')
self.__depth -= 1
self._dec_depth()
self.last_is_data = 0
if self.__depth == 0: self.stream_footer_received()

def handle_data(self, data):
def handle_cdata(self, data):
"""XML Parser callback. Used internally"""
self.DEBUG(DBG_NODEBUILDER, data, 'data')
if not self._ptr: return
if self.last_is_data:
self._ptr.data[-1] += data
else:
self._ptr.data.append(data)
if self.data_buffer:
self.data_buffer.append(data)
elif self._ptr:
self.data_buffer = [data]
self.last_is_data = 1

def handle_namespace_start(self, prefix, uri):
"""XML Parser callback. Used internally"""
if prefix: self.namespaces[uri]=prefix+':'
else: self.xmlns=uri
self.check_data_buffer()

def DEBUG(self, level, text, comment=None):
""" Gets all NodeBuilder walking events. Can be used for debugging if redefined."""
def getDom(self):
""" Returns just built Node. """
self.check_data_buffer()
return self._mini_dom
def dispatch(self,stanza):
""" Gets called when the NodeBuilder reaches some level of depth on it's way up with the built
node as argument. Can be redefined to convert incoming XML stanzas to program events. """
def stream_header_received(self,ns,tag,attrs):
""" Method called when stream just opened. """
self.check_data_buffer()
def stream_footer_received(self):
""" Method called when stream just closed. """
self.check_data_buffer()

def has_received_endtag(self, level=0):
""" Return True if at least one end tag was seen (at level) """
return self.__depth <= level and self.__max_depth > level

def _inc_depth(self):
self.__last_depth = self.__depth
self.__depth += 1
self.__max_depth = max(self.__depth, self.__max_depth)

def _dec_depth(self):
self.__last_depth = self.__depth
self.__depth -= 1

def XML2Node(xml):
""" Converts supplied textual string into XML node. Handy f.e. for reading configuration file.
Expand Down

0 comments on commit 8a6ff2f

Please sign in to comment.