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