def __init__(self, ownerDocument, nodeName, namespaceURI, prefix, localName): FtNode.__init__(self, ownerDocument, namespaceURI, prefix, localName) #Set our attributes self.__dict__[ '__attributes'] = implementation._4dom_createNamedNodeMap( ownerDocument) self.__dict__['__nodeName'] = nodeName
def __init__(self, doctype): FtNode.__init__(self, None) self.__dict__['__doctype'] = None self.__dict__['__implementation'] = implementation self.__dict__['__documentElement'] = None self.__dict__['_singleChildren'] = {Node.ELEMENT_NODE:'__documentElement', Node.DOCUMENT_TYPE_NODE:'__doctype' } self._4dom_setDocumentType(doctype)
def __init__(self, name, entities, notations, publicId, systemId): FtNode.__init__(self, None) self.__dict__['__nodeName'] = name self._entities = entities self._notations = notations self._publicId = publicId self._systemId = systemId #FIXME: Text repr of the entities self._internalSubset = ''
def __init__(self, doctype): FtNode.__init__(self, None) self.__dict__["__doctype"] = None self.__dict__["__implementation"] = implementation self.__dict__["__documentElement"] = None self.__dict__["_singleChildren"] = { Node.ELEMENT_NODE: "__documentElement", Node.DOCUMENT_TYPE_NODE: "__doctype", } self._4dom_setDocumentType(doctype)
def removeChild(self,oldChild): node = FtNode.removeChild(self, oldChild) if self.documentElement == node: self.__dict__['__documentElement'] = None if self.__dict__['__doctype'] == node: self.__dict__['__doctype'] = None return node
def removeChild(self, oldChild): node = FtNode.removeChild(self, oldChild) if self.documentElement == node: self.__dict__['__documentElement'] = None if self.__dict__['__doctype'] == node: self.__dict__['__doctype'] = None return node
def replaceChild(self, newChild, oldChild): if newChild.nodeType != Node.DOCUMENT_FRAGMENT_NODE: root = self.__dict__['__documentElement'] if root in [oldChild, newChild]: self.__dict__['__documentElement'] = None else: raise HierarchyRequestErr() replaced = FtNode.replaceChild(self, newChild, oldChild) if newChild.nodeType == Node.ELEMENT_NODE: self.__dict__['__documentElement'] = newChild if self.__dict__['__doctype']: self.__dict__['__doctype']._4dom_setName(newChild.nodeName) return replaced
def __init__(self, ownerDocument, name, namespaceURI, prefix, localName): FtNode.__init__(self, ownerDocument, namespaceURI, prefix, localName) self.__dict__['__nodeName'] = name self._ownerElement = None
def __init__(self, ownerDocument, data): FtNode.__init__(self, ownerDocument) self.__dict__['__nodeValue'] = data self._length = len(data)
def __init__(self, ownerDocument, nodeName, namespaceURI, prefix, localName): FtNode.__init__(self, ownerDocument, namespaceURI, prefix, localName); #Set our attributes self.__dict__['__attributes'] = implementation._4dom_createNamedNodeMap(ownerDocument) self.__dict__['__nodeName'] = nodeName
def __init__(self, ownerDocument): FtNode.__init__(self, ownerDocument) self.__dict__['__nodeName'] = '#document-fragment'
def __init__(self, ownerDocument, publicId, systemId, notationName): FtNode.__init__(self, ownerDocument) self.__dict__['__nodeName'] = '#entity' self.__dict__['publicId'] = publicId self.__dict__['systemId'] = systemId self.__dict__['notationName'] = notationName
def insertBefore(self, newChild, oldChild): self._4dom_addSingle(newChild) return FtNode.insertBefore(self, newChild, oldChild)
def appendChild(self, newChild): self._4dom_addSingle(newChild) return FtNode.appendChild(self, newChild)
def __init__(self, ownerDocument, name): #Note: the Entity's name is treated as nodeName FtNode.__init__(self, ownerDocument) self.__dict__['__nodeName'] = name
class Document(FtNode): #Base node type for this class nodeType = Node.DOCUMENT_NODE nodeName = "#document" #This is for validation that the proper nodes are added _allowedChildren = [ Node.PROCESSING_INSTRUCTION_NODE, Node.COMMENT_NODE, Node.ELEMENT_NODE, Node.DOCUMENT_TYPE_NODE ] def __init__(self, doctype): FtNode.__init__(self, None) self.__dict__['__doctype'] = None self.__dict__['__implementation'] = implementation self.__dict__['__documentElement'] = None self.__dict__['_singleChildren'] = { Node.ELEMENT_NODE: '__documentElement', Node.DOCUMENT_TYPE_NODE: '__doctype' } self._4dom_setDocumentType(doctype) ### Attribute Methods ### def _get_doctype(self): return self.__dict__['__doctype'] def _get_implementation(self): return self.__dict__['__implementation'] def _get_documentElement(self): return self.__dict__['__documentElement'] def _get_ownerDocument(self): return self ### Methods ### def createAttribute(self, name): if not get_name_pattern().match(name): raise InvalidCharacterErr() import Attr return Attr.Attr(self, name, EMPTY_NAMESPACE, None, None) def createCDATASection(self, data): from CDATASection import CDATASection return CDATASection(self, data) def createComment(self, data): from Comment import Comment return Comment(self, data) def createDocumentFragment(self): from DocumentFragment import DocumentFragment return DocumentFragment(self) def createElement(self, tagname): if not get_name_pattern().match(tagname): raise InvalidCharacterErr() from Element import Element return Element(self, tagname, EMPTY_NAMESPACE, None, None) def createEntityReference(self, name): if not get_name_pattern().match(name): raise InvalidCharacterErr() from EntityReference import EntityReference return EntityReference(self, name) def createProcessingInstruction(self, target, data): if not get_name_pattern().match(target): raise InvalidCharacterErr() #FIXME: Unicode support # Technically, chacters from the unicode surrogate blocks are illegal. #for c in target: # if c in unicode_surrogate_blocks: # raise InvalidCharacterErr() from ProcessingInstruction import ProcessingInstruction return ProcessingInstruction(self, target, data) def createTextNode(self, data): from Text import Text return Text(self, data) def getElementById(self, elementId): #FIXME: Must be implemented in the parser first return None def getElementsByTagName(self, tagName): nodeList = implementation._4dom_createNodeList([]) root = self.documentElement if root: if tagName == '*' or root.tagName == tagName: nodeList.append(root) nodeList.extend(list(root.getElementsByTagName(tagName))) return nodeList ### DOM Level 2 Methods ### def createAttributeNS(self, namespaceURI, qualifiedName): if not get_name_pattern().match(qualifiedName): raise InvalidCharacterErr() from Attr import Attr (prefix, localName) = SplitQName(qualifiedName) if prefix == 'xml' and namespaceURI != XML_NAMESPACE: raise NamespaceErr() if localName == 'xmlns': if namespaceURI != XMLNS_NAMESPACE: raise NamespaceErr() return Attr(self, qualifiedName, XMLNS_NAMESPACE, 'xmlns', prefix) elif namespaceURI == '': raise NamespaceErr("Use None instead of '' for empty namespace") else: if (not namespaceURI and prefix) or (not prefix and namespaceURI): raise NamespaceErr() return Attr(self, qualifiedName, namespaceURI, prefix, localName) def importNode(self, importedNode, deep): importType = importedNode.nodeType # No import allow per spec if importType in [Node.DOCUMENT_NODE, Node.DOCUMENT_TYPE_NODE]: raise NotSupportedErr() # Only the EntRef itself is copied since the source and destination # documents might have defined the entity differently #FIXME: If the document being imported into provides a definition for # this entity name, its value is assigned. # Need entity support for this!! elif importType == Node.ENTITY_REFERENCE_NODE: deep = 0 return importedNode.cloneNode(deep, newOwner=self) def createElementNS(self, namespaceURI, qualifiedName): from Element import Element if not get_name_pattern().match(qualifiedName): raise InvalidCharacterErr() (prefix, localName) = SplitQName(qualifiedName) if prefix == 'xml' and namespaceURI != XML_NAMESPACE: raise NamespaceErr() if prefix and not namespaceURI: raise NamespaceErr() elif namespaceURI == '': raise NamespaceErr("Use None instead of '' for empty namespace") return Element(self, qualifiedName, namespaceURI, prefix, localName) def getElementsByTagNameNS(self, namespaceURI, localName): if namespaceURI == '': raise NamespaceErr("Use None instead of '' for empty namespace") nodeList = implementation._4dom_createNodeList([]) root = self.documentElement if root: if ((namespaceURI == '*' or namespaceURI == root.namespaceURI) and (localName == '*' or localName == root.localName)): nodeList.append(root) nodeList.extend( list(root.getElementsByTagNameNS(namespaceURI, localName))) return nodeList ### Document Traversal Factory Functions ### def createNodeIterator(self, root, whatToShow, filter, entityReferenceExpansion): from NodeIterator import NodeIterator return NodeIterator(root, whatToShow, filter, entityReferenceExpansion) def createTreeWalker(self, root, whatToShow, filter, entityReferenceExpansion): from TreeWalker import TreeWalker return TreeWalker(root, whatToShow, filter, entityReferenceExpansion) ### Document Event Factory Functions ### def createEvent(self, eventType): import Event if eventType in Event.supportedEvents: #Only mutation events are supported return Event.MutationEvent(eventType) else: raise NotSupportedErr() ### Document Range Factory Functions ### def createRange(self): if not self.implementation.hasFeature('RANGE', '2.0'): raise NotSupportedErr() import Range return Range.Range(self) ### Overridden Methods ### def appendChild(self, newChild): self._4dom_addSingle(newChild) return FtNode.appendChild(self, newChild) def insertBefore(self, newChild, oldChild): self._4dom_addSingle(newChild) return FtNode.insertBefore(self, newChild, oldChild) def replaceChild(self, newChild, oldChild): if newChild.nodeType != Node.DOCUMENT_FRAGMENT_NODE: root = self.__dict__['__documentElement'] if root in [oldChild, newChild]: self.__dict__['__documentElement'] = None else: raise HierarchyRequestErr() replaced = FtNode.replaceChild(self, newChild, oldChild) if newChild.nodeType == Node.ELEMENT_NODE: self.__dict__['__documentElement'] = newChild if self.__dict__['__doctype']: self.__dict__['__doctype']._4dom_setName(newChild.nodeName) return replaced def removeChild(self, oldChild): node = FtNode.removeChild(self, oldChild) if self.documentElement == node: self.__dict__['__documentElement'] = None if self.__dict__['__doctype'] == node: self.__dict__['__doctype'] = None return node def cloneNode(self, deep): doc = self.__class__(None) if deep: for child in self.childNodes: clone = child.cloneNode(deep, newOwner=doc) if child.nodeType == Node.DOCUMENT_TYPE_NODE: doc._4dom_setDocumentType(clone) else: doc.appendChild(clone) return doc def __repr__(self): return "<%s Document at %x>" % ( (self.isXml() and 'XML' or 'HTML'), id(self)) ### Internal Methods ### def _4dom_createEntity(self, publicId, systemId, notationName): from Entity import Entity return Entity(self, publicId, systemId, notationName) def _4dom_createNotation(self, publicId, systemId, name): from Notation import Notation return Notation(self, publicId, systemId, name) def _4dom_setDocumentType(self, doctype): if not self.__dict__['__doctype'] and doctype is not None: self.__dict__['__doctype'] = doctype doctype._4dom_setOwnerDocument(self) return FtNode.appendChild(self, doctype) def _4dom_addSingle(self, node): '''Make sure only one Element node is added to a Document''' if node.nodeType == Node.ELEMENT_NODE: self._4dom_validateNode(node) if node.parentNode != None: node.parentNode.removeChild(node) if self.__dict__['__documentElement']: raise HierarchyRequestErr() self.__dict__['__documentElement'] = node if self.__dict__['__doctype']: self.__dict__['__doctype']._4dom_setName(node.nodeName) ### Helper Functions for Pickling ### def __getinitargs__(self): return (None, ) def __getstate__(self): return (self.childNodes, self.doctype, self.documentElement) def __setstate__(self, (children, doctype, root)): FtNode.__setstate__(self, children) self.__dict__['__doctype'] = doctype self.__dict__['__documentElement'] = root return
def _4dom_setDocumentType(self, doctype): if not self.__dict__['__doctype'] and doctype is not None: self.__dict__['__doctype'] = doctype doctype._4dom_setOwnerDocument(self) return FtNode.appendChild(self, doctype)
class Element(FtNode): nodeType = Node.ELEMENT_NODE _allowedChildren = [Node.ELEMENT_NODE, Node.TEXT_NODE, Node.COMMENT_NODE, Node.PROCESSING_INSTRUCTION_NODE, Node.CDATA_SECTION_NODE, Node.ENTITY_REFERENCE_NODE ] def __init__(self, ownerDocument, nodeName, namespaceURI, prefix, localName): FtNode.__init__(self, ownerDocument, namespaceURI, prefix, localName); #Set our attributes self.__dict__['__attributes'] = implementation._4dom_createNamedNodeMap(ownerDocument) self.__dict__['__nodeName'] = nodeName ### Attribute Methods ### def _get_tagName(self): return self.__dict__['__nodeName'] ### Methods ### def getAttribute(self, name): att = self.attributes.getNamedItem(name) return att and att.value or '' def getAttributeNode(self, name): return self.attributes.getNamedItem(name) def getElementsByTagName(self, tagName): nodeList = implementation._4dom_createNodeList() elements = filter(lambda node, type=Node.ELEMENT_NODE: node.nodeType == type, self.childNodes) for element in elements: if tagName == '*' or element.tagName == tagName: nodeList.append(element) nodeList.extend(list(element.getElementsByTagName(tagName))) return nodeList def hasAttribute(self, name): return self.attributes.getNamedItem(name) is not None def removeAttribute(self, name): # Return silently if no node node = self.attributes.getNamedItem(name) if node: self.removeAttributeNode(node) def removeAttributeNode(self, node): # NamedNodeMap will raise exception if needed if node.namespaceURI is None: self.attributes.removeNamedItem(node.name) else: self.attributes.removeNamedItemNS(node.namespaceURI, node.localName) node._4dom_setOwnerElement(None) self._4dom_fireMutationEvent('DOMAttrModified', relatedNode=node, attrName=node.name, attrChange=Event.MutationEvent.REMOVAL) self._4dom_fireMutationEvent('DOMSubtreeModified') return node def setAttribute(self, name, value): if not IsDOMString(value): raise SyntaxErr() if not g_namePattern.match(name): raise InvalidCharacterErr() attr = self.attributes.getNamedItem(name) if attr: attr.value = value else: attr = self.ownerDocument.createAttribute(name) attr.value = value self.setAttributeNode(attr) # the mutation event is fired in Attr.py def setAttributeNode(self, node): if node.ownerDocument != self.ownerDocument: raise WrongDocumentErr() if node.ownerElement != None: raise InuseAttributeErr() old = self.attributes.getNamedItem(node.name) if old: self._4dom_fireMutationEvent('DOMAttrModified', relatedNode=old, prevValue=old.value, attrName=old.name, attrChange=Event.MutationEvent.REMOVAL) self.attributes.setNamedItem(node) node._4dom_setOwnerElement(self) self._4dom_fireMutationEvent('DOMAttrModified', relatedNode=node, newValue=node.value, attrName=node.name, attrChange=Event.MutationEvent.ADDITION) self._4dom_fireMutationEvent('DOMSubtreeModified') return old ### DOM Level 2 Methods ### def getAttributeNS(self, namespaceURI, localName): attr = self.attributes.getNamedItemNS(namespaceURI, localName) return attr and attr.value or '' def getAttributeNodeNS(self, namespaceURI, localName): return self.attributes.getNamedItemNS(namespaceURI, localName) def getElementsByTagNameNS(self, namespaceURI, localName): nodeList = implementation._4dom_createNodeList() elements = filter(lambda node, type=Node.ELEMENT_NODE: node.nodeType == type, self.childNodes) for element in elements: if ((namespaceURI == '*' or element.namespaceURI == namespaceURI) and (localName == '*' or element.localName == localName)): nodeList.append(element) nodeList.extend(list(element.getElementsByTagNameNS(namespaceURI, localName))) return nodeList def hasAttributeNS(self, namespaceURI, localName): return self.attributes.getNamedItemNS(namespaceURI, localName) != None def removeAttributeNS(self, namespaceURI, localName): # Silently return if not attribute node = self.attributes.getNamedItemNS(namespaceURI, localName) if node: self.removeAttributeNode(node) return def setAttributeNS(self, namespaceURI, qualifiedName, value): if not IsDOMString(value): raise SyntaxErr() if not g_namePattern.match(qualifiedName): raise InvalidCharacterErr() prefix, localName = SplitQName(qualifiedName) attr = self.attributes.getNamedItemNS(namespaceURI, localName) if attr: attr.value = value else: attr = self.ownerDocument.createAttributeNS(namespaceURI, qualifiedName) attr.value = value self.setAttributeNodeNS(attr) return def setAttributeNodeNS(self, node): if self.ownerDocument != node.ownerDocument: raise WrongDocumentErr() if node.ownerElement != None: raise InuseAttributeErr() old = self.attributes.getNamedItemNS(node.namespaceURI, node.localName) if old: self._4dom_fireMutationEvent('DOMAttrModified', relatedNode=old, prevValue=old.value, attrName=old.name, attrChange=Event.MutationEvent.REMOVAL) self.attributes.setNamedItemNS(node) node._4dom_setOwnerElement(self) self._4dom_fireMutationEvent('DOMAttrModified', relatedNode=node, newValue=node.value, attrName=node.name, attrChange=Event.MutationEvent.ADDITION) self._4dom_fireMutationEvent('DOMSubtreeModified') return old ### Overridden Methods ### def __repr__(self): return "<Element Node at %x: Name='%s' with %d attributes and %d children>" % ( id(self), self.nodeName, len(self.attributes), len(self.childNodes) ) # Behind the back setting of element's ownerDocument # Also sets the owner of the NamedNodeMaps def _4dom_setOwnerDocument(self, newOwner): self.__dict__['__ownerDocument'] = newOwner self.__dict__['__attributes']._4dom_setOwnerDocument(newOwner) ### Helper Functions For Cloning ### def _4dom_clone(self, owner): e = self.__class__(owner, self.nodeName, self.namespaceURI, self.prefix, self.localName) for attr in self.attributes: clone = attr._4dom_clone(owner) if clone.localName is None: e.attributes.setNamedItem(clone) else: e.attributes.setNamedItemNS(clone) clone._4dom_setOwnerElement(self) return e def __getinitargs__(self): return (self.ownerDocument, self.nodeName, self.namespaceURI, self.prefix, self.localName ) def __getstate__(self): return (self.childNodes, self.attributes) def __setstate__(self, (children, attrs)): FtNode.__setstate__(self, children) self.__dict__['__attributes'] = attrs for attr in attrs: attr._4dom_setOwnerElement(self)
def __init__(self,ownerDocument,target,data): FtNode.__init__(self,ownerDocument,EMPTY_NAMESPACE,'','') self.__dict__['__nodeName'] = target self.__dict__['__nodeValue'] = data
def __init__(self, ownerDocument, publicId, systemId, name): FtNode.__init__(self, ownerDocument) self.__dict__['__nodeName'] = name self.__dict__['publicId'] = publicId self.__dict__['systemId'] = systemId
def __init__(self, ownerDocument, target, data): FtNode.__init__(self, ownerDocument, '', '', '') self.__dict__['__nodeName'] = target self.__dict__['__nodeValue'] = data