[Python-checkins] CVS: python/dist/src/Lib/xml/dom pulldom.py,1.11,1.12

Fred L. Drake python-dev@python.org
Thu, 14 Dec 2000 10:00:22 -0800


Update of /cvsroot/python/python/dist/src/Lib/xml/dom
In directory slayer.i.sourceforge.net:/tmp/cvs-serv22990

Modified Files:
	pulldom.py 
Log Message:

Adjust PullDOM to use a DOMImplementation instance to create new Document
objects; uses minidom if one is not provided to the constructor.

parse():  Pick up the default_bufsize default value dynamically so that
          the value in the module may be (meaningfully) changed at runtime.

This (partially) closes patch #102477.


Index: pulldom.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/xml/dom/pulldom.py,v
retrieving revision 1.11
retrieving revision 1.12
diff -C2 -r1.11 -r1.12
*** pulldom.py	2000/10/23 18:09:50	1.11
--- pulldom.py	2000/12/14 18:00:18	1.12
***************
*** 1,4 ****
! import minidom
! import xml.sax,xml.sax.handler
  
  START_ELEMENT = "START_ELEMENT"
--- 1,4 ----
! import xml.sax
! import xml.sax.handler
  
  START_ELEMENT = "START_ELEMENT"
***************
*** 12,16 ****
  
  class PullDOM(xml.sax.ContentHandler):
!     def __init__(self):
          self.firstEvent = [None, None]
          self.lastEvent = self.firstEvent
--- 12,20 ----
  
  class PullDOM(xml.sax.ContentHandler):
!     _locator = None
!     document = None
! 
!     def __init__(self, documentFactory=None):
!         self.documentFactory = documentFactory
          self.firstEvent = [None, None]
          self.lastEvent = self.firstEvent
***************
*** 18,32 ****
          self._current_context = self._ns_contexts[-1]
  
!     def setDocumentLocator(self, locator): pass
  
      def startPrefixMapping(self, prefix, uri):
          self._ns_contexts.append(self._current_context.copy())
!         self._current_context[uri] = prefix
  
      def endPrefixMapping(self, prefix):
!         del self._ns_contexts[-1]
  
      def startElementNS(self, name, tagName , attrs):
!         uri,localname = name
          if uri:
              # When using namespaces, the reader may or may not
--- 22,37 ----
          self._current_context = self._ns_contexts[-1]
  
!     def setDocumentLocator(self, locator):
!         self._locator = locator
  
      def startPrefixMapping(self, prefix, uri):
          self._ns_contexts.append(self._current_context.copy())
!         self._current_context[uri] = prefix or ''
  
      def endPrefixMapping(self, prefix):
!         self._current_context = self._ns_contexts.pop()
  
      def startElementNS(self, name, tagName , attrs):
!         uri, localname = name
          if uri:
              # When using namespaces, the reader may or may not
***************
*** 51,56 ****
              node.setAttributeNode(attr)
  
!         parent = self.curNode
!         node.parentNode = parent
          self.curNode = node
  
--- 56,60 ----
              node.setAttributeNode(attr)
  
!         node.parentNode = self.curNode
          self.curNode = node
  
***************
*** 64,68 ****
          self.lastEvent = self.lastEvent[1]
          #self.events.append((END_ELEMENT, node))
!         self.curNode = node.parentNode
  
      def startElement(self, name, attrs):
--- 68,72 ----
          self.lastEvent = self.lastEvent[1]
          #self.events.append((END_ELEMENT, node))
!         self.curNode = self.curNode.parentNode
  
      def startElement(self, name, attrs):
***************
*** 74,79 ****
              node.setAttributeNode(attr)
  
!         parent = self.curNode
!         node.parentNode = parent
          self.curNode = node
  
--- 78,82 ----
              node.setAttributeNode(attr)
  
!         node.parentNode = self.curNode
          self.curNode = node
  
***************
*** 107,111 ****
  
      def ignorableWhitespace(self, chars):
!         node = self.document.createTextNode(chars[start:start + length])
          parent = self.curNode
          node.parentNode = parent
--- 110,114 ----
  
      def ignorableWhitespace(self, chars):
!         node = self.document.createTextNode(chars)
          parent = self.curNode
          node.parentNode = parent
***************
*** 122,127 ****
  
      def startDocument(self):
!         node = self.curNode = self.document = minidom.Document()
!         node.parentNode = None
          self.lastEvent[1] = [(START_DOCUMENT, node), None]
          self.lastEvent = self.lastEvent[1]
--- 125,137 ----
  
      def startDocument(self):
!         publicId = systemId = None
!         if self._locator:
!             publicId = self._locator.getPublicId()
!             systemId = self._locator.getSystemId()
!         if self.documentFactory is None:
!             import xml.dom.minidom
!             self.documentFactory = xml.dom.minidom.Document.implementation
!         node = self.documentFactory.createDocument(None, publicId, systemId)
!         self.curNode = self.document = node
          self.lastEvent[1] = [(START_DOCUMENT, node), None]
          self.lastEvent = self.lastEvent[1]
***************
*** 129,139 ****
  
      def endDocument(self):
!         assert not self.curNode.parentNode
!         for node in self.curNode.childNodes:
!             if node.nodeType == node.ELEMENT_NODE:
!                 self.document.documentElement = node
!         #if not self.document.documentElement:
!         #    raise Error, "No document element"
! 
          self.lastEvent[1] = [(END_DOCUMENT, node), None]
          #self.events.append((END_DOCUMENT, self.curNode))
--- 139,147 ----
  
      def endDocument(self):
!         assert self.curNode.parentNode is None, \
!                "not all elements have been properly closed"
!         assert self.curNode.documentElement is not None, \
!                "document does not contain a root element"
!         node = self.curNode.documentElement
          self.lastEvent[1] = [(END_DOCUMENT, node), None]
          #self.events.append((END_DOCUMENT, self.curNode))
***************
*** 157,161 ****
          self.pulldom = PullDOM()
          # This content handler relies on namespace support
!         self.parser.setFeature(xml.sax.handler.feature_namespaces,1)
          self.parser.setContentHandler(self.pulldom)
  
--- 165,169 ----
          self.pulldom = PullDOM()
          # This content handler relies on namespace support
!         self.parser.setFeature(xml.sax.handler.feature_namespaces, 1)
          self.parser.setContentHandler(self.pulldom)
  
***************
*** 180,184 ****
              self.pulldom.lastEvent = self.pulldom.firstEvent
          while not self.pulldom.firstEvent[1]:
!             buf=self.stream.read(self.bufsize)
              if not buf:
                  #FIXME: why doesn't Expat close work?
--- 188,192 ----
              self.pulldom.lastEvent = self.pulldom.firstEvent
          while not self.pulldom.firstEvent[1]:
!             buf = self.stream.read(self.bufsize)
              if not buf:
                  #FIXME: why doesn't Expat close work?
***************
*** 215,222 ****
          node.parentNode.appendChild(node)
  
  default_bufsize = (2 ** 14) - 20
  
! def parse(stream_or_string, parser=None, bufsize=default_bufsize):
!     if type(stream_or_string) is type(""):
          stream = open(stream_or_string)
      else:
--- 223,233 ----
          node.parentNode.appendChild(node)
  
+ 
  default_bufsize = (2 ** 14) - 20
  
! def parse(stream_or_string, parser=None, bufsize=None):
!     if bufsize is None:
!         bufsize = default_bufsize
!     if type(stream_or_string) in [type(""), type(u"")]:
          stream = open(stream_or_string)
      else: