m
	Z>c           @   s!  d  Z  d d d !Z d d d !Z d d d !Z d k Z d k l Z d k Z d k	 Z	 d k
 Z
 d	 k l Z d
 Z d Z d f  d     YZ d f  d     YZ d f  d     YZ d f  d     YZ d f  d     YZ d k l Z e Z y e   Z e i e  Wn d Ge i Gd GHn Xd S(   s0  
$RCSfile: PyanaProcessor.py,v $

This class encapsulates an XSLT Processor for use by ZopeXMLMethods.
This is the Pyana version, including support for XSLT
parameters.  It does not yet include support for URN resolution.

Author: <a href="mailto:cstrong@arielpartners.com">Craeg Strong</a>
Release: 1.0
s	   $Name:  $i   is   $Date: 2003/06/16 05:47:53 $s   $Revision: 1.3 $i
   N(   s   aq_get(   s   IXSLTProcessort   URNnamespacest   XSLparameterst   PyanaProcessorc           B   sP   t  Z d  Z e Z d Z d   Z d   Z e h  e d  Z	 d   Z
 d   Z RS(   s   
    This class encapsulates an XSLT Processor for use by
    ZopeXMLMethods.  This is the Pyana version, including support for
    XSLT parameters.  It does not yet include support for URN
    resolution.
    s	   Pyana 0.6c         C   s   d |  _ t t _ t t _ d S(   s+   Initialize a new instance of PyanaProcessori    N(   t   selft
   debugLevelt   ProblemListenert   Pyanat   defaultProblemListenerFactoryt   ErrorHandlert   defaultErrorHandlerFactory(   R   (    (    t   processors/PyanaProcessor.pyt   __init__2   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   setDebugLevelB   s     c         C   sj  |  i d j o d G| GHn |  i d j o d 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 Wh  }
 | d j	 o |  i |  }
 n |  i d j o d G|
 GHn t i   } d | _ | i |  | i t |
 |   | i d	 | d
 |  } | 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.
        transformObject and REQUEST params may be used to acquire Zope
        content such as XSLT parameters and URN namespaces, if
        required.

        Catches any Pyana specific exceptions and raises an Exception

        i   s   params:s   xsltContents:s   xmlContents:u    t   utf8s   namespaces:i    t   sourcet   styleN(   R   R   t   paramst   xsltContentst   xmlContentst   typet   encodet
   xsltParamst   itemst   keyt   valuet   addParamt   namespaceMapt   transformObjectt   Nonet   retrieveNamespacesR   t   Transformert   tt   useValidationt   setStylesheetParamst   setEntityResolvert   URIResolvert   REQUESTt   transform2Stringt   result(   R   R   t   xmlURLR   t   xsltURLR   R   R%   R'   R   R   R   R    R   (    (    R
   t	   transformQ   s8     	 	c         C   s   d | | | <| 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.
        
        s   '%s'N(   R   t   paramMapt   name(   R   R+   R,   R   (    (    R
   R      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_getR   t   namespacesPropertyNameR   t   NIDsR'   t   nR   t   callablet   str(   R   R   R   R0   R/   R'   (    (    R
   R      s      (   t   __name__t
   __module__t   __doc__t   IXSLTProcessort   __implements__R,   R   R   R   R*   R   R   (    (    (    R
   R   '   s    		4	R   c           B   s   t  Z d  Z d   Z RS(   s   

    This class replaces the default class used by Pyana to report
    errors and XSLT messages that occur during the XSLT transformation
    process.

    c	         C   s   d k  }
 d } h  t i d <t i d <t i d <}	 |	 | | } | p | d j p | d j oJ | d j p | d j o | d | | | f } q | d | f } n t |   d S(	   s   
        
        The default error handler for Pyana prints out
        messages to stderr.  Throw an exception instead.
        
        Nt    s   Error: s	   Warning: s	   Message: is(    [systemID: "%s" (line: %s, column: %s)]s    [systemID: "%s"](   t   syst   messageR   t   eERRORt   eWARNINGt   eMESSAGEt   typeMapt   classificationt   msgt   urit   linet   offsett	   Exception(   R   t   whereR?   t
   sourceNodet	   styleNodeR@   RA   RB   RC   R>   R9   R:   (    (    R
   t   problem   s    	 	*!(   R3   R4   R5   RH   (    (    (    R
   R      s    R   c           B   s;   t  Z d  Z d   Z d   Z d   Z d   Z d   Z RS(   s   

    This class replaces the default class used by Pyana to report
    errors that occur during the parsing of XML files.

    c         C   sY   d | | i f } | i p | i p
 | i o$ | d | i | i | i f } n | S(   Ns   %s: %ss'   [systemID: "%s" (line: %s, column: %s)](   R,   t   eR:   t   systemIDt
   lineNumbert   columnNumber(   R   R,   RI   R:   (    (    R
   t   common   s    $c         C   s   |  i d |  GHd  S(   Nt   Warning(   R   RM   RI   (   R   RI   (    (    R
   t   warning   s    c         C   s   |  i d |  GHd  S(   Nt   Error(   R   RM   RI   (   R   RI   (    (    R
   t   error   s    c         C   s   t  |  i d |    d  S(   Nt
   FatalError(   RD   R   RM   RI   (   R   RI   (    (    R
   t
   fatalError   s    c         C   s   d  S(   N(    (   R   (    (    R
   t   resetErrors   s    (   R3   R4   R5   RM   RO   RQ   RS   RT   (    (    (    R
   R      s    				R$   c           B   s,   t  Z d   Z d   Z d   Z d   Z RS(   Nc         C   s   | |  _  | |  _ d S(   s   
        Remember the URN namespaces corresponding to Zope folders and
        the REQUEST context with which we want to load the resources

        N(   R   R   R%   t   req(   R   R   R%   (    (    R
   R      s     	c         C   s`   | } | 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   contextListt   contextR-   R   R%   t   contents(   R   RW   RY   R%   R[   R\   RX   R]   (    (    R
   t   acquireObjectContents  s      c   
      C   s   |  i |  o | | i d  } | i d  } | d } | d }	 |  i	 i
 | d  } | d j o d Sq t |  t d  j o, t i i | |	  } t i |  i   Sq |  i | |	 |  i  } | d j o d Sq t |  Sn d Sd S(   s8   Resolve the URN to a Zope object, or pass on the requests   urn:t   :i   i   R8   N(   R   t   isRecognizedURNt   sysidt   findRA   RZ   t   uriPartst   nidt   nssR   t   getR   RW   R   t   ost   patht   joint   resolvedURLt   urllibt   urlopent   readR^   RU   t   stt   StringInputStream(
   R   t   publicRa   Rc   RA   Rd   Rn   RW   Rj   Re   (    (    R
   t   resolveEntity  s"     

c         C   sv   | d j o d Sn | i d  d j o d Sn | | i d  } | i d  } | d d j o t |  d j S(	   s3   Return true if this uri is of a format we recognizeR8   i    s   urn:iR_   t   urni   N(   RA   Rb   RZ   Rc   t   len(   R   RA   Rc   (    (    R
   R`   ?  s     (   R3   R4   R   R^   Rq   R`   (    (    (    R
   R$      s   				"Ro   c           B   s   t  Z d   Z d   Z RS(   Nc         C   s   | i d  |  _  d  S(   Ns   utf-8(   t   stringR   R   (   R   Rt   (    (    R
   R   L  s    c         C   s   d k  l  } | |  i  S(   N(   s   StringIO(   t   StringIOR   Rt   (   R   Ru   (    (    R
   t
   makeStreamO  s    (   R3   R4   R   Rv   (    (    (    R
   Ro   J  s   	(   s   ProcessorRegistryt	   Processors)   not available for use with ZopeXMLMethods(   R5   t
   __cvstag__t   __date__t   __version__R   t   AcquisitionR-   t   os.pathRg   R9   Rk   t
   interfacesR6   R.   t   parametersPropertyNameR   R   R   R$   Ro   t   ProcessorRegistryt   klasst   proct   registerR,   (   R6   Ry   R   R$   R   R-   R   R~   Rk   Rx   Ro   R9   R   R   R   R.   Rz   Rg   R   (    (    R
   t   ?
   s*   	')O	