Back to index

plone3  3.1.7
simple.py
Go to the documentation of this file.
00001 # Marshall: A framework for pluggable marshalling policies
00002 # Copyright (C) 2004 Enfold Systems, LLC
00003 #
00004 # This program is free software; you can redistribute it and/or modify
00005 # it under the terms of the GNU General Public License as published by
00006 # the Free Software Foundation; either version 2 of the License, or
00007 # (at your option) any later version.
00008 #
00009 # This program is distributed in the hope that it will be useful,
00010 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 # GNU General Public License for more details.
00013 #
00014 # You should have received a copy of the GNU General Public License
00015 # along with this program; if not, write to the Free Software
00016 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 #
00018 """
00019 $Id: _xml.py 2994 2004-09-09 12:22:37Z dreamcatcher $
00020 """
00021 
00022 import os
00023 import thread
00024 from types import ListType, TupleType
00025 from xml.dom import minidom
00026 from cStringIO import StringIO
00027 from DateTime import DateTime
00028 from Products.CMFCore.utils import getToolByName
00029 from Products.Marshall.handlers.base import Marshaller
00030 from Products.Archetypes.Field import ReferenceField
00031 from Products.Archetypes.config import REFERENCE_CATALOG, UUID_ATTR
00032 from Products.Archetypes.debug import log
00033 from Products.Marshall.config import AT_NS, CMF_NS, ATXML_SCHEMA
00034 from Products.Marshall.exceptions import MarshallingException
00035 
00036 import libxml2
00037 libxml2.initParser()
00038 
00039 class SimpleXMLMarshaller(Marshaller):
00040 
00041     def demarshall(self, instance, data, **kwargs):
00042         doc = libxml2.parseDoc(data)
00043         p = instance.getPrimaryField()
00044         pname = p and p.getName() or None
00045         try:
00046             fields = [f for f in instance.Schema().fields()
00047                       if f.getName() != pname]
00048             for f in fields:
00049                 items = doc.xpathEval('/*/%s' % f.getName())
00050                 if not len(items): continue
00051                 # Note that we ignore all but the first element if
00052                 # we get more than one
00053                 value = items[0].children
00054                 if not value:
00055                     continue
00056                 mutator = f.getMutator(instance)
00057                 if mutator is not None:
00058                     mutator(value.content.strip())
00059         finally:
00060             doc.freeDoc()
00061 
00062     def marshall(self, instance, **kwargs):
00063         response = minidom.Document()
00064         doc = response.createElement(instance.portal_type.lower())
00065         response.appendChild(doc)
00066 
00067         p = instance.getPrimaryField()
00068         pname = p and p.getName() or None
00069         fields = [f for f in instance.Schema().fields()
00070                   if f.getName() != pname]
00071 
00072         for f in fields:
00073             value = instance[f.getName()]
00074             values = [value]
00075             if type(value) in [ListType, TupleType]:
00076                 values = [str(v) for v in value]
00077             elm = response.createElement(f.getName())
00078             for value in values:
00079                 value = response.createTextNode(str(value))
00080                 elm.appendChild(value)
00081             doc.appendChild(elm)
00082 
00083         content_type = 'text/xml'
00084         data = response.toprettyxml().encode('utf-8')
00085         length = len(data)
00086 
00087         return (content_type, length, data)