summaryrefslogtreecommitdiff
path: root/sleekxmpp/xmlstream/tostring26/__init__.py
diff options
context:
space:
mode:
authorNathan Fritz <fritzy@netflint.net>2010-01-29 23:58:14 -0800
committerNathan Fritz <fritzy@netflint.net>2010-01-29 23:58:14 -0800
commit06fa1fcf332c9e0623dff90db8d1daacdfac87f1 (patch)
treeff4466691117e0fee66add9d98f2edd378e55236 /sleekxmpp/xmlstream/tostring26/__init__.py
parent4d8709859096699ad79b8fce295ab05f2cb31de2 (diff)
parent5e736f4b978414c111062ffb220e38eb40765db6 (diff)
downloadslixmpp-06fa1fcf332c9e0623dff90db8d1daacdfac87f1.tar.gz
slixmpp-06fa1fcf332c9e0623dff90db8d1daacdfac87f1.tar.bz2
slixmpp-06fa1fcf332c9e0623dff90db8d1daacdfac87f1.tar.xz
slixmpp-06fa1fcf332c9e0623dff90db8d1daacdfac87f1.zip
Merge branch 'develop'
Diffstat (limited to 'sleekxmpp/xmlstream/tostring26/__init__.py')
-rw-r--r--sleekxmpp/xmlstream/tostring26/__init__.py65
1 files changed, 65 insertions, 0 deletions
diff --git a/sleekxmpp/xmlstream/tostring26/__init__.py b/sleekxmpp/xmlstream/tostring26/__init__.py
new file mode 100644
index 00000000..9711c300
--- /dev/null
+++ b/sleekxmpp/xmlstream/tostring26/__init__.py
@@ -0,0 +1,65 @@
+import types
+
+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 != u'' and ixmlns != self.namespace:
+ if self.stream is not None and ixmlns in self.stream.namespace_map:
+ if self.stream.namespace_map[ixmlns] != u'':
+ 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(u">")
+ 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(u"</%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 u''.join(newoutput)
+
+ def xmlesc(self, text):
+ if type(text) != types.UnicodeType:
+ text = list(unicode(text, 'utf-8', 'ignore'))
+ else:
+ text = list(text)
+
+ cc = 0
+ matches = (u'&', u'<', u'"', u'>', u"'")
+ for c in text:
+ if c in matches:
+ if c == u'&':
+ text[cc] = u'&amp;'
+ elif c == u'<':
+ text[cc] = u'&lt;'
+ elif c == u'>':
+ text[cc] = u'&gt;'
+ elif c == u"'":
+ text[cc] = u'&apos;'
+ else:
+ text[cc] = u'&quot;'
+ cc += 1
+ return ''.join(text)