You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

537 lines
21 KiB

  1. #! /usr/bin/env python
  2. """Compatibility module, imported by ZSI if you don't have PyXML 0.7.
  3. No copyright violations -- we're only using parts of PyXML that we
  4. wrote.
  5. """
  6. _copyright = '''ZSI: Zolera Soap Infrastructure.
  7. Copyright 2001, Zolera Systems, Inc. All Rights Reserved.
  8. Copyright 2002-2003, Rich Salz. All Rights Reserved.
  9. Permission is hereby granted, free of charge, to any person obtaining a
  10. copy of this software and associated documentation files (the "Software"),
  11. to deal in the Software without restriction, including without limitation
  12. the rights to use, copy, modify, merge, publish, distribute, and/or
  13. sell copies of the Software, and to permit persons to whom the Software
  14. is furnished to do so, provided that the above copyright notice(s) and
  15. this permission notice appear in all copies of the Software and that
  16. both the above copyright notice(s) and this permission notice appear in
  17. supporting documentation.
  18. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
  21. OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
  22. INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
  23. OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
  24. OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  25. OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
  26. OR PERFORMANCE OF THIS SOFTWARE.
  27. Except as contained in this notice, the name of a copyright holder
  28. shall not be used in advertising or otherwise to promote the sale, use
  29. or other dealings in this Software without prior written authorization
  30. of the copyright holder.
  31. '''
  32. _copyright += "\n\nPortions are also: "
  33. _copyright += '''Copyright 2001, Zolera Systems Inc. All Rights Reserved.
  34. Copyright 2001, MIT. All Rights Reserved.
  35. Distributed under the terms of:
  36. Python 2.0 License or later.
  37. http://www.python.org/2.0.1/license.html
  38. or
  39. W3C Software License
  40. http://www.w3.org/Consortium/Legal/copyright-software-19980720
  41. '''
  42. from xml.dom import Node
  43. from Namespaces import XMLNS
  44. import cStringIO as StringIO
  45. try:
  46. from xml.dom.ext import c14n
  47. except ImportError, ex:
  48. _implementation2 = None
  49. _attrs = lambda E: (E.attributes and E.attributes.values()) or []
  50. _children = lambda E: E.childNodes or []
  51. else:
  52. class _implementation2(c14n._implementation):
  53. """Patch for exclusive c14n
  54. """
  55. def __init__(self, node, write, **kw):
  56. self.unsuppressedPrefixes = kw.get('unsuppressedPrefixes')
  57. self._exclusive = None
  58. if node.nodeType == Node.ELEMENT_NODE:
  59. if not c14n._inclusive(self):
  60. self._exclusive = self._inherit_context(node)
  61. c14n._implementation.__init__(self, node, write, **kw)
  62. def _do_element(self, node, initial_other_attrs = []):
  63. """Patch for the xml.dom.ext.c14n implemenation _do_element method.
  64. This fixes a problem with sorting of namespaces.
  65. """
  66. # Get state (from the stack) make local copies.
  67. # ns_parent -- NS declarations in parent
  68. # ns_rendered -- NS nodes rendered by ancestors
  69. # ns_local -- NS declarations relevant to this element
  70. # xml_attrs -- Attributes in XML namespace from parent
  71. # xml_attrs_local -- Local attributes in XML namespace.
  72. ns_parent, ns_rendered, xml_attrs = \
  73. self.state[0], self.state[1].copy(), self.state[2].copy() #0422
  74. ns_local = ns_parent.copy()
  75. xml_attrs_local = {}
  76. # Divide attributes into NS, XML, and others.
  77. #other_attrs = initial_other_attrs[:]
  78. other_attrs = []
  79. sort_these_attrs = initial_other_attrs[:]
  80. in_subset = c14n._in_subset(self.subset, node)
  81. #for a in _attrs(node):
  82. sort_these_attrs +=c14n._attrs(node)
  83. for a in sort_these_attrs:
  84. if a.namespaceURI == c14n.XMLNS.BASE:
  85. n = a.nodeName
  86. if n == "xmlns:": n = "xmlns" # DOM bug workaround
  87. ns_local[n] = a.nodeValue
  88. elif a.namespaceURI == c14n.XMLNS.XML:
  89. if c14n._inclusive(self) or (in_subset and c14n._in_subset(self.subset, a)): #020925 Test to see if attribute node in subset
  90. xml_attrs_local[a.nodeName] = a #0426
  91. else:
  92. if c14n._in_subset(self.subset, a): #020925 Test to see if attribute node in subset
  93. other_attrs.append(a)
  94. #add local xml:foo attributes to ancestor's xml:foo attributes
  95. xml_attrs.update(xml_attrs_local)
  96. # Render the node
  97. W, name = self.write, None
  98. if in_subset:
  99. name = node.nodeName
  100. W('<')
  101. W(name)
  102. # Create list of NS attributes to render.
  103. ns_to_render = []
  104. for n,v in ns_local.items():
  105. # If default namespace is XMLNS.BASE or empty,
  106. # and if an ancestor was the same
  107. if n == "xmlns" and v in [ c14n.XMLNS.BASE, '' ] \
  108. and ns_rendered.get('xmlns') in [ c14n.XMLNS.BASE, '', None ]:
  109. continue
  110. # "omit namespace node with local name xml, which defines
  111. # the xml prefix, if its string value is
  112. # http://www.w3.org/XML/1998/namespace."
  113. if n in ["xmlns:xml", "xml"] \
  114. and v in [ 'http://www.w3.org/XML/1998/namespace' ]:
  115. continue
  116. # If not previously rendered
  117. # and it's inclusive or utilized
  118. if (n,v) not in ns_rendered.items() \
  119. and (c14n._inclusive(self) or \
  120. c14n._utilized(n, node, other_attrs, self.unsuppressedPrefixes)):
  121. ns_to_render.append((n, v))
  122. #####################################
  123. # JRB
  124. #####################################
  125. if not c14n._inclusive(self):
  126. if node.prefix is None:
  127. look_for = [('xmlns', node.namespaceURI),]
  128. else:
  129. look_for = [('xmlns:%s' %node.prefix, node.namespaceURI),]
  130. for a in c14n._attrs(node):
  131. if a.namespaceURI != XMLNS.BASE:
  132. #print "ATTRIBUTE: ", (a.namespaceURI, a.prefix)
  133. if a.prefix:
  134. #print "APREFIX: ", a.prefix
  135. look_for.append(('xmlns:%s' %a.prefix, a.namespaceURI))
  136. for key,namespaceURI in look_for:
  137. if ns_rendered.has_key(key):
  138. if ns_rendered[key] == namespaceURI:
  139. # Dont write out
  140. pass
  141. else:
  142. #ns_to_render += [(key, namespaceURI)]
  143. pass
  144. elif (key,namespaceURI) in ns_to_render:
  145. # Dont write out
  146. pass
  147. else:
  148. # Unique write out, rewrite to render
  149. ns_local[key] = namespaceURI
  150. for a in self._exclusive:
  151. if a.nodeName == key:
  152. #self._do_attr(a.nodeName, a.value)
  153. #ns_rendered[key] = namespaceURI
  154. #break
  155. ns_to_render += [(a.nodeName, a.value)]
  156. break
  157. elif key is None and a.nodeName == 'xmlns':
  158. #print "DEFAULT: ", (a.nodeName, a.value)
  159. ns_to_render += [(a.nodeName, a.value)]
  160. break
  161. #print "KEY: ", key
  162. else:
  163. #print "Look for: ", look_for
  164. #print "NS_TO_RENDER: ", ns_to_render
  165. #print "EXCLUSIVE NS: ", map(lambda f: (f.nodeName,f.value),self._exclusive)
  166. raise RuntimeError, \
  167. 'can not find namespace (%s="%s") for exclusive canonicalization'\
  168. %(key, namespaceURI)
  169. #####################################
  170. # Sort and render the ns, marking what was rendered.
  171. ns_to_render.sort(c14n._sorter_ns)
  172. for n,v in ns_to_render:
  173. #XXX JRB, getting 'xmlns,None' here when xmlns=''
  174. if v: self._do_attr(n, v)
  175. else:
  176. v = ''
  177. self._do_attr(n, v)
  178. ns_rendered[n]=v #0417
  179. # If exclusive or the parent is in the subset, add the local xml attributes
  180. # Else, add all local and ancestor xml attributes
  181. # Sort and render the attributes.
  182. if not c14n._inclusive(self) or c14n._in_subset(self.subset,node.parentNode): #0426
  183. other_attrs.extend(xml_attrs_local.values())
  184. else:
  185. other_attrs.extend(xml_attrs.values())
  186. #print "OTHER: ", other_attrs
  187. other_attrs.sort(c14n._sorter)
  188. for a in other_attrs:
  189. self._do_attr(a.nodeName, a.value)
  190. W('>')
  191. # Push state, recurse, pop state.
  192. state, self.state = self.state, (ns_local, ns_rendered, xml_attrs)
  193. for c in c14n._children(node):
  194. c14n._implementation.handlers[c.nodeType](self, c)
  195. self.state = state
  196. if name: W('</%s>' % name)
  197. c14n._implementation.handlers[c14n.Node.ELEMENT_NODE] = _do_element
  198. _IN_XML_NS = lambda n: n.namespaceURI == XMLNS.XML
  199. # Does a document/PI has lesser/greater document order than the
  200. # first element?
  201. _LesserElement, _Element, _GreaterElement = range(3)
  202. def _sorter(n1,n2):
  203. '''_sorter(n1,n2) -> int
  204. Sorting predicate for non-NS attributes.'''
  205. i = cmp(n1.namespaceURI, n2.namespaceURI)
  206. if i: return i
  207. return cmp(n1.localName, n2.localName)
  208. def _sorter_ns(n1,n2):
  209. '''_sorter_ns((n,v),(n,v)) -> int
  210. "(an empty namespace URI is lexicographically least)."'''
  211. if n1[0] == 'xmlns': return -1
  212. if n2[0] == 'xmlns': return 1
  213. return cmp(n1[0], n2[0])
  214. def _utilized(n, node, other_attrs, unsuppressedPrefixes):
  215. '''_utilized(n, node, other_attrs, unsuppressedPrefixes) -> boolean
  216. Return true if that nodespace is utilized within the node'''
  217. if n.startswith('xmlns:'):
  218. n = n[6:]
  219. elif n.startswith('xmlns'):
  220. n = n[5:]
  221. if n == node.prefix or n in unsuppressedPrefixes: return 1
  222. for attr in other_attrs:
  223. if n == attr.prefix: return 1
  224. return 0
  225. _in_subset = lambda subset, node: not subset or node in subset
  226. #
  227. # JRB. Currently there is a bug in do_element, but since the underlying
  228. # Data Structures in c14n have changed I can't just apply the
  229. # _implementation2 patch above. But this will work OK for most uses,
  230. # just not XML Signatures.
  231. #
  232. class _implementation:
  233. '''Implementation class for C14N. This accompanies a node during it's
  234. processing and includes the parameters and processing state.'''
  235. # Handler for each node type; populated during module instantiation.
  236. handlers = {}
  237. def __init__(self, node, write, **kw):
  238. '''Create and run the implementation.'''
  239. self.write = write
  240. self.subset = kw.get('subset')
  241. if self.subset:
  242. self.comments = kw.get('comments', 1)
  243. else:
  244. self.comments = kw.get('comments', 0)
  245. self.unsuppressedPrefixes = kw.get('unsuppressedPrefixes')
  246. nsdict = kw.get('nsdict', { 'xml': XMLNS.XML, 'xmlns': XMLNS.BASE })
  247. # Processing state.
  248. self.state = (nsdict, ['xml'], [])
  249. if node.nodeType == Node.DOCUMENT_NODE:
  250. self._do_document(node)
  251. elif node.nodeType == Node.ELEMENT_NODE:
  252. self.documentOrder = _Element # At document element
  253. if self.unsuppressedPrefixes is not None:
  254. self._do_element(node)
  255. else:
  256. inherited = self._inherit_context(node)
  257. self._do_element(node, inherited)
  258. elif node.nodeType == Node.DOCUMENT_TYPE_NODE:
  259. pass
  260. else:
  261. raise TypeError, str(node)
  262. def _inherit_context(self, node):
  263. '''_inherit_context(self, node) -> list
  264. Scan ancestors of attribute and namespace context. Used only
  265. for single element node canonicalization, not for subset
  266. canonicalization.'''
  267. # Collect the initial list of xml:foo attributes.
  268. xmlattrs = filter(_IN_XML_NS, _attrs(node))
  269. # Walk up and get all xml:XXX attributes we inherit.
  270. inherited, parent = [], node.parentNode
  271. while parent and parent.nodeType == Node.ELEMENT_NODE:
  272. for a in filter(_IN_XML_NS, _attrs(parent)):
  273. n = a.localName
  274. if n not in xmlattrs:
  275. xmlattrs.append(n)
  276. inherited.append(a)
  277. parent = parent.parentNode
  278. return inherited
  279. def _do_document(self, node):
  280. '''_do_document(self, node) -> None
  281. Process a document node. documentOrder holds whether the document
  282. element has been encountered such that PIs/comments can be written
  283. as specified.'''
  284. self.documentOrder = _LesserElement
  285. for child in node.childNodes:
  286. if child.nodeType == Node.ELEMENT_NODE:
  287. self.documentOrder = _Element # At document element
  288. self._do_element(child)
  289. self.documentOrder = _GreaterElement # After document element
  290. elif child.nodeType == Node.PROCESSING_INSTRUCTION_NODE:
  291. self._do_pi(child)
  292. elif child.nodeType == Node.COMMENT_NODE:
  293. self._do_comment(child)
  294. elif child.nodeType == Node.DOCUMENT_TYPE_NODE:
  295. pass
  296. else:
  297. raise TypeError, str(child)
  298. handlers[Node.DOCUMENT_NODE] = _do_document
  299. def _do_text(self, node):
  300. '''_do_text(self, node) -> None
  301. Process a text or CDATA node. Render various special characters
  302. as their C14N entity representations.'''
  303. if not _in_subset(self.subset, node): return
  304. s = node.data \
  305. .replace("&", "&amp;") \
  306. .replace("<", "&lt;") \
  307. .replace(">", "&gt;") \
  308. .replace("\015", "&#xD;")
  309. if s: self.write(s)
  310. handlers[Node.TEXT_NODE] = _do_text
  311. handlers[Node.CDATA_SECTION_NODE] = _do_text
  312. def _do_pi(self, node):
  313. '''_do_pi(self, node) -> None
  314. Process a PI node. Render a leading or trailing #xA if the
  315. document order of the PI is greater or lesser (respectively)
  316. than the document element.
  317. '''
  318. if not _in_subset(self.subset, node): return
  319. W = self.write
  320. if self.documentOrder == _GreaterElement: W('\n')
  321. W('<?')
  322. W(node.nodeName)
  323. s = node.data
  324. if s:
  325. W(' ')
  326. W(s)
  327. W('?>')
  328. if self.documentOrder == _LesserElement: W('\n')
  329. handlers[Node.PROCESSING_INSTRUCTION_NODE] = _do_pi
  330. def _do_comment(self, node):
  331. '''_do_comment(self, node) -> None
  332. Process a comment node. Render a leading or trailing #xA if the
  333. document order of the comment is greater or lesser (respectively)
  334. than the document element.
  335. '''
  336. if not _in_subset(self.subset, node): return
  337. if self.comments:
  338. W = self.write
  339. if self.documentOrder == _GreaterElement: W('\n')
  340. W('<!--')
  341. W(node.data)
  342. W('-->')
  343. if self.documentOrder == _LesserElement: W('\n')
  344. handlers[Node.COMMENT_NODE] = _do_comment
  345. def _do_attr(self, n, value):
  346. ''''_do_attr(self, node) -> None
  347. Process an attribute.'''
  348. W = self.write
  349. W(' ')
  350. W(n)
  351. W('="')
  352. s = value \
  353. .replace("&", "&amp;") \
  354. .replace("<", "&lt;") \
  355. .replace('"', '&quot;') \
  356. .replace('\011', '&#x9') \
  357. .replace('\012', '&#xA') \
  358. .replace('\015', '&#xD')
  359. W(s)
  360. W('"')
  361. def _do_element(self, node, initial_other_attrs = []):
  362. '''_do_element(self, node, initial_other_attrs = []) -> None
  363. Process an element (and its children).'''
  364. # Get state (from the stack) make local copies.
  365. # ns_parent -- NS declarations in parent
  366. # ns_rendered -- NS nodes rendered by ancestors
  367. # xml_attrs -- Attributes in XML namespace from parent
  368. # ns_local -- NS declarations relevant to this element
  369. ns_parent, ns_rendered, xml_attrs = \
  370. self.state[0], self.state[1][:], self.state[2][:]
  371. ns_local = ns_parent.copy()
  372. # Divide attributes into NS, XML, and others.
  373. other_attrs = initial_other_attrs[:]
  374. in_subset = _in_subset(self.subset, node)
  375. for a in _attrs(node):
  376. if a.namespaceURI == XMLNS.BASE:
  377. n = a.nodeName
  378. if n == "xmlns:": n = "xmlns" # DOM bug workaround
  379. ns_local[n] = a.nodeValue
  380. elif a.namespaceURI == XMLNS.XML:
  381. if self.unsuppressedPrefixes is None or in_subset:
  382. xml_attrs.append(a)
  383. else:
  384. other_attrs.append(a)
  385. # Render the node
  386. W, name = self.write, None
  387. if in_subset:
  388. name = node.nodeName
  389. W('<')
  390. W(name)
  391. # Create list of NS attributes to render.
  392. ns_to_render = []
  393. for n,v in ns_local.items():
  394. pval = ns_parent.get(n)
  395. # If default namespace is XMLNS.BASE or empty, skip
  396. if n == "xmlns" \
  397. and v in [ XMLNS.BASE, '' ] and pval in [ XMLNS.BASE, '' ]:
  398. continue
  399. # "omit namespace node with local name xml, which defines
  400. # the xml prefix, if its string value is
  401. # http://www.w3.org/XML/1998/namespace."
  402. if n == "xmlns:xml" \
  403. and v in [ 'http://www.w3.org/XML/1998/namespace' ]:
  404. continue
  405. # If different from parent, or parent didn't render
  406. # and if not exclusive, or this prefix is needed or
  407. # not suppressed
  408. if (v != pval or n not in ns_rendered) \
  409. and (self.unsuppressedPrefixes is None or \
  410. _utilized(n, node, other_attrs, self.unsuppressedPrefixes)):
  411. ns_to_render.append((n, v))
  412. # Sort and render the ns, marking what was rendered.
  413. ns_to_render.sort(_sorter_ns)
  414. for n,v in ns_to_render:
  415. self._do_attr(n, v)
  416. ns_rendered.append(n)
  417. # Add in the XML attributes (don't pass to children, since
  418. # we're rendering them), sort, and render.
  419. other_attrs.extend(xml_attrs)
  420. xml_attrs = []
  421. other_attrs.sort(_sorter)
  422. for a in other_attrs:
  423. self._do_attr(a.nodeName, a.value)
  424. W('>')
  425. # Push state, recurse, pop state.
  426. state, self.state = self.state, (ns_local, ns_rendered, xml_attrs)
  427. for c in _children(node):
  428. _implementation.handlers[c.nodeType](self, c)
  429. self.state = state
  430. if name: W('</%s>' % name)
  431. handlers[Node.ELEMENT_NODE] = _do_element
  432. def Canonicalize(node, output=None, **kw):
  433. '''Canonicalize(node, output=None, **kw) -> UTF-8
  434. Canonicalize a DOM document/element node and all descendents.
  435. Return the text; if output is specified then output.write will
  436. be called to output the text and None will be returned
  437. Keyword parameters:
  438. nsdict: a dictionary of prefix:uri namespace entries
  439. assumed to exist in the surrounding context
  440. comments: keep comments if non-zero (default is 0)
  441. subset: Canonical XML subsetting resulting from XPath
  442. (default is [])
  443. unsuppressedPrefixes: do exclusive C14N, and this specifies the
  444. prefixes that should be inherited.
  445. '''
  446. if output:
  447. if _implementation2 is None:
  448. _implementation(node, output.write, **kw)
  449. else:
  450. apply(_implementation2, (node, output.write), kw)
  451. else:
  452. s = StringIO.StringIO()
  453. if _implementation2 is None:
  454. _implementation(node, s.write, **kw)
  455. else:
  456. apply(_implementation2, (node, s.write), kw)
  457. return s.getvalue()
  458. if __name__ == '__main__': print _copyright