Skip to content

Commit

Permalink
Updated _stringify_content
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkDavidson committed Feb 13, 2014
1 parent 2b4775c commit 69fd221
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions libtaxii/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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., '<Hello/>') 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):
Expand Down

0 comments on commit 69fd221

Please sign in to comment.