home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2012 January / maximum-cd-2012-01.iso / DiscContents / digsby_setup.exe / lib / ZSI / wstools / c14n.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-10-05  |  9.4 KB  |  346 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. _copyright = 'Copyright 2001, Zolera Systems Inc.  All Rights Reserved.\nCopyright 2001, MIT. All Rights Reserved.\n\nDistributed under the terms of:\n  Python 2.0 License or later.\n  http://www.python.org/2.0.1/license.html\nor\n  W3C Software License\n  http://www.w3.org/Consortium/Legal/copyright-software-19980720\n'
  5. import string
  6. from xml.dom import Node
  7.  
  8. try:
  9.     from xml.ns import XMLNS
  10. except:
  11.     
  12.     class XMLNS:
  13.         BASE = 'http://www.w3.org/2000/xmlns/'
  14.         XML = 'http://www.w3.org/XML/1998/namespace'
  15.  
  16.  
  17.  
  18. try:
  19.     import cStringIO
  20.     StringIO = cStringIO
  21. except ImportError:
  22.     import StringIO
  23.  
  24.  
  25. _attrs = lambda E: if not E.attributes or E.attributes.values():
  26. pass[]
  27.  
  28. _children = lambda E: if not E.childNodes:
  29. pass[]
  30.  
  31. _IN_XML_NS = lambda n: n.name.startswith('xmlns')
  32.  
  33. _inclusive = lambda n: n.unsuppressedPrefixes == None
  34. (_LesserElement, _Element, _GreaterElement) = range(3)
  35.  
  36. def _sorter(n1, n2):
  37.     i = cmp(n1.namespaceURI, n2.namespaceURI)
  38.     if i:
  39.         return i
  40.     return cmp(n1.localName, n2.localName)
  41.  
  42.  
  43. def _sorter_ns(n1, n2):
  44.     if n1[0] == 'xmlns':
  45.         return -1
  46.     if n2[0] == 'xmlns':
  47.         return 1
  48.     return cmp(n1[0], n2[0])
  49.  
  50.  
  51. def _utilized(n, node, other_attrs, unsuppressedPrefixes):
  52.     if n.startswith('xmlns:'):
  53.         n = n[6:]
  54.     elif n.startswith('xmlns'):
  55.         n = n[5:]
  56.     
  57.     if (n == '' or node.prefix in ('#default', None)) and n == node.prefix or n in unsuppressedPrefixes:
  58.         return 1
  59.     for attr in other_attrs:
  60.         if n == attr.prefix:
  61.             return 1
  62.     
  63.     return 0
  64.  
  65.  
  66. def _inclusiveNamespacePrefixes(node, context, unsuppressedPrefixes):
  67.     inclusive = []
  68.     if node.prefix:
  69.         usedPrefixes = [
  70.             'xmlns:%s' % node.prefix]
  71.     else:
  72.         usedPrefixes = [
  73.             'xmlns']
  74.     for a in _attrs(node):
  75.         if a.nodeName.startswith('xmlns') or not (a.prefix):
  76.             continue
  77.         
  78.         usedPrefixes.append('xmlns:%s' % a.prefix)
  79.     
  80.     unused_namespace_dict = { }
  81.     for attr in context:
  82.         n = attr.nodeName
  83.         if n in unsuppressedPrefixes:
  84.             inclusive.append(attr)
  85.             continue
  86.         if n.startswith('xmlns:') and n[6:] in unsuppressedPrefixes:
  87.             inclusive.append(attr)
  88.             continue
  89.         if n.startswith('xmlns') and n[5:] in unsuppressedPrefixes:
  90.             inclusive.append(attr)
  91.             continue
  92.         if attr.nodeName in usedPrefixes:
  93.             inclusive.append(attr)
  94.             continue
  95.         if n.startswith('xmlns:'):
  96.             unused_namespace_dict[n] = attr.value
  97.             continue
  98.     
  99.     return (inclusive, unused_namespace_dict)
  100.  
  101.  
  102. _in_subset = lambda subset, node: if not subset is None:
  103. passnode in subset
  104.  
  105. class _implementation:
  106.     handlers = { }
  107.     
  108.     def __init__(self, node, write, **kw):
  109.         self.write = write
  110.         self.subset = kw.get('subset')
  111.         self.comments = kw.get('comments', 0)
  112.         self.unsuppressedPrefixes = kw.get('unsuppressedPrefixes')
  113.         nsdict = kw.get('nsdict', {
  114.             'xml': XMLNS.XML,
  115.             'xmlns': XMLNS.BASE })
  116.         self.state = (nsdict, {
  117.             'xml': '' }, { }, { })
  118.         if node.nodeType == Node.DOCUMENT_NODE:
  119.             self._do_document(node)
  120.         elif node.nodeType == Node.ELEMENT_NODE:
  121.             self.documentOrder = _Element
  122.             if not _inclusive(self):
  123.                 (inherited, unused) = _inclusiveNamespacePrefixes(node, self._inherit_context(node), self.unsuppressedPrefixes)
  124.                 self._do_element(node, inherited, unused = unused)
  125.             else:
  126.                 inherited = self._inherit_context(node)
  127.                 self._do_element(node, inherited)
  128.         elif node.nodeType == Node.DOCUMENT_TYPE_NODE:
  129.             pass
  130.         else:
  131.             raise TypeError, str(node)
  132.         return node.nodeType == Node.DOCUMENT_NODE
  133.  
  134.     
  135.     def _inherit_context(self, node):
  136.         xmlattrs = filter(_IN_XML_NS, _attrs(node))
  137.         inherited = []
  138.         parent = node.parentNode
  139.         while parent and parent.nodeType == Node.ELEMENT_NODE:
  140.             for a in filter(_IN_XML_NS, _attrs(parent)):
  141.                 n = a.localName
  142.                 if n not in xmlattrs:
  143.                     xmlattrs.append(n)
  144.                     inherited.append(a)
  145.                     continue
  146.             
  147.             parent = parent.parentNode
  148.         return inherited
  149.  
  150.     
  151.     def _do_document(self, node):
  152.         self.documentOrder = _LesserElement
  153.         for child in node.childNodes:
  154.             if child.nodeType == Node.ELEMENT_NODE:
  155.                 self.documentOrder = _Element
  156.                 self._do_element(child)
  157.                 self.documentOrder = _GreaterElement
  158.                 continue
  159.             if child.nodeType == Node.PROCESSING_INSTRUCTION_NODE:
  160.                 self._do_pi(child)
  161.                 continue
  162.             if child.nodeType == Node.COMMENT_NODE:
  163.                 self._do_comment(child)
  164.                 continue
  165.             if child.nodeType == Node.DOCUMENT_TYPE_NODE:
  166.                 continue
  167.             raise TypeError, str(child)
  168.         
  169.  
  170.     handlers[Node.DOCUMENT_NODE] = _do_document
  171.     
  172.     def _do_text(self, node):
  173.         if not _in_subset(self.subset, node):
  174.             return None
  175.         s = string.replace(node.data, '&', '&')
  176.         s = string.replace(s, '<', '<')
  177.         s = string.replace(s, '>', '>')
  178.         s = string.replace(s, '\r', ' ')
  179.         if s:
  180.             self.write(s)
  181.         
  182.  
  183.     handlers[Node.TEXT_NODE] = _do_text
  184.     handlers[Node.CDATA_SECTION_NODE] = _do_text
  185.     
  186.     def _do_pi(self, node):
  187.         if not _in_subset(self.subset, node):
  188.             return None
  189.         W = self.write
  190.         if self.documentOrder == _GreaterElement:
  191.             W('\n')
  192.         
  193.         W('<?')
  194.         W(node.nodeName)
  195.         s = node.data
  196.         if s:
  197.             W(' ')
  198.             W(s)
  199.         
  200.         W('?>')
  201.         if self.documentOrder == _LesserElement:
  202.             W('\n')
  203.         
  204.  
  205.     handlers[Node.PROCESSING_INSTRUCTION_NODE] = _do_pi
  206.     
  207.     def _do_comment(self, node):
  208.         if not _in_subset(self.subset, node):
  209.             return None
  210.         if self.comments:
  211.             W = self.write
  212.             if self.documentOrder == _GreaterElement:
  213.                 W('\n')
  214.             
  215.             W('<!--')
  216.             W(node.data)
  217.             W('-->')
  218.             if self.documentOrder == _LesserElement:
  219.                 W('\n')
  220.             
  221.         
  222.  
  223.     handlers[Node.COMMENT_NODE] = _do_comment
  224.     
  225.     def _do_attr(self, n, value):
  226.         W = self.write
  227.         W(' ')
  228.         W(n)
  229.         W('="')
  230.         s = string.replace(value, '&', '&')
  231.         s = string.replace(s, '<', '<')
  232.         s = string.replace(s, '"', '"')
  233.         s = string.replace(s, '\t', ' ')
  234.         s = string.replace(s, '\n', ' ')
  235.         s = string.replace(s, '\r', ' ')
  236.         W(s)
  237.         W('"')
  238.  
  239.     
  240.     def _do_element(self, node, initial_other_attrs = [], unused = None):
  241.         ns_parent = self.state[0]
  242.         ns_rendered = self.state[1].copy()
  243.         xml_attrs = self.state[2].copy()
  244.         ns_unused_inherited = unused
  245.         if unused is None:
  246.             ns_unused_inherited = self.state[3].copy()
  247.         
  248.         ns_local = ns_parent.copy()
  249.         inclusive = _inclusive(self)
  250.         xml_attrs_local = { }
  251.         other_attrs = []
  252.         in_subset = _in_subset(self.subset, node)
  253.         for a in initial_other_attrs + _attrs(node):
  254.             if a.namespaceURI == XMLNS.BASE:
  255.                 n = a.nodeName
  256.                 if n == 'xmlns:':
  257.                     n = 'xmlns'
  258.                 
  259.                 ns_local[n] = a.nodeValue
  260.             elif a.namespaceURI == XMLNS.XML:
  261.                 if (inclusive or in_subset) and _in_subset(self.subset, a):
  262.                     xml_attrs_local[a.nodeName] = a
  263.                 
  264.             elif _in_subset(self.subset, a):
  265.                 other_attrs.append(a)
  266.             
  267.             xml_attrs.update(xml_attrs_local)
  268.         
  269.         W = self.write
  270.         name = None
  271.         if in_subset:
  272.             name = node.nodeName
  273.             if not inclusive:
  274.                 if node.prefix is not None:
  275.                     prefix = 'xmlns:%s' % node.prefix
  276.                 else:
  277.                     prefix = 'xmlns'
  278.                 if not ns_rendered.has_key(prefix) and not ns_local.has_key(prefix):
  279.                     if not ns_unused_inherited.has_key(prefix):
  280.                         raise RuntimeError, 'For exclusive c14n, unable to map prefix "%s" in %s' % (prefix, node)
  281.                     ns_unused_inherited.has_key(prefix)
  282.                     ns_local[prefix] = ns_unused_inherited[prefix]
  283.                     del ns_unused_inherited[prefix]
  284.                 
  285.             
  286.             W('<')
  287.             W(name)
  288.             ns_to_render = []
  289.             for n, v in ns_local.items():
  290.                 if n == 'xmlns' and v in [
  291.                     XMLNS.BASE,
  292.                     ''] and ns_rendered.get('xmlns') in [
  293.                     XMLNS.BASE,
  294.                     '',
  295.                     None]:
  296.                     continue
  297.                 
  298.                 if n in ('xmlns:xml', 'xml') and v in ('http://www.w3.org/XML/1998/namespace',):
  299.                     continue
  300.                 
  301.                 if (n, v) not in ns_rendered.items():
  302.                     if inclusive or _utilized(n, node, other_attrs, self.unsuppressedPrefixes):
  303.                         ns_to_render.append((n, v))
  304.                     elif not inclusive:
  305.                         ns_unused_inherited[n] = v
  306.                     
  307.                 _utilized(n, node, other_attrs, self.unsuppressedPrefixes)
  308.             
  309.             ns_to_render.sort(_sorter_ns)
  310.             for n, v in ns_to_render:
  311.                 self._do_attr(n, v)
  312.                 ns_rendered[n] = v
  313.             
  314.             if not inclusive or _in_subset(self.subset, node.parentNode):
  315.                 other_attrs.extend(xml_attrs_local.values())
  316.             else:
  317.                 other_attrs.extend(xml_attrs.values())
  318.             other_attrs.sort(_sorter)
  319.             for a in other_attrs:
  320.                 self._do_attr(a.nodeName, a.value)
  321.             
  322.             W('>')
  323.         
  324.         state = self.state
  325.         self.state = (ns_local, ns_rendered, xml_attrs, ns_unused_inherited)
  326.         for c in _children(node):
  327.             _implementation.handlers[c.nodeType](self, c)
  328.         
  329.         self.state = state
  330.         if name:
  331.             W('</%s>' % name)
  332.         
  333.  
  334.     handlers[Node.ELEMENT_NODE] = _do_element
  335.  
  336.  
  337. def Canonicalize(node, output = None, **kw):
  338.     if output:
  339.         apply(_implementation, (node, output.write), kw)
  340.     else:
  341.         s = StringIO.StringIO()
  342.         apply(_implementation, (node, s.write), kw)
  343.         return s.getvalue()
  344.     return output
  345.  
  346.