mò
;úCc           @   s¥  d  Z  d d d !Z d d d !Z d d d !Z d k l Z d k Z d k Z d k	 Z	 d k
 Z
 d k Z d	 k l Z l Z d
 k l Z l Z l Z l Z d k l Z l Z d k l Z d k l Z l Z d k l Z d k l Z d k  l! Z! l" Z" d k# l$ Z$ d k% l& Z& l' Z' l( Z( d Z) d Z* d Z+ d f  d „  ƒ  YZ, d e f d „  ƒ  YZ- d k. l. Z. e, Z/ y e/ ƒ  Z0 e. i1 e0 ƒ Wn d Ge/ i2 Gd GHn Xd S(   s2  
$RCSfile: FourSuiteProcessor.py,v $

This class encapsulates an XSLT Processor for use by ZopeXMLMethods.
This is the 4Suite 1.0 alpha version, including support for XSLT
parameters, URL/URN resolution, and OASIS Catalogs.

Author: <a href="mailto:cstrong@arielpartners.com">Craeg Strong</a>
Release: 1.0
s	   $Name:  $i   iþÿÿÿs   $Date: 2005/09/06 02:08:59 $s   $Revision: 1.4 $i
   (   s   StringION(   s   aq_gets   aq_chain(   s   NonvalidatingReaders   ValidatingReaders   Prints   PrettyPrint(   s   InputSources   InputSourceFactory(   s   Evaluate(   s   XPaths   ReaderException(   s   UriException(   s   SchemeRegistryResolver(   s	   Processors   XsltException(   s   Catalog(   s   IXSLTProcessors   IXPathProcessors   IDTDValidatort   URNnamespacest   XSLparameterst
   XMLcatalogt   FourSuiteProcessorc           B   sœ   t  Z d  Z e e e f Z d Z d „  Z d f  d „  ƒ  YZ	 d „  Z
 e e d „ Z e e d „ Z e h  e d „ Z d	 „  Z d
 „  Z d „  Z d „  Z RS(   s»   
    This class encapsulates an XSLT Processor for use by
    ZopeXMLMethods.  This is the 4Suite 1.0 alpha version, including
    support for XSLT parameters and URL/URN resolution.
    s   4Suite 1.0betac         C   s   d |  _ d S(   s/   Initialize a new instance of FourSuiteProcessori    N(   t   selft
   debugLevel(   R   (    (    t    processors/FourSuiteProcessor.pyt   __init__@   s     t   XPathExpressionc           B   s,   t  Z d  Z d „  Z d d „ Z d „  Z RS(   sE   
        This class encapsulates a compiled xpath expression
        c         C   s   | |  _  | |  _ d  S(   N(   t   exprR   R   (   R   R	   R   (    (    R   R   M   s    	i    c   
      C   sN  |  i d j o d GH| GHd GH| GHn t | ƒ t d ƒ j o | i d ƒ } n yÀ t t | ƒ | ƒ }	 t	 i
 |	 ƒ } t i i | ƒ } |  i i | ƒ } t | ƒ d j o d Sn t ƒ  } | o t | d | d d ƒn t | d | d d ƒ| i d ƒ | i ƒ  SWn3 t i t i f j
 o } t t | ƒ ƒ ‚ n Xd	 S(
   sŽ   
            Evaluates the XPath expression against the passed in
            xmlContents, returns the resulting XML as a string.
            i   s   xmlContents:t   xmlURLu    t   utf8i    t    t   asHtmlN(   R   R   t   xmlContentsR
   t   typet   encodet   InputSourcet   StringIOt   docSrct   NonvalidatingReadert   parset   domt   XPatht   Contextt   contextR	   t   evaluatet   resultt   lent   retvalt   prettyPrintt   PrettyPrintt   Printt   seekt   readt   RuntimeExceptiont   CompiletimeExceptiont   et	   Exceptiont   str(
   R   R   R
   R   R%   R   R   R   R   R   (    (    R   t   evaluateStringU   s.     		c         C   sÞ   |  i d j o d GH| GHd GH| GHn t | ƒ t d ƒ j o | i d ƒ } n yP t t | ƒ | ƒ } t	 i
 | ƒ } t i i | ƒ } |  i i | ƒ } | SWn3 t i t i f j
 o } t t | ƒ ƒ ‚ n Xd S(   s¤   
            Evaluates the XPath expression against the passed in
            xmlContents, returns the resulting XML as a list of DOM
            nodes
            i   s   xmlContents:R
   u    R   N(   R   R   R   R
   R   R   R   R   R   R   R   R   R   R   R   R	   R   R   R#   R$   R%   R&   R'   (   R   R   R
   R%   R   R   R   R   (    (    R   t   evaluateDOM|   s      	(   t   __name__t
   __module__t   __doc__R   R(   R)   (    (    (    R   R   H   s    	'c         C   s   | |  _ d S(   s‹   

        Set debug level from 0 to 3.
        0 = silent
        3 = extra verbose
        Debug messages go to Zope server log.

        N(   t   levelR   R   (   R   R-   (    (    R   t   setDebugLevelž   s     c         C   si  |  i d j o d GH| GHd GH| GHn t | ƒ t d ƒ j o | i d ƒ } n d }	 | d j	 o |  i	 | ƒ }	 n |  i d j o& |	 d j o	 d GHq° d G|	 i
 GHn h  } | d j	 o |  i | ƒ } n |  i d j o d G| GHn yG t | | ƒ } t t | ƒ | d	 | d
 |	 ƒ}
 t i |
 ƒ } d SWn( t i t f j
 o } t | ƒ Sn Xd S(   sm   
        Validate the passed in XML document against the DTD that it
        refers to in a DOCTYPE.
        i   s   xmlContents:R
   u    R   s   no XML catalog registereds   catalog:s   namespaces:t   resolvert   catalogs)   Document is valid with respect to its DTDN(   R   R   R   R
   R   R   t   NoneR0   t   methodObjectt   getXMLCatalogt   urit   namespaceMapt   retrieveNamespacest   URNResolvert   REQUESTt
   myResolverR   R   R   t   ValidatingReaderR   R   R   R#   t   ReaderExceptionR%   R'   (   R   R   R
   R2   R8   R%   R5   R   R9   R0   R   (    (    R   t   dtdValidationResultsString­   s:     		c         C   sc  |  i d j o d GH| GHd GH| GHn t | ƒ t d ƒ j o | i d ƒ } n d }	 | d j	 o |  i	 | ƒ }	 n |  i d j o& |	 d j o	 d GHq° d G|	 i
 GHn h  } | d j	 o |  i | ƒ } n |  i d j o d G| GHn yG t | | ƒ } t t | ƒ | d	 | d
 |	 ƒ}
 t i |
 ƒ } d SWn" t i t f j
 o } d Sn Xd S(   sx   
        Validate the passed in XML document against the
        DTD that it internally refers to in a DOCTYPE.
        i   s   xmlContents:R
   u    R   s   no XML catalog registereds   catalog:s   namespaces:R/   R0   i    N(   R   R   R   R
   R   R   R1   R0   R2   R3   R4   R5   R6   R7   R8   R9   R   R   R   R:   R   R   R   R#   R;   R%   (   R   R   R
   R2   R8   R%   R5   R   R9   R0   R   (    (    R   t   isValidForDTDÙ   s:     		c         C   s*  |  i d j o d G| GHn |  i d j o, d GH| GHd GH| GHd GH| GHd GH| GHn t | ƒ t d ƒ j o | i d ƒ } n t | ƒ t d ƒ j o | i d ƒ } n h  } x- | i
 ƒ  D] \ } } |  i | | | ƒ qÄ Wd } | d j	 o |  i | ƒ } n |  i d j o& | d j o	 d	 GHqCd
 G| i GHn h  } | d j	 o |  i | ƒ } n |  i d j o d G| GHn |  i d j oC d k l } | i ƒ  } |  i d j o d | _ d | _ qån t i ƒ  } y¬ t | | ƒ } t d | ƒ | _  t! t" | ƒ | d | d | ƒ} t! t" | ƒ | d | d | ƒ} | i% | ƒ | i& | d | ƒ}	 |  i d j o d GH|	 GHd GHn Wn’ t( j
 o } t* t+ | ƒ ƒ ‚ nm t, i- t, i. f j
 oT } t/ | d ƒ o) d | i0 t+ | ƒ f }
 t* |
 ƒ ‚ q&t* t+ | ƒ ƒ ‚ n X|	 S(   sÆ  

        Transforms the passed in XML into the required output (usually
        HTML) using the passed in XSLT.  Both the XML and XSLT strings
        should be well-formed.  Returns the output as a string.
        methodObject and REQUEST params may be used to acquire Zope
        content such as XSLT parameters and URN namespaces, if
        required.

        Catches any 4Suite specific exceptions thrown and raises an
        Exception.

        i   s   params:s   xsltContents:t   xsltURLs   xmlContents:R
   u    R   s   no XML catalog registereds   catalog:s   namespaces:(   s   ExtendedProcessingElementsi   R/   R0   t   topLevelParamss   ===Result===s   ============t   stylesheetUris   While processing %s
%sN(2   R   R   t   paramst   xsltContentsR>   R   R
   R   R   t
   xsltParamst   itemst   keyt   valuet   addParamR1   R0   R2   R3   R4   R5   R6   t   Ft.Xml.Xsltt   ExtendedProcessingElementst   ExtendedProcessort	   processort   _4xslt_debugt   _4xslt_tracet	   ProcessorR7   R8   R9   t   InputSourceFactoryt   inputSourceFactoryR   R   R   t   styleSrct   appendStylesheett   runR   t   XsltExceptionR%   R&   R'   R   R#   R$   t   hasattrR@   t   message(   R   R   R
   RB   R>   R2   RA   R8   RI   R   RV   RQ   R9   R0   RE   RC   R%   R5   RF   R   RK   (    (    R   t	   transform	  s|     	 		c         C   s   | | d | f <| S(   sÃ   

        This is a convenience function for adding parameters in the
        correct format to the parameter map to be used for the
        'params' parameter in transformGuts.
        
        N(   RF   t   paramMapR1   t   name(   R   RX   RY   RF   (    (    R   RG   j  s     c         C   s`   y& t  i | ƒ } |  i | |  i ƒ SWn3 t  i t  i f j
 o } t
 t | ƒ ƒ ‚ n Xd S(   sÅ   
        Compiles the passed in XPath expression.  Throws an Exception
        if there are any errors, otherwise returns the compiled
        expression as an instance of XPathExpression.
        N(   R   t   Compilet   xpathExpressionR	   R   R   R   R#   R$   R%   R&   R'   (   R   R[   R%   R	   (    (    R   t   compiley  s     c         C   s{   t  | t d ƒ } h  } | d j	 oR xO | D]C } t  | | d ƒ } t | ƒ o | | | <q, t	 | ƒ | | <q, Wn | S(   sC   

        retrieves Namespaces defined for URI Resolution

        N(
   t   aq_getR2   t   namespacesPropertyNameR1   t   NIDsR   t   nRF   t   callableR'   (   R   R2   RF   R`   R_   R   (    (    R   R6   ‹  s      c         C   sl   t  | t d ƒ } | d j	 oI t  | | ƒ } | d j	 o) t | i ƒ  d |  i	 d j ƒ} | Sqh n d S(   sü   

        Find the OASIS TR9401 and XML Input Resolver, if any.  They
        are registered by defining a property called 'XMLcatalog'
        somewhere in the acquisition path, pointing to a zope object
        whose contents is the catalog

        t   quieti    N(   R]   R2   t   catalogPropertyNameR1   t   catalogFileNamet   catalogObjectt   Catalogt   absolute_urlR   R   R0   (   R   R2   Rd   Re   R0   (    (    R   R3   ž  s     (   R*   R+   R,   t   IXSLTProcessort   IXPathProcessort   IDTDValidatort   __implements__RY   R   R   R.   R1   R<   R=   RW   RG   R\   R6   R3   (    (    (    R   R   3   s    	V	,0a			R7   c           B   s5   t  Z d  Z d „  Z d „  Z e d „ Z d „  Z RS(   s-   A resolver that resolves URNs to Zope objectsc         C   sC   t  i |  ƒ |  i |  i d <|  i i d ƒ | |  _ | |  _	 d S(   s”   
        Remember the URN namespaces corresponding to Zope folders and
        the REQUEST context with which we want to load the resources
        t   urnN(
   t   SchemeRegistryResolverR   R   t
   resolveURNt   handlerst   supportedSchemest   appendR5   R8   t   req(   R   R5   R8   (    (    R   R   ¸  s     	c         C   sZ   | } | i d ƒ } x5 | D]- } t | | d ƒ } | d j o d Sq q W| | | ƒ S(   s   

        Obtain the contents of the Zope object indicated by the passed
        in context, starting from the passed in base object.

        t   /N(	   t   baset   zObjectt
   contextURLt   splitt   contextListR   R]   R1   R8   (   R   Rt   Rv   R8   Rx   R   Ru   (    (    R   t   acquireObjectContentsÃ  s      c   	      C   s	  |  i | ƒ p t d | ƒ ‚ n | i d ƒ } | d } | d } |  i i	 | d ƒ } | d j o t d | ƒ ‚ n‰ t | ƒ t i j o) t i i | | ƒ } |  i | | ƒ SnJ |  i | | |  i ƒ } | d j o t d | ƒ ‚ n t | ƒ } | Sd S(   s(   Resolve the URN to a Zope object, or dies   Unable to resolve URI %st   :i   i   N(   R   t   isRecognizedURNR4   t   UriExceptionRw   t   uriPartst   nidt   nssR5   t   getR1   Rt   R   t   typest
   StringTypet   ost   patht   joint   resolvedURLt   resolveRy   Rr   t   stR   t   stream(	   R   R4   Rt   R}   R‰   R~   Rˆ   R†   R   (    (    R   Rn   Ù  s"     

c         C   s0   | i d ƒ } | d d j o t | ƒ d j S(   sE   
        Return true if this uri is of a format we recognize
        Rz   i    Rl   i   N(   R4   Rw   R}   R   (   R   R4   R}   (    (    R   R{   ø  s     (   R*   R+   R,   R   Ry   R1   Rn   R{   (    (    (    R   R7   µ  s
    		(   s   ProcessorRegistryRN   s)   not available for use with ZopeXMLMethods(3   R,   t
   __cvstag__t   __date__t   __version__t	   cStringIOR   t   os.pathRƒ   t   ret   sysR   t	   tracebackt   AcquisitionR]   t   aq_chaint   Ft.Xml.DomletteR   R:   R    R   t   Ft.Xml.InputSourceR   RO   t   Ft.Xml.XPatht   Evaluatet   Ft.XmlR   R;   t   Ft.LibR|   t   Ft.Lib.ResolversRm   RH   RN   RT   t   Ft.Xml.CatalogRf   t
   interfacesRh   Ri   Rj   R^   t   parametersPropertyNameRc   R   R7   t   ProcessorRegistryt   klasst   proct   registerRY   ($   R   R   Rc   R“   R   R    R]   RT   R^   R   Rm   R—   R:   R   R   Rf   Rž   RŒ   R    Rj   R;   Ri   R   RO   RN   R   Rh   R   R   R7   R‘   R‹   RŠ   RŸ   Rƒ   R|   (    (    R   t   ?
   s8   -ÿ ƒN	