2008-12-28 01:39:39 +00:00
|
|
|
# -*- coding:utf-8 -*-
|
2009-01-09 00:49:58 +00:00
|
|
|
## c14n.py
|
2008-12-28 01:39:39 +00:00
|
|
|
##
|
|
|
|
## Copyright (C) 2007-2008 Brendan Taylor <whateley AT gmail.com>
|
|
|
|
##
|
|
|
|
## This file is part of Gajim.
|
|
|
|
##
|
|
|
|
## Gajim is free software; you can redistribute it and/or modify
|
|
|
|
## it under the terms of the GNU General Public License as published
|
|
|
|
## by the Free Software Foundation; version 3 only.
|
|
|
|
##
|
|
|
|
## Gajim is distributed in the hope that it will be useful,
|
|
|
|
## but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
## GNU General Public License for more details.
|
|
|
|
##
|
|
|
|
## You should have received a copy of the GNU General Public License
|
|
|
|
## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
##
|
2009-01-09 00:49:58 +00:00
|
|
|
|
2009-11-26 16:32:56 +02:00
|
|
|
"""
|
|
|
|
XML canonicalisation methods (for XEP-0116)
|
|
|
|
"""
|
|
|
|
|
2007-06-17 10:37:59 +00:00
|
|
|
from simplexml import ustr
|
|
|
|
|
2009-10-01 22:17:19 +02:00
|
|
|
def c14n(node, is_buggy):
|
2010-02-08 15:08:40 +01:00
|
|
|
s = "<" + node.name
|
|
|
|
if node.namespace:
|
|
|
|
if not node.parent or node.parent.namespace != node.namespace:
|
|
|
|
s = s + ' xmlns="%s"' % node.namespace
|
|
|
|
|
|
|
|
sorted_attrs = sorted(node.attrs.keys())
|
|
|
|
for key in sorted_attrs:
|
|
|
|
if not is_buggy and key == 'xmlns':
|
|
|
|
continue
|
|
|
|
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, is_buggy)
|
|
|
|
cnt=cnt+1
|
|
|
|
if (len(node.data)-1) >= cnt: s = s + normalise_text(node.data[cnt])
|
|
|
|
if not node.kids and s.endswith('>'):
|
|
|
|
s=s[:-1]+' />'
|
|
|
|
else:
|
|
|
|
s = s + "</" + node.name + ">"
|
|
|
|
return s.encode('utf-8')
|
2007-06-17 10:37:59 +00:00
|
|
|
|
|
|
|
def normalise_attr(val):
|
2010-02-08 15:08:40 +01:00
|
|
|
return val.replace('&', '&').replace('<', '<').replace('"', '"').replace('\t', '	').replace('\n', '
').replace('\r', '
')
|
2007-06-17 10:37:59 +00:00
|
|
|
|
|
|
|
def normalise_text(val):
|
2010-02-08 15:08:40 +01:00
|
|
|
return val.replace('&', '&').replace('<', '<').replace('>', '>').replace('\r', '
')
|