summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathan Fritz <fritzy@netflint.net>2010-01-29 02:11:45 -0800
committerNathan Fritz <fritzy@netflint.net>2010-01-29 02:11:45 -0800
commit23b9930c44608e633437c8ec80ef687dd9c0a8b2 (patch)
tree4969fb610d5116fc6e30470bce06e4630fef28ed
parent395618d3d37c5c4092466f4c5ea6919925fda681 (diff)
downloadslixmpp-23b9930c44608e633437c8ec80ef687dd9c0a8b2.tar.gz
slixmpp-23b9930c44608e633437c8ec80ef687dd9c0a8b2.tar.bz2
slixmpp-23b9930c44608e633437c8ec80ef687dd9c0a8b2.tar.xz
slixmpp-23b9930c44608e633437c8ec80ef687dd9c0a8b2.zip
added separate tostring files
-rw-r--r--sleekxmpp/xmlstream/tostring.py60
-rw-r--r--sleekxmpp/xmlstream/tostring26.py65
-rw-r--r--tests/test_presencestanzas.py15
3 files changed, 140 insertions, 0 deletions
diff --git a/sleekxmpp/xmlstream/tostring.py b/sleekxmpp/xmlstream/tostring.py
new file mode 100644
index 00000000..6603cbb8
--- /dev/null
+++ b/sleekxmpp/xmlstream/tostring.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)
diff --git a/sleekxmpp/xmlstream/tostring26.py b/sleekxmpp/xmlstream/tostring26.py
new file mode 100644
index 00000000..9711c300
--- /dev/null
+++ b/sleekxmpp/xmlstream/tostring26.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)
diff --git a/tests/test_presencestanzas.py b/tests/test_presencestanzas.py
new file mode 100644
index 00000000..430c71ca
--- /dev/null
+++ b/tests/test_presencestanzas.py
@@ -0,0 +1,15 @@
+import unittest
+
+class testpresencestanzas(unittest.TestCase):
+
+ def setUp(self):
+ import sleekxmpp.stanza.presence as p
+ self.p = p
+
+ def testPresenceShowRegression(self):
+ "Regression check presence['type'] = 'dnd' show value working"
+ p = self.p.Presence()
+ p['type'] = 'dnd'
+ self.failUnless(str(p) == "<presence><show>dnd</show></presence>")
+
+suite = unittest.TestLoader().loadTestsFromTestCase(testpresencestanzas)