From 8b633147171884f9c163e8b6a8c498b2f3676b14 Mon Sep 17 00:00:00 2001 From: Christian Van Wambeke Date: Wed, 27 Jun 2018 13:10:35 +0200 Subject: [PATCH] src.xmlManager escapeSequence --- src/xmlManager.py | 66 +++++++++++++++++++++++------------------------ 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/src/xmlManager.py b/src/xmlManager.py index 20ef4e5..60cebca 100644 --- a/src/xmlManager.py +++ b/src/xmlManager.py @@ -45,38 +45,6 @@ class XmlLogFile(object): # Initialize the field that contain the xml in memory self.xmlroot = etree.Element(rootname, attrib = attrib) - def escapeSequence(self, aStr): - """ - See xml specification: - The ampersand character(&) and the left angle bracket(<) MUST NOT appear in their - literal form, except when used as markup delimiters, or within a comment, a processing - instruction, or a CDATA section. - If they are needed elsewhere, they MUST be escaped using either numeric character references - or the strings '&' and '<' respectively. - The right angle bracket(>) may be - represented using the string '>', and MUST, - for compatibility, be escaped using either '>' or a character reference - when it appears in the string " ]]> " in content, - when that string is not marking the end of a CDATA section. - You can use these escape sequences: - < (less - than) as < or < - > (greater - than) as > or > - & (ampersand) as & - ' (apostrophe or single quote) as ' - " (double-quote) as " - """ - replaces = [ ('&', '&'), - ('>', '>'), - ('<', '<'), - ("'", '''), - ('"', '"'), - ] - res = aStr - for ini, fin in replaces: # order matters - res = res.replace(ini, fin) - return res - - def write_tree(self, stylesheet=None, file_path = None): '''Write the xml tree in the log file path. Add the stylesheet if asked. @@ -240,5 +208,37 @@ def write_report(filename, xmlroot, stylesheet): if len(stylesheet) > 0: f.write("\n" % stylesheet) f.write(etree.tostring(xmlroot, encoding='utf-8')) - f.close() + f.close() + +def escapeSequence(aStr): + """ + See xml specification: + The ampersand character(&) and the left angle bracket(<) MUST NOT appear in their + literal form, except when used as markup delimiters, or within a comment, a processing + instruction, or a CDATA section. + If they are needed elsewhere, they MUST be escaped using either numeric character references + or the strings '&' and '<' respectively. + The right angle bracket(>) may be + represented using the string '>', and MUST, + for compatibility, be escaped using either '>' or a character reference + when it appears in the string " ]]> " in content, + when that string is not marking the end of a CDATA section. + You can use these escape sequences: + < (less - than) as < or < + > (greater - than) as > or > + & (ampersand) as & + ' (apostrophe or single quote) as ' + " (double-quote) as " + """ + replaces = [ ('&', '&'), + ('>', '>'), + ('<', '<'), + ("'", '''), + ('"', '"'), + ] + res = aStr + for ini, fin in replaces: # order matters + res = res.replace(ini, fin) + return res + -- 2.39.2