Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 import xml
00034 from .color import warning
00035
00036 def first_child_element(elt):
00037 c = elt.firstChild
00038 while c and c.nodeType != xml.dom.Node.ELEMENT_NODE:
00039 c = c.nextSibling
00040 return c
00041
00042
00043 def next_sibling_element(node):
00044 c = node.nextSibling
00045 while c and c.nodeType != xml.dom.Node.ELEMENT_NODE:
00046 c = c.nextSibling
00047 return c
00048
00049
00050 def replace_node(node, by, content_only=False):
00051 parent = node.parentNode
00052
00053 if by is not None:
00054 if not isinstance(by, list):
00055 by = [by]
00056
00057
00058 for doc in by:
00059 if content_only:
00060 c = doc.firstChild
00061 while c:
00062 n = c.nextSibling
00063 parent.insertBefore(c, node)
00064 c = n
00065 else:
00066 parent.insertBefore(doc, node)
00067
00068
00069 parent.removeChild(node)
00070
00071
00072 def attribute(tag, a):
00073 """
00074 Helper function to fetch a single attribute value from tag
00075 :param tag (xml.dom.Element): DOM element node
00076 :param a (str): attribute name
00077 :return: attribute value if present, otherwise None
00078 """
00079 if tag.hasAttribute(a):
00080
00081
00082 return tag.getAttribute(a)
00083 else:
00084 return None
00085
00086
00087 def opt_attrs(tag, attrs):
00088 """
00089 Helper routine for fetching optional tag attributes
00090 :param tag (xml.dom.Element): DOM element node
00091 :param attrs [str]: list of attributes to fetch
00092 """
00093 return [attribute(tag, a) for a in attrs]
00094
00095
00096 def reqd_attrs(tag, attrs):
00097 """
00098 Helper routine for fetching required tag attributes
00099 :param tag (xml.dom.Element): DOM element node
00100 :param attrs [str]: list of attributes to fetch
00101 :raise RuntimeError: if required attribute is missing
00102 """
00103 result = opt_attrs(tag, attrs)
00104 for (res, name) in zip(result, attrs):
00105 if res is None:
00106 raise RuntimeError("%s: missing attribute '%s'" % (tag.nodeName, name))
00107 return result
00108
00109
00110 def check_attrs(tag, required, optional):
00111 """
00112 Helper routine to fetch required and optional attributes
00113 and complain about any additional attributes.
00114 :param tag (xml.dom.Element): DOM element node
00115 :param required [str]: list of required attributes
00116 :param optional [str]: list of optional attributes
00117 """
00118 result = reqd_attrs(tag, required)
00119 result.extend(opt_attrs(tag, optional))
00120 allowed = required + optional
00121 extra = [a for a in tag.attributes.keys() if a not in allowed]
00122 if extra:
00123 warning("%s: unknown attribute(s): %s" % (tag.nodeName, ', '.join(extra)))
00124 return result
00125
00126
00127
00128
00129 def fixed_writexml(self, writer, indent="", addindent="", newl=""):
00130
00131
00132
00133 writer.write(indent + "<" + self.tagName)
00134
00135 attrs = self._get_attributes()
00136 a_names = list(attrs.keys())
00137 a_names.sort()
00138
00139 for a_name in a_names:
00140 writer.write(" %s=\"" % a_name)
00141 xml.dom.minidom._write_data(writer, attrs[a_name].value)
00142 writer.write("\"")
00143 if self.childNodes:
00144 if len(self.childNodes) == 1 \
00145 and self.childNodes[0].nodeType == xml.dom.minidom.Node.TEXT_NODE:
00146 writer.write(">")
00147 self.childNodes[0].writexml(writer, "", "", "")
00148 writer.write("</%s>%s" % (self.tagName, newl))
00149 return
00150 writer.write(">%s" % newl)
00151 for node in self.childNodes:
00152
00153 if node.nodeType == xml.dom.minidom.Node.TEXT_NODE and \
00154 (not node.data or node.data.isspace()):
00155 continue
00156 node.writexml(writer, indent + addindent, addindent, newl)
00157 writer.write("%s</%s>%s" % (indent, self.tagName, newl))
00158 else:
00159 writer.write("/>%s" % newl)
00160
00161 xml.dom.minidom.Element.writexml = fixed_writexml
00162
00163