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.
 
 
 

763 lines
28 KiB

  1. # Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
  2. #
  3. # This software is subject to the provisions of the Zope Public License,
  4. # Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
  5. # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
  6. # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  7. # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
  8. # FOR A PARTICULAR PURPOSE.
  9. ident = "$Id$"
  10. from string import join, strip, split
  11. from UserDict import UserDict
  12. from StringIO import StringIO
  13. import xml.dom.minidom, weakref
  14. import string, httplib, smtplib, urllib, socket
  15. from TimeoutSocket import TimeoutSocket, TimeoutError
  16. from StringIO import StringIO
  17. from urlparse import urlparse
  18. from httplib import HTTPConnection, HTTPSConnection
  19. from exceptions import Exception
  20. class RecursionError(Exception):
  21. """Used to indicate a HTTP redirect recursion."""
  22. pass
  23. class HTTPResponse:
  24. """Captures the information in an HTTP response message."""
  25. def __init__(self, response):
  26. self.status = response.status
  27. self.reason = response.reason
  28. self.headers = response.msg
  29. self.body = response.read() or None
  30. response.close()
  31. class TimeoutHTTP(HTTPConnection):
  32. """A custom http connection object that supports socket timeout."""
  33. def __init__(self, host, port=None, timeout=20):
  34. HTTPConnection.__init__(self, host, port)
  35. self.timeout = timeout
  36. def connect(self):
  37. self.sock = TimeoutSocket(self.timeout)
  38. self.sock.connect((self.host, self.port))
  39. class TimeoutHTTPS(HTTPSConnection):
  40. """A custom https object that supports socket timeout. Note that this
  41. is not really complete. The builtin SSL support in the Python socket
  42. module requires a real socket (type) to be passed in to be hooked to
  43. SSL. That means our fake socket won't work and our timeout hacks are
  44. bypassed for send and recv calls. Since our hack _is_ in place at
  45. connect() time, it should at least provide some timeout protection."""
  46. def __init__(self, host, port=None, timeout=20, **kwargs):
  47. if not hasattr(socket, 'ssl'):
  48. raise ValueError(
  49. 'This Python installation does not have SSL support.'
  50. )
  51. HTTPSConnection.__init__(self, str(host), port, **kwargs)
  52. self.timeout = timeout
  53. def connect(self):
  54. sock = TimeoutSocket(self.timeout)
  55. sock.connect((self.host, self.port))
  56. realsock = getattr(sock.sock, '_sock', sock.sock)
  57. ssl = socket.ssl(realsock, self.key_file, self.cert_file)
  58. self.sock = httplib.FakeSocket(sock, ssl)
  59. def urlopen(url, timeout=20, redirects=None):
  60. """A minimal urlopen replacement hack that supports timeouts for http.
  61. Note that this supports GET only."""
  62. scheme, host, path, params, query, frag = urlparse(url)
  63. if not scheme in ('http', 'https'):
  64. return urllib.urlopen(url)
  65. if params: path = '%s;%s' % (path, params)
  66. if query: path = '%s?%s' % (path, query)
  67. if frag: path = '%s#%s' % (path, frag)
  68. if scheme == 'https':
  69. if not hasattr(socket, 'ssl'):
  70. raise ValueError(
  71. 'This Python installation does not have SSL support.'
  72. )
  73. conn = TimeoutHTTPS(host, None, timeout)
  74. else:
  75. conn = TimeoutHTTP(host, None, timeout)
  76. conn.putrequest('GET', path)
  77. conn.putheader('Connection', 'close')
  78. conn.endheaders()
  79. response = None
  80. while 1:
  81. response = conn.getresponse()
  82. if response.status != 100:
  83. break
  84. conn._HTTPConnection__state = httplib._CS_REQ_SENT
  85. conn._HTTPConnection__response = None
  86. status = response.status
  87. # If we get an HTTP redirect, we will follow it automatically.
  88. if status >= 300 and status < 400:
  89. location = response.msg.getheader('location')
  90. if location is not None:
  91. response.close()
  92. if redirects is not None and redirects.has_key(location):
  93. raise RecursionError(
  94. 'Circular HTTP redirection detected.'
  95. )
  96. if redirects is None:
  97. redirects = {}
  98. redirects[location] = 1
  99. return urlopen(location, timeout, redirects)
  100. raise HTTPResponse(response)
  101. if not (status >= 200 and status < 300):
  102. raise HTTPResponse(response)
  103. body = StringIO(response.read())
  104. response.close()
  105. return body
  106. class DOM:
  107. """The DOM singleton defines a number of XML related constants and
  108. provides a number of utility methods for DOM related tasks. It
  109. also provides some basic abstractions so that the rest of the
  110. package need not care about actual DOM implementation in use."""
  111. # Namespace stuff related to the SOAP specification.
  112. NS_SOAP_ENV_1_1 = 'http://schemas.xmlsoap.org/soap/envelope/'
  113. NS_SOAP_ENC_1_1 = 'http://schemas.xmlsoap.org/soap/encoding/'
  114. NS_SOAP_ENV_1_2 = 'http://www.w3.org/2001/06/soap-envelope'
  115. NS_SOAP_ENC_1_2 = 'http://www.w3.org/2001/06/soap-encoding'
  116. NS_SOAP_ENV_ALL = (NS_SOAP_ENV_1_1, NS_SOAP_ENV_1_2)
  117. NS_SOAP_ENC_ALL = (NS_SOAP_ENC_1_1, NS_SOAP_ENC_1_2)
  118. NS_SOAP_ENV = NS_SOAP_ENV_1_1
  119. NS_SOAP_ENC = NS_SOAP_ENC_1_1
  120. _soap_uri_mapping = {
  121. NS_SOAP_ENV_1_1 : '1.1',
  122. NS_SOAP_ENV_1_2 : '1.2',
  123. }
  124. SOAP_ACTOR_NEXT_1_1 = 'http://schemas.xmlsoap.org/soap/actor/next'
  125. SOAP_ACTOR_NEXT_1_2 = 'http://www.w3.org/2001/06/soap-envelope/actor/next'
  126. SOAP_ACTOR_NEXT_ALL = (SOAP_ACTOR_NEXT_1_1, SOAP_ACTOR_NEXT_1_2)
  127. def SOAPUriToVersion(self, uri):
  128. """Return the SOAP version related to an envelope uri."""
  129. value = self._soap_uri_mapping.get(uri)
  130. if value is not None:
  131. return value
  132. raise ValueError(
  133. 'Unsupported SOAP envelope uri: %s' % uri
  134. )
  135. def GetSOAPEnvUri(self, version):
  136. """Return the appropriate SOAP envelope uri for a given
  137. human-friendly SOAP version string (e.g. '1.1')."""
  138. attrname = 'NS_SOAP_ENV_%s' % join(split(version, '.'), '_')
  139. value = getattr(self, attrname, None)
  140. if value is not None:
  141. return value
  142. raise ValueError(
  143. 'Unsupported SOAP version: %s' % version
  144. )
  145. def GetSOAPEncUri(self, version):
  146. """Return the appropriate SOAP encoding uri for a given
  147. human-friendly SOAP version string (e.g. '1.1')."""
  148. attrname = 'NS_SOAP_ENC_%s' % join(split(version, '.'), '_')
  149. value = getattr(self, attrname, None)
  150. if value is not None:
  151. return value
  152. raise ValueError(
  153. 'Unsupported SOAP version: %s' % version
  154. )
  155. def GetSOAPActorNextUri(self, version):
  156. """Return the right special next-actor uri for a given
  157. human-friendly SOAP version string (e.g. '1.1')."""
  158. attrname = 'SOAP_ACTOR_NEXT_%s' % join(split(version, '.'), '_')
  159. value = getattr(self, attrname, None)
  160. if value is not None:
  161. return value
  162. raise ValueError(
  163. 'Unsupported SOAP version: %s' % version
  164. )
  165. # Namespace stuff related to XML Schema.
  166. NS_XSD_99 = 'http://www.w3.org/1999/XMLSchema'
  167. NS_XSI_99 = 'http://www.w3.org/1999/XMLSchema-instance'
  168. NS_XSD_00 = 'http://www.w3.org/2000/10/XMLSchema'
  169. NS_XSI_00 = 'http://www.w3.org/2000/10/XMLSchema-instance'
  170. NS_XSD_01 = 'http://www.w3.org/2001/XMLSchema'
  171. NS_XSI_01 = 'http://www.w3.org/2001/XMLSchema-instance'
  172. NS_XSD_ALL = (NS_XSD_99, NS_XSD_00, NS_XSD_01)
  173. NS_XSI_ALL = (NS_XSI_99, NS_XSI_00, NS_XSI_01)
  174. NS_XSD = NS_XSD_01
  175. NS_XSI = NS_XSI_01
  176. _xsd_uri_mapping = {
  177. NS_XSD_99 : NS_XSI_99,
  178. NS_XSD_00 : NS_XSI_00,
  179. NS_XSD_01 : NS_XSI_01,
  180. }
  181. for key, value in _xsd_uri_mapping.items():
  182. _xsd_uri_mapping[value] = key
  183. def InstanceUriForSchemaUri(self, uri):
  184. """Return the appropriate matching XML Schema instance uri for
  185. the given XML Schema namespace uri."""
  186. return self._xsd_uri_mapping.get(uri)
  187. def SchemaUriForInstanceUri(self, uri):
  188. """Return the appropriate matching XML Schema namespace uri for
  189. the given XML Schema instance namespace uri."""
  190. return self._xsd_uri_mapping.get(uri)
  191. # Namespace stuff related to WSDL.
  192. NS_WSDL_1_1 = 'http://schemas.xmlsoap.org/wsdl/'
  193. NS_WSDL_ALL = (NS_WSDL_1_1,)
  194. NS_WSDL = NS_WSDL_1_1
  195. NS_SOAP_BINDING_1_1 = 'http://schemas.xmlsoap.org/wsdl/soap/'
  196. NS_HTTP_BINDING_1_1 = 'http://schemas.xmlsoap.org/wsdl/http/'
  197. NS_MIME_BINDING_1_1 = 'http://schemas.xmlsoap.org/wsdl/mime/'
  198. NS_SOAP_BINDING_ALL = (NS_SOAP_BINDING_1_1,)
  199. NS_HTTP_BINDING_ALL = (NS_HTTP_BINDING_1_1,)
  200. NS_MIME_BINDING_ALL = (NS_MIME_BINDING_1_1,)
  201. NS_SOAP_BINDING = NS_SOAP_BINDING_1_1
  202. NS_HTTP_BINDING = NS_HTTP_BINDING_1_1
  203. NS_MIME_BINDING = NS_MIME_BINDING_1_1
  204. NS_SOAP_HTTP_1_1 = 'http://schemas.xmlsoap.org/soap/http'
  205. NS_SOAP_HTTP_ALL = (NS_SOAP_HTTP_1_1,)
  206. NS_SOAP_HTTP = NS_SOAP_HTTP_1_1
  207. _wsdl_uri_mapping = {
  208. NS_WSDL_1_1 : '1.1',
  209. }
  210. def WSDLUriToVersion(self, uri):
  211. """Return the WSDL version related to a WSDL namespace uri."""
  212. value = self._wsdl_uri_mapping.get(uri)
  213. if value is not None:
  214. return value
  215. raise ValueError(
  216. 'Unsupported SOAP envelope uri: %s' % uri
  217. )
  218. def GetWSDLUri(self, version):
  219. attr = 'NS_WSDL_%s' % join(split(version, '.'), '_')
  220. value = getattr(self, attr, None)
  221. if value is not None:
  222. return value
  223. raise ValueError(
  224. 'Unsupported WSDL version: %s' % version
  225. )
  226. def GetWSDLSoapBindingUri(self, version):
  227. attr = 'NS_SOAP_BINDING_%s' % join(split(version, '.'), '_')
  228. value = getattr(self, attr, None)
  229. if value is not None:
  230. return value
  231. raise ValueError(
  232. 'Unsupported WSDL version: %s' % version
  233. )
  234. def GetWSDLHttpBindingUri(self, version):
  235. attr = 'NS_HTTP_BINDING_%s' % join(split(version, '.'), '_')
  236. value = getattr(self, attr, None)
  237. if value is not None:
  238. return value
  239. raise ValueError(
  240. 'Unsupported WSDL version: %s' % version
  241. )
  242. def GetWSDLMimeBindingUri(self, version):
  243. attr = 'NS_MIME_BINDING_%s' % join(split(version, '.'), '_')
  244. value = getattr(self, attr, None)
  245. if value is not None:
  246. return value
  247. raise ValueError(
  248. 'Unsupported WSDL version: %s' % version
  249. )
  250. def GetWSDLHttpTransportUri(self, version):
  251. attr = 'NS_SOAP_HTTP_%s' % join(split(version, '.'), '_')
  252. value = getattr(self, attr, None)
  253. if value is not None:
  254. return value
  255. raise ValueError(
  256. 'Unsupported WSDL version: %s' % version
  257. )
  258. # Other xml namespace constants.
  259. NS_XMLNS = 'http://www.w3.org/2000/xmlns/'
  260. def isElement(self, node, name, nsuri=None):
  261. """Return true if the given node is an element with the given
  262. name and optional namespace uri."""
  263. if node.nodeType == node.ELEMENT_NODE:
  264. return 0
  265. return node.localName == name and \
  266. (nsuri is None or self.nsUriMatch(node.namespaceURI, nsuri))
  267. def getElement(self, node, name, nsuri=None, default=join):
  268. """Return the first child of node with a matching name and
  269. namespace uri, or the default if one is provided."""
  270. nsmatch = self.nsUriMatch
  271. ELEMENT_NODE = node.ELEMENT_NODE
  272. for child in node.childNodes:
  273. if child.nodeType == ELEMENT_NODE:
  274. if ((child.localName == name or name is None) and
  275. (nsuri is None or nsmatch(child.namespaceURI, nsuri))
  276. ):
  277. return child
  278. if default is not join:
  279. return default
  280. raise KeyError, name
  281. def getElementById(self, node, id, default=join):
  282. """Return the first child of node matching an id reference."""
  283. attrget = self.getAttr
  284. ELEMENT_NODE = node.ELEMENT_NODE
  285. for child in node.childNodes:
  286. if child.nodeType == ELEMENT_NODE:
  287. if attrget(child, 'id') == id:
  288. return child
  289. if default is not join:
  290. return default
  291. raise KeyError, name
  292. def getMappingById(self, document, depth=None, element=None,
  293. mapping=None, level=1):
  294. """Create an id -> element mapping of those elements within a
  295. document that define an id attribute. The depth of the search
  296. may be controlled by using the (1-based) depth argument."""
  297. if document is not None:
  298. element = document.documentElement
  299. mapping = {}
  300. attr = element._attrs.get('id', None)
  301. if attr is not None:
  302. mapping[attr.value] = element
  303. if depth is None or depth > level:
  304. level = level + 1
  305. ELEMENT_NODE = element.ELEMENT_NODE
  306. for child in element.childNodes:
  307. if child.nodeType == ELEMENT_NODE:
  308. self.getMappingById(None, depth, child, mapping, level)
  309. return mapping
  310. def getElements(self, node, name, nsuri=None):
  311. """Return a sequence of the child elements of the given node that
  312. match the given name and optional namespace uri."""
  313. nsmatch = self.nsUriMatch
  314. result = []
  315. ELEMENT_NODE = node.ELEMENT_NODE
  316. for child in node.childNodes:
  317. if child.nodeType == ELEMENT_NODE:
  318. if ((child.localName == name or name is None) and (
  319. (nsuri is None) or nsmatch(child.namespaceURI, nsuri))):
  320. result.append(child)
  321. return result
  322. def hasAttr(self, node, name, nsuri=None):
  323. """Return true if element has attribute with the given name and
  324. optional nsuri. If nsuri is not specified, returns true if an
  325. attribute exists with the given name with any namespace."""
  326. if nsuri is None:
  327. if node._attrs.has_key(name):
  328. return 1
  329. for item in node._attrsNS.keys():
  330. if item[1] == name:
  331. return 1
  332. return 0
  333. return node.attrsNS.has_key((nsuri, name))
  334. def getAttr(self, node, name, nsuri=None, default=join):
  335. """Return the value of the attribute named 'name' with the
  336. optional nsuri, or the default if one is specified. If
  337. nsuri is not specified, an attribute that matches the
  338. given name will be returned regardless of namespace."""
  339. if nsuri is None:
  340. result = node._attrs.get(name, None)
  341. if result is None:
  342. for item in node._attrsNS.keys():
  343. if item[1] == name:
  344. result = node._attrsNS[item]
  345. break
  346. else:
  347. result = node._attrsNS.get((nsuri, name), None)
  348. if result is not None:
  349. return result.value
  350. if default is not join:
  351. return default
  352. return ''
  353. def getElementText(self, node, preserve_ws=None):
  354. """Return the text value of an xml element node. Leading and trailing
  355. whitespace is stripped from the value unless the preserve_ws flag
  356. is passed with a true value."""
  357. result = []
  358. for child in node.childNodes:
  359. nodetype = child.nodeType
  360. if nodetype == child.TEXT_NODE or \
  361. nodetype == child.CDATA_SECTION_NODE:
  362. result.append(child.nodeValue)
  363. value = join(result, '')
  364. if preserve_ws is None:
  365. value = strip(value)
  366. return value
  367. def findNamespaceURI(self, prefix, node):
  368. """Find a namespace uri given a prefix and a context node."""
  369. attrkey = (self.NS_XMLNS, prefix)
  370. DOCUMENT_NODE = node.DOCUMENT_NODE
  371. ELEMENT_NODE = node.ELEMENT_NODE
  372. while 1:
  373. if node.nodeType != ELEMENT_NODE:
  374. node = node.parentNode
  375. continue
  376. result = node._attrsNS.get(attrkey, None)
  377. if result is not None:
  378. return result.value
  379. if hasattr(node, '__imported__'):
  380. raise DOMException('Value for prefix %s not found.' % prefix)
  381. node = node.parentNode
  382. if node.nodeType == DOCUMENT_NODE:
  383. raise DOMException('Value for prefix %s not found.' % prefix)
  384. def findDefaultNS(self, node):
  385. """Return the current default namespace uri for the given node."""
  386. attrkey = (self.NS_XMLNS, 'xmlns')
  387. DOCUMENT_NODE = node.DOCUMENT_NODE
  388. ELEMENT_NODE = node.ELEMENT_NODE
  389. while 1:
  390. if node.nodeType != ELEMENT_NODE:
  391. node = node.parentNode
  392. continue
  393. result = node._attrsNS.get(attrkey, None)
  394. if result is not None:
  395. return result.value
  396. if hasattr(node, '__imported__'):
  397. raise DOMException('Cannot determine default namespace.')
  398. node = node.parentNode
  399. if node.nodeType == DOCUMENT_NODE:
  400. raise DOMException('Cannot determine default namespace.')
  401. def findTargetNS(self, node):
  402. """Return the defined target namespace uri for the given node."""
  403. attrget = self.getAttr
  404. attrkey = (self.NS_XMLNS, 'xmlns')
  405. DOCUMENT_NODE = node.DOCUMENT_NODE
  406. ELEMENT_NODE = node.ELEMENT_NODE
  407. while 1:
  408. if node.nodeType != ELEMENT_NODE:
  409. node = node.parentNode
  410. continue
  411. result = attrget(node, 'targetNamespace', default=None)
  412. if result is not None:
  413. return result
  414. node = node.parentNode
  415. if node.nodeType == DOCUMENT_NODE:
  416. raise DOMException('Cannot determine target namespace.')
  417. def getTypeRef(self, element):
  418. """Return (namespaceURI, name) for a type attribue of the given
  419. element, or None if the element does not have a type attribute."""
  420. typeattr = self.getAttr(element, 'type', default=None)
  421. if typeattr is None:
  422. return None
  423. parts = typeattr.split(':', 1)
  424. if len(parts) == 2:
  425. nsuri = self.findNamespaceURI(parts[0], element)
  426. else:
  427. nsuri = self.findDefaultNS(element)
  428. return (nsuri, parts[1])
  429. def importNode(self, document, node, deep=0):
  430. """Implements (well enough for our purposes) DOM node import."""
  431. nodetype = node.nodeType
  432. if nodetype in (node.DOCUMENT_NODE, node.DOCUMENT_TYPE_NODE):
  433. raise DOMException('Illegal node type for importNode')
  434. if nodetype == node.ENTITY_REFERENCE_NODE:
  435. deep = 0
  436. clone = node.cloneNode(deep)
  437. self._setOwnerDoc(document, clone)
  438. clone.__imported__ = 1
  439. return clone
  440. def _setOwnerDoc(self, document, node):
  441. node.ownerDocument = document
  442. for child in node.childNodes:
  443. self._setOwnerDoc(document, child)
  444. def nsUriMatch(self, value, wanted, strict=0, tt=type(())):
  445. """Return a true value if two namespace uri values match."""
  446. if value == wanted or (type(wanted) is tt) and value in wanted:
  447. return 1
  448. if not strict:
  449. wanted = type(wanted) is tt and wanted or (wanted,)
  450. value = value[-1:] != '/' and value or value[:-1]
  451. for item in wanted:
  452. if item == value or item[:-1] == value:
  453. return 1
  454. return 0
  455. def createDocument(self, nsuri, qname, doctype=None):
  456. """Create a new writable DOM document object."""
  457. impl = xml.dom.minidom.getDOMImplementation()
  458. return impl.createDocument(nsuri, qname, doctype)
  459. def loadDocument(self, data):
  460. """Load an xml file from a file-like object and return a DOM
  461. document instance."""
  462. return xml.dom.minidom.parse(data)
  463. def loadFromURL(self, url):
  464. """Load an xml file from a URL and return a DOM document."""
  465. file = urlopen(url)
  466. try: result = self.loadDocument(file)
  467. finally: file.close()
  468. return result
  469. class DOMException(Exception):
  470. pass
  471. DOM = DOM()
  472. class Collection(UserDict):
  473. """Helper class for maintaining ordered named collections."""
  474. default = lambda k: k.name
  475. def __init__(self, parent, key=None):
  476. UserDict.__init__(self)
  477. self.parent = weakref.ref(parent)
  478. self.list = []
  479. self._func = key or self.default
  480. def __getitem__(self, key):
  481. if type(key) is type(1):
  482. return self.list[key]
  483. return self.data[key]
  484. def __setitem__(self, key, item):
  485. item.parent = weakref.ref(self)
  486. self.list.append(item)
  487. self.data[key] = item
  488. def keys(self):
  489. return map(lambda i: self._func(i), self.list)
  490. def items(self):
  491. return map(lambda i: (self._func(i), i), self.list)
  492. def values(self):
  493. return self.list
  494. # This is a runtime guerilla patch for pulldom (used by minidom) so
  495. # that xml namespace declaration attributes are not lost in parsing.
  496. # We need them to do correct QName linking for XML Schema and WSDL.
  497. # The patch has been submitted to SF for the next Python version.
  498. from xml.dom.pulldom import PullDOM, START_ELEMENT
  499. if 1:
  500. def startPrefixMapping(self, prefix, uri):
  501. if not hasattr(self, '_xmlns_attrs'):
  502. self._xmlns_attrs = []
  503. self._xmlns_attrs.append((prefix or 'xmlns', uri))
  504. self._ns_contexts.append(self._current_context.copy())
  505. self._current_context[uri] = prefix or ''
  506. PullDOM.startPrefixMapping = startPrefixMapping
  507. def startElementNS(self, name, tagName , attrs):
  508. # Retrieve xml namespace declaration attributes.
  509. xmlns_uri = 'http://www.w3.org/2000/xmlns/'
  510. xmlns_attrs = getattr(self, '_xmlns_attrs', None)
  511. if xmlns_attrs is not None:
  512. for aname, value in xmlns_attrs:
  513. attrs._attrs[(xmlns_uri, aname)] = value
  514. self._xmlns_attrs = []
  515. uri, localname = name
  516. if uri:
  517. # When using namespaces, the reader may or may not
  518. # provide us with the original name. If not, create
  519. # *a* valid tagName from the current context.
  520. if tagName is None:
  521. prefix = self._current_context[uri]
  522. if prefix:
  523. tagName = prefix + ":" + localname
  524. else:
  525. tagName = localname
  526. if self.document:
  527. node = self.document.createElementNS(uri, tagName)
  528. else:
  529. node = self.buildDocument(uri, tagName)
  530. else:
  531. # When the tagname is not prefixed, it just appears as
  532. # localname
  533. if self.document:
  534. node = self.document.createElement(localname)
  535. else:
  536. node = self.buildDocument(None, localname)
  537. for aname,value in attrs.items():
  538. a_uri, a_localname = aname
  539. if a_uri == xmlns_uri:
  540. if a_localname == 'xmlns':
  541. qname = a_localname
  542. else:
  543. qname = 'xmlns:' + a_localname
  544. attr = self.document.createAttributeNS(a_uri, qname)
  545. node.setAttributeNodeNS(attr)
  546. elif a_uri:
  547. prefix = self._current_context[a_uri]
  548. if prefix:
  549. qname = prefix + ":" + a_localname
  550. else:
  551. qname = a_localname
  552. attr = self.document.createAttributeNS(a_uri, qname)
  553. node.setAttributeNodeNS(attr)
  554. else:
  555. attr = self.document.createAttribute(a_localname)
  556. node.setAttributeNode(attr)
  557. attr.value = value
  558. self.lastEvent[1] = [(START_ELEMENT, node), None]
  559. self.lastEvent = self.lastEvent[1]
  560. self.push(node)
  561. PullDOM.startElementNS = startElementNS
  562. #
  563. # This is a runtime guerilla patch for minidom so
  564. # that xmlns prefixed attributes dont raise AttributeErrors
  565. # during cloning.
  566. #
  567. # Namespace declarations can appear in any start-tag, must look for xmlns
  568. # prefixed attribute names during cloning.
  569. #
  570. # key (attr.namespaceURI, tag)
  571. # ('http://www.w3.org/2000/xmlns/', u'xsd') <xml.dom.minidom.Attr instance at 0x82227c4>
  572. # ('http://www.w3.org/2000/xmlns/', 'xmlns') <xml.dom.minidom.Attr instance at 0x8414b3c>
  573. #
  574. # xml.dom.minidom.Attr.nodeName = xmlns:xsd
  575. # xml.dom.minidom.Attr.value = = http://www.w3.org/2001/XMLSchema
  576. if 1:
  577. def _clone_node(node, deep, newOwnerDocument):
  578. """
  579. Clone a node and give it the new owner document.
  580. Called by Node.cloneNode and Document.importNode
  581. """
  582. if node.ownerDocument.isSameNode(newOwnerDocument):
  583. operation = xml.dom.UserDataHandler.NODE_CLONED
  584. else:
  585. operation = xml.dom.UserDataHandler.NODE_IMPORTED
  586. if node.nodeType == xml.dom.minidom.Node.ELEMENT_NODE:
  587. clone = newOwnerDocument.createElementNS(node.namespaceURI,
  588. node.nodeName)
  589. for attr in node.attributes.values():
  590. clone.setAttributeNS(attr.namespaceURI, attr.nodeName, attr.value)
  591. prefix, tag = xml.dom.minidom._nssplit(attr.nodeName)
  592. if prefix == 'xmlns':
  593. a = clone.getAttributeNodeNS(attr.namespaceURI, tag)
  594. else:
  595. a = clone.getAttributeNodeNS(attr.namespaceURI, attr.nodeName)
  596. a.specified = attr.specified
  597. if deep:
  598. for child in node.childNodes:
  599. c = xml.dom.minidom._clone_node(child, deep, newOwnerDocument)
  600. clone.appendChild(c)
  601. elif node.nodeType == xml.dom.minidom.Node.DOCUMENT_FRAGMENT_NODE:
  602. clone = newOwnerDocument.createDocumentFragment()
  603. if deep:
  604. for child in node.childNodes:
  605. c = xml.dom.minidom._clone_node(child, deep, newOwnerDocument)
  606. clone.appendChild(c)
  607. elif node.nodeType == xml.dom.minidom.Node.TEXT_NODE:
  608. clone = newOwnerDocument.createTextNode(node.data)
  609. elif node.nodeType == xml.dom.minidom.Node.CDATA_SECTION_NODE:
  610. clone = newOwnerDocument.createCDATASection(node.data)
  611. elif node.nodeType == xml.dom.minidom.Node.PROCESSING_INSTRUCTION_NODE:
  612. clone = newOwnerDocument.createProcessingInstruction(node.target,
  613. node.data)
  614. elif node.nodeType == xml.dom.minidom.Node.COMMENT_NODE:
  615. clone = newOwnerDocument.createComment(node.data)
  616. elif node.nodeType == xml.dom.minidom.Node.ATTRIBUTE_NODE:
  617. clone = newOwnerDocument.createAttributeNS(node.namespaceURI,
  618. node.nodeName)
  619. clone.specified = True
  620. clone.value = node.value
  621. elif node.nodeType == xml.dom.minidom.Node.DOCUMENT_TYPE_NODE:
  622. assert node.ownerDocument is not newOwnerDocument
  623. operation = xml.dom.UserDataHandler.NODE_IMPORTED
  624. clone = newOwnerDocument.implementation.createDocumentType(
  625. node.name, node.publicId, node.systemId)
  626. clone.ownerDocument = newOwnerDocument
  627. if deep:
  628. clone.entities._seq = []
  629. clone.notations._seq = []
  630. for n in node.notations._seq:
  631. notation = xml.dom.minidom.Notation(n.nodeName, n.publicId, n.systemId)
  632. notation.ownerDocument = newOwnerDocument
  633. clone.notations._seq.append(notation)
  634. if hasattr(n, '_call_user_data_handler'):
  635. n._call_user_data_handler(operation, n, notation)
  636. for e in node.entities._seq:
  637. entity = xml.dom.minidom.Entity(e.nodeName, e.publicId, e.systemId,
  638. e.notationName)
  639. entity.actualEncoding = e.actualEncoding
  640. entity.encoding = e.encoding
  641. entity.version = e.version
  642. entity.ownerDocument = newOwnerDocument
  643. clone.entities._seq.append(entity)
  644. if hasattr(e, '_call_user_data_handler'):
  645. e._call_user_data_handler(operation, n, entity)
  646. else:
  647. # Note the cloning of Document and DocumentType nodes is
  648. # implemenetation specific. minidom handles those cases
  649. # directly in the cloneNode() methods.
  650. raise xml.dom.NotSupportedErr("Cannot clone node %s" % repr(node))
  651. # Check for _call_user_data_handler() since this could conceivably
  652. # used with other DOM implementations (one of the FourThought
  653. # DOMs, perhaps?).
  654. if hasattr(node, '_call_user_data_handler'):
  655. node._call_user_data_handler(operation, node, clone)
  656. return clone
  657. xml.dom.minidom._clone_node = _clone_node