Ñò
3.'Ic           @   s­  d  Z  d d d !Z d d d !Z d d d !Z d d k l Z d d	 k l Z d d
 k l	 Z	 d d k
 l
 Z
 d d k l Z l Z l Z l Z l Z l Z l Z d d k Z d d k l Z d d k l Z l Z d d k l Z d d k l Z d Z d „  Z d d d d d d d d d „ Z e d e ƒ  ƒ Z d d d d d d d d e  e  d „
 Z! d d d d d d d d d „ Z" d „  Z# d e f d „  ƒ  YZ$ e i% e$ ƒ d S(   s  
$RCSfile: XSLTMethod.py,v $

ZopeXMLMethods provides methods to apply to Zope objects for XML/XSLT
processing.  XSLTMethod associates XSLT transformers with XML
documents.  XSLTMethod automatically transforms an XML document via
XSLT, where the XML document is obtained from another Zope object (the
'source' object) via acquisition.

Author: Craeg Strong <cstrong@arielpartners.com>
Modified by Philipp von Weitershausen <philikon@philikon.de>
Release: 1.0

$Id: XSLTMethod.py,v 1.8 2003/06/19 00:31:48 arielpartners Exp $
s	   $Name:  $i   iþÿÿÿs   $Date: 2003/06/19 00:31:48 $s   $Revision: 1.8 $i
   iÿÿÿÿ(   t   IXSLTMethod(   t   IXSLTProcessor(   t   ProcessorRegistry(   t   GeneratorRegistry(   t	   XMLMethodt   getPublishedResultt	   PERM_VIEWt	   PERM_EDITt   PERM_FTPt   PERM_CONTENTt   PERM_MANAGEN(   t   MessageDialog(   t	   aq_parentt   aq_get(   t   ClassSecurityInfo(   t   PageTemplateFilet   XSLparametersc         C   s˜   d } d } |  } |  i ƒ  } xs | d j oe | i o( | i | ƒ } | o | d } q` n | d j o" | | j o d St | ƒ } q! q! W| S(   sü  
    Find an instance of CacheManager to use (optionally, provided
    the caching property is set to 'on')

    CacheManager is purely optional.  If it is present, caching may be
    done.  If applicable, the XSLTMethod will use the CacheManager
    it finds via this method
        
    The current policy is as follows: use the first cache manager
    found by acquisition.  Alternatively, a more complex policy could
    be used such as
    a) looking in some predefined place, where the place could be
    specified in some property (a la Zope3 explicit acquisition), or
    b) finding all cache managers in the ZODB and presenting a list
    for selection by the user.
    For right now, we don't want to over-engineer the damn thing.  CKS
    8/4/2002.
    s   XML Method Cache Manageri    N(   t   Nonet   getPhysicalRoott   isPrincipiaFolderisht   objectValuesR   (   t   selft   metaTypet   cmt   foldert   roott   cacheManagers(    (    s/   /srv/Zope/Products/ZopeXMLMethods/XSLTMethod.pyt   findCacheManager/   s     
t    s	   text/htmlt   oni    c
   
   
   C   s4   |  i  d i | | | | | | | | |	 ƒ	 |  | S(   sû   
    This is a convenience factory method for creating an instance of
    XSLTMethod.  It returns the object created, and may therefore be
    more convenient than the addXSLTMethod() method it calls.  It is
    used by the unit testing programs.
    t   ZopeXMLMethods(   t   manage_addProductt   addXSLTMethod(
   R   t   idt   titlet   descriptiont   selected_processort   xslTransformerIdt   content_typet   behave_liket   cachingt
   debugLevel(    (    s/   /srv/Zope/Products/ZopeXMLMethods/XSLTMethod.pyt   addInstancec   s    
s   www/create.ptc         C   s  yº |  i  | | | | | | | | |	 ƒ	 d | d } |
 d j o d Sy |  i ƒ  } Wn |
 d } n X|
 d d j o( d | | f } |
 i i | d ƒ n |
 i i | d	 ƒ WnG t j
 o; } t | ƒ } | i d
 d ƒ t d d d | d d ƒ SXd S(   sª   
    Factory method to create an instance of XSLTMethod, called from
    a GUI in the Zope Management Interface.  It calls addXSLTMethod
    to actually do the work.
    s   Successfully created s    XSLTMethod object.Nt   URL1t   submits    Add and Edit s   %s/%ss   /manage_editForms   /manage_mains   
s   <br/>R"   t   Errort   messaget   actiont   manage_main(	   R    R   t   DestinationURLt   RESPONSEt   redirectt	   Exceptiont   strt   replaceR   (   R   R!   R"   R#   R$   R%   R&   R'   R(   R)   t   REQUESTR2   R.   t   urlt   e(    (    s/   /srv/Zope/Products/ZopeXMLMethods/XSLTMethod.pyt   manage_addXSLTMethodw   s*    
	
c
         C   sž   | p | o t  d ƒ ‚ n |  i | d ƒ }
 |
 d j o d | } t  | ƒ ‚ n |  i | t | | | | | | | | |	 ƒ	 ƒ |  i | ƒ i ƒ  d S(   sÅ   
    Factory method to actually create an instance of XSLTMethod and
    return it.

    You should call this method directly if you are creating an
    instance of XSLTMethod programatically.
    s!   Required fields must not be blanks&   Invalid transformer name. %s not foundN(   R4   t   restrictedTraverseR   t
   _setObjectt
   XSLTMethodt   _getObt   reindex_object(   R   R!   R"   R#   R$   R%   R&   R'   R(   R)   t   tranR.   (    (    s/   /srv/Zope/Products/ZopeXMLMethods/XSLTMethod.pyR    œ   s    
c         C   s   t  i t ƒ S(   sN   
        Return names of currently available XSLT processor libraries
        (   R   t   namesR   (   R   (    (    s/   /srv/Zope/Products/ZopeXMLMethods/XSLTMethod.pyt   availableProcessors¶   s    R=   c           B   s  e  Z d  Z d Z e Z e ƒ  Z e i	 h d d 6d d 6d d 6d d	 6f Z	 h d
 d 6d d 6d2 d 6f e i
 Z
 d d d d d „ Z e i e d ƒ e d e ƒ  ƒ Z e i e d ƒ d3 d3 d „ Z e i e d ƒ d „  Z e i e d ƒ e Z e i d ƒ d „  Z e i d ƒ d „  Z e i d  ƒ d! „  Z e i e d" ƒ d# „  Z e i e d$ ƒ d3 d% „ Z e i e d& ƒ d3 d' „ Z e i d( ƒ d) „  Z e i e d* ƒ d+ „  Z e i d ƒ d, „  Z  e i! d- ƒ d. „  Z" e i e d/ ƒ d3 d3 d3 d0 „ Z# d1 „  Z$ RS(4   sŸ   
    Automatically transforms an XML document via XSLT, where both the
    XML document and XSLT are obtained from other Zope objects via
    acquisition.
    s   XSLT MethodR(   R!   t	   selectiont   typet   wt   modet   onOfft   select_variablet   Editt   labelt   manage_editFormR/   R   s   edit.stxt   helps	   text/htmlR   R   i    c
   
   	   C   sX   t  i |  | | | | | | |	 ƒ | |  _ | |  _ |  i p t i t ƒ |  _ n d  S(   N(   R   t   __init__R%   R(   R$   R   t   defaultNameR   (
   R   R!   R"   R#   R$   R%   R&   R'   R(   R)   (    (    s/   /srv/Zope/Products/ZopeXMLMethods/XSLTMethod.pyRM   ×   s    
		
s   www/edit.ptt   manage_editc         C   sd   y |  i  | ƒ Wn1 t j
 o% } t d d d t | ƒ d d ƒ SX| |  _ d } |  i d | ƒ S(   s*   
        Edit XSLTMethod settings
        R"   R-   R.   R/   RK   s   Changes saved.t   manage_tabs_message(   t   editTransformR4   R   R5   R'   RK   (   R   R%   R'   R7   R2   R9   R.   (    (    s/   /srv/Zope/Products/ZopeXMLMethods/XSLTMethod.pyRO   ï   s    		t   isCacheFileUpToDatec         C   s   |  i  ƒ  } | d j o d S|  i ƒ  } |  i ƒ  } t | i ƒ  i ƒ  d ƒ } t | i ƒ  i ƒ  d ƒ } | i | i d ƒ ƒ } | | j o
 | | j S(   ss  
        Return 1 if and only if self should use the cached value
        rather than regenerating the transformed value, and the cache
        manager exists, and the cache file exists.  Note: this
        algorithm is rather simple and limited right now, but we
        intend to improve it over time.  Many items are not taken into
        account today: a) XML fragments that are included into the
        main document via the XSLT document() function b) XSLT
        transformer parameters c) XSLT transformers themselves (as
        well as all included and imported transformers).  FIXME cks
        11/26/2001
        i    g      à?t   URLN(	   R   R   t   getXmlSourceObjectt   getXslTransformert   roundt   bobobase_modification_timet   timeTimet   cacheFileTimeStampt   get(   R   R7   t   managert	   srcObjectt   xformObjectt
   sourceTimet	   xformTimet	   cacheTime(    (    s/   /srv/Zope/Products/ZopeXMLMethods/XSLTMethod.pyRR     s    R   RB   c         C   s   t  i t ƒ S(   sN   
        Return names of currently available XSLT processor libraries
        (   R   RA   R   (   R   (    (    s/   /srv/Zope/Products/ZopeXMLMethods/XSLTMethod.pyRB   ,  s    t	   processorc         C   s   t  i t |  i ƒ S(   sN   
        Obtain the object encapsulating the selected XSLT processor.
        (   R   t   itemR   R$   (   R   (    (    s/   /srv/Zope/Products/ZopeXMLMethods/XSLTMethod.pyRa   3  s    t   xslTransformerc         C   s   |  i  |  i d ƒ S(   sx   
        Obtain the Zope object holding the XSLT, or None if the name
        does not point to a valid object.
        N(   R;   R%   R   (   R   (    (    s/   /srv/Zope/Products/ZopeXMLMethods/XSLTMethod.pyRU   :  s    t	   transformc         O   s»   |  i  ƒ  } |  i ƒ  } t d | | ƒ } | i ƒ  } h  } | o | i ƒ  } n |  i |   }	 |  i ƒ  }
 t d |
 | ƒ } |
 i ƒ  } | i |  i ƒ | i	 | | | | |  |	 | ƒ S(   sM   
        Generate result using transformer and return it as a string
        s   XSL transformers
   XML source(
   Ra   RU   R   t   absolute_urlt   copyt   getXSLParametersRT   t   setDebugLevelR)   Rd   (   R   R7   t   argst   kwargsRa   t	   xslObjectt   xslContentst   xslURLt   kwt	   xslParamst	   xmlObjectt   xmlContentst   xmlURL(    (    s/   /srv/Zope/Products/ZopeXMLMethods/XSLTMethod.pyRd   B  s&    	t   setCachingOnc         C   s   d |  _  d S(   s\   
        same as changing the property.  For use in scripts or by the cache Manager
        R   N(   R(   (   R   R7   (    (    s/   /srv/Zope/Products/ZopeXMLMethods/XSLTMethod.pyRs   ]  s    t   setCachingOffc         C   s   d |  _  d S(   s\   
        same as changing the property.  For use in scripts or by the cache Manager
        t   offN(   R(   (   R   R7   (    (    s/   /srv/Zope/Products/ZopeXMLMethods/XSLTMethod.pyRt   d  s    t   isCachingOnc         C   s   |  i  d j S(   s5   
        Return true if caching is turned on
        R   (   R(   (   R   (    (    s/   /srv/Zope/Products/ZopeXMLMethods/XSLTMethod.pyRv   l  s    RQ   c         C   sF   |  i  | d ƒ } | d j o d | } t | ƒ ‚ n | |  _ d S(   sF   
        Change transformer to be used or ID of source object
        s   Invalid xslTransformerId %sN(   R;   R   R4   R%   (   R   R%   R@   R.   (    (    s/   /srv/Zope/Products/ZopeXMLMethods/XSLTMethod.pyRQ   w  s
    
c         C   s
   d d g S(   NR   Ru   (    (   R   (    (    s/   /srv/Zope/Products/ZopeXMLMethods/XSLTMethod.pyRG   ˆ  s    Rg   c   	      O   s½   t  |  t d ƒ } h  } | d j	 o” x‘ | D]… } t  |  | d ƒ } t | ƒ o | i ƒ  } n t | ƒ } |  i i | | ƒ } | | | <x" | i ƒ  D] } | | | | <q™ Wq, Wn | S(   sà  

        Return XSL Transformer parameters as a dictionary of strings
        in the form 'name:value' as would be passed to an XSLT engine
        like Saxon, 4suite, etc. The values are obtained by looking
        for a property in the current context called 'XSLparameters',
        which should be a list of strings. Each name on the list is
        looked up in the current context. If its value is a scalar,
        then the pair 'name:value' is returned. If the value is an
        object, then the pair 'name:url' is returned where url is the
        absolute URL of the object.  The key (name) is actually a
        tuple of two strings, the first of which is an optional
        namespace (we don't use this today).

        N(	   R   t   parametersPropertyNameR   t   callableRe   R5   R7   RZ   t   keys(	   R   Ri   Rj   t   parmst   resultt   pt   valuet   valt   arg(    (    s/   /srv/Zope/Products/ZopeXMLMethods/XSLTMethod.pyRg   Œ  s     
 t   __call__c         O   s°  d } |  i ƒ  oM |  i | ƒ o9 |  i ƒ  } | d j	 o | i | i d ƒ ƒ } q\ q` n | d j ov h  } | o | i ƒ  } n |  i | |  } |  i ƒ  } |  i ƒ  o* | d j	 o | i | i d ƒ | ƒ qã n |  i	 d j o |  i
 ƒ  i }	 n
 |  i	 }	 t i |	 ƒ }
 |
 d j o t i ƒ  }
 n | d j	 o | i d |  i ƒ n |
 i |  i |  i | d |  i ƒ} | d j o
 |  } n |
 i | | | | ƒ S(   sV   
        Render self by processing its content with the named XSLT stylesheet
        RS   R   s   Content-TypeR&   N(   R   Rv   RR   R   t   valueFromCacheRZ   Rf   Rd   t   saveToCacheR'   RT   t	   meta_typeR   t   getGeneratort   getDefaultGeneratort	   setHeaderR&   t   createObjectR!   R"   t	   getResult(   R   t   clientR7   R2   Ri   Rj   t	   rawResultR[   Rn   R'   t   gent   obj(    (    s/   /srv/Zope/Products/ZopeXMLMethods/XSLTMethod.pyR€   ¹  s6    $!	
c         C   s   |  i  d ƒ o
 |  ` n d S(   s¿   
        Repair this object.  This method is used for schema migration,
        when the class definition changes and existing instances of
        previous versions must be updated.
        t   _processorChooserN(   t   hasattrR   (   R   (    (    s/   /srv/Zope/Products/ZopeXMLMethods/XSLTMethod.pyt   repairë  s    (   s   ZopeXMLMethodss   edit.stxN(%   t   __name__t
   __module__t   __doc__Rƒ   R    t   __implements__R   t	   _securityR   t   _propertiest   manage_optionsRM   t   declareProtectedR
   R   t   globalsRK   R   R   RO   R   RR   R   t   declarePublicRB   Ra   RU   Rd   Rs   Rt   Rv   RQ   RG   t   declarePrivateRg   R€   R   (    (    (    s/   /srv/Zope/Products/ZopeXMLMethods/XSLTMethod.pyR=   Á   sT   	
	#					
			,2(&   R’   t
   __cvstag__t   __date__t   __version__t
   interfacesR    t   processors.interfacesR   t   processors.ProcessorRegistryR   R   R   R   R   R   R   R	   R
   t   GlobalsR   t   AcquisitionR   R   t   AccessControlR   t'   Products.PageTemplates.PageTemplateFileR   Rw   R   R*   R˜   t   manage_addXSLTMethodFormR   R:   R    RB   R=   t   InitializeClass(    (    (    s/   /srv/Zope/Products/ZopeXMLMethods/XSLTMethod.pyt   <module>   s@   4	5	!	ÿ 5