Here is the class declaration. Instances contain an
attribute named .node that will
contain the actual DOM Document node.
# - - - - - c l a s s D o c u m e n t - - - - -
class Document:
"""Represents an XML document.
State/invariants:
.doctype: [ as passed to constructor, read-only ]
.nsMap: [ as passed to constructor, read-only ]
.dom: [ the DOMImplementation object ]
.node: [ the DOM Document node ]
"""
Here's the class constructor:
# - - - D o c u m e n t . _ _ i n i t _ _ - - -
def __init__ ( self, rootGI, doctype=None, nsMap=None ):
"""Constructor for Document.
[ (domlette is Ft.Xml.Domlette) and
(rootGI is the root element's qualified name) and
(doctype is a DocumentType object or None) ->
return a new Document object with a rootGI Element
child and document type given by doctype ]
"""
#-- 1 --
# [ domlette is Ft.Xml.Domlette ->
# self.doctype := doctype
# self.nsMap := nsMap
# self.dom := a DOMImplementation object ]
self.doctype = doctype
self.nsMap = nsMap
self.dom = domlette.implementation
#-- 2 --
# [ if rootGI has a namespace prefix that is not a key in
# self.nsMap ->
# raise KeyError
# else if rootGI has a namespace prefix ->
# rootNsuri := corresponding namespace URI from
# self.nsMap
# else ->
# rootNsuri := None ]
rootNsuri, rootLocal = self.splitQName ( rootGI )
#-- 3 --
# [ self.dom is a DOMImplementation object ->
# self.node := a new DOM Document node with default
# namespace and root element rootGI ]
self.node = self.dom.createDocument ( rootNsuri,
rootGI, None )
If the caller gave us a DocumentType
object, we copy its public and system identifiers
over into the DOM Document object.
#-- 4 --
if doctype:
self.node.publicId = doctype.publicId
self.node.systemId = doctype.systemId
Finally, we create the root node. Here, we take
advantage of the polymorphism of the Element constructor: it can take a Document
object as the parent. In that case, instead of creating
a new DOM Document object, it takes the
one found in self.node.documentElement and
wraps that instead.
#-- 5 --
# [ if rootGI has a namespace prefix that is not a key in
# nsMap ->
# raise KeyError
# else ->
# self.root := a new Element with qName rootGI ]
self.root = Element ( self, rootGI )