From 69fd221e46bf777b3d37a49d015476ff5a1d3459 Mon Sep 17 00:00:00 2001 From: MarkDavidson Date: Thu, 13 Feb 2014 14:57:35 -0500 Subject: [PATCH] Updated _stringify_content --- libtaxii/messages.py | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/libtaxii/messages.py b/libtaxii/messages.py index 5f3273c..3678b1a 100644 --- a/libtaxii/messages.py +++ b/libtaxii/messages.py @@ -775,10 +775,34 @@ def timestamp_label(self, value): self._timestamp_label = value def _stringify_content(self, content): - """Always a string or raises an error.""" + """Always a string or raises an error. + Returns the string representation and whether the data is XML. + """ + #If it's an etree, it's definitely XML if isinstance(content, etree._ElementTree) or isinstance(content, etree._Element): return etree.tostring(content), True - + + #It might be a string representation of XML + #There is an edge case here where a string that looks like XML + # (e.g., '') but isn't actually XML (and, honestly, in the + # string representation the difference is somewhat academic) + # will get interpreted as XML. + try: + etree.parse(content, get_xml_parser()) + return str(content), True + except IOError:#This error happens if the content is not a file-like object (e.g., StringIO.StringIO) + pass + except etree.XMLSyntaxError:#This happens when it is a file like object, but does not contain well formed XML + pass + + try: + sio_content = StringIO.StringIO(content) + etree.parse(sio_content, get_xml_parser()) + return str(content), True + except etree.XMLSyntaxError:#This happens if the content is not well formed XML + pass + + #The default is that it's not XML return str(content), False def to_etree(self):