PageRenderTime 54ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 1ms

/plugins/org.python.pydev.jython/Lib/xml/dom/Element.py

https://github.com/MrGreen123/Pydev
Python | 267 lines | 227 code | 20 blank | 20 comment | 6 complexity | 533c261d6b6074347b39a8d2fe8608ca MD5 | raw file
  1. ########################################################################
  2. #
  3. # File Name: Element.py
  4. #
  5. # Documentation: http://docs.4suite.com/4DOM/Element.py.html
  6. #
  7. """
  8. WWW: http://4suite.com/4DOM e-mail: support@4suite.com
  9. Copyright (c) 2000 Fourthought Inc, USA. All Rights Reserved.
  10. See http://4suite.com/COPYRIGHT for license and copyright information
  11. """
  12. from DOMImplementation import implementation
  13. from FtNode import FtNode
  14. import Event
  15. from xml.dom import Node
  16. from xml.dom import XML_NAMESPACE
  17. from xml.dom import InvalidCharacterErr
  18. from xml.dom import WrongDocumentErr
  19. from xml.dom import InuseAttributeErr
  20. from xml.dom import NotFoundErr
  21. from xml.dom import SyntaxErr
  22. from ext import SplitQName, IsDOMString
  23. import re, string
  24. #FIXME: should allow combining characters: fix when Python gets Unicode
  25. g_namePattern = re.compile('[a-zA-Z_:][\w\.\-_:]*\Z')
  26. class Element(FtNode):
  27. nodeType = Node.ELEMENT_NODE
  28. _allowedChildren = [Node.ELEMENT_NODE,
  29. Node.TEXT_NODE,
  30. Node.COMMENT_NODE,
  31. Node.PROCESSING_INSTRUCTION_NODE,
  32. Node.CDATA_SECTION_NODE,
  33. Node.ENTITY_REFERENCE_NODE
  34. ]
  35. def __init__(self, ownerDocument, nodeName, namespaceURI, prefix, localName):
  36. FtNode.__init__(self, ownerDocument, namespaceURI, prefix, localName);
  37. #Set our attributes
  38. self.__dict__['__attributes'] = implementation._4dom_createNamedNodeMap(ownerDocument)
  39. self.__dict__['__nodeName'] = nodeName
  40. ### Attribute Methods ###
  41. def _get_tagName(self):
  42. return self.__dict__['__nodeName']
  43. ### Methods ###
  44. def getAttribute(self, name):
  45. att = self.attributes.getNamedItem(name)
  46. return att and att.value or ''
  47. def getAttributeNode(self, name):
  48. return self.attributes.getNamedItem(name)
  49. def getElementsByTagName(self, tagName):
  50. nodeList = implementation._4dom_createNodeList()
  51. elements = filter(lambda node, type=Node.ELEMENT_NODE:
  52. node.nodeType == type,
  53. self.childNodes)
  54. for element in elements:
  55. if tagName == '*' or element.tagName == tagName:
  56. nodeList.append(element)
  57. nodeList.extend(list(element.getElementsByTagName(tagName)))
  58. return nodeList
  59. def hasAttribute(self, name):
  60. return self.attributes.getNamedItem(name) is not None
  61. def removeAttribute(self, name):
  62. # Return silently if no node
  63. node = self.attributes.getNamedItem(name)
  64. if node:
  65. self.removeAttributeNode(node)
  66. def removeAttributeNode(self, node):
  67. # NamedNodeMap will raise exception if needed
  68. if node.namespaceURI is None:
  69. self.attributes.removeNamedItem(node.name)
  70. else:
  71. self.attributes.removeNamedItemNS(node.namespaceURI, node.localName)
  72. node._4dom_setOwnerElement(None)
  73. self._4dom_fireMutationEvent('DOMAttrModified',
  74. relatedNode=node,
  75. attrName=node.name,
  76. attrChange=Event.MutationEvent.REMOVAL)
  77. self._4dom_fireMutationEvent('DOMSubtreeModified')
  78. return node
  79. def setAttribute(self, name, value):
  80. if not IsDOMString(value):
  81. raise SyntaxErr()
  82. if not g_namePattern.match(name):
  83. raise InvalidCharacterErr()
  84. attr = self.attributes.getNamedItem(name)
  85. if attr:
  86. attr.value = value
  87. else:
  88. attr = self.ownerDocument.createAttribute(name)
  89. attr.value = value
  90. self.setAttributeNode(attr)
  91. # the mutation event is fired in Attr.py
  92. def setAttributeNode(self, node):
  93. if node.ownerDocument != self.ownerDocument:
  94. raise WrongDocumentErr()
  95. if node.ownerElement != None:
  96. raise InuseAttributeErr()
  97. old = self.attributes.getNamedItem(node.name)
  98. if old:
  99. self._4dom_fireMutationEvent('DOMAttrModified',
  100. relatedNode=old,
  101. prevValue=old.value,
  102. attrName=old.name,
  103. attrChange=Event.MutationEvent.REMOVAL)
  104. self.attributes.setNamedItem(node)
  105. node._4dom_setOwnerElement(self)
  106. self._4dom_fireMutationEvent('DOMAttrModified',
  107. relatedNode=node,
  108. newValue=node.value,
  109. attrName=node.name,
  110. attrChange=Event.MutationEvent.ADDITION)
  111. self._4dom_fireMutationEvent('DOMSubtreeModified')
  112. return old
  113. ### DOM Level 2 Methods ###
  114. def getAttributeNS(self, namespaceURI, localName):
  115. attr = self.attributes.getNamedItemNS(namespaceURI, localName)
  116. return attr and attr.value or ''
  117. def getAttributeNodeNS(self, namespaceURI, localName):
  118. return self.attributes.getNamedItemNS(namespaceURI, localName)
  119. def getElementsByTagNameNS(self, namespaceURI, localName):
  120. nodeList = implementation._4dom_createNodeList()
  121. elements = filter(lambda node, type=Node.ELEMENT_NODE:
  122. node.nodeType == type,
  123. self.childNodes)
  124. for element in elements:
  125. if ((namespaceURI == '*' or element.namespaceURI == namespaceURI)
  126. and (localName == '*' or element.localName == localName)):
  127. nodeList.append(element)
  128. nodeList.extend(list(element.getElementsByTagNameNS(namespaceURI,
  129. localName)))
  130. return nodeList
  131. def hasAttributeNS(self, namespaceURI, localName):
  132. return self.attributes.getNamedItemNS(namespaceURI, localName) != None
  133. def removeAttributeNS(self, namespaceURI, localName):
  134. # Silently return if not attribute
  135. node = self.attributes.getNamedItemNS(namespaceURI, localName)
  136. if node:
  137. self.removeAttributeNode(node)
  138. return
  139. def setAttributeNS(self, namespaceURI, qualifiedName, value):
  140. if not IsDOMString(value):
  141. raise SyntaxErr()
  142. if not g_namePattern.match(qualifiedName):
  143. raise InvalidCharacterErr()
  144. prefix, localName = SplitQName(qualifiedName)
  145. attr = self.attributes.getNamedItemNS(namespaceURI, localName)
  146. if attr:
  147. attr.value = value
  148. else:
  149. attr = self.ownerDocument.createAttributeNS(namespaceURI, qualifiedName)
  150. attr.value = value
  151. self.setAttributeNodeNS(attr)
  152. return
  153. def setAttributeNodeNS(self, node):
  154. if self.ownerDocument != node.ownerDocument:
  155. raise WrongDocumentErr()
  156. if node.ownerElement != None:
  157. raise InuseAttributeErr()
  158. old = self.attributes.getNamedItemNS(node.namespaceURI, node.localName)
  159. if old:
  160. self._4dom_fireMutationEvent('DOMAttrModified',
  161. relatedNode=old,
  162. prevValue=old.value,
  163. attrName=old.name,
  164. attrChange=Event.MutationEvent.REMOVAL)
  165. self.attributes.setNamedItemNS(node)
  166. node._4dom_setOwnerElement(self)
  167. self._4dom_fireMutationEvent('DOMAttrModified',
  168. relatedNode=node,
  169. newValue=node.value,
  170. attrName=node.name,
  171. attrChange=Event.MutationEvent.ADDITION)
  172. self._4dom_fireMutationEvent('DOMSubtreeModified')
  173. return old
  174. ### Overridden Methods ###
  175. def __repr__(self):
  176. return "<Element Node at %x: Name='%s' with %d attributes and %d children>" % (
  177. id(self),
  178. self.nodeName,
  179. len(self.attributes),
  180. len(self.childNodes)
  181. )
  182. # Behind the back setting of element's ownerDocument
  183. # Also sets the owner of the NamedNodeMaps
  184. def _4dom_setOwnerDocument(self, newOwner):
  185. self.__dict__['__ownerDocument'] = newOwner
  186. self.__dict__['__attributes']._4dom_setOwnerDocument(newOwner)
  187. ### Helper Functions For Cloning ###
  188. def _4dom_clone(self, owner):
  189. e = self.__class__(owner,
  190. self.nodeName,
  191. self.namespaceURI,
  192. self.prefix,
  193. self.localName)
  194. for attr in self.attributes:
  195. clone = attr._4dom_clone(owner)
  196. if clone.localName is None:
  197. e.attributes.setNamedItem(clone)
  198. else:
  199. e.attributes.setNamedItemNS(clone)
  200. clone._4dom_setOwnerElement(self)
  201. return e
  202. def __getinitargs__(self):
  203. return (self.ownerDocument,
  204. self.nodeName,
  205. self.namespaceURI,
  206. self.prefix,
  207. self.localName
  208. )
  209. def __getstate__(self):
  210. return (self.childNodes, self.attributes)
  211. def __setstate__(self, (children, attrs)):
  212. FtNode.__setstate__(self, children)
  213. self.__dict__['__attributes'] = attrs
  214. for attr in attrs:
  215. attr._4dom_setOwnerElement(self)
  216. ### Attribute Access Mappings ###
  217. _readComputedAttrs = FtNode._readComputedAttrs.copy()
  218. _readComputedAttrs.update({'tagName':_get_tagName,
  219. })
  220. _writeComputedAttrs = FtNode._writeComputedAttrs.copy()
  221. _writeComputedAttrs.update({
  222. })
  223. # Create the read-only list of attributes
  224. _readOnlyAttrs = filter(lambda k,m=_writeComputedAttrs: not m.has_key(k),
  225. FtNode._readOnlyAttrs + _readComputedAttrs.keys())