
    /$ZL@                         d Z dZddlZddlmZ 	 ddlmZ n#   G d d          ZY nxY w	 ddlZeZn# e	$ r ddlZY nw xY wd Z
d	 Zd
 Zd Z ed          \  ZZZd Zd Zd Zd Zd Z G d d          ZddZdS )a  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: 2006-03-30 23:47:16 +0000 (Thu, 30 Mar 2006) $ by $Author: boverhof $
a*  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
    N)Node)XMLNSc                       e Zd ZdZdZdS )r   zhttp://www.w3.org/2000/xmlns/$http://www.w3.org/XML/1998/namespaceN)__name__
__module____qualname__BASEXML     3/usr/lib/python3/dist-packages/pysimplesoap/c14n.pyr   r   7   s        .4r   r   c                 F    | j         r| j                                         pg S N)
attributesvaluesEs    r   <lambda>r   @   s     AL:Q\%8%8%:%:Ar r   c                     | j         pg S r   )
childNodesr   s    r   r   r   A   s    al(b r   c                 6    | j                             d          S )Nxmlns)name
startswithns    r   r   r   B   s    qv((11 r   c                     | j         d k    S r   )unsuppressedPrefixesr   s    r   r   r   C   s    q-5 r      c                 r    t          | j        |j                  }|r|S t          | j        |j                  S )zB_sorter(n1,n2) -> int
    Sorting predicate for non-NS attributes.)cmpnamespaceURI	localName)n1n2is      r   _sorterr(   J   s4     	BOR_--AN(r|R\***r   c                 r    | d         dk    rdS |d         dk    rdS t          | d         |d                   S )zY_sorter_ns((n,v),(n,v)) -> int
    "(an empty namespace URI is lexicographically least)."r   r      )r"   )r%   r&   s     r   
_sorter_nsr,   S   sB     
!u	!ur!uber   c                 .   |                      d          r| dd         } n|                      d          r
| dd         } | dk    r	|j        dv s| |j        k    s| |v rdS |D ]}| |j        k    r dS | t          |          D ]}| |j        k    r dS d	S )
z~_utilized(n, node, other_attrs, unsuppressedPrefixes) -> boolean
    Return true if that nodespace is utilized within the nodexmlns:   Nr       )z#defaultNr+   r   )r   prefix_attrs)r   nodeother_attrsr   attrs        r   	_utilizedr7   [   s     	||H abbE	
g		 abbE	2$+!333	T[A!5551 & &AAA'4LL 	* 	*DDK1r   c                    g }| j         rd| j         z  g}ndg}t          |           D ]A}|j                            d          s|j         s$|                    d|j         z             Bi }|D ]}|j        }||v r|                    |           #|                    d          r"|dd         |v r|                    |           Z|                    d          r"|dd         |v r|                    |           |j        |v r|                    |           |                    d          r
|j        ||<   ||fS )zhttp://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 Recommendationxmlns:%sr   r.   r/   Nr0   )r2   r3   nodeNamer   appendvalue)	r4   contextr   	inclusiveusedPrefixesaunused_namespace_dictr6   r   s	            r   _inclusiveNamespacePrefixesrB   o   s    I{ !"DK/0yD\\ 2 2:  ))CC8J01111 2 2M$$$T""""\\(## 	2!""1E(E(ET""""\\'"" 	2qu0D'D'DT""""]l**T""""\\(## 	2'+z!!$+++r   c                     | d u p|| v S r   r   )subsetr4   s     r   r   r      s    &D."BDFN r   c                       e Zd 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	fd
Zeeej        <   d	S )_implementationzImplementation class for C14N. This accompanies a node during it's
    processing and includes the parameters and processing state.c                 &   || _         |                    d          | _        |                    dd          | _        |                    d          | _        |                    dt
          j        t
          j        d          }|ddii i f| _        |j	        t          j        k    r|                     |           d
S |j	        t          j        k    rt          | _        t!          |           sFt#          ||                     |          | j                  \  }}|                     |||	           d
S |                     |          }|                     ||           d
S |j	        t          j        k    rd
S t+          t-          |                    )z"Create and run the implementation.rD   commentsr   r   nsdict)xmlr   rJ   r1   )unusedN)writegetrD   rH   r   r   r   r
   statenodeTyper   DOCUMENT_NODE_do_documentELEMENT_NODE_ElementdocumentOrder
_inclusiverB   _inherit_context_do_elementDOCUMENT_TYPE_NODE	TypeErrorstr)selfr4   rL   kwrI   	inheritedrK   s          r   __init__z_implementation.__init__   ss   
ffX&&z1--$&FF+A$B$B!59uz"K"KLL uRj"b1
=D...d#####]d///!)Dd## 2#>tTEZEZ[_E`E` $ 9$; $; 	&  y @@@@@ 11$77	  y11111]d555DCII&&&r   c                 z   t          t          t          |                    }g |j        }}|r|j        t
          j        k    rxt          t          t          |                    D ]7}|j        }||vr*|                    |           |                    |           8|j        }|r|j        t
          j        k    x|S )z_inherit_context(self, node) -> list
        Scan ancestors of attribute and namespace context.  Used only
        for single element node canonicalization, not for subset
        canonicalization.)	filter
_IN_XML_NSr3   
parentNoderO   r   rR   r$   r;   )r[   r4   xmlattrsr]   parentr@   r   s          r   rV   z _implementation._inherit_context   s     *fTll33 6	 	'D,===Jv77 ( (KH$$OOA&&&$$Q'''&F  	'D,=== r   c                    t           | _        |j        D ]}|j        t          j        k    r.t          | _        |                     |           t          | _        E|j        t          j	        k    r| 
                    |           p|j        t          j        k    r|                     |           |j        t          j        k    rt          t          |                    dS )z_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)_LesserElementrT   r   rO   r   rR   rS   rW   _GreaterElementPROCESSING_INSTRUCTION_NODE_do_piCOMMENT_NODE_do_commentrX   rY   rZ   )r[   r4   childs      r   rQ   z_implementation._do_document   s     ,_ 	, 	,E~!222%-"  '''%4""4#CCCE""""4#444  ''''4#:::E

+++	, 	,r   c                     t          | j        |          sdS t          j        |j        dd          }t          j        |dd          }t          j        |dd          }t          j        |dd	          }|r|                     |           dS dS )
z_do_text(self, node) -> None
        Process a text or CDATA node.  Render various special characters
        as their C14N entity representations.N&&amp;<&lt;>z&gt;z&#xD;)
_in_subsetrD   stringreplacedatarL   )r[   r4   ss      r   _do_textz_implementation._do_text   s     $+t,,4ffN49c733N1c6**N1c6**N1fg..djjmmmmmr   c                 <   t          | j        |          sdS | j        }| j        t          k    r |d            |d            ||j                   |j        }|r |d            ||            |d           | j        t          k    r |d           dS dS )z_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.
        N
z<? z?>)rt   rD   rL   rT   rg   r:   rw   rf   )r[   r4   Wrx   s       r   ri   z_implementation._do_pi   s     $+t,,4ffJ00!!D'''	$	$-I 	AcFFFAaDDD	$//4//r   c                    t          | j        |          sdS | j        re| j        }| j        t
          k    r |d            |d            ||j                    |d           | j        t          k    r |d           dS dS dS )z_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.
        Nr{   z<!--z-->)rt   rD   rH   rL   rT   rg   rw   rf   )r[   r4   r}   s      r   rk   z_implementation._do_comment  s     $+t,,4ff= 	=
A!_44aagggAfIIIAdiLLLAeHHH!^33QQtWWWWW	= 	= 43r   c                    | 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 )z;'_do_attr(self, node) -> None
        Process an attribute.r|   z="rn   ro   rp   rq   "z&quot;	z&#x9r{   z&#xArs   z&#xDN)rL   ru   rv   )r[   r   r<   r}   rx   s        r   _do_attrz_implementation._do_attr  s     J	#	!	$N5#w//N1c6**N1c8,,N1ff--N1ff--N1ff--	!	#r   Nc                    | j         d         | j         d                                         | j         d                                         }}}|}|| j         d                                         }|                                }t          |           }	i }
g }t          | j        |          }|t          |          z   D ]}|j        t          j        k    r|j	        }|dk    rd}|j
        ||<   nc|j        t          j        k    r$|	s|rt          | j        |          r
||
|j	        <   n*t          | j        |          r|                    |           |                    |
           | j        d}}|r7|j	        }|	sv|j        d|j        z  }nd}|                    |          sM|                    |          s8|                    |          st#          d	|d
|          ||         ||<   ||=  |d            ||           g }|                                D ]\  }}|dk    r5|t          j        dfv r%|                    d          t          j        ddfv r@|dv r|dv rI||f|                                vr8|	st)          |||| j                  r|                    ||f           |	s|||<   |                    t.                     |D ] \  }}|                     ||           |||<   !|	rt          | j        |j                  r(|                    |
                                           n'|                    |                                           |                    t8                     |D ]"}|                     |j	        |j                   # |d           | j         ||||fc}| _         t=          |          D ]"}t?          j         |j!                 | |           #|| _         |r |d|z             dS dS )zu_do_element(self, node, initial_other_attrs = [], unused = {}) -> None
        Process an element (and its children).r   r+      Nr    r.   r   r9   z*For exclusive c14n, unable to map prefix "z" in rp   r1   )z	xmlns:xmlrJ   )r   rr   z</%s>)"rN   copyrU   rt   rD   r3   r#   r   r
   r:   	nodeValuer   r;   updaterL   r2   has_keyRuntimeErroritemsrM   r7   r   sortr,   r   rb   extendr   r(   r<   	_childrenrF   handlersrO   )r[   r4   initial_other_attrsrK   	ns_parentns_rendered	xml_attrsns_unused_inheritedns_localr>   xml_attrs_localr5   	in_subsetr@   r   r}   r   r2   ns_to_rendervrN   cs                         r   rW   z_implementation._do_element%  s    
1tz!}1133TZ]5G5G5I5I !*;	 %>"&*Q-"4"4"6"6>>##t$$	 t{D11	$vd||3 	. 	.A~++J==g!k59,, 4 4
4;0J0J 423OAJ/t{A.. *&&q))) _---- *d4 >	=D 4;*'4FF$F"**622 48;K;KF;S;S 4.66v>> +*l"FFDD*+ + + (;6'BHV$+F3AcFFFAdGGG L~~'' 3 3! <<A5:r);$;$;OOG,,%*b$0HHH
 ,,,CCC
 a5 1 1 3 333  3Ia{DD]$^$^ 3$++QF3333& 312+A. j)))# ! !!a### A
  7
4;t G G 7""?#9#9#;#;<<<<""9#3#3#5#5666W%%%  3 3aj!'2222AcFFF !J;	K^(_tz4 	: 	:A$QZ0q9999
"7T>"""""""r   )r   r   r	   __doc__r   r^   rV   rQ   r   rP   ry   	TEXT_NODECDATA_SECTION_NODEri   rh   rk   rj   r   rW   rR   r   r   r   rF   rF      s       D D H' ' '8  *, , ,( $0HT 	 	 	  (HT^(0HT$%9 9 9" 28HT-.= = = #.HT  $ 794 t# t# t# t#j #.HTr   rF   c                     |rt          t          | |j        f|           dS t          j                    }t          t          | |j        f|           |                                S )a  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)applyrF   rL   StringIOgetvalue)r4   outputr\   rx   s       r   Canonicalizer     s_      ofl3R88888oag333zz||r   r   )r   
_copyrightru   xml.domr   xml.nsr   	cStringIOr   ImportErrorr3   r   ra   rU   rangerf   rS   rg   r(   r,   r7   rB   rt   rF   r   r   r   r   <module>r      s  # #J	
       555 5 5 5 5 5 5 5 5 5HH   OOOOO 
B	A((	11
55

 -2E!HH )/+ + +    (, , ,> CB
I. I. I. I. I. I. I. I.X     s    (3 	??