Project

General

Profile

« Previous | Next » 

Revision 301

xml_dom.py: Added minidom.Document write_opening() and write_closing() methods. Changed writexml(writer, node) to use new Node.writeprettyxml() method.

View differences:

lib/xml_dom.py
19 19

  
20 20
def is_empty(node): return node.firstChild == None
21 21

  
22
def has_one_child(node):
23
    return node.firstChild != None and node.firstChild.nextSibling == None
24

  
22 25
def is_elem(node): return node.nodeType == Node.ELEMENT_NODE
23 26

  
24 27
class NodeElemIter:
......
73 76

  
74 77
def is_text_node(node): return node.nodeType == Node.TEXT_NODE
75 78

  
76
def is_text(node):
77
    child = node.firstChild
78
    return child != None and child.nextSibling == None and is_text_node(child)
79
def is_text(node): return has_one_child(node) and is_text_node(node.firstChild)
79 80

  
80 81
def value(node):
81 82
    if node.firstChild != None: return node.firstChild.nodeValue
......
126 127
    return minidom.getDOMImplementation().createDocument(None, root,
127 128
        None)
128 129

  
129
def writexml(writer, node): node.writexml(writer, addindent='    ', newl='\n')
130
def writexml(writer, node): node.writeprettyxml(writer)
130 131

  
131 132
# minidom modifications
132 133

  
133 134
minidom._write_data = lambda writer, data: writer.write(escape(data))
134 135

  
135
def __write_opening(self, writer, indent='', addindent='', newl=''):
136
minidom.Node.__str__ = lambda self: self.toxml()
137

  
138
def __Node_writeprettyxml(self, writer, indent='', addindent='    ', newl='\n'):
139
    self.writexml(writer, indent, addindent, newl)
140
minidom.Node.writeprettyxml = __Node_writeprettyxml
141

  
142
def __Element_write_opening(self, writer, indent='', addindent='', newl=''):
136 143
    writer.write(indent+'<'+self.tagName)
137 144
    for attr_idx in xrange(self.attributes.length):
138 145
        attr = self.attributes.item(attr_idx)
139 146
        writer.write(' '+attr.name+'='+escape(attr.value))
140 147
    writer.write('>'+newl)
141
minidom.Element.write_opening = __write_opening
148
minidom.Element.write_opening = __Element_write_opening
142 149

  
143
def __write_closing(self, writer, indent='', addindent='', newl=''):
150
def __Element_write_closing(self, writer, indent='', addindent='', newl=''):
144 151
    writer.write('</'+self.tagName+'>'+newl)
145
minidom.Element.write_closing = __write_closing
152
minidom.Element.write_closing = __Element_write_closing
146 153

  
147 154
_writexml_orig = minidom.Element.writexml
148
def __writexml(self, writer, indent='', addindent='', newl=''):
155
def __Element_writexml(self, writer, indent='', addindent='', newl=''):
149 156
    if is_text(self):
150 157
        self.write_opening(writer, indent, addindent, '') # no newline
151 158
        writer.write(escape(value(self)))
152 159
        self.write_closing(writer, indent, addindent, newl)
153 160
    else: _writexml_orig(self, writer, indent, addindent, newl)
154
minidom.Element.writexml = __writexml
161
minidom.Element.writexml = __Element_writexml
155 162

  
156
minidom.Node.__str__ = lambda self: self.toxml()
163
def __Document_write_opening(self, writer, indent='', addindent='', newl='',
164
    encoding=None):
165
    xmlDecl = '<?xml version="1.0" '
166
    if encoding != None: xmlDecl += 'encoding="'+escape(encoding)+'"'
167
    xmlDecl += '?>'+newl
168
    writer.write(xmlDecl)
169
    assert has_one_child(self)
170
    assert is_elem(self.firstChild)
171
    self.firstChild.write_opening(writer, indent, addindent, newl)
172
minidom.Document.write_opening = __Document_write_opening
173

  
174
def __Document_write_closing(self, writer, indent='', addindent='', newl=''):
175
    self.firstChild.write_closing(writer, indent, addindent, newl)
176
minidom.Document.write_closing = __Document_write_closing

Also available in: Unified diff