mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Do not allow empty qualifiedName in createDocument.
Rearrange pulldom to create documents with root element. Provide clear methods so that the ContentHandler releases its hold on the document.
This commit is contained in:
parent
269b83bc05
commit
b417be2ad9
2 changed files with 47 additions and 19 deletions
|
@ -651,17 +651,23 @@ class DOMImplementation:
|
||||||
doc = Document()
|
doc = Document()
|
||||||
if doctype is None:
|
if doctype is None:
|
||||||
doctype = self.createDocumentType(qualifiedName, None, None)
|
doctype = self.createDocumentType(qualifiedName, None, None)
|
||||||
if qualifiedName:
|
if not qualifiedName:
|
||||||
prefix, localname = _nssplit(qualifiedName)
|
# The spec is unclear what to raise here; SyntaxErr
|
||||||
if prefix == "xml" \
|
# would be the other obvious candidate. Since Xerces raises
|
||||||
and namespaceURI != "http://www.w3.org/XML/1998/namespace":
|
# InvalidCharacterErr, and since SyntaxErr is not listed
|
||||||
raise xml.dom.NamespaceErr("illegal use of 'xml' prefix")
|
# for createDocument, that seems to be the better choice.
|
||||||
if prefix and not namespaceURI:
|
# XXX: need to check for illegal characters here and in
|
||||||
raise xml.dom.NamespaceErr(
|
# createElement.
|
||||||
"illegal use of prefix without namespaces")
|
raise xml.dom.InvalidCharacterErr("Element with no name")
|
||||||
element = doc.createElementNS(namespaceURI, qualifiedName)
|
prefix, localname = _nssplit(qualifiedName)
|
||||||
doc.appendChild(element)
|
if prefix == "xml" \
|
||||||
# XXX else, raise an error? Empty qname is illegal in the DOM spec!
|
and namespaceURI != "http://www.w3.org/XML/1998/namespace":
|
||||||
|
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)
|
||||||
doctype.parentNode = doc
|
doctype.parentNode = doc
|
||||||
doc.doctype = doctype
|
doc.doctype = doctype
|
||||||
doc.implementation = self
|
doc.implementation = self
|
||||||
|
@ -761,6 +767,7 @@ def _doparse(func, args, kwargs):
|
||||||
events = apply(func, args, kwargs)
|
events = apply(func, args, kwargs)
|
||||||
toktype, rootNode = events.getEvent()
|
toktype, rootNode = events.getEvent()
|
||||||
events.expandNode(rootNode)
|
events.expandNode(rootNode)
|
||||||
|
events.clear()
|
||||||
return rootNode
|
return rootNode
|
||||||
|
|
||||||
def parse(*args, **kwargs):
|
def parse(*args, **kwargs):
|
||||||
|
|
|
@ -61,11 +61,17 @@ class PullDOM(xml.sax.ContentHandler):
|
||||||
tagName = prefix + ":" + localname
|
tagName = prefix + ":" + localname
|
||||||
else:
|
else:
|
||||||
tagName = localname
|
tagName = localname
|
||||||
node = self.document.createElementNS(uri, tagName)
|
if self.document:
|
||||||
|
node = self.document.createElementNS(uri, tagName)
|
||||||
|
else:
|
||||||
|
node = self.buildDocument(uri, tagName)
|
||||||
else:
|
else:
|
||||||
# When the tagname is not prefixed, it just appears as
|
# When the tagname is not prefixed, it just appears as
|
||||||
# localname
|
# localname
|
||||||
node = self.document.createElement(localname)
|
if self.document:
|
||||||
|
node = self.document.createElement(localname)
|
||||||
|
else:
|
||||||
|
node = self.buildDocument(None, localname)
|
||||||
|
|
||||||
for aname,value in attrs.items():
|
for aname,value in attrs.items():
|
||||||
a_uri, a_localname = aname
|
a_uri, a_localname = aname
|
||||||
|
@ -90,7 +96,10 @@ class PullDOM(xml.sax.ContentHandler):
|
||||||
self.lastEvent = self.lastEvent[1]
|
self.lastEvent = self.lastEvent[1]
|
||||||
|
|
||||||
def startElement(self, name, attrs):
|
def startElement(self, name, attrs):
|
||||||
node = self.document.createElement(name)
|
if self.document:
|
||||||
|
node = self.document.createElement(name)
|
||||||
|
else:
|
||||||
|
node = self.buildDocument(None, name)
|
||||||
|
|
||||||
for aname,value in attrs.items():
|
for aname,value in attrs.items():
|
||||||
attr = self.document.createAttribute(aname)
|
attr = self.document.createAttribute(aname)
|
||||||
|
@ -127,23 +136,28 @@ class PullDOM(xml.sax.ContentHandler):
|
||||||
self.lastEvent = self.lastEvent[1]
|
self.lastEvent = self.lastEvent[1]
|
||||||
|
|
||||||
def startDocument(self):
|
def startDocument(self):
|
||||||
publicId = systemId = None
|
|
||||||
if self._locator:
|
|
||||||
publicId = self._locator.getPublicId()
|
|
||||||
systemId = self._locator.getSystemId()
|
|
||||||
if self.documentFactory is None:
|
if self.documentFactory is None:
|
||||||
import xml.dom.minidom
|
import xml.dom.minidom
|
||||||
self.documentFactory = xml.dom.minidom.Document.implementation
|
self.documentFactory = xml.dom.minidom.Document.implementation
|
||||||
node = self.documentFactory.createDocument(None, publicId, systemId)
|
|
||||||
|
def buildDocument(self, uri, tagname):
|
||||||
|
# Can't do that in startDocument, since we need the tagname
|
||||||
|
# XXX: obtain DocumentType
|
||||||
|
node = self.documentFactory.createDocument(uri, tagname, None)
|
||||||
self.document = node
|
self.document = node
|
||||||
self.lastEvent[1] = [(START_DOCUMENT, node), None]
|
self.lastEvent[1] = [(START_DOCUMENT, node), None]
|
||||||
self.lastEvent = self.lastEvent[1]
|
self.lastEvent = self.lastEvent[1]
|
||||||
self.push(node)
|
self.push(node)
|
||||||
|
return node.firstChild
|
||||||
|
|
||||||
def endDocument(self):
|
def endDocument(self):
|
||||||
self.lastEvent[1] = [(END_DOCUMENT, self.document), None]
|
self.lastEvent[1] = [(END_DOCUMENT, self.document), None]
|
||||||
self.pop()
|
self.pop()
|
||||||
|
|
||||||
|
def clear(self):
|
||||||
|
"clear(): Explicitly release parsing structures"
|
||||||
|
self.document = None
|
||||||
|
|
||||||
class ErrorHandler:
|
class ErrorHandler:
|
||||||
def warning(self, exception):
|
def warning(self, exception):
|
||||||
print exception
|
print exception
|
||||||
|
@ -199,6 +213,13 @@ class DOMEventStream:
|
||||||
self.pulldom.firstEvent[1] = self.pulldom.firstEvent[1][1]
|
self.pulldom.firstEvent[1] = self.pulldom.firstEvent[1][1]
|
||||||
return rc
|
return rc
|
||||||
|
|
||||||
|
def clear(self):
|
||||||
|
"clear(): Explicitly release parsing objects"
|
||||||
|
self.pulldom.clear()
|
||||||
|
del self.pulldom
|
||||||
|
self.parser = None
|
||||||
|
self.stream = None
|
||||||
|
|
||||||
class SAX2DOM(PullDOM):
|
class SAX2DOM(PullDOM):
|
||||||
|
|
||||||
def startElementNS(self, name, tagName , attrs):
|
def startElementNS(self, name, tagName , attrs):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue