Exemplo n.º 1
0
 def _prettywrite(self, file, node, encoding, namespaces, depth=0):
     # write XML to file
     tag = node.tag
     if tag is Comment:
         file.write("\r\n" + "  " * depth)
         file.write("<!-- %s -->" % _escape_cdata(node.text, encoding))
     elif tag is ProcessingInstruction:
         file.write("\r\n" + "  " * depth)
         file.write("<?%s?>" % _escape_cdata(node.text, encoding))
     else:
         items = node.items()
         xmlns_items = [] # new namespaces in this scope
         try:
             if isinstance(tag, QName) or tag[:1] == "{":
                 tag, xmlns = myfixtag(tag, namespaces)
                 if xmlns:
                     xmlns_items.append(xmlns)
         except TypeError:
             _raise_serialization_error(tag)
         file.write("\r\n" + "  " * depth)
         file.write("<" + _encode(tag, encoding))
         if items or xmlns_items:
             items.sort() # lexical order
             for k, v in items:
                 try:
                     if isinstance(k, QName) or k[:1] == "{":
                         k, xmlns = myfixtag(k, namespaces)
                         if xmlns:
                             xmlns_items.append(xmlns)
                 except TypeError:
                     _raise_serialization_error(k)
                 try:
                     if isinstance(v, QName):
                         v, xmlns = myfixtag(v, namespaces)
                         if xmlns:
                             xmlns_items.append(xmlns)
                 except TypeError:
                     _raise_serialization_error(v)
                 file.write(" %s=\"%s\"" % (_encode(k, encoding),
                                            _escape_attrib(v, encoding)))
             for k, v in xmlns_items:
                 file.write(" %s=\"%s\"" % (_encode(k, encoding),
                                            _escape_attrib(v, encoding)))
         if node.text or len(node):
             file.write(">")
             if node.text:
                 file.write(_escape_cdata(node.text, encoding))
             for n in node:
                 self._prettywrite(file, n, encoding, namespaces, depth=depth + 1)
             if not node.text or len(node):
                 file.write("\r\n" + "  " * depth)
             file.write("</" + _encode(tag, encoding) + ">")
         else:
             file.write(" />")
         for k, v in xmlns_items:
             del namespaces[v]
     if node.tail:
         file.write(_escape_cdata(node.tail, encoding))
Exemplo n.º 2
0
def Etree_pretty__write(self, file, node, encoding, namespaces,
                        level=0, identator="    "):
    tag = node.tag
    if tag is Comment:
        file.write(level * identator + "<!-- %s -->" % _escape_cdata(node.text, encoding))
    elif tag is ProcessingInstruction:
        file.write("<?%s?>" % _escape_cdata(node.text, encoding))
    else:
        items = node.items()
        xmlns_items = [] # new namespaces in this scope
        try:
            if isinstance(tag, QName) or tag[:1] == "{":
                tag, xmlns = fixtag(tag, namespaces)
                if xmlns: xmlns_items.append(xmlns)
        except TypeError:
            raise #_raise_serialization_error(tag)
        file.write("\n" + level * identator + "<" + _encode(tag, encoding))
        if items or xmlns_items:
            items.sort() # lexical order
            for k, v in items:
                try:
                    if isinstance(k, QName) or k[:1] == "{":
                        k, xmlns = fixtag(k, namespaces)
                        if xmlns: xmlns_items.append(xmlns)
                except TypeError:
                    raise #_raise_serialization_error(k)
                try:
                    if isinstance(v, QName):
                        v, xmlns = fixtag(v, namespaces)
                        if xmlns: xmlns_items.append(xmlns)
                except TypeError:
                    raise #_raise_serialization_error(v)
                file.write(u" %s=\"%s\"" % (_encode(k, encoding),
                                            _escape_attrib(v, encoding)))
            for k, v in xmlns_items:
                file.write(u" %s=\"%s\"" % (_encode(k, encoding),
                                            _escape_attrib(v, encoding)))
        if node.text or len(node):
            file.write(">")
            if node.text:
                file.write(_escape_cdata(node.text.replace("\n", (level + 1) * identator + "\n"), encoding))
            for n in node:
                self._write(file, n, encoding, namespaces, level + 1, identator)
            if node.text and len(node.text) < 125:
                file.write("</" + _encode(tag, encoding) + ">")
            else:
                file.write("\n" + level * identator + "</" + _encode(tag, encoding) + ">")
        else:
            file.write(" />")
        for k, v in xmlns_items:
            del namespaces[v]
    if node.tail:
        file.write(_escape_cdata(node.tail.replace("\n", level * identator + "\n"), encoding))
Exemplo n.º 3
0
def Etree_pretty__write(self, file, node, encoding, namespaces,
                        level=0, identator="    "):
    tag = node.tag
    if tag is Comment:
        file.write(level * identator + "<!-- %s -->" % _escape_cdata(node.text, encoding))
    elif tag is ProcessingInstruction:
        file.write("<?%s?>" % _escape_cdata(node.text, encoding))
    else:
        items = node.items()
        xmlns_items = [] # new namespaces in this scope
        try:
            if isinstance(tag, QName) or tag[:1] == "{":
                tag, xmlns = fixtag(tag, namespaces)
                if xmlns: xmlns_items.append(xmlns)
        except TypeError:
            raise #_raise_serialization_error(tag)
        file.write("\n" + level * identator + "<" + _encode(tag, encoding))
        if items or xmlns_items:
            items.sort() # lexical order
            for k, v in items:
                try:
                    if isinstance(k, QName) or k[:1] == "{":
                        k, xmlns = fixtag(k, namespaces)
                        if xmlns: xmlns_items.append(xmlns)
                except TypeError:
                    raise #_raise_serialization_error(k)
                try:
                    if isinstance(v, QName):
                        v, xmlns = fixtag(v, namespaces)
                        if xmlns: xmlns_items.append(xmlns)
                except TypeError:
                    raise #_raise_serialization_error(v)
                file.write(u" %s=\"%s\"" % (_encode(k, encoding),
                                            _escape_attrib(v, encoding)))
            for k, v in xmlns_items:
                file.write(u" %s=\"%s\"" % (_encode(k, encoding),
                                            _escape_attrib(v, encoding)))
        if node.text or len(node):
            file.write(">")
            if node.text:
                file.write(_escape_cdata(node.text, encoding))
            for n in node:
                self._write(file, n, encoding, namespaces, level + 1, identator)
            if node.text and len(node.text) < 125:
                file.write("</" + _encode(tag, encoding) + ">")
            else:
                file.write("\n" + level * identator + "</" + _encode(tag, encoding) + ">")
        else:
            file.write(" />")
        for k, v in xmlns_items:
            del namespaces[v]
    if node.tail:
        file.write(_escape_cdata(node.tail.replace("\n", (level * identator )+ "\n"), encoding))
Exemplo n.º 4
0
 def _write(self, file, node, encoding, namespaces):
     # write XML to file
     tag = node.tag
     if tag is Comment:
         file.write("\n<!-- %s -->" % _escape_cdata(node.text, encoding))
     elif tag is ProcessingInstruction:
         file.write("\n<?%s?>" % _escape_cdata(node.text, encoding))
     else:
         items = node.items()
         xmlns_items = []  # new namespaces in this scope
         try:
             if isinstance(tag, QName) or tag[:1] == "{":
                 tag, xmlns = fixtag(tag, namespaces)
                 if xmlns: xmlns_items.append(xmlns)
         except TypeError:
             _raise_serialization_error(tag)
         file.write("\n<" + _encode(tag, encoding))
         if items or xmlns_items:
             items.sort()  # lexical order
             for k, v in items:
                 try:
                     if isinstance(k, QName) or k[:1] == "{":
                         k, xmlns = fixtag(k, namespaces)
                         if xmlns: xmlns_items.append(xmlns)
                 except TypeError:
                     _raise_serialization_error(k)
                 try:
                     if isinstance(v, QName):
                         v, xmlns = fixtag(v, namespaces)
                         if xmlns: xmlns_items.append(xmlns)
                 except TypeError:
                     _raise_serialization_error(v)
                 file.write(
                     " %s=\"%s\"" %
                     (_encode(k, encoding), _escape_attrib(v, encoding)))
             for k, v in xmlns_items:
                 file.write(
                     " %s=\"%s\"" %
                     (_encode(k, encoding), _escape_attrib(v, encoding)))
         if node.text or len(node):
             file.write(">")
             if node.text:
                 file.write(_escape_cdata(node.text, encoding))
             for n in node:
                 self._write(file, n, encoding, namespaces)
             file.write("</" + _encode(tag, encoding) + ">")
         else:
             file.write(" />")
         for k, v in xmlns_items:
             del namespaces[v]
     if node.tail:
         file.write(_escape_cdata(node.tail, encoding))
Exemplo n.º 5
0
    def _print_node(self, node, encoding):
        tree_string = ""

        tag = node.tag
        if tag is Comment:
            tree_string += "<!-- %s -->" % _escape_cdata(node.text, encoding)
        elif tag is ProcessingInstruction:
            tree_string += "<?%s?>" % _escape_cdata(node.text, encoding)
        else:
            items = node.items()
            xmlns_items = [] # new namespaces in this scope
            try:
                if isinstance(tag, QName) or tag[:1] == "{":
                    tag, xmlns = fixtag(tag, namespaces)
                    if xmlns: xmlns_items.append(xmlns)
            except TypeError:
                _raise_serialization_error(tag)
            tree_string += "<" + _encode(tag, encoding)
            if items or xmlns_items:
                items.sort() # lexical order
                for k, v in items:
                    try:
                        if isinstance(k, QName) or k[:1] == "{":
                            k, xmlns = fixtag(k, namespaces)
                            if xmlns: xmlns_items.append(xmlns)
                    except TypeError:
                        _raise_serialization_error(k)
                    try:
                        if isinstance(v, QName):
                            v, xmlns = fixtag(v, namespaces)
                            if xmlns: xmlns_items.append(xmlns)
                    except TypeError:
                        _raise_serialization_error(v)
                    tree_string += " %s=\"%s\"" % (_encode(k, encoding),
                                               _escape_attrib(v, encoding))
                for k, v in xmlns_items:
                    tree_string += " %s=\"%s\"" % (_encode(k, encoding),
                                               _escape_attrib(v, encoding))

            tree_string += ">"
            if node.text:
                tree_string += _escape_cdata(node.text, encoding).replace('"', '&quot;')
            for n in node:
                tree_string += self._print_node(n, encoding)
            tree_string += "</" + _encode(tag, encoding) + ">"
            for k, v in xmlns_items:
                del namespaces[v]

        if node.tail:
            tree_string += _escape_cdata(node.tail, encoding)

        return tree_string
Exemplo n.º 6
0
def _escape_cdata(text, encoding=None, replace=string.replace):
    try:
        if encoding:
            text = _encode(text, encoding)
        text = replace(text, "&", "&amp;")
        text = replace(text, "<", "&lt;")
        text = replace(text, ">", "&gt;")
        return text
    except (TypeError, AttributeError):
        _raise_serialization_error(text)
Exemplo n.º 7
0
def _escape_attrib(attr_val, encoding=None, replace=string.replace):
    # escape attribute value
    text = str(attr_val)
    try:
        if encoding:
            text = _encode(text, encoding)
        text = replace(text, "&", "&amp;")
        #text = replace(text, "'", "&apos;") # FIXME: overkill
        text = replace(text, "\"", "&quot;")
        text = replace(text, "<", "&lt;")
        text = replace(text, ">", "&gt;")
        return text
    except (TypeError, AttributeError):
        _raise_serialization_error(text)
Exemplo n.º 8
0
 def write(
         self,
         file_or_filename,
         # keyword arguments
         encoding=None,
         xml_declaration=None,
         default_namespace=None,
         method=None):
     if not method:
         method = "xml"
     elif method not in _serialize:
         # FIXME: raise an ImportError for c14n if ElementC14N is missing?
         raise ValueError("unknown method %r" % method)
     if hasattr(file_or_filename, "write"):
         file = file_or_filename
     else:
         file = open(file_or_filename, "wb")
     write = file.write
     if not encoding:
         if method == "c14n":
             encoding = "utf-8"
         else:
             encoding = "us-ascii"
     elif xml_declaration or (xml_declaration is None
                              and encoding not in ("utf-8", "us-ascii")):
         if method == "xml":
             write("<?xml version='1.0' encoding='%s'?>\n" % encoding)
     # !!! Print ugly comments prior to root node !!!
     for ugly_comment in self._comments_prior_root:
         write("<!--%s-->\n" % _encode(ugly_comment.text, encoding))
     # !!! end of ugly part
     if method == "text":
         _serialize[method](write, self._root, encoding)
     else:
         qnames, namespaces = _namespaces(self._root, encoding,
                                          default_namespace)
         serialize = _serialize[method]
         serialize(write, self._root, encoding, qnames, namespaces)
     if file_or_filename is not file:
         file.close()
Exemplo n.º 9
0
    def _write(self, file, node, encoding, namespaces, sortflag="default", sortcmp=None): # don't break existing code that relies on _write()s parameters, if any
        # write XML to file
        tag = node.tag
        if tag is Comment:
            file.write("<!-- %s -->" % _escape_cdata(node.text, encoding))
        elif tag is ProcessingInstruction:
            file.write("<?%s?>" % _escape_cdata(node.text, encoding))
        else:
            items = node.items()
            xmlns_items = [] # new namespaces in this scope
            try:
                if isinstance(tag, QName) or tag[:1] == "{":
                    tag, xmlns = fixtag(tag, namespaces)
                    if xmlns: xmlns_items.append(xmlns)
            except TypeError:
                _raise_serialization_error(tag)
            file.write("<" + _encode(tag, encoding))
            if items or xmlns_items:
                
                ##NEW

                if sortflag!="default":
                    if ":" not in sortflag:
                        sortflag = ":"+sortflag
                    sortitems = sortflag.split(";")
                    try:
                        sortitems = [[tagdef.split(":")[0], tagdef.split(":")[1].split(",")] for tagdef in sortitems]
                        temp = []
                        for tagdef in sortitems:
                            if tagdef[0] in node.tag:
                                for sortitem in tagdef[1]:
                                    temp.extend([i for i in items if sortitem in i]) # add what matches pattern
                                break
                        # then sort and add what's left
                        items.sort(cmp=sortcmp)
                        temp.extend([i for i in items if i not in temp])
                        items = temp
                    except IndexError:
                        sys.stderr.write("sortflag not formatted correctly, order won't be applied")
                        try:
                            items.sort(cmp=sortcmp)
                        except:
                            sys.stderr.write("sortcmp not a valid comparator, sorting alphabetically instead")
                            items.sort()
                else:
                    try:
                        items.sort(cmp=sortcmp)
                    except:
                        sys.stderr.write("sortcmp not a valid comparator, sorting alphabetically instead")
                        items.sort()
                ###

                        
                for k, v in items:
                    try:
                        if isinstance(k, QName) or k[:1] == "{":
                            k, xmlns = fixtag(k, namespaces)
                            if xmlns: xmlns_items.append(xmlns)
                    except TypeError:
                        _raise_serialization_error(k)
                    try:
                        if isinstance(v, QName):
                            v, xmlns = fixtag(v, namespaces)
                            if xmlns: xmlns_items.append(xmlns)
                    except TypeError:
                        _raise_serialization_error(v)
                    file.write(" %s=\"%s\"" % (_encode(k, encoding),
                                               _escape_attrib(v, encoding)))
                for k, v in xmlns_items:
                    file.write(" %s=\"%s\"" % (_encode(k, encoding),
                                               _escape_attrib(v, encoding)))
            if node.text or len(node):
                file.write(">")
                if node.text:
                    file.write(_escape_cdata(node.text, encoding))
                for n in node:
                    self._write(file, n, encoding, namespaces, sortflag, sortcmp)
                file.write("</" + _encode(tag, encoding) + ">")
            else:
                file.write(" />")
            for k, v in xmlns_items:
                del namespaces[v]
        if node.tail:
            file.write(_escape_cdata(node.tail, encoding))