2010-12-23 03:25:27 -06:00
|
|
|
|
# -*- coding: utf-8 -*-
|
2009-06-29 07:06:01 -05:00
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
# Appy is a framework for building applications in the Python language.
|
|
|
|
|
# Copyright (C) 2007 Gaetan Delannay
|
|
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
2012-05-14 10:35:34 -05:00
|
|
|
|
import xml.sax, difflib, types, cgi
|
2012-12-07 05:05:39 -06:00
|
|
|
|
from xml.parsers.expat import XML_PARAM_ENTITY_PARSING_NEVER
|
2012-09-25 14:43:45 -05:00
|
|
|
|
from xml.sax.handler import ContentHandler, ErrorHandler, feature_external_ges
|
2009-06-29 07:06:01 -05:00
|
|
|
|
from xml.sax.xmlreader import InputSource
|
2012-05-22 09:42:20 -05:00
|
|
|
|
from xml.sax import SAXParseException
|
2012-12-07 05:05:39 -06:00
|
|
|
|
|
2012-10-26 06:09:44 -05:00
|
|
|
|
from appy.shared import UnicodeBuffer
|
2009-10-25 15:42:08 -05:00
|
|
|
|
from appy.shared.errors import AppyError
|
2012-01-12 14:49:23 -06:00
|
|
|
|
from appy.shared.utils import sequenceTypes
|
2012-05-24 05:54:40 -05:00
|
|
|
|
from appy.shared.css import parseStyleAttribute
|
2009-10-25 15:42:08 -05:00
|
|
|
|
|
2010-12-23 03:25:27 -06:00
|
|
|
|
# Constants --------------------------------------------------------------------
|
2012-10-26 06:09:44 -05:00
|
|
|
|
xmlPrologue = '<?xml version="1.0" encoding="utf-8" ?>\n'
|
2013-07-11 09:41:45 -05:00
|
|
|
|
xhtmlPrologue = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" '\
|
|
|
|
|
'"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n'
|
|
|
|
|
|
2009-10-25 15:42:08 -05:00
|
|
|
|
CONVERSION_ERROR = '"%s" value "%s" could not be converted by the XML ' \
|
2010-12-23 03:25:27 -06:00
|
|
|
|
'unmarshaller.'
|
2009-10-25 15:42:08 -05:00
|
|
|
|
CUSTOM_CONVERSION_ERROR = 'Custom converter for "%s" values produced an ' \
|
2010-12-23 03:25:27 -06:00
|
|
|
|
'error while converting value "%s". %s'
|
2012-10-26 06:09:44 -05:00
|
|
|
|
XML_SPECIAL_CHARS = {'<': '<', '>': '>', '&': '&', '"': '"',
|
|
|
|
|
"'": '''}
|
2014-01-20 11:43:18 -06:00
|
|
|
|
XML_SPECIAL_CHARS_NO_APOS = XML_SPECIAL_CHARS.copy()
|
|
|
|
|
del XML_SPECIAL_CHARS_NO_APOS["'"]
|
2013-06-26 06:44:31 -05:00
|
|
|
|
XML_ENTITIES = {'lt': '<', 'gt': '>', 'amp': '&', 'quot': '"', 'apos': "'"}
|
2010-12-23 03:25:27 -06:00
|
|
|
|
HTML_ENTITIES = {
|
|
|
|
|
'iexcl': '¡', 'cent': '¢', 'pound': '£', 'curren': '€', 'yen': '¥',
|
|
|
|
|
'brvbar': 'Š', 'sect': '§', 'uml': '¨', 'copy':'©', 'ordf':'ª',
|
|
|
|
|
'laquo':'«', 'not':'¬', 'shy':'', 'reg':'®', 'macr':'¯', 'deg':'°',
|
|
|
|
|
'plusmn':'±', 'sup2':'²', 'sup3':'³', 'acute':'Ž',
|
|
|
|
|
'micro':'µ', 'para':'¶', 'middot':'·', 'cedil':'ž', 'sup1':'¹',
|
|
|
|
|
'ordm':'º', 'raquo':'»', 'frac14':'Œ', 'frac12':'œ', 'frac34':'Ÿ',
|
|
|
|
|
'iquest':'¿', 'Agrave':'À', 'Aacute':'Á', 'Acirc':'Â', 'Atilde':'Ã',
|
|
|
|
|
'Auml':'Ä', 'Aring':'Å', 'AElig':'Æ', 'Ccedil':'Ç', 'Egrave':'È',
|
|
|
|
|
'Eacute':'É', 'Ecirc':'Ê', 'Euml':'Ë', 'Igrave':'Ì', 'Iacute':'Í',
|
|
|
|
|
'Icirc':'Î', 'Iuml':'Ï', 'ETH':'Ð', 'Ntilde':'Ñ', 'Ograve':'Ò',
|
|
|
|
|
'Oacute':'Ó', 'Ocirc':'Ó', 'Otilde':'Õ', 'Ouml':'Ö', 'times':'×',
|
|
|
|
|
'Oslash':'Ø', 'Ugrave':'Ù', 'Uacute':'Ú', 'Ucirc':'Û', 'Uuml':'Ü',
|
|
|
|
|
'Yacute':'Ý', 'THORN':'Þ', 'szlig':'ß', 'agrave':'à', 'aacute':'á',
|
|
|
|
|
'acirc':'â', 'atilde':'ã', 'auml':'ä', 'aring':'å', 'aelig':'æ',
|
|
|
|
|
'ccedil':'ç', 'egrave':'è', 'eacute':'é', 'ecirc':'ê', 'euml':'ë',
|
|
|
|
|
'igrave':'ì', 'iacute':'í', 'icirc':'î', 'iuml':'ï', 'eth':'ð',
|
|
|
|
|
'ntilde':'ñ', 'ograve':'ò', 'oacute':'ó', 'ocirc':'ô', 'otilde':'õ',
|
|
|
|
|
'ouml':'ö', 'divide':'÷', 'oslash':'ø', 'ugrave':'ù', 'uacute':'ú',
|
|
|
|
|
'ucirc':'û', 'uuml':'ü', 'yacute':'ý', 'thorn':'þ', 'yuml':'ÿ',
|
2013-09-05 03:42:19 -05:00
|
|
|
|
'euro':'€', 'nbsp':' ', "rsquo":"'", "lsquo":"'", "ldquo":"'",
|
2013-03-18 07:13:29 -05:00
|
|
|
|
"rdquo":"'", 'ndash': '—', 'mdash': '—', 'oelig':'oe', 'quot': "'",
|
|
|
|
|
'mu': 'µ'}
|
2010-12-23 03:25:27 -06:00
|
|
|
|
import htmlentitydefs
|
|
|
|
|
for k, v in htmlentitydefs.entitydefs.iteritems():
|
|
|
|
|
if not HTML_ENTITIES.has_key(k) and not XML_ENTITIES.has_key(k):
|
|
|
|
|
HTML_ENTITIES[k] = ''
|
2009-06-29 07:06:01 -05:00
|
|
|
|
|
2013-06-12 03:30:20 -05:00
|
|
|
|
def escapeXml(s, format='xml', nsText='text'):
|
2012-10-26 06:09:44 -05:00
|
|
|
|
'''Returns p_s, whose XML special chars have been replaced with escaped XML
|
2013-06-12 03:30:20 -05:00
|
|
|
|
entities. If p_format is "odf", line breaks are converted to ODF line
|
|
|
|
|
breaks. In this case, it is needed to give the name of the "text"
|
|
|
|
|
namespace (p_nsText) as defined in the ODF document where the line breaks
|
|
|
|
|
must be inserted.'''
|
2012-10-26 06:09:44 -05:00
|
|
|
|
if isinstance(s, unicode):
|
|
|
|
|
res = u''
|
|
|
|
|
else:
|
|
|
|
|
res = ''
|
2013-06-12 03:30:20 -05:00
|
|
|
|
odf = format == 'odf'
|
2012-10-26 06:09:44 -05:00
|
|
|
|
for c in s:
|
2014-01-20 11:43:18 -06:00
|
|
|
|
if XML_SPECIAL_CHARS_NO_APOS.has_key(c):
|
|
|
|
|
# We do not escape 'apos': there is no particular need for that.
|
|
|
|
|
res += XML_SPECIAL_CHARS_NO_APOS[c]
|
2013-06-12 03:30:20 -05:00
|
|
|
|
elif odf and (c == '\n'):
|
|
|
|
|
res += '<%s:line-break/>' % nsText
|
|
|
|
|
elif odf and (c == '\r'):
|
|
|
|
|
pass
|
2012-10-26 06:09:44 -05:00
|
|
|
|
else:
|
|
|
|
|
res += c
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
|
def escapeXhtml(s):
|
|
|
|
|
'''Return p_s, whose XHTML special chars and carriage return chars have
|
|
|
|
|
been replaced with corresponding XHTML entities.'''
|
|
|
|
|
if isinstance(s, unicode):
|
|
|
|
|
res = u''
|
|
|
|
|
else:
|
|
|
|
|
res = ''
|
|
|
|
|
for c in s:
|
2014-01-20 11:43:18 -06:00
|
|
|
|
if XML_SPECIAL_CHARS_NO_APOS.has_key(c):
|
|
|
|
|
res += XML_SPECIAL_CHARS_NO_APOS[c]
|
2012-10-26 06:09:44 -05:00
|
|
|
|
elif c == '\n':
|
|
|
|
|
res += '<br/>'
|
|
|
|
|
elif c == '\r':
|
|
|
|
|
pass
|
|
|
|
|
else:
|
|
|
|
|
res += c
|
|
|
|
|
return res
|
|
|
|
|
|
2009-06-29 07:06:01 -05:00
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
class XmlElement:
|
2009-08-04 07:39:43 -05:00
|
|
|
|
'''Represents an XML tag.'''
|
2009-06-29 07:06:01 -05:00
|
|
|
|
def __init__(self, elem, attrs=None, nsUri=None):
|
|
|
|
|
'''An XmlElement instance may represent:
|
|
|
|
|
- an already parsed tag (in this case, p_elem may be prefixed with a
|
|
|
|
|
namespace);
|
|
|
|
|
- the definition of an XML element (in this case, no namespace can be
|
|
|
|
|
found in p_elem; but a namespace URI may be defined in p_nsUri).'''
|
|
|
|
|
self.elem = elem
|
|
|
|
|
self.attrs = attrs
|
|
|
|
|
if elem.find(':') != -1:
|
|
|
|
|
self.ns, self.name = elem.split(':')
|
|
|
|
|
else:
|
|
|
|
|
self.ns = ''
|
|
|
|
|
self.name = elem
|
|
|
|
|
self.nsUri = nsUri
|
|
|
|
|
def equalsTo(self, other, namespaces=None):
|
|
|
|
|
'''Does p_elem == p_other? If a p_namespaces dict is given, p_other must
|
|
|
|
|
define a nsUri.'''
|
|
|
|
|
res = None
|
|
|
|
|
if namespaces:
|
|
|
|
|
res = self.elem == ('%s:%s' % (namespaces[other.nsUri], other.name))
|
|
|
|
|
else:
|
|
|
|
|
res = self.elem == other.elem
|
|
|
|
|
return res
|
|
|
|
|
def __repr__(self):
|
|
|
|
|
res = self.elem
|
|
|
|
|
if self.attrs:
|
|
|
|
|
res += '('
|
|
|
|
|
for attrName, attrValue in self.attrs.items():
|
|
|
|
|
res += '%s="%s"' % (attrName, attrValue)
|
|
|
|
|
res += ')'
|
|
|
|
|
return res
|
|
|
|
|
def getFullName(self, namespaces=None):
|
|
|
|
|
'''Gets the name of the element including the namespace prefix.'''
|
|
|
|
|
if not namespaces:
|
|
|
|
|
res = self.elem
|
|
|
|
|
else:
|
|
|
|
|
res = '%s:%s' % (namespaces[self.nsUri], self.name)
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
|
class XmlEnvironment:
|
|
|
|
|
'''An XML environment remembers a series of elements during a SAX parsing.
|
|
|
|
|
This class is an abstract class that gathers basic things like
|
|
|
|
|
namespaces.'''
|
|
|
|
|
def __init__(self):
|
|
|
|
|
# This dict contains the xml namespace declarations encountered so far
|
|
|
|
|
self.namespaces = {} # ~{s_namespaceUri:s_namespaceName}~
|
|
|
|
|
self.currentElem = None # The currently parsed element
|
|
|
|
|
self.parser = None
|
|
|
|
|
def manageNamespaces(self, attrs):
|
|
|
|
|
'''Manages namespaces definitions encountered in p_attrs.'''
|
|
|
|
|
for attrName, attrValue in attrs.items():
|
|
|
|
|
if attrName.startswith('xmlns:'):
|
|
|
|
|
self.namespaces[attrValue] = attrName[6:]
|
|
|
|
|
def ns(self, nsUri):
|
|
|
|
|
'''Returns the namespace corresponding to o_nsUri.'''
|
|
|
|
|
return self.namespaces[nsUri]
|
|
|
|
|
|
|
|
|
|
class XmlParser(ContentHandler, ErrorHandler):
|
2010-12-23 03:25:27 -06:00
|
|
|
|
'''Basic expat-based XML parser that does things like :
|
2009-06-29 07:06:01 -05:00
|
|
|
|
- remembering the currently parsed element;
|
2010-12-23 03:25:27 -06:00
|
|
|
|
- managing namespace declarations.
|
|
|
|
|
This parser also knows about HTML entities.'''
|
2012-09-26 16:13:02 -05:00
|
|
|
|
def __init__(self, env=None, caller=None, raiseOnError=True):
|
2009-06-29 07:06:01 -05:00
|
|
|
|
'''p_env should be an instance of a class that inherits from
|
|
|
|
|
XmlEnvironment: it specifies the environment to use for this SAX
|
|
|
|
|
parser.'''
|
|
|
|
|
ContentHandler.__init__(self)
|
2009-07-10 08:01:50 -05:00
|
|
|
|
if not env: env = XmlEnvironment()
|
2009-06-29 07:06:01 -05:00
|
|
|
|
self.env = env
|
|
|
|
|
self.env.parser = self
|
|
|
|
|
self.caller = caller # The class calling this parser
|
|
|
|
|
self.parser = xml.sax.make_parser() # Fast, standard expat parser
|
2009-08-04 07:39:43 -05:00
|
|
|
|
self.res = None # The result of parsing.
|
2012-09-26 16:13:02 -05:00
|
|
|
|
# Raise or not an error when a parsing error is encountered.
|
|
|
|
|
self.raiseOnError = raiseOnError
|
2010-12-23 03:25:27 -06:00
|
|
|
|
|
|
|
|
|
# ContentHandler methods ---------------------------------------------------
|
|
|
|
|
def startDocument(self):
|
2012-12-07 05:05:39 -06:00
|
|
|
|
parser = self.parser._parser
|
|
|
|
|
parser.UseForeignDTD(True)
|
|
|
|
|
parser.SetParamEntityParsing(XML_PARAM_ENTITY_PARSING_NEVER)
|
2009-06-29 07:06:01 -05:00
|
|
|
|
def setDocumentLocator(self, locator):
|
|
|
|
|
self.locator = locator
|
|
|
|
|
return self.env
|
|
|
|
|
def endDocument(self):
|
|
|
|
|
return self.env
|
|
|
|
|
def startElement(self, elem, attrs):
|
|
|
|
|
self.env.manageNamespaces(attrs)
|
|
|
|
|
if self.env.currentElem == None:
|
|
|
|
|
self.env.currentElem = XmlElement(elem, attrs=attrs)
|
|
|
|
|
else:
|
|
|
|
|
# Reuse the exiting instance in order to avoid creating one instance
|
|
|
|
|
# every time an elem is met in the XML file.
|
|
|
|
|
self.env.currentElem.__init__(elem, attrs)
|
|
|
|
|
return self.env
|
|
|
|
|
def endElement(self, elem):
|
|
|
|
|
self.env.currentElem.__init__(elem)
|
|
|
|
|
return self.env
|
|
|
|
|
def characters(self, content):
|
|
|
|
|
return self.env
|
2010-12-23 03:25:27 -06:00
|
|
|
|
|
|
|
|
|
def skippedEntity(self, name):
|
|
|
|
|
'''This method is called every time expat does not recognize an entity.
|
|
|
|
|
We provide here support for HTML entities.'''
|
|
|
|
|
if HTML_ENTITIES.has_key(name):
|
|
|
|
|
self.characters(HTML_ENTITIES[name].decode('utf-8'))
|
|
|
|
|
else:
|
|
|
|
|
# Put a question mark instead of raising an exception.
|
|
|
|
|
self.characters('?')
|
|
|
|
|
|
2012-09-25 14:43:45 -05:00
|
|
|
|
# ErrorHandler methods ---------------------------------------------------
|
2012-09-26 16:13:02 -05:00
|
|
|
|
def error(self, error):
|
|
|
|
|
if self.raiseOnError: raise error
|
2013-05-29 17:46:11 -05:00
|
|
|
|
else: print('SAX error %s' % str(error))
|
2012-09-26 16:13:02 -05:00
|
|
|
|
def fatalError(self, error):
|
|
|
|
|
if self.raiseOnError: raise error
|
2013-05-29 17:46:11 -05:00
|
|
|
|
else: print('SAX fatal error %s' % str(error))
|
2012-09-26 16:13:02 -05:00
|
|
|
|
def warning(self, error): pass
|
2012-09-25 14:43:45 -05:00
|
|
|
|
|
2011-02-17 11:13:42 -06:00
|
|
|
|
def parse(self, xml, source='string'):
|
|
|
|
|
'''Parses a XML stream.
|
|
|
|
|
* If p_source is "string", p_xml must be a string containing
|
|
|
|
|
valid XML content.
|
|
|
|
|
* If p_source is "file": p_xml can be:
|
|
|
|
|
- a string containing the path to the XML file on disk;
|
|
|
|
|
- a file instance opened for reading. Note that in this case, this
|
|
|
|
|
method will close it.
|
|
|
|
|
'''
|
2009-06-29 07:06:01 -05:00
|
|
|
|
try:
|
|
|
|
|
from cStringIO import StringIO
|
|
|
|
|
except ImportError:
|
|
|
|
|
from StringIO import StringIO
|
|
|
|
|
self.parser.setContentHandler(self)
|
|
|
|
|
self.parser.setErrorHandler(self)
|
2010-12-23 03:25:27 -06:00
|
|
|
|
self.parser.setFeature(feature_external_ges, False)
|
2009-06-29 07:06:01 -05:00
|
|
|
|
inputSource = InputSource()
|
|
|
|
|
if source == 'string':
|
2011-02-17 11:13:42 -06:00
|
|
|
|
inputSource.setByteStream(StringIO(xml))
|
2009-06-29 07:06:01 -05:00
|
|
|
|
else:
|
2011-02-17 11:13:42 -06:00
|
|
|
|
if not isinstance(xml, file):
|
|
|
|
|
xml = file(xml)
|
|
|
|
|
inputSource.setByteStream(xml)
|
2009-06-29 07:06:01 -05:00
|
|
|
|
self.parser.parse(inputSource)
|
2011-02-17 11:13:42 -06:00
|
|
|
|
if isinstance(xml, file): xml.close()
|
2009-08-04 07:39:43 -05:00
|
|
|
|
return self.res
|
2009-08-17 10:06:17 -05:00
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
2010-11-08 04:40:41 -06:00
|
|
|
|
from appy.shared import UnmarshalledFile
|
|
|
|
|
from appy import Object
|
2009-08-17 10:06:17 -05:00
|
|
|
|
try:
|
|
|
|
|
from DateTime import DateTime
|
|
|
|
|
except ImportError:
|
|
|
|
|
DateTime = 'unicode'
|
|
|
|
|
|
|
|
|
|
class XmlUnmarshaller(XmlParser):
|
|
|
|
|
'''This class allows to parse a XML file and recreate the corresponding web
|
|
|
|
|
of Python objects. This parser assumes that the XML file respects this
|
|
|
|
|
convention: any tag may define in attribute "type" storing the type of
|
|
|
|
|
its content, which may be:
|
|
|
|
|
|
|
|
|
|
bool * int * float * long * DateTime * tuple * list * object
|
|
|
|
|
|
|
|
|
|
If "object" is specified, it means that the tag contains sub-tags, each
|
|
|
|
|
one corresponding to the value of an attribute for this object.
|
|
|
|
|
if "tuple" is specified, it will be converted to a list.'''
|
2012-06-02 07:36:49 -05:00
|
|
|
|
def __init__(self, classes={}, tagTypes={}, conversionFunctions={},
|
|
|
|
|
utf8=True):
|
2009-08-17 10:06:17 -05:00
|
|
|
|
XmlParser.__init__(self)
|
2010-03-31 08:49:54 -05:00
|
|
|
|
# self.classes below is a dict whose keys are tag names and values are
|
|
|
|
|
# Python classes. During the unmarshalling process, when an object is
|
2010-11-08 04:40:41 -06:00
|
|
|
|
# encountered, instead of creating an instance of Object, we will create
|
|
|
|
|
# an instance of the class specified in self.classes.
|
2010-03-31 08:49:54 -05:00
|
|
|
|
# Root tag is named "xmlPythonData" by default by the XmlMarshaller.
|
2010-11-08 04:40:41 -06:00
|
|
|
|
# This will not work if the object in the specified tag is not an
|
|
|
|
|
# Object instance (ie it is a list or tuple or simple value). Note that
|
|
|
|
|
# we will not call the constructor of the specified class. We will
|
|
|
|
|
# simply create an instance of Objects and dynamically change the class
|
|
|
|
|
# of the created instance to this class.
|
2010-03-31 08:49:54 -05:00
|
|
|
|
if not isinstance(classes, dict) and classes:
|
|
|
|
|
# The user may only need to define a class for the root tag
|
|
|
|
|
self.classes = {'xmlPythonData': classes}
|
|
|
|
|
else:
|
|
|
|
|
self.classes = classes
|
2009-09-18 07:42:31 -05:00
|
|
|
|
# We expect that the parsed XML file will follow some conventions
|
|
|
|
|
# (ie, a tag that corresponds to a list has attribute type="list" or a
|
|
|
|
|
# tag that corresponds to an object has attribute type="object".). If
|
|
|
|
|
# it is not the case of p_xmlContent, you can provide the missing type
|
|
|
|
|
# information in p_tagTypes. Here is an example of p_tagTypes:
|
|
|
|
|
# {"information": "list", "days": "list", "person": "object"}.
|
2010-03-31 08:49:54 -05:00
|
|
|
|
self.tagTypes = tagTypes
|
2009-10-25 15:42:08 -05:00
|
|
|
|
# The parser assumes that data is represented in some standard way. If
|
|
|
|
|
# it is not the case, you may provide, in this dict, custom functions
|
|
|
|
|
# allowing to convert values of basic types (long, float, DateTime...).
|
|
|
|
|
# Every such function must take a single arg which is the value to
|
|
|
|
|
# convert and return the converted value. Dict keys are strings
|
|
|
|
|
# representing types ('bool', 'int', 'unicode', etc) and dict values are
|
|
|
|
|
# conversion functions. Here is an example:
|
|
|
|
|
# {'int': convertInteger, 'DateTime': convertDate}
|
|
|
|
|
# NOTE: you can even invent a new basic type, put it in self.tagTypes,
|
|
|
|
|
# and create a specific conversionFunction for it. This way, you can
|
|
|
|
|
# for example convert strings that have specific values (in this case,
|
2010-03-31 08:49:54 -05:00
|
|
|
|
# knowing that the value is a 'string' is not sufficient).
|
|
|
|
|
self.conversionFunctions = conversionFunctions
|
2012-06-02 07:36:49 -05:00
|
|
|
|
self.utf8 = utf8
|
2009-08-17 10:06:17 -05:00
|
|
|
|
|
2009-11-11 13:22:13 -06:00
|
|
|
|
def convertAttrs(self, attrs):
|
|
|
|
|
'''Converts XML attrs to a dict.'''
|
|
|
|
|
res = {}
|
2010-11-08 04:40:41 -06:00
|
|
|
|
for k, v in attrs.items():
|
|
|
|
|
if ':' in k: # An attr prefixed with a namespace. Remove this.
|
|
|
|
|
k = k.split(':')[-1]
|
|
|
|
|
res[str(k)] = v
|
2009-11-11 13:22:13 -06:00
|
|
|
|
return res
|
|
|
|
|
|
2009-08-17 10:06:17 -05:00
|
|
|
|
def startDocument(self):
|
2012-12-07 05:05:39 -06:00
|
|
|
|
XmlParser.startDocument(self)
|
2010-11-08 04:40:41 -06:00
|
|
|
|
self.res = None # The resulting web of Python objects (Object instances)
|
2009-08-17 10:06:17 -05:00
|
|
|
|
self.env.containerStack = [] # The stack of current "containers" where
|
|
|
|
|
# to store the next parsed element. A container can be a list, a tuple,
|
|
|
|
|
# an object (the root object of the whole web or a sub-object).
|
|
|
|
|
self.env.currentBasicType = None # Will hold the name of the currently
|
2009-08-28 08:14:26 -05:00
|
|
|
|
# parsed basic type (unicode, float...)
|
2009-08-17 10:06:17 -05:00
|
|
|
|
self.env.currentContent = '' # We store here the content of tags.
|
|
|
|
|
|
2011-09-06 03:23:56 -05:00
|
|
|
|
containerTags = ('tuple', 'list', 'dict', 'object', 'file')
|
2009-08-17 10:06:17 -05:00
|
|
|
|
numericTypes = ('bool', 'int', 'float', 'long')
|
|
|
|
|
def startElement(self, elem, attrs):
|
2010-11-08 04:40:41 -06:00
|
|
|
|
# Remember the name of the previous element
|
|
|
|
|
previousElem = None
|
|
|
|
|
if self.env.currentElem:
|
|
|
|
|
previousElem = self.env.currentElem.name
|
2009-08-17 10:06:17 -05:00
|
|
|
|
e = XmlParser.startElement(self, elem, attrs)
|
|
|
|
|
# Determine the type of the element.
|
|
|
|
|
elemType = 'unicode' # Default value
|
|
|
|
|
if attrs.has_key('type'):
|
|
|
|
|
elemType = attrs['type']
|
2009-09-18 07:42:31 -05:00
|
|
|
|
elif self.tagTypes.has_key(elem):
|
|
|
|
|
elemType = self.tagTypes[elem]
|
2009-08-17 10:06:17 -05:00
|
|
|
|
if elemType in self.containerTags:
|
|
|
|
|
# I must create a new container object.
|
2009-11-11 13:22:13 -06:00
|
|
|
|
if elemType == 'object':
|
2010-11-08 04:40:41 -06:00
|
|
|
|
newObject = Object(**self.convertAttrs(attrs))
|
2009-08-17 10:06:17 -05:00
|
|
|
|
elif elemType == 'tuple': newObject = [] # Tuples become lists
|
|
|
|
|
elif elemType == 'list': newObject = []
|
2011-09-06 03:23:56 -05:00
|
|
|
|
elif elemType == 'dict': newObject = {}
|
2009-08-28 08:14:26 -05:00
|
|
|
|
elif elemType == 'file':
|
|
|
|
|
newObject = UnmarshalledFile()
|
|
|
|
|
if attrs.has_key('name'):
|
|
|
|
|
newObject.name = attrs['name']
|
|
|
|
|
if attrs.has_key('mimeType'):
|
|
|
|
|
newObject.mimeType = attrs['mimeType']
|
2010-11-08 04:40:41 -06:00
|
|
|
|
else: newObject = Object(**self.convertAttrs(attrs))
|
2009-08-17 10:06:17 -05:00
|
|
|
|
# Store the value on the last container, or on the root object.
|
|
|
|
|
self.storeValue(elem, newObject)
|
|
|
|
|
# Push the new object on the container stack
|
|
|
|
|
e.containerStack.append(newObject)
|
|
|
|
|
else:
|
2010-11-08 04:40:41 -06:00
|
|
|
|
# If we are already parsing a basic type, it means that we were
|
|
|
|
|
# wrong for our diagnotsic of the containing element: it was not
|
|
|
|
|
# basic. We will make the assumption that the containing element is
|
|
|
|
|
# then an object.
|
|
|
|
|
if e.currentBasicType:
|
|
|
|
|
# Previous elem was an object: create it on the stack.
|
|
|
|
|
newObject = Object()
|
|
|
|
|
self.storeValue(previousElem, newObject)
|
|
|
|
|
e.containerStack.append(newObject)
|
2009-08-17 10:06:17 -05:00
|
|
|
|
e.currentBasicType = elemType
|
|
|
|
|
|
|
|
|
|
def storeValue(self, name, value):
|
|
|
|
|
'''Stores the newly parsed p_value (contained in tag p_name) on the
|
2010-03-31 08:49:54 -05:00
|
|
|
|
current container in environment self.env.'''
|
2009-08-17 10:06:17 -05:00
|
|
|
|
e = self.env
|
2010-11-08 04:40:41 -06:00
|
|
|
|
# Remove namespace prefix when relevant
|
|
|
|
|
if ':' in name: name = name.split(':')[-1]
|
2010-03-31 08:49:54 -05:00
|
|
|
|
# Change the class of the value if relevant
|
2010-11-08 04:40:41 -06:00
|
|
|
|
if (name in self.classes) and isinstance(value, Object):
|
2010-03-31 08:49:54 -05:00
|
|
|
|
value.__class__ = self.classes[name]
|
2009-08-17 10:06:17 -05:00
|
|
|
|
# Where must I store this value?
|
|
|
|
|
if not e.containerStack:
|
|
|
|
|
# I store the object at the root of the web.
|
|
|
|
|
self.res = value
|
|
|
|
|
else:
|
|
|
|
|
currentContainer = e.containerStack[-1]
|
2009-08-28 08:14:26 -05:00
|
|
|
|
if isinstance(currentContainer, list):
|
2009-08-17 10:06:17 -05:00
|
|
|
|
currentContainer.append(value)
|
2011-09-06 03:23:56 -05:00
|
|
|
|
elif isinstance(currentContainer, dict):
|
|
|
|
|
# If the current container is a dict, it means that p_value is
|
|
|
|
|
# a dict entry object named "entry" by convention and having
|
|
|
|
|
# attributes "k" and "v" that store, respectively, the key and
|
|
|
|
|
# the value of the entry. But this object is under construction:
|
|
|
|
|
# at this time, attributes "k" and "v" are not created yet. We
|
|
|
|
|
# will act in m_endElement, when the object will be finalized.
|
|
|
|
|
pass
|
2009-08-28 08:14:26 -05:00
|
|
|
|
elif isinstance(currentContainer, UnmarshalledFile):
|
2011-07-26 15:15:04 -05:00
|
|
|
|
currentContainer.content += value or ''
|
2009-08-17 10:06:17 -05:00
|
|
|
|
else:
|
|
|
|
|
# Current container is an object
|
2010-03-31 08:49:54 -05:00
|
|
|
|
if hasattr(currentContainer, name) and \
|
|
|
|
|
getattr(currentContainer, name):
|
2009-09-18 07:42:31 -05:00
|
|
|
|
# We have already encountered a sub-object with this name.
|
|
|
|
|
# Having several sub-objects with the same name, we will
|
|
|
|
|
# create a list.
|
|
|
|
|
attrValue = getattr(currentContainer, name)
|
|
|
|
|
if not isinstance(attrValue, list):
|
|
|
|
|
attrValue = [attrValue, value]
|
|
|
|
|
else:
|
|
|
|
|
attrValue.append(value)
|
|
|
|
|
else:
|
|
|
|
|
attrValue = value
|
|
|
|
|
setattr(currentContainer, name, attrValue)
|
2009-08-17 10:06:17 -05:00
|
|
|
|
|
|
|
|
|
def characters(self, content):
|
2012-06-02 07:36:49 -05:00
|
|
|
|
if not self.utf8:
|
|
|
|
|
content = content.encode('utf-8')
|
2009-08-17 10:06:17 -05:00
|
|
|
|
e = XmlParser.characters(self, content)
|
|
|
|
|
if e.currentBasicType:
|
|
|
|
|
e.currentContent += content
|
|
|
|
|
|
|
|
|
|
def endElement(self, elem):
|
|
|
|
|
e = XmlParser.endElement(self, elem)
|
|
|
|
|
if e.currentBasicType:
|
2009-10-25 15:42:08 -05:00
|
|
|
|
value = e.currentContent.strip()
|
|
|
|
|
if not value: value = None
|
2009-08-17 10:06:17 -05:00
|
|
|
|
else:
|
2009-10-25 15:42:08 -05:00
|
|
|
|
# If we have a custom converter for values of this type, use it.
|
|
|
|
|
if self.conversionFunctions.has_key(e.currentBasicType):
|
|
|
|
|
try:
|
|
|
|
|
value = self.conversionFunctions[e.currentBasicType](
|
|
|
|
|
value)
|
|
|
|
|
except Exception, err:
|
|
|
|
|
raise AppyError(CUSTOM_CONVERSION_ERROR % (
|
|
|
|
|
e.currentBasicType, value, str(err)))
|
|
|
|
|
# If not, try a standard conversion
|
|
|
|
|
elif e.currentBasicType in self.numericTypes:
|
|
|
|
|
try:
|
|
|
|
|
exec 'value = %s' % value
|
|
|
|
|
except SyntaxError:
|
|
|
|
|
raise AppyError(CONVERSION_ERROR % (
|
|
|
|
|
e.currentBasicType, value))
|
|
|
|
|
except NameError:
|
|
|
|
|
raise AppyError(CONVERSION_ERROR % (
|
|
|
|
|
e.currentBasicType, value))
|
|
|
|
|
# Check that the value is of the correct type. For instance,
|
|
|
|
|
# a float value with a comma in it could have been converted
|
|
|
|
|
# to a tuple instead of a float.
|
|
|
|
|
if not isinstance(value, eval(e.currentBasicType)):
|
|
|
|
|
raise AppyError(CONVERSION_ERROR % (
|
|
|
|
|
e.currentBasicType, value))
|
|
|
|
|
elif e.currentBasicType == 'DateTime':
|
|
|
|
|
value = DateTime(value)
|
|
|
|
|
elif e.currentBasicType == 'base64':
|
|
|
|
|
value = e.currentContent.decode('base64')
|
2009-08-17 10:06:17 -05:00
|
|
|
|
# Store the value on the last container
|
|
|
|
|
self.storeValue(elem, value)
|
|
|
|
|
# Clean the environment
|
|
|
|
|
e.currentBasicType = None
|
|
|
|
|
e.currentContent = ''
|
|
|
|
|
else:
|
2011-09-06 03:23:56 -05:00
|
|
|
|
elem = e.containerStack.pop()
|
|
|
|
|
# This element can be a temporary "entry" object representing a dict
|
|
|
|
|
# entry.
|
|
|
|
|
if e.containerStack:
|
|
|
|
|
lastContainer = e.containerStack[-1]
|
|
|
|
|
if isinstance(lastContainer, dict):
|
|
|
|
|
lastContainer[elem.k] = elem.v
|
2009-08-17 10:06:17 -05:00
|
|
|
|
|
2009-09-18 07:42:31 -05:00
|
|
|
|
# Alias: "unmarshall" -> "parse"
|
2009-08-17 10:06:17 -05:00
|
|
|
|
unmarshall = XmlParser.parse
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
class XmlMarshaller:
|
|
|
|
|
'''This class allows to produce a XML version of a Python object, which
|
|
|
|
|
respects some conventions as described in the doc of the corresponding
|
|
|
|
|
Unmarshaller (see above).'''
|
|
|
|
|
xmlEntities = {'<': '<', '>': '>', '&': '&', '"': '"',
|
|
|
|
|
"'": '''}
|
|
|
|
|
trueFalse = {True: 'True', False: 'False'}
|
|
|
|
|
fieldsToMarshall = 'all'
|
|
|
|
|
fieldsToExclude = []
|
|
|
|
|
atFiles = ('image', 'file') # Types of archetypes fields that contain files.
|
|
|
|
|
|
2010-03-31 08:49:54 -05:00
|
|
|
|
def __init__(self, cdata=False, dumpUnicode=False, conversionFunctions={},
|
2010-11-08 04:40:41 -06:00
|
|
|
|
dumpXmlPrologue=True, rootTag='xmlPythonData', namespaces={},
|
|
|
|
|
namespacedTags={}):
|
2010-03-31 08:49:54 -05:00
|
|
|
|
# If p_cdata is True, all string values will be dumped as XML CDATA.
|
2009-12-07 13:28:41 -06:00
|
|
|
|
self.cdata = cdata
|
2010-03-31 08:49:54 -05:00
|
|
|
|
# If p_dumpUnicode is True, the result will be unicode.
|
2010-01-29 04:28:39 -06:00
|
|
|
|
self.dumpUnicode = dumpUnicode
|
2010-02-24 03:27:41 -06:00
|
|
|
|
# The following dict stores specific conversion (=Python to XML)
|
|
|
|
|
# functions. A specific conversion function is useful when you are not
|
|
|
|
|
# happy with the way built-in converters work, or if you want to
|
|
|
|
|
# specify a specific way to represent, in XML, some particular Python
|
|
|
|
|
# object or value. In this dict, every key represents a given type
|
|
|
|
|
# (class names must be full-path class names); every value is a function
|
|
|
|
|
# accepting 2 args: first one is the UnicodeBuffer where the result is
|
|
|
|
|
# being dumped, while the second one is the Python object or value to
|
|
|
|
|
# dump.
|
|
|
|
|
self.conversionFunctions = conversionFunctions
|
2010-03-31 08:49:54 -05:00
|
|
|
|
# If dumpXmlPrologue is True, the XML prologue will be dumped.
|
|
|
|
|
self.dumpXmlPrologue = dumpXmlPrologue
|
|
|
|
|
# The name of the root tag
|
|
|
|
|
self.rootElementName = rootTag
|
2010-11-08 04:40:41 -06:00
|
|
|
|
# The namespaces that will be defined at the root of the XML message.
|
|
|
|
|
# It is a dict whose keys are namespace prefixes and whose values are
|
2011-10-26 03:21:09 -05:00
|
|
|
|
# namespace URLs. If you want to specify a default namespace, specify an
|
|
|
|
|
# entry with an empty string as a key.
|
2010-11-08 04:40:41 -06:00
|
|
|
|
self.namespaces = namespaces
|
|
|
|
|
# The following dict will tell which XML tags will get which namespace
|
|
|
|
|
# prefix ({s_tagName: s_prefix}). Special optional dict entry
|
|
|
|
|
# '*':s_prefix will indicate a default prefix that will be applied to
|
|
|
|
|
# any tag that does not have it own key in this dict.
|
|
|
|
|
self.namespacedTags = namespacedTags
|
|
|
|
|
self.objectType = None # Will be given by method m_marshal
|
|
|
|
|
|
|
|
|
|
def getTagName(self, name):
|
|
|
|
|
'''Returns the name of tag p_name as will be dumped. It can be p_name,
|
|
|
|
|
or p_name prefixed with a namespace prefix (will depend on
|
|
|
|
|
self.prefixedTags).'''
|
|
|
|
|
# Determine the prefix
|
|
|
|
|
prefix = ''
|
|
|
|
|
if name in self.namespacedTags: prefix = self.namespacedTags[name]
|
|
|
|
|
elif '*' in self.namespacedTags: prefix = self.namespacedTags['*']
|
|
|
|
|
if prefix: return '%s:%s' % (prefix, name)
|
|
|
|
|
return name
|
|
|
|
|
|
2012-02-13 13:41:28 -06:00
|
|
|
|
def isAnObject(self, instance):
|
|
|
|
|
'''Returns True if p_instance is a class instance, False if it is a
|
|
|
|
|
basic type, or tuple, sequence, etc.'''
|
|
|
|
|
if instance.__class__.__name__ == 'LazyMap': return False
|
|
|
|
|
iType = type(instance)
|
|
|
|
|
if iType == types.InstanceType:
|
|
|
|
|
return True
|
|
|
|
|
elif iType.__name__ == 'ImplicitAcquirerWrapper':
|
|
|
|
|
# This is the case with Archetype instances
|
|
|
|
|
return True
|
|
|
|
|
elif iType.__class__.__name__ == 'ExtensionClass':
|
|
|
|
|
return True
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
def isAList(self, value):
|
|
|
|
|
'''Is p_value a list?'''
|
|
|
|
|
return value.__class__.__name__ in ('list', 'PersistentList', 'LazyMap')
|
|
|
|
|
|
|
|
|
|
def isADict(self, value):
|
|
|
|
|
'''Is p_value a dict?'''
|
|
|
|
|
return value.__class__.__name__ in ('dict', 'PersistentMapping')
|
|
|
|
|
|
2010-11-08 04:40:41 -06:00
|
|
|
|
def dumpRootTag(self, res, instance):
|
|
|
|
|
'''Dumps the root tag.'''
|
|
|
|
|
# Dumps the name of the tag.
|
|
|
|
|
tagName = self.getTagName(self.rootElementName)
|
|
|
|
|
res.write('<'); res.write(tagName)
|
|
|
|
|
# Dumps namespace definitions if any
|
|
|
|
|
for prefix, url in self.namespaces.iteritems():
|
2011-10-26 03:21:09 -05:00
|
|
|
|
if not prefix:
|
|
|
|
|
pre = 'xmlns' # The default namespace
|
|
|
|
|
else:
|
|
|
|
|
pre = 'xmlns:%s' % prefix
|
|
|
|
|
res.write(' %s="%s"' % (pre, url))
|
|
|
|
|
# Dumps Appy- or Plone-specific attribute
|
2010-11-08 04:40:41 -06:00
|
|
|
|
if self.objectType != 'popo':
|
|
|
|
|
res.write(' type="object" id="%s"' % instance.UID())
|
|
|
|
|
res.write('>')
|
|
|
|
|
return tagName
|
2009-12-07 13:28:41 -06:00
|
|
|
|
|
2009-08-28 08:14:26 -05:00
|
|
|
|
def dumpString(self, res, s):
|
|
|
|
|
'''Dumps a string into the result.'''
|
2010-02-02 09:25:10 -06:00
|
|
|
|
if self.cdata: res.write('<![CDATA[')
|
|
|
|
|
if isinstance(s, str):
|
|
|
|
|
s = s.decode('utf-8')
|
2009-08-28 08:14:26 -05:00
|
|
|
|
# Replace special chars by XML entities
|
|
|
|
|
for c in s:
|
|
|
|
|
if self.xmlEntities.has_key(c):
|
|
|
|
|
res.write(self.xmlEntities[c])
|
|
|
|
|
else:
|
|
|
|
|
res.write(c)
|
2010-02-02 09:25:10 -06:00
|
|
|
|
if self.cdata: res.write(']]>')
|
2009-08-28 08:14:26 -05:00
|
|
|
|
|
|
|
|
|
def dumpFile(self, res, v):
|
|
|
|
|
'''Dumps a file into the result.'''
|
2010-01-29 04:28:39 -06:00
|
|
|
|
if not v: return
|
2014-03-03 11:54:21 -06:00
|
|
|
|
w = res.write
|
2009-08-28 08:14:26 -05:00
|
|
|
|
# p_value contains the (possibly binary) content of a file. We will
|
|
|
|
|
# encode it in Base64, in one or several parts.
|
2010-11-08 04:40:41 -06:00
|
|
|
|
partTag = self.getTagName('part')
|
|
|
|
|
res.write('<%s type="base64" number="1">' % partTag)
|
2009-08-28 08:14:26 -05:00
|
|
|
|
if hasattr(v, 'data'):
|
|
|
|
|
# The file is an Archetypes file.
|
2014-03-03 11:54:21 -06:00
|
|
|
|
if v.data.__class__.__name__ == 'Pdata':
|
2009-08-28 08:14:26 -05:00
|
|
|
|
# There will be several parts.
|
2014-03-03 11:54:21 -06:00
|
|
|
|
w(v.data.data.encode('base64'))
|
2009-08-28 08:14:26 -05:00
|
|
|
|
# Write subsequent parts
|
|
|
|
|
nextPart = v.data.next
|
2014-03-03 11:54:21 -06:00
|
|
|
|
nextPartNb = 2
|
2009-08-28 08:14:26 -05:00
|
|
|
|
while nextPart:
|
2014-03-03 11:54:21 -06:00
|
|
|
|
w('</%s>' % partTag) # Close the previous part
|
|
|
|
|
w('<%s type="base64" number="%d">' % (partTag, nextPartNb))
|
|
|
|
|
w(nextPart.data.encode('base64'))
|
2009-08-28 08:14:26 -05:00
|
|
|
|
nextPart = nextPart.next
|
2014-03-03 11:54:21 -06:00
|
|
|
|
nextPartNb += 1
|
2009-08-28 08:14:26 -05:00
|
|
|
|
else:
|
2014-03-03 11:54:21 -06:00
|
|
|
|
w(v.data.encode('base64'))
|
|
|
|
|
w('</%s>' % partTag)
|
|
|
|
|
elif hasattr(v, 'uploadName'):
|
|
|
|
|
# The file is a Appy FileInfo instance. Read the file from disk.
|
|
|
|
|
filePath = v.getFilePath(self.instance)
|
|
|
|
|
f = file(filePath, 'rb')
|
|
|
|
|
partNb = 1
|
|
|
|
|
while True:
|
|
|
|
|
chunk = f.read(v.BYTES)
|
|
|
|
|
if not chunk: break
|
|
|
|
|
# We have one more chunk. Dump the start tag (excepted if it is
|
|
|
|
|
# the first chunk: the start tag has already been dumped, see
|
|
|
|
|
# above).
|
|
|
|
|
if partNb > 1:
|
|
|
|
|
w('<%s type="base64" number="%d">' % (partTag, partNb))
|
|
|
|
|
w(chunk.encode('base64'))
|
|
|
|
|
w('</%s>' % partTag) # Close the tag
|
|
|
|
|
partNb += 1
|
|
|
|
|
f.close()
|
2009-08-28 08:14:26 -05:00
|
|
|
|
else:
|
2014-03-03 11:54:21 -06:00
|
|
|
|
w(v.encode('base64'))
|
|
|
|
|
w('</%s>' % partTag)
|
2009-08-28 08:14:26 -05:00
|
|
|
|
|
2011-09-06 03:23:56 -05:00
|
|
|
|
def dumpDict(self, res, v):
|
|
|
|
|
'''Dumps the XML version of dict p_v.'''
|
|
|
|
|
for key, value in v.iteritems():
|
|
|
|
|
res.write('<entry type="object">')
|
|
|
|
|
self.dumpField(res, 'k', key)
|
|
|
|
|
self.dumpField(res, 'v', value)
|
|
|
|
|
res.write('</entry>')
|
|
|
|
|
|
2010-03-31 08:49:54 -05:00
|
|
|
|
def dumpValue(self, res, value, fieldType, isRef=False):
|
2009-08-17 10:06:17 -05:00
|
|
|
|
'''Dumps the XML version of p_value to p_res.'''
|
2010-02-24 03:27:41 -06:00
|
|
|
|
# Use a custom function if one is defined for this type of value.
|
2010-03-31 08:49:54 -05:00
|
|
|
|
className = value.__class__.__name__
|
|
|
|
|
if className in self.conversionFunctions:
|
|
|
|
|
self.conversionFunctions[className](res, value)
|
2010-02-24 03:27:41 -06:00
|
|
|
|
return
|
|
|
|
|
# Use a standard conversion else.
|
2011-09-06 03:23:56 -05:00
|
|
|
|
if fieldType == 'file': self.dumpFile(res, value)
|
|
|
|
|
elif fieldType == 'dict': self.dumpDict(res, value)
|
2010-03-31 08:49:54 -05:00
|
|
|
|
elif isRef:
|
2009-08-28 08:14:26 -05:00
|
|
|
|
if value:
|
2012-11-23 08:20:12 -06:00
|
|
|
|
if self.objectType == 'appy':
|
|
|
|
|
suffix = '/xml'
|
|
|
|
|
else:
|
|
|
|
|
suffix = ''
|
2012-01-12 14:49:23 -06:00
|
|
|
|
if type(value) in sequenceTypes:
|
2009-08-28 08:14:26 -05:00
|
|
|
|
for elem in value:
|
2012-11-23 08:20:12 -06:00
|
|
|
|
self.dumpField(res, 'url', elem.absolute_url()+suffix)
|
2009-08-17 10:06:17 -05:00
|
|
|
|
else:
|
2012-11-23 08:20:12 -06:00
|
|
|
|
self.dumpField(res, 'url', value.absolute_url()+suffix)
|
2012-02-13 13:41:28 -06:00
|
|
|
|
elif fieldType in ('list', 'tuple'):
|
2009-08-28 08:14:26 -05:00
|
|
|
|
# The previous condition must be checked before this one because
|
2010-02-24 03:27:41 -06:00
|
|
|
|
# referred objects may be stored in lists or tuples, too.
|
2011-09-06 03:23:56 -05:00
|
|
|
|
for elem in value: self.dumpField(res, 'e', elem)
|
|
|
|
|
elif isinstance(value, basestring): self.dumpString(res, value)
|
|
|
|
|
elif isinstance(value, bool): res.write(self.trueFalse[value])
|
2010-03-31 08:49:54 -05:00
|
|
|
|
elif fieldType == 'object':
|
2010-02-23 07:55:51 -06:00
|
|
|
|
if hasattr(value, 'absolute_url'):
|
2010-11-08 04:40:41 -06:00
|
|
|
|
# Dump the URL to the object only
|
2010-02-23 07:55:51 -06:00
|
|
|
|
res.write(value.absolute_url())
|
|
|
|
|
else:
|
2010-11-08 04:40:41 -06:00
|
|
|
|
# Dump the entire object content
|
|
|
|
|
for k, v in value.__dict__.iteritems():
|
|
|
|
|
if not k.startswith('__'):
|
|
|
|
|
self.dumpField(res, k, v)
|
|
|
|
|
# Maybe we could add a parameter to the marshaller to know how
|
|
|
|
|
# to marshall objects (produce an ID, an URL, include the entire
|
|
|
|
|
# tag but we need to take care of circular references,...)
|
2009-08-17 10:06:17 -05:00
|
|
|
|
else:
|
|
|
|
|
res.write(value)
|
|
|
|
|
|
|
|
|
|
def dumpField(self, res, fieldName, fieldValue, fieldType='basic'):
|
|
|
|
|
'''Dumps in p_res, the value of the p_field for p_instance.'''
|
2011-10-26 03:21:09 -05:00
|
|
|
|
# As a preamble, manage special case of p_fieldName == "_any". In that
|
|
|
|
|
# case, p_fieldValue corresponds to a previously marshalled string that
|
|
|
|
|
# must be included as is here, without dumping the tag name.
|
2011-11-03 10:14:23 -05:00
|
|
|
|
if fieldName == '_any':
|
|
|
|
|
res.write(value)
|
|
|
|
|
return
|
2011-10-26 03:21:09 -05:00
|
|
|
|
# Now, dump "normal" fields.
|
2010-11-08 04:40:41 -06:00
|
|
|
|
fieldTag = self.getTagName(fieldName)
|
|
|
|
|
res.write('<'); res.write(fieldTag)
|
2009-08-17 10:06:17 -05:00
|
|
|
|
# Dump the type of the field as an XML attribute
|
2009-08-28 08:14:26 -05:00
|
|
|
|
fType = None # No type will mean "unicode".
|
2010-03-31 08:49:54 -05:00
|
|
|
|
if fieldType == 'file': fType = 'file'
|
|
|
|
|
elif fieldType == 'ref': fType = 'list'
|
|
|
|
|
elif isinstance(fieldValue, bool): fType = 'bool'
|
|
|
|
|
elif isinstance(fieldValue, int): fType = 'int'
|
|
|
|
|
elif isinstance(fieldValue, float): fType = 'float'
|
|
|
|
|
elif isinstance(fieldValue, long): fType = 'long'
|
|
|
|
|
elif isinstance(fieldValue, tuple): fType = 'tuple'
|
2012-02-13 13:41:28 -06:00
|
|
|
|
elif self.isAList(fieldValue): fType = 'list'
|
|
|
|
|
elif self.isADict(fieldValue): fType = 'dict'
|
2009-08-17 10:06:17 -05:00
|
|
|
|
elif fieldValue.__class__.__name__ == 'DateTime': fType = 'DateTime'
|
2010-03-31 08:49:54 -05:00
|
|
|
|
elif self.isAnObject(fieldValue): fType = 'object'
|
2010-11-08 04:40:41 -06:00
|
|
|
|
if self.objectType != 'popo':
|
|
|
|
|
if fType: res.write(' type="%s"' % fType)
|
|
|
|
|
# Dump other attributes if needed
|
2012-02-13 13:41:28 -06:00
|
|
|
|
if fType in ('list', 'tuple'):
|
2012-04-24 09:22:12 -05:00
|
|
|
|
length = 0
|
|
|
|
|
if fieldValue: length = len(fieldValue)
|
|
|
|
|
res.write(' count="%d"' % length)
|
2010-03-31 08:49:54 -05:00
|
|
|
|
if fType == 'file':
|
2014-03-03 11:54:21 -06:00
|
|
|
|
# Get the MIME type
|
|
|
|
|
mimeType = None
|
2009-08-17 10:06:17 -05:00
|
|
|
|
if hasattr(fieldValue, 'content_type'):
|
2014-03-03 11:54:21 -06:00
|
|
|
|
mimeType = fieldValue.content_type
|
|
|
|
|
elif hasattr(fieldValue, 'mimeType'):
|
|
|
|
|
mimeType = fieldValue.mimeType
|
|
|
|
|
if mimeType: res.write(' mimeType="%s"' % mimeType)
|
|
|
|
|
# Get the file name
|
|
|
|
|
fileName = None
|
2009-08-17 10:06:17 -05:00
|
|
|
|
if hasattr(fieldValue, 'filename'):
|
2014-03-03 11:54:21 -06:00
|
|
|
|
fileName = fieldValue.filename
|
|
|
|
|
elif hasattr(fieldValue, 'uploadName'):
|
|
|
|
|
fileName = fieldValue.uploadName
|
|
|
|
|
if fileName:
|
2009-08-28 08:14:26 -05:00
|
|
|
|
res.write(' name="')
|
2014-03-03 11:54:21 -06:00
|
|
|
|
self.dumpString(res, fileName)
|
2009-08-28 08:14:26 -05:00
|
|
|
|
res.write('"')
|
2009-08-17 10:06:17 -05:00
|
|
|
|
res.write('>')
|
2009-08-28 08:14:26 -05:00
|
|
|
|
# Dump the field value
|
2010-03-31 08:49:54 -05:00
|
|
|
|
self.dumpValue(res, fieldValue, fType, isRef=(fieldType=='ref'))
|
2010-11-08 04:40:41 -06:00
|
|
|
|
res.write('</'); res.write(fieldTag); res.write('>')
|
2009-08-17 10:06:17 -05:00
|
|
|
|
|
2010-02-24 03:27:41 -06:00
|
|
|
|
def marshall(self, instance, objectType='popo', conversionFunctions={}):
|
2010-02-02 09:25:10 -06:00
|
|
|
|
'''Returns in a UnicodeBuffer the XML version of p_instance. If
|
|
|
|
|
p_instance corresponds to a Plain Old Python Object, specify 'popo'
|
|
|
|
|
for p_objectType. If p_instance corresponds to an Archetypes object
|
2009-12-07 13:28:41 -06:00
|
|
|
|
(Zope/Plone), specify 'archetype' for p_objectType. if p_instance is
|
2010-02-23 07:55:51 -06:00
|
|
|
|
a Appy object, specify "appy" as p_objectType. If p_instance is not
|
|
|
|
|
an instance at all, but another Python data structure or basic type,
|
|
|
|
|
p_objectType is ignored.'''
|
2010-11-08 04:40:41 -06:00
|
|
|
|
self.objectType = objectType
|
2014-03-03 11:54:21 -06:00
|
|
|
|
# The Appy object is needed to marshall its File fields.
|
|
|
|
|
if objectType == 'appy': self.instance = instance
|
2010-02-23 07:55:51 -06:00
|
|
|
|
# Call the XmlMarshaller constructor if it hasn't been called yet.
|
2010-02-02 09:25:10 -06:00
|
|
|
|
if not hasattr(self, 'cdata'):
|
|
|
|
|
XmlMarshaller.__init__(self)
|
2010-02-24 03:27:41 -06:00
|
|
|
|
if conversionFunctions:
|
|
|
|
|
self.conversionFunctions.update(conversionFunctions)
|
2010-02-23 07:55:51 -06:00
|
|
|
|
# Create the buffer where the XML result will be dumped.
|
2010-02-02 09:25:10 -06:00
|
|
|
|
res = UnicodeBuffer()
|
2010-03-31 08:49:54 -05:00
|
|
|
|
# Dump the XML prologue if required
|
|
|
|
|
if self.dumpXmlPrologue:
|
|
|
|
|
res.write(xmlPrologue)
|
2010-02-23 07:55:51 -06:00
|
|
|
|
if self.isAnObject(instance):
|
2010-11-08 04:40:41 -06:00
|
|
|
|
# Dump the root tag
|
|
|
|
|
rootTagName = self.dumpRootTag(res, instance)
|
|
|
|
|
# Dump the fields of this root object
|
2010-02-23 07:55:51 -06:00
|
|
|
|
if objectType == 'popo':
|
|
|
|
|
for fieldName, fieldValue in instance.__dict__.iteritems():
|
2009-08-17 10:06:17 -05:00
|
|
|
|
mustDump = False
|
2010-02-23 07:55:51 -06:00
|
|
|
|
if fieldName in self.fieldsToExclude:
|
|
|
|
|
mustDump = False
|
|
|
|
|
elif self.fieldsToMarshall == 'all':
|
2009-08-17 10:06:17 -05:00
|
|
|
|
mustDump = True
|
2010-02-23 07:55:51 -06:00
|
|
|
|
else:
|
2012-01-12 14:49:23 -06:00
|
|
|
|
if (type(self.fieldsToMarshall) in sequenceTypes) \
|
2010-02-23 07:55:51 -06:00
|
|
|
|
and (fieldName in self.fieldsToMarshall):
|
|
|
|
|
mustDump = True
|
|
|
|
|
if mustDump:
|
|
|
|
|
self.dumpField(res, fieldName, fieldValue)
|
2010-08-05 11:23:17 -05:00
|
|
|
|
elif objectType == 'archetype':
|
2010-02-23 07:55:51 -06:00
|
|
|
|
for field in instance.schema.fields():
|
|
|
|
|
# Dump only needed fields
|
2009-08-17 10:06:17 -05:00
|
|
|
|
mustDump = False
|
2010-02-23 07:55:51 -06:00
|
|
|
|
if field.getName() in self.fieldsToExclude:
|
|
|
|
|
mustDump = False
|
|
|
|
|
elif (self.fieldsToMarshall == 'all') and \
|
|
|
|
|
(field.schemata != 'metadata'):
|
|
|
|
|
mustDump = True
|
|
|
|
|
elif self.fieldsToMarshall == 'all_with_metadata':
|
2009-08-17 10:06:17 -05:00
|
|
|
|
mustDump = True
|
2010-02-23 07:55:51 -06:00
|
|
|
|
else:
|
2012-01-12 14:49:23 -06:00
|
|
|
|
if (type(self.fieldsToMarshall) in sequenceTypes) \
|
2010-02-23 07:55:51 -06:00
|
|
|
|
and (field.getName() in self.fieldsToMarshall):
|
|
|
|
|
mustDump = True
|
|
|
|
|
if mustDump:
|
|
|
|
|
fieldType = 'basic'
|
|
|
|
|
if field.type in self.atFiles:
|
|
|
|
|
fieldType = 'file'
|
|
|
|
|
elif field.type == 'reference':
|
|
|
|
|
fieldType = 'ref'
|
|
|
|
|
self.dumpField(res, field.getName(),field.get(instance),
|
|
|
|
|
fieldType=fieldType)
|
2010-08-05 11:23:17 -05:00
|
|
|
|
elif objectType == 'appy':
|
2012-11-23 08:20:12 -06:00
|
|
|
|
for field in instance.getAppyTypes('view', None):
|
2010-08-05 11:23:17 -05:00
|
|
|
|
# Dump only needed fields
|
2012-11-23 08:20:12 -06:00
|
|
|
|
if (field.type == 'Computed') and not field.plainText:
|
|
|
|
|
# Ignore fields used for producing custom chunks of HTML
|
|
|
|
|
# within the web UI.
|
|
|
|
|
continue
|
2010-08-05 11:23:17 -05:00
|
|
|
|
if field.name in self.fieldsToExclude: continue
|
2012-01-12 14:49:23 -06:00
|
|
|
|
if (type(self.fieldsToMarshall) in sequenceTypes) \
|
2010-08-05 11:23:17 -05:00
|
|
|
|
and (field.name not in self.fieldsToMarshall): continue
|
2012-11-23 08:20:12 -06:00
|
|
|
|
# Determine field type and value
|
2010-08-05 11:23:17 -05:00
|
|
|
|
fieldType = 'basic'
|
|
|
|
|
if field.type == 'File':
|
|
|
|
|
fieldType = 'file'
|
2012-11-23 08:20:12 -06:00
|
|
|
|
v = field.getValue(instance)
|
2010-08-05 11:23:17 -05:00
|
|
|
|
elif field.type == 'Ref':
|
|
|
|
|
fieldType = 'ref'
|
2012-11-23 08:20:12 -06:00
|
|
|
|
v = field.getValue(instance, type='zobjects')
|
|
|
|
|
else:
|
|
|
|
|
v = field.getValue(instance)
|
|
|
|
|
self.dumpField(res, field.name, v, fieldType=fieldType)
|
2010-08-05 11:23:17 -05:00
|
|
|
|
# Dump the object history.
|
2012-11-23 08:20:12 -06:00
|
|
|
|
if hasattr(instance.aq_base, 'workflow_history'):
|
|
|
|
|
histTag = self.getTagName('history')
|
|
|
|
|
eventTag = self.getTagName('event')
|
|
|
|
|
res.write('<%s type="list">' % histTag)
|
|
|
|
|
key = instance.workflow_history.keys()[0]
|
|
|
|
|
history = instance.workflow_history[key]
|
2010-08-05 11:23:17 -05:00
|
|
|
|
for event in history:
|
2010-11-08 04:40:41 -06:00
|
|
|
|
res.write('<%s type="object">' % eventTag)
|
2010-08-05 11:23:17 -05:00
|
|
|
|
for k, v in event.iteritems():
|
|
|
|
|
self.dumpField(res, k, v)
|
2010-11-08 04:40:41 -06:00
|
|
|
|
res.write('</%s>' % eventTag)
|
2012-11-23 08:20:12 -06:00
|
|
|
|
res.write('</%s>' % histTag)
|
2010-02-23 07:55:51 -06:00
|
|
|
|
self.marshallSpecificElements(instance, res)
|
2010-11-08 04:40:41 -06:00
|
|
|
|
res.write('</'); res.write(rootTagName); res.write('>')
|
2010-02-23 07:55:51 -06:00
|
|
|
|
else:
|
|
|
|
|
self.dumpField(res, self.rootElementName, instance)
|
2009-08-17 10:06:17 -05:00
|
|
|
|
# Return the result
|
2010-02-05 08:39:52 -06:00
|
|
|
|
res = res.getValue()
|
2010-02-02 09:25:10 -06:00
|
|
|
|
if not self.dumpUnicode:
|
|
|
|
|
res = res.encode('utf-8')
|
|
|
|
|
return res
|
2009-08-17 10:06:17 -05:00
|
|
|
|
|
|
|
|
|
def marshallSpecificElements(self, instance, res):
|
|
|
|
|
'''You can use this marshaller as a base class for creating your own.
|
|
|
|
|
In this case, this method will be called by the marshall method
|
|
|
|
|
for allowing your concrete marshaller to insert more things in the
|
2010-02-24 03:27:41 -06:00
|
|
|
|
result. p_res is the UnicodeBuffer buffer where the result of the
|
2009-08-17 10:06:17 -05:00
|
|
|
|
marshalling process is currently dumped; p_instance is the instance
|
|
|
|
|
currently marshalled.'''
|
2009-11-11 13:22:13 -06:00
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
class XmlHandler(ContentHandler):
|
|
|
|
|
'''This handler is used for producing, in self.res, a readable XML
|
|
|
|
|
(with carriage returns) and for removing some tags that always change
|
|
|
|
|
(like dates) from a file that need to be compared to another file.'''
|
|
|
|
|
def __init__(self, xmlTagsToIgnore, xmlAttrsToIgnore):
|
|
|
|
|
ContentHandler.__init__(self)
|
2010-03-31 08:49:54 -05:00
|
|
|
|
self.res = unicode(xmlPrologue)
|
2009-11-11 13:22:13 -06:00
|
|
|
|
self.namespaces = {} # ~{s_namespaceUri:s_namespaceName}~
|
|
|
|
|
self.indentLevel = -1
|
|
|
|
|
self.tabWidth = 3
|
|
|
|
|
self.tagsToIgnore = xmlTagsToIgnore
|
|
|
|
|
self.attrsToIgnore = xmlAttrsToIgnore
|
|
|
|
|
self.ignoring = False # Some content must be ignored, and not dumped
|
|
|
|
|
# into the result.
|
|
|
|
|
def isIgnorable(self, elem):
|
|
|
|
|
'''Is p_elem an ignorable element ?'''
|
|
|
|
|
res = False
|
|
|
|
|
for tagName in self.tagsToIgnore:
|
|
|
|
|
if isinstance(tagName, list) or isinstance(tagName, tuple):
|
|
|
|
|
# We have a namespace
|
|
|
|
|
nsUri, elemName = tagName
|
|
|
|
|
try:
|
|
|
|
|
nsName = self.ns(nsUri)
|
|
|
|
|
elemFullName = '%s:%s' % (nsName, elemName)
|
|
|
|
|
except KeyError:
|
|
|
|
|
elemFullName = ''
|
|
|
|
|
else:
|
|
|
|
|
# No namespace
|
|
|
|
|
elemFullName = tagName
|
|
|
|
|
if elemFullName == elem:
|
|
|
|
|
res = True
|
|
|
|
|
break
|
|
|
|
|
return res
|
|
|
|
|
def setDocumentLocator(self, locator):
|
|
|
|
|
self.locator = locator
|
|
|
|
|
def endDocument(self):
|
|
|
|
|
pass
|
|
|
|
|
def dumpSpaces(self):
|
|
|
|
|
self.res += '\n' + (' ' * self.indentLevel * self.tabWidth)
|
|
|
|
|
def manageNamespaces(self, attrs):
|
|
|
|
|
'''Manage namespaces definitions encountered in attrs'''
|
|
|
|
|
for attrName, attrValue in attrs.items():
|
|
|
|
|
if attrName.startswith('xmlns:'):
|
|
|
|
|
self.namespaces[attrValue] = attrName[6:]
|
|
|
|
|
def ns(self, nsUri):
|
|
|
|
|
return self.namespaces[nsUri]
|
|
|
|
|
def startElement(self, elem, attrs):
|
|
|
|
|
self.manageNamespaces(attrs)
|
|
|
|
|
# Do we enter into a ignorable element ?
|
|
|
|
|
if self.isIgnorable(elem):
|
|
|
|
|
self.ignoring = True
|
|
|
|
|
else:
|
|
|
|
|
if not self.ignoring:
|
|
|
|
|
self.indentLevel += 1
|
|
|
|
|
self.dumpSpaces()
|
|
|
|
|
self.res += '<%s' % elem
|
|
|
|
|
attrsNames = attrs.keys()
|
|
|
|
|
attrsNames.sort()
|
|
|
|
|
for attrToIgnore in self.attrsToIgnore:
|
|
|
|
|
if attrToIgnore in attrsNames:
|
|
|
|
|
attrsNames.remove(attrToIgnore)
|
|
|
|
|
for attrName in attrsNames:
|
|
|
|
|
self.res += ' %s="%s"' % (attrName, attrs[attrName])
|
|
|
|
|
self.res += '>'
|
|
|
|
|
def endElement(self, elem):
|
|
|
|
|
if self.isIgnorable(elem):
|
|
|
|
|
self.ignoring = False
|
|
|
|
|
else:
|
|
|
|
|
if not self.ignoring:
|
|
|
|
|
self.dumpSpaces()
|
|
|
|
|
self.indentLevel -= 1
|
|
|
|
|
self.res += '</%s>' % elem
|
|
|
|
|
def characters(self, content):
|
|
|
|
|
if not self.ignoring:
|
|
|
|
|
self.res += content.replace('\n', '')
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
class XmlComparator:
|
|
|
|
|
'''Compares 2 XML files and produces a diff.'''
|
|
|
|
|
def __init__(self, fileNameA, fileNameB, areXml=True, xmlTagsToIgnore=(),
|
|
|
|
|
xmlAttrsToIgnore=()):
|
|
|
|
|
self.fileNameA = fileNameA
|
|
|
|
|
self.fileNameB = fileNameB
|
|
|
|
|
self.areXml = areXml # Can also diff non-XML files.
|
|
|
|
|
self.xmlTagsToIgnore = xmlTagsToIgnore
|
|
|
|
|
self.xmlAttrsToIgnore = xmlAttrsToIgnore
|
|
|
|
|
|
|
|
|
|
def filesAreIdentical(self, report=None, encoding=None):
|
|
|
|
|
'''Compares the 2 files and returns True if they are identical (if we
|
|
|
|
|
ignore xmlTagsToIgnore and xmlAttrsToIgnore).
|
|
|
|
|
If p_report is specified, it must be an instance of
|
|
|
|
|
appy.shared.test.TestReport; the diffs will be dumped in it.'''
|
|
|
|
|
# Perform the comparison
|
|
|
|
|
differ = difflib.Differ()
|
|
|
|
|
if self.areXml:
|
|
|
|
|
f = file(self.fileNameA)
|
|
|
|
|
contentA = f.read()
|
|
|
|
|
f.close()
|
|
|
|
|
f = file(self.fileNameB)
|
|
|
|
|
contentB = f.read()
|
|
|
|
|
f.close()
|
|
|
|
|
xmlHandler = XmlHandler(self.xmlTagsToIgnore, self.xmlAttrsToIgnore)
|
|
|
|
|
xml.sax.parseString(contentA, xmlHandler)
|
|
|
|
|
contentA = xmlHandler.res.split('\n')
|
|
|
|
|
xmlHandler = XmlHandler(self.xmlTagsToIgnore, self.xmlAttrsToIgnore)
|
|
|
|
|
xml.sax.parseString(contentB, xmlHandler)
|
|
|
|
|
contentB = xmlHandler.res.split('\n')
|
|
|
|
|
else:
|
|
|
|
|
f = file(self.fileNameA)
|
|
|
|
|
contentA = f.readlines()
|
|
|
|
|
f.close()
|
|
|
|
|
f = file(self.fileNameB)
|
|
|
|
|
contentB = f.readlines()
|
|
|
|
|
f.close()
|
|
|
|
|
diffResult = list(differ.compare(contentA, contentB))
|
|
|
|
|
# Analyse, format and report the result.
|
|
|
|
|
atLeastOneDiff = False
|
|
|
|
|
lastLinePrinted = False
|
|
|
|
|
i = -1
|
|
|
|
|
for line in diffResult:
|
|
|
|
|
i += 1
|
|
|
|
|
if line and (line[0] != ' '):
|
|
|
|
|
if not atLeastOneDiff:
|
2009-12-03 09:45:05 -06:00
|
|
|
|
msg = 'Difference(s) detected between files %s and %s:' % \
|
|
|
|
|
(self.fileNameA, self.fileNameB)
|
|
|
|
|
if report: report.say(msg, encoding='utf-8')
|
2013-05-29 17:46:11 -05:00
|
|
|
|
else: print(msg)
|
2009-11-11 13:22:13 -06:00
|
|
|
|
atLeastOneDiff = True
|
|
|
|
|
if not lastLinePrinted:
|
|
|
|
|
if report: report.say('...')
|
2013-05-29 17:46:11 -05:00
|
|
|
|
else: print('...')
|
2009-11-11 13:22:13 -06:00
|
|
|
|
if self.areXml:
|
|
|
|
|
if report: report.say(line, encoding=encoding)
|
2013-05-29 17:46:11 -05:00
|
|
|
|
else: print(line)
|
2009-11-11 13:22:13 -06:00
|
|
|
|
else:
|
|
|
|
|
if report: report.say(line[:-1], encoding=encoding)
|
2013-05-29 17:46:11 -05:00
|
|
|
|
else: print(line[:-1])
|
2009-11-11 13:22:13 -06:00
|
|
|
|
lastLinePrinted = True
|
|
|
|
|
else:
|
|
|
|
|
lastLinePrinted = False
|
|
|
|
|
return not atLeastOneDiff
|
2012-05-14 10:35:34 -05:00
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
class XhtmlCleaner(XmlParser):
|
2012-05-22 09:42:20 -05:00
|
|
|
|
'''This class cleans XHTML content, so it becomes ready to be stored into a
|
|
|
|
|
Appy-compliant format.'''
|
|
|
|
|
class Error(Exception): pass
|
2012-05-14 10:35:34 -05:00
|
|
|
|
|
2013-01-11 10:16:36 -06:00
|
|
|
|
# Tags that will never be in the result, content included.
|
2012-05-24 08:27:24 -05:00
|
|
|
|
tagsToIgnoreWithContent = ('style', 'colgroup', 'head')
|
2012-05-14 10:35:34 -05:00
|
|
|
|
# Tags that will be removed from the result, but whose content will be kept,
|
2013-01-11 10:16:36 -06:00
|
|
|
|
tagsToIgnoreKeepContent = ('x', 'html', 'body')
|
|
|
|
|
allTagsToIgnore = tagsToIgnoreWithContent + tagsToIgnoreKeepContent
|
|
|
|
|
|
|
|
|
|
# Additional tags that will be removed, but content kept, if keepStyles is
|
|
|
|
|
# False.
|
|
|
|
|
tagsToIgnoreKeepContentDropStyles = ('font', 'center')
|
|
|
|
|
|
2012-05-14 10:35:34 -05:00
|
|
|
|
# Attributes to ignore, if keepStyles if False.
|
|
|
|
|
attrsToIgnore = ('align', 'valign', 'cellpadding', 'cellspacing', 'width',
|
2013-03-28 15:32:47 -05:00
|
|
|
|
'height', 'bgcolor', 'lang', 'border', 'class', 'rules',
|
|
|
|
|
'id', 'name')
|
2013-01-11 10:16:36 -06:00
|
|
|
|
# CSS attributes to keep even if keepStyles if False. These attributes can
|
|
|
|
|
# be used by pod (to align a paragraph, center/resize an image...).
|
2012-05-24 05:54:40 -05:00
|
|
|
|
cssAttrsToKeep = ('width', 'height', 'float', 'text-align',
|
|
|
|
|
'font-style', 'font-weight')
|
2012-05-14 10:35:34 -05:00
|
|
|
|
# Attrs to add, if not present, to ensure good formatting, be it at the web
|
|
|
|
|
# or ODT levels.
|
|
|
|
|
attrsToAdd = {'table': {'cellspacing':'0', 'cellpadding':'6', 'border':'1'},
|
|
|
|
|
'tr': {'valign': 'top'}}
|
|
|
|
|
|
2013-01-11 10:16:36 -06:00
|
|
|
|
# Tags that require a line break to be inserted after them.
|
2012-05-14 10:35:34 -05:00
|
|
|
|
lineBreakTags = ('p', 'li', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'td')
|
2012-05-22 09:42:20 -05:00
|
|
|
|
|
2013-01-11 10:16:36 -06:00
|
|
|
|
# No-end tags
|
|
|
|
|
noEndTags = ('br', 'img')
|
|
|
|
|
|
|
|
|
|
def __init__(self, keepStyles=True):
|
|
|
|
|
XmlParser.__init__(self)
|
|
|
|
|
self.keepStyles = keepStyles
|
|
|
|
|
# Compute tags to ignore, which may vary according to p_keepStyles.
|
|
|
|
|
self.tagsToIgnore = self.allTagsToIgnore
|
|
|
|
|
if not keepStyles:
|
|
|
|
|
self.tagsToIgnore += self.tagsToIgnoreKeepContentDropStyles
|
|
|
|
|
|
|
|
|
|
def clean(self, s):
|
2012-05-22 09:42:20 -05:00
|
|
|
|
'''Cleaning XHTML code is done for 2 reasons:
|
|
|
|
|
|
|
|
|
|
1. The main objective is to format XHTML p_s to be storable in the
|
|
|
|
|
ZODB according to Appy rules.
|
|
|
|
|
a. Every <p> or <li> must be on a single line (ending with a
|
|
|
|
|
carriage return); else, appy.shared.diff will not be able to
|
|
|
|
|
compute XHTML diffs;
|
|
|
|
|
b. Optimize size: HTML comments are removed.
|
|
|
|
|
|
|
|
|
|
2. If p_keepStyles (or m_clean) is False, some style-related
|
|
|
|
|
information will be removed, in order to get a standardized
|
|
|
|
|
content that can be dumped in an elegant and systematic manner
|
|
|
|
|
into a POD template.
|
|
|
|
|
'''
|
2012-05-14 10:35:34 -05:00
|
|
|
|
self.env.currentContent = ''
|
|
|
|
|
# The stack of currently parsed elements (will contain only ignored
|
|
|
|
|
# ones).
|
|
|
|
|
self.env.currentElems = []
|
|
|
|
|
# 'ignoreTag' is True if we must ignore the currently walked tag.
|
|
|
|
|
self.env.ignoreTag = False
|
|
|
|
|
# 'ignoreContent' is True if, within the currently ignored tag, we must
|
|
|
|
|
# also ignore its content.
|
|
|
|
|
self.env.ignoreContent = False
|
2012-05-22 09:42:20 -05:00
|
|
|
|
try:
|
2012-12-07 05:05:39 -06:00
|
|
|
|
res = self.parse('<x>%s</x>' % s).encode('utf-8')
|
2012-05-22 09:42:20 -05:00
|
|
|
|
except SAXParseException, e:
|
|
|
|
|
raise self.Error(str(e))
|
|
|
|
|
return res
|
2012-05-14 10:35:34 -05:00
|
|
|
|
|
2012-05-24 05:54:40 -05:00
|
|
|
|
def cleanStyleAttribute(self, value):
|
|
|
|
|
'''p_value contains some CSS attributes from a "style" attribute. We
|
|
|
|
|
keep those that pod can manage.'''
|
|
|
|
|
res = []
|
|
|
|
|
for name, v in parseStyleAttribute(value):
|
|
|
|
|
if name in self.cssAttrsToKeep:
|
|
|
|
|
res.append('%s: %s' % (name, v))
|
|
|
|
|
return '; '.join(res)
|
|
|
|
|
|
2012-05-14 10:35:34 -05:00
|
|
|
|
def startDocument(self):
|
|
|
|
|
# The result will be cleaned XHTML, joined from self.res.
|
2012-12-07 05:05:39 -06:00
|
|
|
|
XmlParser.startDocument(self)
|
2012-05-14 10:35:34 -05:00
|
|
|
|
self.res = []
|
|
|
|
|
|
|
|
|
|
def endDocument(self):
|
|
|
|
|
self.res = ''.join(self.res)
|
|
|
|
|
|
|
|
|
|
def startElement(self, elem, attrs):
|
|
|
|
|
e = self.env
|
|
|
|
|
# Dump any previously gathered content if any
|
|
|
|
|
if e.currentContent:
|
|
|
|
|
self.res.append(e.currentContent)
|
|
|
|
|
e.currentContent = ''
|
|
|
|
|
if e.ignoreTag and e.ignoreContent: return
|
2013-01-11 10:16:36 -06:00
|
|
|
|
if elem in self.tagsToIgnore:
|
2012-05-14 10:35:34 -05:00
|
|
|
|
e.ignoreTag = True
|
|
|
|
|
if elem in self.tagsToIgnoreWithContent:
|
|
|
|
|
e.ignoreContent = True
|
|
|
|
|
else:
|
|
|
|
|
e.ignoreContent = False
|
|
|
|
|
e.currentElems.append( (elem, e.ignoreContent) )
|
|
|
|
|
return
|
|
|
|
|
# Add a line break before the start tag if required (ie: xhtml differ
|
|
|
|
|
# needs to get paragraphs and other elements on separate lines).
|
|
|
|
|
if (elem in self.lineBreakTags) and self.res and \
|
|
|
|
|
(self.res[-1][-1] != '\n'):
|
|
|
|
|
prefix = '\n'
|
|
|
|
|
else:
|
|
|
|
|
prefix = ''
|
|
|
|
|
res = '%s<%s' % (prefix, elem)
|
|
|
|
|
# Include the found attributes, excepted those that must be ignored.
|
|
|
|
|
for name, value in attrs.items():
|
2013-01-11 10:16:36 -06:00
|
|
|
|
if not self.keepStyles:
|
2012-05-24 05:54:40 -05:00
|
|
|
|
if name in self.attrsToIgnore: continue
|
|
|
|
|
elif name == 'style':
|
|
|
|
|
value = self.cleanStyleAttribute(value)
|
|
|
|
|
if not value: continue
|
2012-05-14 10:35:34 -05:00
|
|
|
|
res += ' %s="%s"' % (name, value)
|
|
|
|
|
# Include additional attributes if required.
|
|
|
|
|
if elem in self.attrsToAdd:
|
|
|
|
|
for name, value in self.attrsToAdd[elem].iteritems():
|
|
|
|
|
res += ' %s="%s"' % (name, value)
|
2013-01-11 10:16:36 -06:00
|
|
|
|
# Close the tag if it is a no-end tag
|
|
|
|
|
if elem in self.noEndTags:
|
|
|
|
|
suffix = '/>'
|
|
|
|
|
else:
|
|
|
|
|
suffix = '>'
|
|
|
|
|
self.res.append('%s%s' % (res, suffix))
|
2012-05-14 10:35:34 -05:00
|
|
|
|
|
|
|
|
|
def endElement(self, elem):
|
|
|
|
|
e = self.env
|
|
|
|
|
if e.ignoreTag and (elem in self.tagsToIgnore):
|
|
|
|
|
# Pop the currently ignored tag
|
|
|
|
|
e.currentElems.pop()
|
|
|
|
|
if e.currentElems:
|
|
|
|
|
# Keep ignoring tags.
|
|
|
|
|
e.ignoreContent = e.currentElems[-1][1]
|
|
|
|
|
else:
|
|
|
|
|
# Stop ignoring elems
|
|
|
|
|
e.ignoreTag = e.ignoreContent = False
|
|
|
|
|
elif e.ignoreTag and e.ignoreContent:
|
|
|
|
|
# This is the end of a sub-tag within a region that we must ignore.
|
|
|
|
|
pass
|
|
|
|
|
else:
|
2013-01-10 08:59:01 -06:00
|
|
|
|
if self.env.currentContent:
|
|
|
|
|
self.res.append(self.env.currentContent)
|
2013-01-11 10:16:36 -06:00
|
|
|
|
# Close the tag only if it is a no-end tag.
|
|
|
|
|
if elem not in self.noEndTags:
|
|
|
|
|
# Add a line break after the end tag if required (ie: xhtml
|
|
|
|
|
# differ needs to get paragraphs and other elements on separate
|
|
|
|
|
# lines).
|
|
|
|
|
if (elem in self.lineBreakTags) and self.res and \
|
|
|
|
|
(self.res[-1][-1] != '\n'):
|
|
|
|
|
suffix = '\n'
|
|
|
|
|
else:
|
|
|
|
|
suffix = ''
|
|
|
|
|
self.res.append('</%s>%s' % (elem, suffix))
|
2012-05-14 10:35:34 -05:00
|
|
|
|
self.env.currentContent = ''
|
|
|
|
|
|
|
|
|
|
def characters(self, content):
|
|
|
|
|
if self.env.ignoreContent: return
|
2013-03-28 15:32:47 -05:00
|
|
|
|
# Remove blanks that ckeditor may add just after a start tag or
|
|
|
|
|
# between tags.
|
2013-01-10 08:59:01 -06:00
|
|
|
|
if not self.env.currentContent or \
|
|
|
|
|
self.env.currentContent[-1] in ('\n', ' '):
|
2013-09-05 03:42:19 -05:00
|
|
|
|
# I give here to lstrip an explicit list of what is to be considered
|
|
|
|
|
# as blank chars, because I do not want unicode NBSP chars to be in
|
|
|
|
|
# this list.
|
|
|
|
|
toAdd = content.lstrip(u' \n\r\t')
|
2012-05-14 10:35:34 -05:00
|
|
|
|
else:
|
|
|
|
|
toAdd = content
|
|
|
|
|
# Re-transform XML special chars to entities.
|
2013-01-10 08:59:01 -06:00
|
|
|
|
self.env.currentContent += cgi.escape(toAdd)
|
2009-06-29 07:06:01 -05:00
|
|
|
|
# ------------------------------------------------------------------------------
|