0
  C   ˆl–Ým_J•/”Šu@»pÜ ”Å£�þZDY¶åtCyçˆëÀþ¿„�8Â_ŠI|¥�+Wé3ž¯ÇÆ ?÷     #! /usr/bin/env python
'''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 "\n" 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 $
'''

_copyright = '''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
'''

import string
from xml.dom import Node
try:
    from xml.ns import XMLNS
except:
    class XMLNS:
        BASE = "http://www.w3.org/2000/xmlns/"
        XML = "http://www.w3.org/XML/1998/namespace"
try:
    import cStringIO
    StringIO = cStringIO
except ImportError:
    import StringIO

_attrs = lambda E: (E.attributes and E.attributes.values()) or []
_children = lambda E: E.childNodes or []
_IN_XML_NS = lambda n: n.name.startswith("xmlns")
_inclusive = lambda n: n.unsuppressedPrefixes == None


# Does a document/PI has lesser/greater document order than the
# first element?
_LesserElement, _Element, _GreaterElement = range(3)

def _sorter(n1,n2):
    '''_sorter(n1,n2) -> int
    Sorting predicate for non-NS attributes.'''

    i = cmp(n1.namespaceURI, n2.namespaceURI)
    if i: return i
    return cmp(n1.localName, n2.localName)


def _sorter_ns(n1,n2):
    '''_sorter_ns((n,v),(n,v)) -> int
    "(an empty namespace URI is lexicographically least)."'''

    if n1[0] == 'xmlns': return -1
    if n2[0] == 'xmlns': return 1
    return cmp(n1[0], n2[0])

def _utilized(n, node, other_attrs, unsuppressedPrefixes):
    '''_utilized(n, node, other_attrs, unsuppressedPrefixes) -> boolean
    Return true if that nodespace is utilized within the node'''
    if n.startswith('xmlns:'):
        n = n[6:]
    elif n.startswith('xmlns'):
        n = n[5:]
    if (n=="" and node.prefix in ["#default", None]) or \
        n == node.prefix or n in unsuppressedPrefixes: 
            return 1
    for attr in other_attrs:
        if n == attr.prefix: return 1
    # For exclusive need to look at attributes
    if unsuppressedPrefixes is not None:
        for attr in _attrs(node):
            if n == attr.prefix: return 1
            
    return 0


def _inclusiveNamespacePrefixes(node, context, unsuppressedPrefixes):
    '''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 Recommendation'''
    inclusive = []
    if node.prefix:
        usedPrefixes = ['xmlns:%s' %node.prefix]
    else:
        usedPrefixes = ['xmlns']

    for a in _attrs(node):
        if a.nodeName.startswith('xmlns') or not 