ó
µôoQc           @   s  d  Z  d Z d d l Z d d l m Z y d d l m Z Wn d f  d „  ƒ  YZ n Xy d d l Z e Z Wn e	 k
 rŽ d d l Z n Xd „  Z
 d	 „  Z d
 „  Z d „  Z e d ƒ \ Z Z Z d „  Z d „  Z d „  Z d „  Z d „  Z d f  d „  ƒ  YZ e d „ Z d S(   sL  XML Canonicalization

Patches Applied to xml.dom.ext.c14n:
    http://sourceforge.net/projects/pyxml/

    [ 1444526 ] c14n.py: http://www.w3.org/TR/xml-exc-c14n/ fix
        -- includes [ 829905 ] c14n.py fix for bug #825115,
           Date Submitted: 2003-10-24 23:43
        -- include dependent namespace declarations declared in ancestor nodes
           (checking attributes and tags),
        -- handle InclusiveNamespaces PrefixList parameter

This module generates canonical XML of a document or element.
    http://www.w3.org/TR/2001/REC-xml-c14n-20010315
and includes a prototype of exclusive canonicalization
    http://www.w3.org/Signature/Drafts/xml-exc-c14n

Requires PyXML 0.7.0 or later.

Known issues if using Ft.Lib.pDomlette:
    1. Unicode
    2. does not white space normalize attributes of type NMTOKEN and ID?
    3. seems to be include "
" after importing external entities?

Note, this version processes a DOM tree, and consequently it processes
namespace nodes as attributes, not from a node's namespace axis. This
permits simple document and element canonicalization without
XPath. When XPath is used, the XPath result node list is passed and used to
determine if the node is in the XPath result list, but little else.

Authors:
    "Joseph M. Reagle Jr." <reagle@w3.org>
    "Rich Salz" <rsalz@zolera.com>

$Date$ by $Author$
s*  Copyright 2001, Zolera Systems Inc.  All Rights Reserved.
Copyright 2001, MIT. All Rights Reserved.

Distributed under the terms of:
  Python 2.0 License or later.
  http://www.python.org/2.0.1/license.html
or
  W3C Software License
  http://www.w3.org/Consortium/Legal/copyright-software-19980720
iÿÿÿÿN(   t   Node(   t   XMLNSR   c           B   s   e  Z d  Z d Z RS(   s   http://www.w3.org/2000/xmlns/s$   http://www.w3.org/XML/1998/namespace(   t   __name__t
   __module__t   BASEt   XML(    (    (    s0   /usr/lib/python2.7/dist-packages/wstools/c14n.pyR   7   s   c         C   s   |  j  r |  j  j ƒ  p g  S(   N(   t
   attributest   values(   t   E(    (    s0   /usr/lib/python2.7/dist-packages/wstools/c14n.pyt   <lambda>@   s    c         C   s   |  j  p g  S(   N(   t
   childNodes(   R   (    (    s0   /usr/lib/python2.7/dist-packages/wstools/c14n.pyR	   A   s    c         C   s   |  j  j d ƒ S(   Nt   xmlns(   t   namet
   startswith(   t   n(    (    s0   /usr/lib/python2.7/dist-packages/wstools/c14n.pyR	   B   s    c         C   s   |  j  d  k S(   N(   t   unsuppressedPrefixest   None(   R   (    (    s0   /usr/lib/python2.7/dist-packages/wstools/c14n.pyR	   C   s    i   c         C   s2   t  |  j | j ƒ } | r | St  |  j | j ƒ S(   sB   _sorter(n1,n2) -> int
    Sorting predicate for non-NS attributes.(   t   cmpt   namespaceURIt	   localName(   t   n1t   n2t   i(    (    s0   /usr/lib/python2.7/dist-packages/wstools/c14n.pyt   _sorterK   s    c         C   s=   |  d d k r d S| d d k r( d St  |  d | d ƒ S(   sY   _sorter_ns((n,v),(n,v)) -> int
    "(an empty namespace URI is lexicographically least)."i    R   iÿÿÿÿi   (   R   (   R   R   (    (    s0   /usr/lib/python2.7/dist-packages/wstools/c14n.pyt
   _sorter_nsU   s
    c         C   sÓ   |  j  d ƒ r |  d }  n |  j  d ƒ r8 |  d }  n  |  d k rS | j d
 k sn |  | j k sn |  | k rr d Sx! | D] } |  | j k ry d Sqy W| d	 k	 rÏ x* t | ƒ D] } |  | j k r¯ d Sq¯ Wn  d S(   s~   _utilized(n, node, other_attrs, unsuppressedPrefixes) -> boolean
    Return true if that nodespace is utilized within the nodes   xmlns:i   R   i   t    s   #defaulti   i    N(   s   #defaultN(   R   t   prefixR   t   _attrs(   R   t   nodet   other_attrsR   t   attr(    (    s0   /usr/lib/python2.7/dist-packages/wstools/c14n.pyt	   _utilized`   s    c   	      C   sZ  g  } |  j  r" d |  j  g } n	 d g } xJ t |  ƒ D]< } | j j d ƒ s8 | j  r` q8 n  | j d | j  ƒ q8 Wi  } xÏ | D]Ç } | j } | | k r° | j | ƒ q… | j d ƒ rß | d | k rß | j | ƒ q… | j d ƒ r| d | k r| j | ƒ q… | j | k r-| j | ƒ q… | j d ƒ r… | j | | <q… q… W| | f S(   sÀ   http://www.w3.org/TR/xml-exc-c14n/
    InclusiveNamespaces PrefixList parameter, which lists namespace prefixes that
    are handled in the manner described by the Canonical XML Recommendations   xmlns:%sR   s   xmlns:i   i   (   R   R   t   nodeNameR   t   appendt   value(	   R   t   contextR   t	   inclusivet   usedPrefixest   at   unused_namespace_dictR   R   (    (    s0   /usr/lib/python2.7/dist-packages/wstools/c14n.pyt   _inclusiveNamespacePrefixesv   s,    			c         C   s   |  d  k p | |  k S(   N(   R   (   t   subsetR   (    (    s0   /usr/lib/python2.7/dist-packages/wstools/c14n.pyR	   –   s    t   _implementationc           B   s°   e  Z d  Z i  Z d „  Z d „  Z d „  Z e e e j <d „  Z	 e	 e e j
 <e	 e e j <d „  Z e e e j <d „  Z e e e j <d „  Z g  d	 d „ Z e e e j <RS(
   sƒ   Implementation class for C14N. This accompanies a node during it's
    processing and includes the parameters and processing state.c         K   sW  | |  _  | j d ƒ |  _ | j d d ƒ |  _ | j d ƒ |  _ | j d i t j d 6t j d 6ƒ } | i d d 6i  i  f |  _ | j	 t
 j k r¦ |  j | ƒ n­ | j	 t
 j k r,t |  _ t |  ƒ s
t | |  j | ƒ |  j ƒ \ } } |  j | | d	 | ƒqS|  j | ƒ } |  j | | ƒ n' | j	 t
 j k rAn t t | ƒ ƒ ‚ d
 S(   s"   Create and run the implementation.R)   t   commentsi    R   t   nsdictt   xmlR   R   t   unusedN(   t   writet   getR)   R+   R   R   R   R   t   statet   nodeTypeR    t   DOCUMENT_NODEt   _do_documentt   ELEMENT_NODEt   _Elementt   documentOrdert
   _inclusiveR(   t   _inherit_contextt   _do_elementt   DOCUMENT_TYPE_NODEt	   TypeErrort   str(   t   selfR   R/   t   kwR,   t	   inheritedR.   (    (    s0   /usr/lib/python2.7/dist-packages/wstools/c14n.pyt   __init__    s&    	&	c         C   s£   t  t t | ƒ ƒ } g  | j } } xw | rž | j t j k rž xO t  t t | ƒ ƒ D]8 } | j } | | k rV | j | ƒ | j | ƒ qV qV W| j } q( W| S(   sÅ   _inherit_context(self, node) -> list
        Scan ancestors of attribute and namespace context.  Used only
        for single element node canonicalization, not for subset
        canonicalization.(	   t   filtert
   _IN_XML_NSR   t
   parentNodeR2   R    R5   R   R!   (   R>   R   t   xmlattrsR@   t   parentR&   R   (    (    s0   /usr/lib/python2.7/dist-packages/wstools/c14n.pyR9   »   s    	c         C   sÀ   t  |  _ x° | j D]¥ } | j t j k rM t |  _ |  j | ƒ t |  _ q | j t j	 k ro |  j
 | ƒ q | j t j k r‘ |  j | ƒ q | j t j k r¦ q t t | ƒ ƒ ‚ q Wd S(   sË   _do_document(self, node) -> None
        Process a document node. documentOrder holds whether the document
        element has been encountered such that PIs/comments can be written
        as specified.N(   t   _LesserElementR7   R
   R2   R    R5   R6   R:   t   _GreaterElementt   PROCESSING_INSTRUCTION_NODEt   _do_pit   COMMENT_NODEt   _do_commentR;   R<   R=   (   R>   R   t   child(    (    s0   /usr/lib/python2.7/dist-packages/wstools/c14n.pyR4   Ï   s    		c         C   s‡   t  |  j | ƒ s d St j | j d d ƒ } t j | d d ƒ } t j | d d ƒ } t j | d d	 ƒ } | rƒ |  j | ƒ n  d S(
   s“   _do_text(self, node) -> None
        Process a text or CDATA node.  Render various special characters
        as their C14N entity representations.Nt   &s   &amp;t   <s   &lt;t   >s   &gt;s   s   &#xD;(   t
   _in_subsetR)   t   stringt   replacet   dataR/   (   R>   R   t   s(    (    s0   /usr/lib/python2.7/dist-packages/wstools/c14n.pyt   _do_textå   s    c         C   s¢   t  |  j | ƒ s d S|  j } |  j t k r; | d ƒ n  | d ƒ | | j ƒ | j } | rx | d ƒ | | ƒ n  | d ƒ |  j t k rž | d ƒ n  d S(   sÎ   _do_pi(self, node) -> None
        Process a PI node. Render a leading or trailing #xA if the
        document order of the PI is greater or lesser (respectively)
        than the document element.
        Ns   
s   <?t    s   ?>(   RQ   R)   R/   R7   RH   R    RT   RG   (   R>   R   t   WRU   (    (    s0   /usr/lib/python2.7/dist-packages/wstools/c14n.pyRJ   ô   s    	
	

c         C   sˆ   t  |  j | ƒ s d S|  j r„ |  j } |  j t k rD | d ƒ n  | d ƒ | | j ƒ | d ƒ |  j t k r„ | d ƒ q„ n  d S(   sÝ   _do_comment(self, node) -> None
        Process a comment node. Render a leading or trailing #xA if the
        document order of the comment is greater or lesser (respectively)
        than the document element.
        Ns   
s   <!--s   -->(   RQ   R)   R+   R/   R7   RH   RT   RG   (   R>   R   RX   (    (    s0   /usr/lib/python2.7/dist-packages/wstools/c14n.pyRL   
  s    		

c         C   s½   |  j  } | d ƒ | | ƒ | d ƒ t j | d d ƒ } t j | d d ƒ } t j | d d ƒ } t j | d	 d
 ƒ } t j | d d ƒ } t j | d d ƒ } | | ƒ | d ƒ d S(   s;   '_do_attr(self, node) -> None
        Process an attribute.RW   s   ="RN   s   &amp;RO   s   &lt;t   "s   &quot;s   	s   &#x9s   
s   &#xAs   s   &#xDN(   R/   RR   RS   (   R>   R   R"   RX   RU   (    (    s0   /usr/lib/python2.7/dist-packages/wstools/c14n.pyt   _do_attr  s    	



c         C   s6  |  j  d |  j  d j ƒ  |  j  d j ƒ  } } } | } | d k r] |  j  d j ƒ  } n  | j ƒ  } t |  ƒ }	 i  }
 g  } t |  j | ƒ } xÊ | t | ƒ D]¸ } | j t j	 k rê | j
 } | d k rÚ d } n  | j | | <ne | j t j k r-|	 s| rOt |  j | ƒ rO| |
 | j
 <qOn" t |  j | ƒ rO| j | ƒ n  | j |
 ƒ q¤ W|  j d } } | rÅ| j
 } |	 s| j d k	 r¤d | j } n d } | | k r| | k r| | k rçt d | | f ƒ ‚ n  | | | | <| | =qn  | d	 ƒ | | ƒ g  } xâ | j ƒ  D]Ô \ } } | d k r}| t j	 d
 g k r}| j d ƒ t j	 d
 d g k r}q)n  | d k r›| d k r›q)n  | | f | j ƒ  k r)|	 sÑt | | | |  j ƒ rç| j | | f ƒ qý|	 sý| | | <qýq)q)W| j t ƒ x. | D]& \ } } |  j | | ƒ | | | <qW|	 s[t |  j | j ƒ rq| j |
 j ƒ  ƒ n | j | j ƒ  ƒ | j t ƒ x$ | D] } |  j | j
 | j ƒ q˜W| d ƒ n  |  j  | | | | f } |  _  x+ t | ƒ D] } t j  | j! |  | ƒ qñW| |  _  | r2| d | ƒ n  d S(   su   _do_element(self, node, initial_other_attrs = [], unused = {}) -> None
        Process an element (and its children).i    i   i   i   s   xmlns:R   s   xmlns:%ss3   For exclusive c14n, unable to map prefix "%s" in %sRO   R   s	   xmlns:xmlR-   s$   http://www.w3.org/XML/1998/namespaceRP   s   </%s>N(   s	   xmlns:xmls   xml(   s$   http://www.w3.org/XML/1998/namespace("   R1   t   copyR   R8   RQ   R)   R   R   R   R   R    t	   nodeValueR   R!   t   updateR/   R   t   RuntimeErrort   itemsR0   R   R   t   sortR   RZ   RD   t   extendR   R   R"   t	   _childrenR*   t   handlersR2   (   R>   R   t   initial_other_attrsR.   t	   ns_parentt   ns_renderedt	   xml_attrst   ns_unused_inheritedt   ns_localR$   t   xml_attrs_localR   t	   in_subsetR&   R   RX   R   R   t   ns_to_rendert   vR1   t   c(    (    s0   /usr/lib/python2.7/dist-packages/wstools/c14n.pyR:   .  s„    5			

!!	N(   R   R   t   __doc__Rc   RA   R9   R4   R    R3   RV   t	   TEXT_NODEt   CDATA_SECTION_NODERJ   RI   RL   RK   RZ   R   R:   R5   (    (    (    s0   /usr/lib/python2.7/dist-packages/wstools/c14n.pyR*   ™   s    							uc         K   sU   | r" t  t |  | j f | ƒ n/ t j ƒ  } t  t |  | j f | ƒ | j ƒ  Sd S(   s™  Canonicalize(node, output=None, **kw) -> UTF-8

    Canonicalize a DOM document/element node and all descendents.
    Return the text; if output is specified then output.write will
    be called to output the text and None will be returned
    Keyword parameters:
        nsdict: a dictionary of prefix:uri namespace entries
                assumed to exist in the surrounding context
        comments: keep comments if non-zero (default is 0)
        subset: Canonical XML subsetting resulting from XPath
                (default is [])
        unsuppressedPrefixes: do exclusive C14N, and this specifies the
                prefixes that should be inherited.
    N(   t   applyR*   R/   t   StringIOt   getvalue(   R   t   outputR?   RU   (    (    s0   /usr/lib/python2.7/dist-packages/wstools/c14n.pyt   Canonicalize¦  s
    (   Ro   t
   _copyrightRR   t   xml.domR    t   xml.nsR   t	   cStringIORs   t   ImportErrorR   Rb   RC   R8   t   rangeRG   R6   RH   R   R   R   R(   RQ   R*   R   Rv   (    (    (    s0   /usr/lib/python2.7/dist-packages/wstools/c14n.pyt   <module>%   s2   
					
			 	ÿ 