Example #1
0
def escape_attrib_element_tree(text):
    # escape attribute value
    try:
        if "&" in text:
            text = text.replace("&", "&")
        if "<" in text:
            text = text.replace("<", "&lt;")
        if ">" in text:
            # Needed for the i3d format
            pass
            #text = text.replace(">", "&gt;")
        if "\"" in text:
            text = text.replace("\"", "&quot;")
        # The following business with carriage returns is to satisfy
        # Section 2.11 of the XML specification, stating that
        # CR or CR LN should be replaced with just LN
        # http://www.w3.org/TR/REC-xml/#sec-line-ends
        if "\r\n" in text:
            text = text.replace("\r\n", "\n")
        if "\r" in text:
            text = text.replace("\r", "\n")
        #The following four lines are issue 17582
        if "\n" in text:
            text = text.replace("\n", "&#10;")
        if "\t" in text:
            text = text.replace("\t", "&#09;")
        return text
    except (TypeError, AttributeError):
        ET._raise_serialization_error(text)
Example #2
0
 def _escape_cdata(text, encoding):
     if "<![CDATA[" in text:
         try:
             return text.encode(encoding, "xmlcharrefreplace")
         except (TypeError, AttributeError):
             ElementTree._raise_serialization_error(text)
     else:
         return original_escape_cdata(text, encoding)
Example #3
0
 def _escape_cdata(text, encoding):
     if "<![CDATA[" in text:
         try:
             return text.encode(encoding, "xmlcharrefreplace")
         except (TypeError, AttributeError):
             ElementTree._raise_serialization_error(text)
     else:
         return original_escape_cdata(text, encoding)
Example #4
0
def _escape_cdata(text):
    # escape character data
    try:
        if any(
                chr(c) in text for c in
            [c for c in range(0, 32) if c not in (
                ord("\n"),
                ord("\t"),
            )]):
            return "<![CDATA[" + escape_cdata_control_chars(text) + "]]>"
    except (TypeError, AttributeError):
        ET._raise_serialization_error(text)
    return ET._original_escape_cdata(text)
Example #5
0
 def _escape_attrib(text, encoding=None, replace=etree.string.replace):
     # escape attribute value
     try:
         if encoding:
             try:
                 text = etree._encode(text, encoding)
             except UnicodeError:
                 return etree._encode_entity(text)
         text = replace(text, "&", "&amp;")
         text = replace(text, "\"", "&quot;")
         text = replace(text, "<", "&lt;")
         text = replace(text, ">", "&gt;")
         return text
     except (TypeError, AttributeError):
         etree._raise_serialization_error(text)
Example #6
0
def _escape_attrib(text, encoding=None, replace=string.replace):
    # escape attribute value
    try:
        if encoding:
            try:
                text = ET._encode(text, encoding)
            except UnicodeError:
                return ET._encode_entity(text)
        text = replace(text, "&", "&amp;")
        text = replace(text, "'", "&apos;") 
        #text = replace(text, "\"", "&quot;")
        text = replace(text, "<", "&lt;")
        text = replace(text, ">", "&gt;")
        return text
    except (TypeError, AttributeError):
        ET._raise_serialization_error(text)
Example #7
0
def _escape_cdata_c14n(text):
    # escape character data
    try:
        # it's worth avoiding do-nothing calls for strings that are
        # shorter than 500 character, or so.  assume that's, by far,
        # the most common case in most applications.
        if "&" in text:
            text = text.replace("&", "&amp;")
        if "<" in text:
            text = text.replace("<", "&lt;")
        if ">" in text:
            text = text.replace(">", "&gt;")
        if "\r" in text:
            text = text.replace("\r", "&#xD;")
        return text
    except (TypeError, AttributeError):
        ElementTree._raise_serialization_error(text)
Example #8
0
def _escape_attrib(text):
    # escape character data
    try:
        # Copied from _original_escape_attrib
        if "&" in text:
            text = text.replace("&", "&amp;")
        if "<" in text:
            text = text.replace("<", "&lt;")
        if ">" in text:
            text = text.replace(">", "&gt;")
        if "\"" in text:
            text = text.replace("\"", "&quot;")
        # Additionally, change control chars to entity numbers
        if any(chr(c) in text for c in [c for c in range(0, 32)]):
            return escape_attribute_control_chars(text)
    except (TypeError, AttributeError):
        ET._raise_serialization_error(text)
    return text  #ET._original_escape_attrib(text)
Example #9
0
def et_escape_cdata_mind_binary(text):
    # escape character data
    try:
        if True:
            if "&" in text:
                text = text.replace("&", "&amp;")
            if "<" in text:
                text = text.replace("<", "&lt;")
            if ">" in text:
                text = text.replace(">", "&gt;")
            #if '"' in text:
            #    text = text.replace('"', "&quot;")
            for i in range(0, 32):
                if i in [ord("\n"), ord("\t")]: continue
                text = text.replace(chr(i), "&#x{:02X};".format(i))
        return text
    except (TypeError, AttributeError):
        ET._raise_serialization_error(text)
Example #10
0
def _escape_cdata(text, encoding):
    # escape character data
    try:
        # it's worth avoiding do-nothing calls for strings that are
        # shorter than 500 character, or so.  assume that's, by far,
        # the most common case in most applications.
        if "&" in text:
            text = text.replace("&", "&amp;")
        if "<" in text:
            text = text.replace("<", "&lt;")
        if ">" in text:
            text = text.replace(">", "&gt;")
        return text.encode(encoding, "xmlcharrefreplace")
    except (TypeError, AttributeError):
        ElementTree._raise_serialization_error(text)
    #XXX this is the patch
    except (UnicodeDecodeError):
        return text.decode("utf-8").encode(encoding, "xmlcharrefreplace")
Example #11
0
def _escape_attrib_c14n(text):
    # escape attribute value
    try:
        if "&" in text:
            text = text.replace("&", "&amp;")
        if "<" in text:
            text = text.replace("<", "&lt;")
        if '"' in text:
            text = text.replace('"', "&quot;")
        if "\t" in text:
            text = text.replace("\t", "&#x9;")
        if "\n" in text:
            text = text.replace("\n", "&#xA;")
        if "\r" in text:
            text = text.replace("\r", "&#xD;")
        return text
    except (TypeError, AttributeError):
        ElementTree._raise_serialization_error(text)
Example #12
0
 def _write2(self, file, node, encoding, namespaces):
     # write XML to file
     tag = node.tag
     if tag is ElementTree.Comment:
         text = node.text.encode(encoding)
         file.write("<!--%s-->" % text)
     elif tag is ElementTree.ProcessingInstruction:
         text = node.text.encode(encoding)
         file.write("<?%s?>" % text)
     else:
         items = node.items()
         xmlns_items = [] # new namespaces in this scope
         try:
             if isinstance(tag, ElementTree.QName) or tag[:1] == "{":
                 tag, xmlns = ElementTree.fixtag(tag, namespaces)
                 if xmlns: xmlns_items.append(xmlns)
         except TypeError:
             ElementTree._raise_serialization_error(tag)
         file.write("<" + ElementTree._encode(tag, encoding))
         if items or xmlns_items:
             items.sort() # lexical order
             for k, v in items:
                 try:
                     if isinstance(k, ElementTree.QName) or k[:1] == "{":
                         k, xmlns = ElementTree.fixtag(k, namespaces)
                         if xmlns: xmlns_items.append(xmlns)
                 except TypeError:
                     ElementTree._raise_serialization_error(k)
                 try:
                     if isinstance(v, ElementTree.QName):
                         v, xmlns = ElementTree.fixtag(v, namespaces)
                         if xmlns: xmlns_items.append(xmlns)
                 except TypeError:
                     ElementTree._raise_serialization_error(v)
                 file.write(" %s=\"%s\"" % (ElementTree._encode(k, encoding),
                                            ElementTree._escape_attrib(v, encoding)))
             for k, v in xmlns_items:
                 file.write(" %s=\"%s\"" % (ElementTree._encode(k, encoding),
                                            ElementTree._escape_attrib(v, encoding)))
         if node.text or len(node):
             file.write(">")
             if node.text:
                 text = node.text.encode(encoding)
                 file.write(text)
             for n in node:
                 self._write(file, n, encoding, namespaces)
             file.write("</" + ElementTree._encode(tag, encoding) + ">")
         else:
             file.write(" />")
         for k, v in xmlns_items:
             del namespaces[v]
     if node.tail:
         tail = node.tail.encode(encoding)
         file.write(tail)
Example #13
0
 def _write(self, file, node, encoding, namespaces):
     # write XML to file
     tag = node.tag
     if tag is ET.Comment:
         file.write("<!-- %s -->" % ET._escape_cdata(node.text, encoding))
     elif tag is ET.ProcessingInstruction:
         file.write("<?%s?>" % ET._escape_cdata(node.text, encoding))
     else:
         items = node.items()
         xmlns_items = []  # new namespaces in this scope
         try:
             if isinstance(tag, ET.QName) or tag[:1] == "{":
                 tag, xmlns = ET.fixtag(tag, namespaces)
                 if xmlns: xmlns_items.append(xmlns)
         except TypeError:
             ET._raise_serialization_error(tag)
         file.write("<" + ET._encode(tag, encoding))
         if items or xmlns_items:
             items.sort()  # lexical order
             for k, v in items:
                 try:
                     if isinstance(k, ET.QName) or k[:1] == "{":
                         k, xmlns = ET.fixtag(k, namespaces)
                         if xmlns: ET.xmlns_items.append(xmlns)
                 except TypeError:
                     ET._raise_serialization_error(k)
                 try:
                     if isinstance(v, ET.QName):
                         v, xmlns = ET.fixtag(v, namespaces)
                         if xmlns: xmlns_items.append(xmlns)
                 except TypeError:
                     ET._raise_serialization_error(v)
                 file.write(
                     " %s=\'%s\'" %
                     (ET._encode(k, encoding), _escape_attrib(v, encoding)))
             for k, v in xmlns_items:
                 file.write(
                     " %s=\'%s\'" %
                     (ET._encode(k, encoding), _escape_attrib(v, encoding)))
         if node.text or len(node):
             file.write(">")
             if node.text:
                 file.write(ET._escape_cdata(node.text, encoding))
             for n in node:
                 self._write(file, n, encoding, namespaces)
             file.write("</" + ET._encode(tag, encoding) + ">")
         else:
             file.write(" />")
         for k, v in xmlns_items:
             del namespaces[v]
     if node.tail:
         file.write(ET._escape_cdata(node.tail, encoding))
Example #14
-1
def custom_xml_write(self, file, node, encoding, namespaces, indentation='\n'):
    """
    Custom write function based on ElementTree.ElementTree._write only for python 2.6
    Basically it does the same but writes each attribute in a different line
    The same was done with custom_serialize_xml for python 2.7
    """
    tag = node.tag
    next_indentation = node.tail
    if tag is pyET.Comment:
        file.write("<!-- %s -->" % pyET._escape_cdata(node.text, encoding))
    elif tag is pyET.ProcessingInstruction:
        file.write("<?%s?>" % pyET._escape_cdata(node.text, encoding))
    else:
        items = node.items()
        xmlns_items = []
        try:
            if isinstance(tag, pyET.QName) or tag[:1] == "{":
                tag, xmlns = pyET.fixtag(tag, namespaces)
                if xmlns: xmlns_items.append(xmlns)
        except TypeError:
            pyET._raise_serialization_error(tag)
        file.write("<" + pyET._encode(tag, encoding))
        if items or xmlns_items:
            items.sort()
            for k, v in items:
                try:
                    if isinstance(k, pyET.QName) or k[:1] == "{":
                        k, xmlns = pyET.fixtag(k, namespaces)
                        if xmlns: xmlns_items.append(xmlns)
                except TypeError:
                    pyET._raise_serialization_error(k)
                try:
                    if isinstance(v, pyET.QName):
                        v, xmlns = pyET.fixtag(v, namespaces)
                        if xmlns: xmlns_items.append(xmlns)
                except TypeError:
                    pyET._raise_serialization_error(v)
                file.write("%s\t\t%s=\"%s\"" % (indentation, pyET._encode(k, encoding),
                                                pyET._escape_attrib(v, encoding)))
            for k, v in xmlns_items:
                file.write("%s\t\t%s=\"%s\"" % (indentation, pyET._encode(k, encoding),
                                                pyET._escape_attrib(v, encoding)))
        if node.text or len(node):
            file.write(">")
            if node.text:
                file.write(pyET._escape_cdata(node.text, encoding))
            for n in node:
                self._write(file, n, encoding, namespaces, next_indentation)
            file.write("</" + pyET._encode(tag, encoding) + ">")
        else:
            file.write(" />")
        for k, v in xmlns_items:
            del namespaces[v]
    if node.tail:
        file.write(pyET._escape_cdata(node.tail, encoding))