Welcome to mirror list, hosted at ThFree Co, Russian Federation.

c14n.py « xmpp « common « src - dev.gajim.org/gajim/gajim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b58e0a1d6cf70e2f871ac191d04d0970d77d0c2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from simplexml import ustr

# XML canonicalisation methods (for XEP-0116)
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

	sorted_attrs = node.attrs.keys()
	sorted_attrs.sort()
	for key in sorted_attrs:
		val = ustr(node.attrs[key])
		# like XMLescape() but with whitespace and without &gt;
		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])
	if not node.kids and s[-1:]=='>':
		s=s[:-1]+' />'
	else:
		s = s + "</" + node.name + ">"
	return s.encode('utf-8')

def normalise_attr(val):
	return val.replace('&', '&amp;').replace('<', '&lt;').replace('"', '&quot;').replace('\t', '&#x9;').replace('\n', '&#xA;').replace('\r', '&#xD;')

def normalise_text(val):
	return val.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;').replace('\r', '&#xD;')