## Zope shared file, mixin class, provides XML export/import facilities ## Copyright (C) 2000 J. David Ibáñez, Andres Marzal ## Universitat Jaume I, Spain ## email: al028823@alumail.uji.es ## 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. __doc__ = """XML support""" __version__ = "0.1" from string import join, replace from types import StringType from Globals import HTML from Shared.DC.xml import pyexpat ## Mixin class, to export/import objects to/from XML class XMLio: xml_attributes = () xml_elements = () ## To XML def _xml_get_attributes(self): attributes = [] for x in self.xml_attributes: attributes.append('%s="%s"' % (x, str(getattr(self, x)))) return attributes def xml_get_attributes(self): return self._xml_get_attributes() def _xml_get_elements(self): elements = [] for x in self.xml_elements: elements.append('<%s>' % (x, str(getattr(self, x)), x)) return elements def xml_get_elements(self): return self._xml_get_elements() def _toXML(self): attributes = self.xml_get_attributes() elements = self.xml_get_elements() if elements: s = ['<%s %s>' % (self.xml_name, join(attributes))] s = s + elements s.append('' % self.xml_name) else: s = ['<%s %s />' % (self.xml_name, join(attributes))] return s def toXML(self): """ """ return join(self._toXML(), '\n ') ## From XML def xml_start(self, attributes): pass def start(self, tag, attrs): attributes = {} for i in range(0, len(attrs), 2): attributes[attrs[i]] = decode(attrs[i+1]) if self._xml_stack: element = self._xml_stack[-1] if type(element) is not StringType: method = '_xml_start_%s' % tag if hasattr(element, method): new_object = getattr(element, method)(attributes) self._xml_stack.append(new_object) return elif tag in element.xml_elements: self._xml_stack.append(tag) return elif self.xml_name == tag: self.xml_start(attributes) self._xml_stack.append(self) return raise 'Syntax Error: unexpected %s start tag' % tag def end(self, tag): del self._xml_stack[-1] return def cdata(self, data): if self.in_cdata: self.xml_data = self.xml_data + data def start_cdata(self): self.xml_data = '' self.in_cdata=1 def end_cdata(self): self.in_cdata=0 if len(self._xml_stack) >= 2: tag = self._xml_stack[-1] element = self._xml_stack[-2] if type(tag) is StringType and type(element) is not StringType: setattr(element, tag, self.xml_data) del self.xml_data return raise 'Syntax Error: unexpacted CDATA section' def fromXML(self, file, text=''): """ """ self._xml_stack = [] p = pyexpat.ParserCreate() p.StartElementHandler = self.start p.EndElementHandler = self.end p.CharacterDataHandler = self.cdata p.StartCdataSectionHandler = self.start_cdata p.EndCdataSectionHandler = self.end_cdata self.in_cdata = 0 source = text if file != None: if hasattr(file, 'filename'): ## File upload if file.filename: source = file.read() else: ## python file source = file.read() for c in source[:-1]: c = encode(c) rv = p.Parse(c, 0) rv = p.Parse(source[-1], 1) del self.in_cdata del self._xml_stack if not rv: raise '%s at line %s, column %s' % (pyexpat.ErrorString(p.ErrorCode), p.ErrorLineNumber, p.ErrorColumnNumber) XMLForm = HTML( """

Export

Import

""", globals()) schars = {'á': '_aacute_', 'à': '_agrave_', 'é': '_eacute_', 'è': '_egrave_', 'í': '_iacute_', 'ì': '_igrave_', 'ó': '_oacute_', 'ò': '_ograve_', 'ú': '_uacute_', 'ù': '_ugrave_', 'Á': '_Aacute_', 'À': '_Agrave_', 'É': '_Eacute_', 'È': '_Egrave_', 'Í': '_Iacute_', 'Ì': '_Igrave_', 'Ó': '_Oacute_', 'Ò': '_Ograve_', 'Ú': '_Uacute_', 'Ù': '_Ugrave_', 'ñ': '_ntilde_', 'Ñ': '_Ntilde_', '·': '_mdt_'} def encode(c): return schars.get(c, c) def decode(s): for c, e in schars.items(): s = replace(s, e, c) return s