2000-12-14 14:00:18 -04:00
|
|
|
import xml.sax
|
|
|
|
import xml.sax.handler
|
2000-12-28 14:43:02 -04:00
|
|
|
import types
|
|
|
|
|
|
|
|
try:
|
|
|
|
_StringTypes = [types.StringType, types.UnicodeType]
|
|
|
|
except AttributeError:
|
|
|
|
_StringTypes = [types.StringType]
|
2000-06-29 16:39:57 -03:00
|
|
|
|
2000-09-24 02:21:58 -03:00
|
|
|
START_ELEMENT = "START_ELEMENT"
|
|
|
|
END_ELEMENT = "END_ELEMENT"
|
|
|
|
COMMENT = "COMMENT"
|
|
|
|
START_DOCUMENT = "START_DOCUMENT"
|
|
|
|
END_DOCUMENT = "END_DOCUMENT"
|
|
|
|
PROCESSING_INSTRUCTION = "PROCESSING_INSTRUCTION"
|
|
|
|
IGNORABLE_WHITESPACE = "IGNORABLE_WHITESPACE"
|
|
|
|
CHARACTERS = "CHARACTERS"
|
2000-06-29 16:39:57 -03:00
|
|
|
|
2000-09-24 18:54:14 -03:00
|
|
|
class PullDOM(xml.sax.ContentHandler):
|
2000-12-14 14:00:18 -04:00
|
|
|
_locator = None
|
|
|
|
document = None
|
|
|
|
|
|
|
|
def __init__(self, documentFactory=None):
|
|
|
|
self.documentFactory = documentFactory
|
2000-09-24 02:21:58 -03:00
|
|
|
self.firstEvent = [None, None]
|
|
|
|
self.lastEvent = self.firstEvent
|
2001-01-26 14:53:42 -04:00
|
|
|
self.elementStack = []
|
|
|
|
self.push = self.elementStack.append
|
|
|
|
try:
|
|
|
|
self.pop = self.elementStack.pop
|
|
|
|
except AttributeError:
|
|
|
|
# use class' pop instead
|
|
|
|
pass
|
2000-09-24 18:54:14 -03:00
|
|
|
self._ns_contexts = [{}] # contains uri -> prefix dicts
|
|
|
|
self._current_context = self._ns_contexts[-1]
|
2000-06-29 16:39:57 -03:00
|
|
|
|
2001-01-26 14:53:42 -04:00
|
|
|
def pop(self):
|
|
|
|
result = self.elementStack[-1]
|
2001-01-27 04:47:37 -04:00
|
|
|
del self.elementStack[-1]
|
2001-01-26 14:53:42 -04:00
|
|
|
return result
|
|
|
|
|
2000-12-14 14:00:18 -04:00
|
|
|
def setDocumentLocator(self, locator):
|
|
|
|
self._locator = locator
|
2000-06-29 16:39:57 -03:00
|
|
|
|
2000-09-24 18:54:14 -03:00
|
|
|
def startPrefixMapping(self, prefix, uri):
|
|
|
|
self._ns_contexts.append(self._current_context.copy())
|
2000-12-14 14:00:18 -04:00
|
|
|
self._current_context[uri] = prefix or ''
|
2000-09-24 18:54:14 -03:00
|
|
|
|
|
|
|
def endPrefixMapping(self, prefix):
|
2000-12-14 14:00:18 -04:00
|
|
|
self._current_context = self._ns_contexts.pop()
|
2000-09-24 18:54:14 -03:00
|
|
|
|
|
|
|
def startElementNS(self, name, tagName , attrs):
|
2000-12-14 14:00:18 -04:00
|
|
|
uri, localname = name
|
2000-10-06 19:36:03 -03:00
|
|
|
if uri:
|
2000-09-24 18:54:14 -03:00
|
|
|
# When using namespaces, the reader may or may not
|
|
|
|
# provide us with the original name. If not, create
|
|
|
|
# *a* valid tagName from the current context.
|
|
|
|
if tagName is None:
|
2001-02-05 14:50:15 -04:00
|
|
|
prefix = self._current_context[uri]
|
|
|
|
if prefix:
|
|
|
|
tagName = prefix + ":" + localname
|
|
|
|
else:
|
|
|
|
tagName = localname
|
2000-10-06 19:36:03 -03:00
|
|
|
node = self.document.createElementNS(uri, tagName)
|
2000-09-24 18:54:14 -03:00
|
|
|
else:
|
|
|
|
# When the tagname is not prefixed, it just appears as
|
2000-10-06 19:36:03 -03:00
|
|
|
# localname
|
|
|
|
node = self.document.createElement(localname)
|
2000-09-24 18:54:14 -03:00
|
|
|
|
|
|
|
for aname,value in attrs.items():
|
2000-10-06 19:36:03 -03:00
|
|
|
a_uri, a_localname = aname
|
|
|
|
if a_uri:
|
2001-02-05 14:50:15 -04:00
|
|
|
prefix = self._current_context[a_uri]
|
|
|
|
if prefix:
|
|
|
|
qname = prefix + ":" + a_localname
|
|
|
|
else:
|
|
|
|
qname = a_localname
|
2000-10-06 19:36:03 -03:00
|
|
|
attr = self.document.createAttributeNS(a_uri, qname)
|
2000-09-24 18:54:14 -03:00
|
|
|
else:
|
2000-10-06 19:36:03 -03:00
|
|
|
attr = self.document.createAttribute(a_localname)
|
2000-09-24 18:54:14 -03:00
|
|
|
attr.value = value
|
2000-10-06 19:36:03 -03:00
|
|
|
node.setAttributeNode(attr)
|
2000-10-23 15:09:50 -03:00
|
|
|
|
2000-09-24 02:21:58 -03:00
|
|
|
self.lastEvent[1] = [(START_ELEMENT, node), None]
|
|
|
|
self.lastEvent = self.lastEvent[1]
|
2001-01-26 14:53:42 -04:00
|
|
|
self.push(node)
|
2000-06-29 16:39:57 -03:00
|
|
|
|
2000-09-24 18:54:14 -03:00
|
|
|
def endElementNS(self, name, tagName):
|
2001-01-26 14:53:42 -04:00
|
|
|
self.lastEvent[1] = [(END_ELEMENT, self.pop()), None]
|
2000-09-24 02:21:58 -03:00
|
|
|
self.lastEvent = self.lastEvent[1]
|
2000-06-29 16:39:57 -03:00
|
|
|
|
2000-10-11 19:34:04 -03:00
|
|
|
def startElement(self, name, attrs):
|
|
|
|
node = self.document.createElement(name)
|
|
|
|
|
|
|
|
for aname,value in attrs.items():
|
|
|
|
attr = self.document.createAttribute(aname)
|
|
|
|
attr.value = value
|
|
|
|
node.setAttributeNode(attr)
|
2000-10-23 15:09:50 -03:00
|
|
|
|
2000-10-11 19:34:04 -03:00
|
|
|
self.lastEvent[1] = [(START_ELEMENT, node), None]
|
|
|
|
self.lastEvent = self.lastEvent[1]
|
2001-01-26 14:53:42 -04:00
|
|
|
self.push(node)
|
2000-10-11 19:34:04 -03:00
|
|
|
|
|
|
|
def endElement(self, name):
|
2001-01-26 14:53:42 -04:00
|
|
|
self.lastEvent[1] = [(END_ELEMENT, self.pop()), None]
|
2000-10-11 19:34:04 -03:00
|
|
|
self.lastEvent = self.lastEvent[1]
|
2000-10-23 15:09:50 -03:00
|
|
|
|
2000-09-24 02:21:58 -03:00
|
|
|
def comment(self, s):
|
|
|
|
node = self.document.createComment(s)
|
|
|
|
self.lastEvent[1] = [(COMMENT, node), None]
|
|
|
|
self.lastEvent = self.lastEvent[1]
|
|
|
|
|
|
|
|
def processingInstruction(self, target, data):
|
|
|
|
node = self.document.createProcessingInstruction(target, data)
|
2000-10-23 15:09:50 -03:00
|
|
|
|
2000-09-24 02:21:58 -03:00
|
|
|
self.lastEvent[1] = [(PROCESSING_INSTRUCTION, node), None]
|
|
|
|
self.lastEvent = self.lastEvent[1]
|
|
|
|
|
|
|
|
def ignorableWhitespace(self, chars):
|
2000-12-14 14:00:18 -04:00
|
|
|
node = self.document.createTextNode(chars)
|
2000-09-24 02:21:58 -03:00
|
|
|
self.lastEvent[1] = [(IGNORABLE_WHITESPACE, node), None]
|
|
|
|
self.lastEvent = self.lastEvent[1]
|
|
|
|
|
|
|
|
def characters(self, chars):
|
|
|
|
node = self.document.createTextNode(chars)
|
|
|
|
self.lastEvent[1] = [(CHARACTERS, node), None]
|
|
|
|
self.lastEvent = self.lastEvent[1]
|
|
|
|
|
|
|
|
def startDocument(self):
|
2000-12-14 14:00:18 -04:00
|
|
|
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)
|
2001-01-26 14:53:42 -04:00
|
|
|
self.document = node
|
2000-09-24 02:21:58 -03:00
|
|
|
self.lastEvent[1] = [(START_DOCUMENT, node), None]
|
|
|
|
self.lastEvent = self.lastEvent[1]
|
2001-01-26 14:53:42 -04:00
|
|
|
self.push(node)
|
2000-09-24 02:21:58 -03:00
|
|
|
|
|
|
|
def endDocument(self):
|
2001-01-26 14:53:42 -04:00
|
|
|
self.lastEvent[1] = [(END_DOCUMENT, self.document), None]
|
|
|
|
self.pop()
|
2000-06-29 16:39:57 -03:00
|
|
|
|
|
|
|
class ErrorHandler:
|
2000-09-24 02:21:58 -03:00
|
|
|
def warning(self, exception):
|
2000-06-29 16:39:57 -03:00
|
|
|
print exception
|
2000-09-24 02:21:58 -03:00
|
|
|
def error(self, exception):
|
2000-10-23 15:09:50 -03:00
|
|
|
raise exception
|
2000-09-24 02:21:58 -03:00
|
|
|
def fatalError(self, exception):
|
2000-10-23 15:09:50 -03:00
|
|
|
raise exception
|
2000-06-29 16:39:57 -03:00
|
|
|
|
|
|
|
class DOMEventStream:
|
2000-09-24 02:21:58 -03:00
|
|
|
def __init__(self, stream, parser, bufsize):
|
|
|
|
self.stream = stream
|
|
|
|
self.parser = parser
|
|
|
|
self.bufsize = bufsize
|
2000-06-29 16:39:57 -03:00
|
|
|
self.reset()
|
|
|
|
|
2000-09-24 02:21:58 -03:00
|
|
|
def reset(self):
|
2000-06-29 16:39:57 -03:00
|
|
|
self.pulldom = PullDOM()
|
2000-09-24 18:54:14 -03:00
|
|
|
# This content handler relies on namespace support
|
2000-12-14 14:00:18 -04:00
|
|
|
self.parser.setFeature(xml.sax.handler.feature_namespaces, 1)
|
2000-09-24 02:21:58 -03:00
|
|
|
self.parser.setContentHandler(self.pulldom)
|
2000-06-29 16:39:57 -03:00
|
|
|
|
2000-09-24 02:21:58 -03:00
|
|
|
def __getitem__(self, pos):
|
|
|
|
rc = self.getEvent()
|
|
|
|
if rc:
|
|
|
|
return rc
|
2000-06-29 16:39:57 -03:00
|
|
|
raise IndexError
|
|
|
|
|
2000-09-24 02:21:58 -03:00
|
|
|
def expandNode(self, node):
|
|
|
|
event = self.getEvent()
|
2001-01-26 14:53:42 -04:00
|
|
|
parents = [node]
|
2000-06-29 16:39:57 -03:00
|
|
|
while event:
|
2000-09-24 02:21:58 -03:00
|
|
|
token, cur_node = event
|
|
|
|
if cur_node is node:
|
|
|
|
return
|
2000-10-13 17:53:27 -03:00
|
|
|
if token != END_ELEMENT:
|
2001-01-26 14:53:42 -04:00
|
|
|
parents[-1].appendChild(cur_node)
|
|
|
|
if token == START_ELEMENT:
|
|
|
|
parents.append(cur_node)
|
|
|
|
elif token == END_ELEMENT:
|
|
|
|
del parents[-1]
|
2000-09-24 02:21:58 -03:00
|
|
|
event = self.getEvent()
|
|
|
|
|
|
|
|
def getEvent(self):
|
2000-06-29 16:39:57 -03:00
|
|
|
if not self.pulldom.firstEvent[1]:
|
2000-09-24 02:21:58 -03:00
|
|
|
self.pulldom.lastEvent = self.pulldom.firstEvent
|
2000-06-29 16:39:57 -03:00
|
|
|
while not self.pulldom.firstEvent[1]:
|
2000-12-14 14:00:18 -04:00
|
|
|
buf = self.stream.read(self.bufsize)
|
2000-06-29 16:39:57 -03:00
|
|
|
if not buf:
|
2001-01-27 04:34:21 -04:00
|
|
|
self.parser.close()
|
2000-06-29 16:39:57 -03:00
|
|
|
return None
|
2000-09-24 02:21:58 -03:00
|
|
|
self.parser.feed(buf)
|
|
|
|
rc = self.pulldom.firstEvent[1][0]
|
|
|
|
self.pulldom.firstEvent[1] = self.pulldom.firstEvent[1][1]
|
2000-06-29 16:39:57 -03:00
|
|
|
return rc
|
|
|
|
|
2000-10-13 17:53:27 -03:00
|
|
|
class SAX2DOM(PullDOM):
|
|
|
|
|
|
|
|
def startElementNS(self, name, tagName , attrs):
|
|
|
|
PullDOM.startElementNS(self, name, tagName, attrs)
|
2001-01-26 14:53:42 -04:00
|
|
|
curNode = self.elementStack[-1]
|
|
|
|
parentNode = self.elementStack[-2]
|
|
|
|
parentNode.appendChild(curNode)
|
2000-10-13 17:53:27 -03:00
|
|
|
|
|
|
|
def startElement(self, name, attrs):
|
|
|
|
PullDOM.startElement(self, name, attrs)
|
2001-01-26 14:53:42 -04:00
|
|
|
curNode = self.elementStack[-1]
|
|
|
|
parentNode = self.elementStack[-2]
|
|
|
|
parentNode.appendChild(curNode)
|
2000-10-13 17:53:27 -03:00
|
|
|
|
|
|
|
def processingInstruction(self, target, data):
|
|
|
|
PullDOM.processingInstruction(self, target, data)
|
|
|
|
node = self.lastEvent[0][1]
|
2001-01-26 14:53:42 -04:00
|
|
|
parentNode = self.elementStack[-1]
|
|
|
|
parentNode.appendChild(node)
|
2000-10-13 17:53:27 -03:00
|
|
|
|
|
|
|
def ignorableWhitespace(self, chars):
|
|
|
|
PullDOM.ignorableWhitespace(self, chars)
|
|
|
|
node = self.lastEvent[0][1]
|
2001-01-26 14:53:42 -04:00
|
|
|
parentNode = self.elementStack[-1]
|
|
|
|
parentNode.appendChild(node)
|
2000-10-13 17:53:27 -03:00
|
|
|
|
|
|
|
def characters(self, chars):
|
|
|
|
PullDOM.characters(self, chars)
|
|
|
|
node = self.lastEvent[0][1]
|
2001-01-26 14:53:42 -04:00
|
|
|
parentNode = self.elementStack[-1]
|
|
|
|
parentNode.appendChild(node)
|
2000-10-23 15:09:50 -03:00
|
|
|
|
2000-12-14 14:00:18 -04:00
|
|
|
|
2000-09-24 02:21:58 -03:00
|
|
|
default_bufsize = (2 ** 14) - 20
|
|
|
|
|
2000-12-14 14:00:18 -04:00
|
|
|
def parse(stream_or_string, parser=None, bufsize=None):
|
|
|
|
if bufsize is None:
|
|
|
|
bufsize = default_bufsize
|
2000-12-28 14:43:02 -04:00
|
|
|
if type(stream_or_string) in _StringTypes:
|
2000-09-24 02:21:58 -03:00
|
|
|
stream = open(stream_or_string)
|
2000-06-29 16:39:57 -03:00
|
|
|
else:
|
2000-09-24 02:21:58 -03:00
|
|
|
stream = stream_or_string
|
2000-10-23 15:09:50 -03:00
|
|
|
if not parser:
|
2000-09-24 18:54:14 -03:00
|
|
|
parser = xml.sax.make_parser()
|
2000-09-24 02:21:58 -03:00
|
|
|
return DOMEventStream(stream, parser, bufsize)
|
2000-06-29 16:39:57 -03:00
|
|
|
|
2000-09-24 02:21:58 -03:00
|
|
|
def parseString(string, parser=None):
|
2000-06-29 16:39:57 -03:00
|
|
|
try:
|
2000-09-24 02:21:58 -03:00
|
|
|
from cStringIO import StringIO
|
2000-06-29 16:39:57 -03:00
|
|
|
except ImportError:
|
2000-09-24 02:21:58 -03:00
|
|
|
from StringIO import StringIO
|
2000-10-23 15:09:50 -03:00
|
|
|
|
2000-09-24 02:21:58 -03:00
|
|
|
bufsize = len(string)
|
|
|
|
buf = StringIO(string)
|
2000-09-24 18:54:14 -03:00
|
|
|
if not parser:
|
|
|
|
parser = xml.sax.make_parser()
|
2000-09-24 02:21:58 -03:00
|
|
|
return DOMEventStream(buf, parser, bufsize)
|