summaryrefslogtreecommitdiff
path: root/sleekxmpp/xmlstream/tostring/__init__.py
diff options
context:
space:
mode:
authorNathan Fritz <fritzy@netflint.net>2010-01-29 23:57:57 -0800
committerNathan Fritz <fritzy@netflint.net>2010-01-29 23:57:57 -0800
commit5e736f4b978414c111062ffb220e38eb40765db6 (patch)
tree9ef6f7c278e102a6ffb0db9ce0015a7919dfa900 /sleekxmpp/xmlstream/tostring/__init__.py
parent23b9930c44608e633437c8ec80ef687dd9c0a8b2 (diff)
downloadslixmpp-5e736f4b978414c111062ffb220e38eb40765db6.tar.gz
slixmpp-5e736f4b978414c111062ffb220e38eb40765db6.tar.bz2
slixmpp-5e736f4b978414c111062ffb220e38eb40765db6.tar.xz
slixmpp-5e736f4b978414c111062ffb220e38eb40765db6.zip
fixed setup.py issue with unicode in 3.x
Diffstat (limited to 'sleekxmpp/xmlstream/tostring/__init__.py')
-rw-r--r--sleekxmpp/xmlstream/tostring/__init__.py60
1 files changed, 60 insertions, 0 deletions
diff --git a/sleekxmpp/xmlstream/tostring/__init__.py b/sleekxmpp/xmlstream/tostring/__init__.py
new file mode 100644
index 00000000..6603cbb8
--- /dev/null
+++ b/sleekxmpp/xmlstream/tostring/__init__.py
@@ -0,0 +1,60 @@
+
+class ToString(object):
+ def __str__(self, xml=None, xmlns='', stringbuffer=''):
+ if xml is None:
+ xml = self.xml
+ newoutput = [stringbuffer]
+ #TODO respect ET mapped namespaces
+ itag = xml.tag.split('}', 1)[-1]
+ if '}' in xml.tag:
+ ixmlns = xml.tag.split('}', 1)[0][1:]
+ else:
+ ixmlns = ''
+ nsbuffer = ''
+ if xmlns != ixmlns and ixmlns != '' and ixmlns != self.namespace:
+ if self.stream is not None and ixmlns in self.stream.namespace_map:
+ if self.stream.namespace_map[ixmlns] != '':
+ itag = "%s:%s" % (self.stream.namespace_map[ixmlns], itag)
+ else:
+ nsbuffer = """ xmlns="%s\"""" % ixmlns
+ if ixmlns not in ('', xmlns, self.namespace):
+ nsbuffer = """ xmlns="%s\"""" % ixmlns
+ newoutput.append("<%s" % itag)
+ newoutput.append(nsbuffer)
+ for attrib in xml.attrib:
+ if '{' not in attrib:
+ newoutput.append(""" %s="%s\"""" % (attrib, self.xmlesc(xml.attrib[attrib])))
+ if len(xml) or xml.text or xml.tail:
+ newoutput.append(">")
+ if xml.text:
+ newoutput.append(self.xmlesc(xml.text))
+ if len(xml):
+ for child in xml.getchildren():
+ newoutput.append(self.__str__(child, ixmlns))
+ newoutput.append("</%s>" % (itag, ))
+ if xml.tail:
+ newoutput.append(self.xmlesc(xml.tail))
+ elif xml.text:
+ newoutput.append(">%s</%s>" % (self.xmlesc(xml.text), itag))
+ else:
+ newoutput.append(" />")
+ return ''.join(newoutput)
+
+ def xmlesc(self, text):
+ text = list(text)
+ cc = 0
+ matches = ('&', '<', '"', '>', "'")
+ for c in text:
+ if c in matches:
+ if c == '&':
+ text[cc] = '&amp;'
+ elif c == '<':
+ text[cc] = '&lt;'
+ elif c == '>':
+ text[cc] = '&gt;'
+ elif c == "'":
+ text[cc] = '&apos;'
+ else:
+ text[cc] = '&quot;'
+ cc += 1
+ return ''.join(text)