2004-08-16 19:28:42 +00:00
|
|
|
# -*- coding: iso-8859-1 -*-
|
2010-03-26 19:51:59 +00:00
|
|
|
# Copyright (C) 2000-2010 Bastian Kleineidam
|
2004-08-16 19:28:42 +00:00
|
|
|
#
|
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
#
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
#
|
2009-07-24 21:58:20 +00:00
|
|
|
# You should have received a copy of the GNU General Public License along
|
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2005-01-19 15:08:02 +00:00
|
|
|
"""
|
2005-07-15 21:33:15 +00:00
|
|
|
Base class for XML loggers.
|
2005-01-19 15:08:02 +00:00
|
|
|
"""
|
2004-08-16 19:28:42 +00:00
|
|
|
|
|
|
|
|
import xml.sax.saxutils
|
2008-05-09 06:16:03 +00:00
|
|
|
from . import Logger
|
2004-08-16 19:28:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
xmlattr_entities = {
|
|
|
|
|
"&": "&",
|
|
|
|
|
"<": "<",
|
|
|
|
|
">": ">",
|
|
|
|
|
"\"": """,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def xmlquote (s):
|
2005-01-19 14:38:01 +00:00
|
|
|
"""
|
|
|
|
|
Quote characters for XML.
|
|
|
|
|
"""
|
2004-08-16 19:28:42 +00:00
|
|
|
return xml.sax.saxutils.escape(s)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def xmlquoteattr (s):
|
2005-01-19 14:38:01 +00:00
|
|
|
"""
|
|
|
|
|
Quote XML attribute, ready for inclusion with double quotes.
|
|
|
|
|
"""
|
2004-08-16 19:28:42 +00:00
|
|
|
return xml.sax.saxutils.escape(s, xmlattr_entities)
|
|
|
|
|
|
|
|
|
|
|
2008-05-09 06:16:03 +00:00
|
|
|
class XMLLogger (Logger):
|
2008-06-07 13:08:54 +00:00
|
|
|
"""XML output; easy to parse with any XML tool."""
|
2004-08-16 19:28:42 +00:00
|
|
|
|
|
|
|
|
def __init__ (self, **args):
|
2005-01-19 14:38:01 +00:00
|
|
|
"""
|
|
|
|
|
Initialize graph node list and internal id counter.
|
|
|
|
|
"""
|
2004-08-16 19:28:42 +00:00
|
|
|
super(XMLLogger, self).__init__(**args)
|
2004-08-25 20:08:53 +00:00
|
|
|
self.init_fileoutput(args)
|
2005-07-15 21:33:15 +00:00
|
|
|
self.indent = u" "
|
|
|
|
|
self.level = 0
|
2004-08-16 19:28:42 +00:00
|
|
|
|
2004-11-25 16:20:58 +00:00
|
|
|
def comment (self, s, **args):
|
2005-01-19 14:38:01 +00:00
|
|
|
"""
|
2005-07-15 21:33:15 +00:00
|
|
|
Write XML comment.
|
2005-01-19 14:38:01 +00:00
|
|
|
"""
|
2004-11-25 16:20:58 +00:00
|
|
|
self.write(u"<!-- ")
|
|
|
|
|
self.write(s, **args)
|
2004-11-29 00:49:44 +00:00
|
|
|
self.writeln(u" -->")
|
2004-11-25 16:20:58 +00:00
|
|
|
|
2009-02-18 16:14:50 +00:00
|
|
|
def xml_start_output (self):
|
2005-01-19 14:38:01 +00:00
|
|
|
"""
|
2005-07-15 21:33:15 +00:00
|
|
|
Write start of checking info as xml comment.
|
2005-01-19 14:38:01 +00:00
|
|
|
"""
|
2009-02-18 16:14:50 +00:00
|
|
|
self.writeln(u'<?xml version="1.0" encoding="%s"?>' %
|
|
|
|
|
xmlquoteattr(self.output_encoding))
|
2005-05-08 20:06:40 +00:00
|
|
|
if self.has_part("intro"):
|
2010-11-01 08:58:03 +00:00
|
|
|
self.write_intro()
|
2004-10-27 22:34:50 +00:00
|
|
|
self.writeln()
|
2004-08-16 19:28:42 +00:00
|
|
|
|
2005-07-15 21:33:15 +00:00
|
|
|
def xml_end_output (self):
|
2005-01-19 14:38:01 +00:00
|
|
|
"""
|
2005-07-15 21:33:15 +00:00
|
|
|
Write end of checking info as xml comment.
|
2005-01-19 14:38:01 +00:00
|
|
|
"""
|
2005-05-08 20:06:40 +00:00
|
|
|
if self.has_part("outro"):
|
2010-11-01 08:58:03 +00:00
|
|
|
self.write_outro()
|
2005-07-15 21:33:15 +00:00
|
|
|
|
|
|
|
|
def xml_starttag (self, name, attrs=None):
|
|
|
|
|
"""
|
|
|
|
|
Write XML start tag.
|
|
|
|
|
"""
|
|
|
|
|
self.write(self.indent*self.level)
|
|
|
|
|
self.write(u"<%s" % xmlquote(name))
|
|
|
|
|
if attrs:
|
2008-04-27 11:39:21 +00:00
|
|
|
for name, value in attrs.items():
|
2005-07-15 21:33:15 +00:00
|
|
|
args = (xmlquote(name), xmlquoteattr(value))
|
|
|
|
|
self.write(u' %s="%s"' % args)
|
2010-03-13 07:47:12 +00:00
|
|
|
self.writeln(u">")
|
2005-07-15 21:33:15 +00:00
|
|
|
self.level += 1
|
|
|
|
|
|
|
|
|
|
def xml_endtag (self, name):
|
|
|
|
|
"""
|
|
|
|
|
Write XML end tag.
|
|
|
|
|
"""
|
|
|
|
|
self.level -= 1
|
|
|
|
|
assert self.level >= 0
|
2005-07-15 21:46:13 +00:00
|
|
|
self.write(self.indent*self.level)
|
|
|
|
|
self.writeln(u"</%s>" % xmlquote(name))
|
2005-07-15 21:33:15 +00:00
|
|
|
|
|
|
|
|
def xml_tag (self, name, content, attrs=None):
|
|
|
|
|
"""
|
|
|
|
|
Write XML tag with content.
|
|
|
|
|
"""
|
|
|
|
|
self.write(self.indent*self.level)
|
|
|
|
|
self.write(u"<%s" % xmlquote(name))
|
|
|
|
|
if attrs:
|
2008-04-27 11:39:21 +00:00
|
|
|
for aname, avalue in attrs.items():
|
2005-07-18 08:19:11 +00:00
|
|
|
args = (xmlquote(aname), xmlquoteattr(avalue))
|
2005-07-15 21:33:15 +00:00
|
|
|
self.write(u' %s="%s"' % args)
|
|
|
|
|
self.writeln(u">%s</%s>" % (xmlquote(content), xmlquote(name)))
|