Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 38e5e34

Browse files
committed
added prettyprint module with fixed toprettyxml() method
1 parent 01f2dfe commit 38e5e34

3 files changed

Lines changed: 121 additions & 2 deletions

File tree

extra/prettyprint/__init__.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env python
2+
#
3+
# Copyright 2008-2009 Jose Fonseca
4+
#
5+
# This program is free software: you can redistribute it and/or modify it
6+
# under the terms of the GNU Lesser General Public License as published
7+
# by the Free Software Foundation, either version 3 of the License, or
8+
# (at your option) any later version.
9+
#
10+
# This program is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU Lesser General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU Lesser General Public License
16+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
#
18+
19+
pass

extra/prettyprint/prettyprint.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#!/usr/bin/env python
2+
3+
#Copyright (c) 2010, Miroslav Stampar <[email protected]>
4+
#Added formatXML method
5+
6+
#Copyright (c) 2010, Chris Hall <[email protected]>
7+
#All rights reserved.
8+
9+
#Redistribution and use in source and binary forms, with or without modification,
10+
#are permitted provided that the following conditions are met:
11+
12+
#* Redistributions of source code must retain the above copyright notice,
13+
#this list of conditions and the following disclaimer.
14+
#* Redistributions in binary form must reproduce the above copyright notice,
15+
#this list of conditions and the following disclaimer in the documentation
16+
#and/or other materials provided with the distribution.
17+
18+
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19+
#ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20+
#WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
#DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
22+
#ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23+
#(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24+
#LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25+
#ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
#(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27+
#SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+
29+
from xml.dom import minidom
30+
from xml.dom import Node
31+
32+
def format(text):
33+
doc = minidom.parseString(text)
34+
root = doc.childNodes[0]
35+
return root.toprettyxml(indent=' ')
36+
37+
def formatXML(doc, encoding=None):
38+
root = doc.childNodes[0]
39+
return root.toprettyxml(indent=' ', encoding=encoding)
40+
41+
def _patch_minidom():
42+
minidom.Text.writexml = _writexml_text
43+
minidom.Element.writexml = _writexml_element
44+
minidom.Node.toprettyxml = _toprettyxml_node
45+
46+
def _collapse(node):
47+
for child in node.childNodes:
48+
if child.nodeType == Node.TEXT_NODE and len(child.data.strip()) == 0:
49+
child.data = ''
50+
else:
51+
_collapse(child)
52+
53+
def _writexml_text(self, writer, indent="", addindent="", newl=""):
54+
minidom._write_data(writer, "%s"%(self.data.strip()))
55+
56+
def _writexml_element(self, writer, indent="", addindent="", newl=""):
57+
# indent = current indentation
58+
# addindent = indentation to add to higher levels
59+
# newl = newline string
60+
writer.write(indent+"<" + self.tagName)
61+
62+
attrs = self._get_attributes()
63+
a_names = attrs.keys()
64+
a_names.sort()
65+
66+
for a_name in a_names:
67+
writer.write(" %s=\"" % a_name)
68+
minidom._write_data(writer, attrs[a_name].value)
69+
writer.write("\"")
70+
if self.childNodes:
71+
if self.childNodes[0].nodeType == Node.TEXT_NODE and len(self.childNodes[0].data) > 0:
72+
writer.write(">")
73+
else:
74+
writer.write(">%s"%(newl))
75+
for node in self.childNodes:
76+
node.writexml(writer,indent+addindent,addindent,newl)
77+
if self.childNodes[-1].nodeType == Node.TEXT_NODE and len(self.childNodes[0].data) > 0:
78+
writer.write("</%s>%s" % (self.tagName,newl))
79+
else:
80+
writer.write("%s</%s>%s" % (indent,self.tagName,newl))
81+
else:
82+
writer.write("/>%s"%(newl))
83+
84+
def _toprettyxml_node(self, indent="\t", newl="\n", encoding = None):
85+
_collapse(self)
86+
# indent = the indentation string to prepend, per level
87+
# newl = the newline string to append
88+
writer = minidom._get_StringIO()
89+
if encoding is not None:
90+
import codecs
91+
# Can't use codecs.getwriter to preserve 2.0 compatibility
92+
writer = codecs.lookup(encoding)[3](writer)
93+
if self.nodeType == Node.DOCUMENT_NODE:
94+
# Can pass encoding only to document, to put it into XML header
95+
self.writexml(writer, "", indent, newl, encoding)
96+
else:
97+
self.writexml(writer, "", indent, newl)
98+
return writer.getvalue()
99+
100+
_patch_minidom()

lib/core/xmldump.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from xml.dom.minidom import Document
1111
from xml.parsers.expat import ExpatError
1212

13+
from extra.prettyprint import prettyprint
1314
from lib.core.common import getUnicode
1415
from lib.core.data import conf
1516
from lib.core.data import logger
@@ -524,8 +525,7 @@ def finish(self, resultStatus, resultMsg=""):
524525
statusElem.appendChild(errorElem)
525526

526527
self.__addToRoot(statusElem)
527-
#self.__write(self.__doc.toprettyxml(encoding=conf.dataEncoding)) ##don't use toprettyxml, lots of bugs with it
528-
self.__write(self.__doc.toxml(encoding=conf.dataEncoding)) ##not human readable, but at least without bugs
528+
self.__write(prettyprint.formatXML(self.__doc, encoding=conf.dataEncoding))
529529
self.__outputFP.close()
530530

531531
def closeDumper(status, msg=""):

0 commit comments

Comments
 (0)