Back to index

plone3  3.1.7
_xmlns.py
Go to the documentation of this file.
00001 # Marshall: A framework for pluggable marshalling policies
00002 # Copyright (C) 2004-2006 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: _xmlns.py 6805 2006-08-25 12:57:16Z yenzenz $
00020 """
00021 
00022 from xml.dom import minidom
00023 from AccessControl import ClassSecurityInfo
00024 from Globals import InitializeClass
00025 from Products.CMFCore.permissions import ManagePortal
00026 from _base import Predicate
00027 from Products.Marshall.registry import registerPredicate
00028 from Products.PageTemplates.PageTemplateFile import PageTemplateFile
00029 
00030 def attrMethod(attr, default=''):
00031     def boundAttrMethod(self):
00032         return getattr(self, attr, default)
00033     return boundAttrMethod
00034 
00035 class XMLNS(Predicate):
00036     """ Predicate matching xml files on a xmlns:attribute pair.
00037     """
00038 
00039     security = ClassSecurityInfo()
00040     security.declareProtected(ManagePortal, 'edit',
00041                               'getElementNS', 'getElementName',
00042                               'getAttributeNS', 'getAttributeName',
00043                               'getValue')
00044     getElementNS = attrMethod('_element_ns')
00045     getElementName = attrMethod('_element_name')
00046     getAttributeNS = attrMethod('_attr_ns')
00047     getAttributeName = attrMethod('_attr_name')
00048     getValue = attrMethod('_value', None)
00049 
00050     manage_options = (
00051         Predicate.manage_options[0],
00052         {'label':'Settings', 'action':'manage_changeSettingsForm'},
00053         ) + Predicate.manage_options[1:]
00054 
00055     security.declareProtected('View management screens',
00056       'manage_changeSettingsForm')
00057 
00058     manage_changeSettingsForm = PageTemplateFile(
00059         '../www/xmlnsSettings', globals(),
00060         __name__='manage_changeSettingsForm')
00061 
00062     manage_changeSettingsForm._owner = None
00063 
00064     security.declareProtected(ManagePortal, 'edit')
00065     def edit(self, element_ns='', element_name='',
00066              attr_ns='', attr_name='', value='', REQUEST=None):
00067         """Change settings germane to this predicate
00068         """
00069 
00070         for n in (element_ns, element_name, attr_ns, attr_name):
00071             if not isinstance(n, basestring):
00072                 raise TypeError, 'string required, got %r.' % n
00073         self._value = value
00074         self._element_ns = element_ns
00075         self._element_name = element_name
00076         self._attr_ns = attr_ns
00077         self._attr_name = attr_name
00078 
00079         if REQUEST is not None:
00080             message = 'Predicate Settings Changed.'
00081             return self.manage_changeSettingsForm(
00082                 manage_tabs_message=message,
00083                 management_view='Settings')
00084 
00085     security.declareProtected(ManagePortal, 'apply')
00086     def apply(self, obj, **kw):
00087         """ Return component name if the rule matches, else an empty tuple.
00088         """
00089         retval = Predicate.apply(self, obj, **kw)
00090         if not retval:
00091             return ()
00092         body = kw.get('data')
00093         if not body:
00094             f = kw.get('file')
00095             if f is None:
00096                 return ()
00097             # Try 'file', though that may bloat memory with big files :(
00098             body = f.read()
00099             f.seek(0)
00100             if not body:
00101                 return ()
00102         try:
00103             doc = minidom.parseString(body)
00104         except:
00105             return ()
00106         elm_args = filter(None, (self.getElementNS(), self.getElementName()))
00107         get_elm = (len(elm_args) == 2 and doc.getElementsByTagNameNS
00108                    or doc.getElementsByTagName)
00109         elm = get_elm(*elm_args)
00110         if not elm:
00111             return ()
00112         match = True
00113         elm = elm[0]
00114         attr_args = filter(None, (self.getAttributeNS(), self.getAttributeName()))
00115         if not attr_args:
00116             get_attr = lambda elm=elm: elm.firstChild.nodeValue.strip()
00117         else:
00118             get_attr = (len(attr_args) == 2 and elm.getAttributeNS or
00119                         elm.getAttribute)
00120         expected = self.getValue()
00121         if expected is not None:
00122             got = get_attr(*attr_args)
00123             match = got == expected
00124         return match and retval or ()
00125 
00126 InitializeClass(XMLNS)
00127 registerPredicate('xmlns_attr', 'XMLNS Element/Attribute', XMLNS)