2008-12-28 01:32:34 +00:00
|
|
|
''' XML canonicalisation methods (for XEP-0116) '''
|
|
|
|
# FIXME: Add licence, copyright
|
|
|
|
|
2007-06-17 10:37:59 +00:00
|
|
|
from simplexml import ustr
|
|
|
|
|
|
|
|
def c14n(node):
|
|
|
|
s = "<" + node.name
|
|
|
|
if node.namespace:
|
|
|
|
if not node.parent or node.parent.namespace != node.namespace:
|
|
|
|
s = s + ' xmlns="%s"' % node.namespace
|
|
|
|
|
2008-10-11 09:59:52 +00:00
|
|
|
sorted_attrs = sorted(node.attrs.keys())
|
2007-06-17 10:37:59 +00:00
|
|
|
for key in sorted_attrs:
|
|
|
|
val = ustr(node.attrs[key])
|
|
|
|
# like XMLescape() but with whitespace and without >
|
|
|
|
s = s + ' %s="%s"' % ( key, normalise_attr(val) )
|
|
|
|
s = s + ">"
|
|
|
|
cnt = 0
|
|
|
|
if node.kids:
|
|
|
|
for a in node.kids:
|
|
|
|
if (len(node.data)-1) >= cnt:
|
|
|
|
s = s + normalise_text(node.data[cnt])
|
|
|
|
s = s + c14n(a)
|
|
|
|
cnt=cnt+1
|
|
|
|
if (len(node.data)-1) >= cnt: s = s + normalise_text(node.data[cnt])
|
2008-10-11 09:32:59 +00:00
|
|
|
if not node.kids and s.endswith('>'):
|
2007-06-17 10:37:59 +00:00
|
|
|
s=s[:-1]+' />'
|
|
|
|
else:
|
|
|
|
s = s + "</" + node.name + ">"
|
|
|
|
return s.encode('utf-8')
|
|
|
|
|
|
|
|
def normalise_attr(val):
|
|
|
|
return val.replace('&', '&').replace('<', '<').replace('"', '"').replace('\t', '	').replace('\n', '
').replace('\r', '
')
|
|
|
|
|
|
|
|
def normalise_text(val):
|
|
|
|
return val.replace('&', '&').replace('<', '<').replace('>', '>').replace('\r', '
')
|
|
|
|
|
2008-07-29 19:49:31 +00:00
|
|
|
|
2008-12-28 01:32:34 +00:00
|
|
|
# vim: se ts=3:
|