[Python-checkins] CVS: python/dist/src/Lib/xml/dom minidom.py,1.22,1.23

Guido van Rossum gvanrossum@users.sourceforge.net
Mon, 05 Feb 2001 11:17:52 -0800


Update of /cvsroot/python/python/dist/src/Lib/xml/dom
In directory usw-pr-cvs1:/tmp/cvs-serv29651

Modified Files:
	minidom.py 
Log Message:
A couple of changes to make this more conformant.  MvL and Uche agree.
This will make it incompatible with the version found in Python 2.0.
Does this need to be done to PyXML too?

Changes that might break existing code are marked with (!) below.

- Formatting nit: no spaces inside parentheses: foo( a ) -> foo(a).

- Break long lines.

- (!) Fix getAttribute() and getAttributeNS() to return "" instead of
  raising KeyError when the attribute is not found.

- (!) Fix getAttributeNodeNS() to return None instead of raising
  KeyError.  (Curiously, getAttributeNode() already did this.)

- Added hasAttributes(), which returns true iff the node has any
  attributes.  )This is DOM level 3.)

- (!) In createDocument(), if the qualified name is not empty,
  actually create and insert the first element with that name (this
  will become doc.documentElement).  MvL believes that it should be an
  error to specify an empty qualified name; I'm not going there today,
  since it would require making a matching change to pulldom.  Maybe
  MvL will do this.

- In Document.writexml(), insert an xml declaration at the top.  (This
  doesn't include the encoding since there's no way to specify the
  encoding.  If that's preferred, all writexml() methods should be
  fixed to support an optional encoding argument that they pass to
  each other -- and they should use it to encode all text they write,
  too.  Later.)


Index: minidom.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/xml/dom/minidom.py,v
retrieving revision 1.22
retrieving revision 1.23
diff -C2 -r1.22 -r1.23
*** minidom.py	2001/02/02 19:40:19	1.22
--- minidom.py	2001/02/05 19:17:50	1.23
***************
*** 2,8 ****
  minidom.py -- a lightweight DOM implementation.
  
! parse( "foo.xml" )
  
! parseString( "<foo><bar/></foo>" )
  
  Todo:
--- 2,8 ----
  minidom.py -- a lightweight DOM implementation.
  
! parse("foo.xml")
  
! parseString("<foo><bar/></foo>")
  
  Todo:
***************
*** 48,52 ****
              if Node.debug is None:
                  Node.debug = _get_StringIO()
!                 #open( "debug4.out", "w" )
              Node.debug.write("create %s\n" % index)
  
--- 48,52 ----
              if Node.debug is None:
                  Node.debug = _get_StringIO()
!                 #open("debug4.out", "w")
              Node.debug.write("create %s\n" % index)
  
***************
*** 103,107 ****
          if newChild.nodeType not in self.childNodeTypes:
              raise HierarchyRequestErr, \
!                   "%s cannot be child of %s" % (repr(newChild), repr(self) )
          if newChild.parentNode is not None:
              newChild.parentNode.removeChild(newChild)
--- 103,107 ----
          if newChild.nodeType not in self.childNodeTypes:
              raise HierarchyRequestErr, \
!                   "%s cannot be child of %s" % (repr(newChild), repr(self))
          if newChild.parentNode is not None:
              newChild.parentNode.removeChild(newChild)
***************
*** 126,130 ****
          if node.nodeType not in self.childNodeTypes:
              raise HierarchyRequestErr, \
!                   "%s cannot be child of %s" % (repr(node), repr(self) )
          if node.parentNode is not None:
              node.parentNode.removeChild(node)
--- 126,130 ----
          if node.nodeType not in self.childNodeTypes:
              raise HierarchyRequestErr, \
!                   "%s cannot be child of %s" % (repr(node), repr(self))
          if node.parentNode is not None:
              node.parentNode.removeChild(node)
***************
*** 144,148 ****
          if newChild.nodeType not in self.childNodeTypes:
              raise HierarchyRequestErr, \
!                   "%s cannot be child of %s" % (repr(newChild), repr(self) )
          if newChild.parentNode is not None:
              newChild.parentNode.removeChild(newChild)
--- 144,148 ----
          if newChild.nodeType not in self.childNodeTypes:
              raise HierarchyRequestErr, \
!                   "%s cannot be child of %s" % (repr(newChild), repr(self))
          if newChild.parentNode is not None:
              newChild.parentNode.removeChild(newChild)
***************
*** 436,443 ****
  
      def getAttribute(self, attname):
!         return self._attrs[attname].value
  
      def getAttributeNS(self, namespaceURI, localName):
!         return self._attrsNS[(namespaceURI, localName)].value
  
      def setAttribute(self, attname, value):
--- 436,449 ----
  
      def getAttribute(self, attname):
!         try:
!             return self._attrs[attname].value
!         except KeyError:
!             return ""
  
      def getAttributeNS(self, namespaceURI, localName):
!         try:
!             return self._attrsNS[(namespaceURI, localName)].value
!         except KeyError:
!             return ""
  
      def setAttribute(self, attname, value):
***************
*** 458,462 ****
  
      def getAttributeNodeNS(self, namespaceURI, localName):
!         return self._attrsNS[(namespaceURI, localName)]
  
      def setAttributeNode(self, attr):
--- 464,468 ----
  
      def getAttributeNodeNS(self, namespaceURI, localName):
!         return self._attrsNS.get((namespaceURI, localName))
  
      def setAttributeNode(self, attr):
***************
*** 529,532 ****
--- 535,544 ----
          return AttributeList(self._attrs, self._attrsNS)
  
+     def hasAttributes(self):
+         if self._attrs or self._attrsNS:
+             return 1
+         else:
+             return 0
+ 
  class Comment(Node):
      nodeType = Node.COMMENT_NODE
***************
*** 625,629 ****
      def createDocument(self, namespaceURI, qualifiedName, doctype):
          if doctype and doctype.parentNode is not None:
!             raise xml.dom.WrongDocumentErr("doctype object owned by another DOM tree")
          doc = Document()
          if doctype is None:
--- 637,642 ----
      def createDocument(self, namespaceURI, qualifiedName, doctype):
          if doctype and doctype.parentNode is not None:
!             raise xml.dom.WrongDocumentErr(
!                 "doctype object owned by another DOM tree")
          doc = Document()
          if doctype is None:
***************
*** 635,639 ****
                  raise xml.dom.NamespaceErr("illegal use of 'xml' prefix")
              if prefix and not namespaceURI:
!                 raise xml.dom.NamespaceErr("illegal use of prefix without namespaces")
          doctype.parentNode = doc
          doc.doctype = doctype
--- 648,656 ----
                  raise xml.dom.NamespaceErr("illegal use of 'xml' prefix")
              if prefix and not namespaceURI:
!                 raise xml.dom.NamespaceErr(
!                     "illegal use of prefix without namespaces")
!             element = doc.createElementNS(namespaceURI, qualifiedName)
!             doc.appendChild(element)
!         # XXX else, raise an error?  Empty qname is illegal in the DOM spec!
          doctype.parentNode = doc
          doc.doctype = doctype
***************
*** 663,667 ****
          if node.nodeType not in self.childNodeTypes:
              raise HierarchyRequestErr, \
!                   "%s cannot be child of %s" % (repr(node), repr(self) )
          if node.parentNode is not None:
              node.parentNode.removeChild(node)
--- 680,684 ----
          if node.nodeType not in self.childNodeTypes:
              raise HierarchyRequestErr, \
!                   "%s cannot be child of %s" % (repr(node), repr(self))
          if node.parentNode is not None:
              node.parentNode.removeChild(node)
***************
*** 669,673 ****
          if node.nodeType == Node.ELEMENT_NODE \
             and self._get_documentElement():
!             raise xml.dom.HierarchyRequestErr("two document elements disallowed")
          return Node.appendChild(self, node)
  
--- 686,691 ----
          if node.nodeType == Node.ELEMENT_NODE \
             and self._get_documentElement():
!             raise xml.dom.HierarchyRequestErr(
!                 "two document elements disallowed")
          return Node.appendChild(self, node)
  
***************
*** 721,724 ****
--- 739,743 ----
  
      def writexml(self, writer):
+         writer.write('<?xml version="1.0" ?>\n')
          for node in self.childNodes:
              node.writexml(writer)