|
| 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() |
0 commit comments