2000-06-29 16:34:54 -03:00
|
|
|
"""
|
|
|
|
SAX driver for the Pyexpat C module. This driver works with
|
|
|
|
pyexpat.__version__ == '1.5'.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Todo on driver:
|
|
|
|
# - make it support external entities (wait for pyexpat.c)
|
|
|
|
# - enable configuration between reset() and feed() calls
|
|
|
|
# - support lexical events?
|
|
|
|
# - proper inputsource handling
|
|
|
|
# - properties and features
|
|
|
|
|
|
|
|
# Todo on pyexpat.c:
|
|
|
|
# - support XML_ExternalEntityParserCreate
|
|
|
|
# - exceptions in callouts from pyexpat to python code lose position info
|
|
|
|
|
|
|
|
version = "0.20"
|
|
|
|
|
2000-09-24 15:29:24 -03:00
|
|
|
from xml.sax._exceptions import *
|
2000-09-23 01:49:30 -03:00
|
|
|
from xml.parsers import expat
|
2000-06-29 16:34:54 -03:00
|
|
|
from xml.sax import xmlreader
|
|
|
|
|
|
|
|
# --- ExpatParser
|
|
|
|
|
2000-09-23 02:32:26 -03:00
|
|
|
class ExpatParser(xmlreader.IncrementalParser, xmlreader.Locator):
|
2000-06-29 16:34:54 -03:00
|
|
|
"SAX driver for the Pyexpat C module."
|
|
|
|
|
|
|
|
def __init__(self, namespaceHandling=0, bufsize=2**16-20):
|
|
|
|
xmlreader.IncrementalParser.__init__(self, bufsize)
|
|
|
|
self._source = None
|
|
|
|
self._parser = None
|
|
|
|
self._namespaces = namespaceHandling
|
|
|
|
self._parsing = 0
|
2000-09-24 15:29:24 -03:00
|
|
|
self._attrs = xmlreader.AttributesImpl({}, {})
|
2000-06-29 16:34:54 -03:00
|
|
|
|
|
|
|
# XMLReader methods
|
|
|
|
|
2000-09-23 02:32:26 -03:00
|
|
|
def parse(self, stream_or_string):
|
2000-07-05 23:56:36 -03:00
|
|
|
"Parse an XML document from a URL."
|
2000-09-23 02:32:26 -03:00
|
|
|
if type(stream_or_string) is type(""):
|
|
|
|
stream = open(stream_or_string)
|
2000-06-29 16:34:54 -03:00
|
|
|
else:
|
2000-09-23 02:32:26 -03:00
|
|
|
stream = stream_or_string
|
2000-06-29 16:34:54 -03:00
|
|
|
|
|
|
|
self.reset()
|
|
|
|
self._cont_handler.setDocumentLocator(self)
|
|
|
|
try:
|
|
|
|
xmlreader.IncrementalParser.parse(self, stream)
|
2000-09-23 01:49:30 -03:00
|
|
|
except expat.error:
|
2000-06-29 16:34:54 -03:00
|
|
|
error_code = self._parser.ErrorCode
|
2000-09-24 15:29:24 -03:00
|
|
|
raise SAXParseException(expat.ErrorString(error_code), None, self)
|
2000-06-29 16:34:54 -03:00
|
|
|
|
|
|
|
self._cont_handler.endDocument()
|
|
|
|
|
|
|
|
def prepareParser(self, filename=None):
|
|
|
|
self._source = filename
|
|
|
|
|
|
|
|
if self._source != None:
|
|
|
|
self._parser.SetBase(self._source)
|
|
|
|
|
|
|
|
def getFeature(self, name):
|
2000-09-24 15:29:24 -03:00
|
|
|
if name == feature_namespaces:
|
|
|
|
return self._namespaces
|
2000-06-29 16:34:54 -03:00
|
|
|
raise SAXNotRecognizedException("Feature '%s' not recognized" % name)
|
|
|
|
|
|
|
|
def setFeature(self, name, state):
|
2000-09-24 15:29:24 -03:00
|
|
|
if self._parsing:
|
|
|
|
raise SAXNotSupportedException("Cannot set features while parsing")
|
|
|
|
if name == feature_namespaces:
|
|
|
|
self._namespaces = state
|
|
|
|
else:
|
|
|
|
raise SAXNotRecognizedException("Feature '%s' not recognized" %
|
|
|
|
name)
|
2000-06-29 16:34:54 -03:00
|
|
|
|
|
|
|
def getProperty(self, name):
|
|
|
|
raise SAXNotRecognizedException("Property '%s' not recognized" % name)
|
|
|
|
|
|
|
|
def setProperty(self, name, value):
|
|
|
|
raise SAXNotRecognizedException("Property '%s' not recognized" % name)
|
|
|
|
|
|
|
|
# IncrementalParser methods
|
|
|
|
|
|
|
|
def feed(self, data):
|
|
|
|
if not self._parsing:
|
2000-09-23 02:32:26 -03:00
|
|
|
self._parsing = 1
|
2000-06-29 16:34:54 -03:00
|
|
|
self.reset()
|
|
|
|
self._cont_handler.startDocument()
|
2000-09-24 15:29:24 -03:00
|
|
|
|
|
|
|
if not self._parser.Parse(data, 0):
|
|
|
|
msg = pyexpat.ErrorString(self._parser.ErrorCode)
|
|
|
|
raise SAXParseException(msg, None, self)
|
2000-06-29 16:34:54 -03:00
|
|
|
|
|
|
|
def close(self):
|
|
|
|
if self._parsing:
|
|
|
|
self._cont_handler.endDocument()
|
2000-09-23 02:32:26 -03:00
|
|
|
self._parsing = 0
|
2000-06-29 16:34:54 -03:00
|
|
|
self._parser.Parse("", 1)
|
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
if self._namespaces:
|
2000-09-23 01:49:30 -03:00
|
|
|
self._parser = expat.ParserCreate(None, " ")
|
2000-06-29 16:34:54 -03:00
|
|
|
self._parser.StartElementHandler = self.start_element_ns
|
|
|
|
self._parser.EndElementHandler = self.end_element_ns
|
|
|
|
else:
|
2000-09-23 01:49:30 -03:00
|
|
|
self._parser = expat.ParserCreate()
|
2000-07-04 00:39:33 -03:00
|
|
|
self._parser.StartElementHandler = self.start_element
|
|
|
|
self._parser.EndElementHandler = self.end_element
|
2000-06-29 16:34:54 -03:00
|
|
|
|
|
|
|
self._parser.ProcessingInstructionHandler = \
|
|
|
|
self._cont_handler.processingInstruction
|
|
|
|
self._parser.CharacterDataHandler = self._cont_handler.characters
|
|
|
|
self._parser.UnparsedEntityDeclHandler = self.unparsed_entity_decl
|
|
|
|
self._parser.NotationDeclHandler = self.notation_decl
|
|
|
|
self._parser.StartNamespaceDeclHandler = self.start_namespace_decl
|
|
|
|
self._parser.EndNamespaceDeclHandler = self.end_namespace_decl
|
|
|
|
# self._parser.CommentHandler =
|
|
|
|
# self._parser.StartCdataSectionHandler =
|
|
|
|
# self._parser.EndCdataSectionHandler =
|
|
|
|
# self._parser.DefaultHandler =
|
|
|
|
# self._parser.DefaultHandlerExpand =
|
|
|
|
# self._parser.NotStandaloneHandler =
|
|
|
|
self._parser.ExternalEntityRefHandler = self.external_entity_ref
|
|
|
|
|
|
|
|
# Locator methods
|
|
|
|
|
|
|
|
def getColumnNumber(self):
|
|
|
|
return self._parser.ErrorColumnNumber
|
|
|
|
|
|
|
|
def getLineNumber(self):
|
|
|
|
return self._parser.ErrorLineNumber
|
|
|
|
|
|
|
|
def getPublicId(self):
|
|
|
|
return self._source.getPublicId()
|
|
|
|
|
|
|
|
def getSystemId(self):
|
|
|
|
return self._parser.GetBase()
|
|
|
|
|
|
|
|
# event handlers
|
|
|
|
def start_element(self, name, attrs):
|
2000-09-24 15:29:24 -03:00
|
|
|
self._cont_handler.startElement(name, self._attrs)
|
2000-06-29 16:34:54 -03:00
|
|
|
|
|
|
|
def end_element(self, name):
|
2000-09-24 15:29:24 -03:00
|
|
|
self._cont_handler.endElement(name)
|
2000-06-29 16:34:54 -03:00
|
|
|
|
|
|
|
def start_element_ns(self, name, attrs):
|
2000-09-23 01:49:30 -03:00
|
|
|
pair = name.split()
|
2000-06-29 16:34:54 -03:00
|
|
|
if len(pair) == 1:
|
2000-09-24 15:29:24 -03:00
|
|
|
pair = (None, name)
|
2000-06-29 16:34:54 -03:00
|
|
|
|
2000-09-24 15:29:24 -03:00
|
|
|
self._cont_handler.startElementNS(pair, None, self._attrs)
|
2000-06-29 16:34:54 -03:00
|
|
|
|
|
|
|
def end_element_ns(self, name):
|
2000-09-23 01:49:30 -03:00
|
|
|
pair = name.split()
|
2000-06-29 16:34:54 -03:00
|
|
|
if len(pair) == 1:
|
2000-09-24 15:29:24 -03:00
|
|
|
name = (None, name)
|
2000-06-29 16:34:54 -03:00
|
|
|
|
2000-09-24 15:29:24 -03:00
|
|
|
self._cont_handler.endElementNS(pair, None)
|
2000-06-29 16:34:54 -03:00
|
|
|
|
2000-09-24 15:29:24 -03:00
|
|
|
# this is not used (call directly to ContentHandler)
|
2000-06-29 16:34:54 -03:00
|
|
|
def processing_instruction(self, target, data):
|
|
|
|
self._cont_handler.processingInstruction(target, data)
|
|
|
|
|
2000-09-24 15:29:24 -03:00
|
|
|
# this is not used (call directly to ContentHandler)
|
2000-06-29 16:34:54 -03:00
|
|
|
def character_data(self, data):
|
|
|
|
self._cont_handler.characters(data)
|
|
|
|
|
|
|
|
def start_namespace_decl(self, prefix, uri):
|
|
|
|
self._cont_handler.startPrefixMapping(prefix, uri)
|
|
|
|
|
|
|
|
def end_namespace_decl(self, prefix):
|
|
|
|
self._cont_handler.endPrefixMapping(prefix)
|
|
|
|
|
|
|
|
def unparsed_entity_decl(self, name, base, sysid, pubid, notation_name):
|
|
|
|
self._dtd_handler.unparsedEntityDecl(name, pubid, sysid, notation_name)
|
|
|
|
|
|
|
|
def notation_decl(self, name, base, sysid, pubid):
|
|
|
|
self._dtd_handler.notationDecl(name, pubid, sysid)
|
|
|
|
|
|
|
|
def external_entity_ref(self, context, base, sysid, pubid):
|
2000-09-24 15:29:24 -03:00
|
|
|
raise NotImplementedError()
|
2000-06-29 16:34:54 -03:00
|
|
|
source = self._ent_handler.resolveEntity(pubid, sysid)
|
|
|
|
source = saxutils.prepare_input_source(source)
|
|
|
|
# FIXME: create new parser, stack self._source and self._parser
|
|
|
|
# FIXME: reuse code from self.parse(...)
|
|
|
|
return 1
|
|
|
|
|
|
|
|
# ---
|
|
|
|
|
|
|
|
def create_parser(*args, **kwargs):
|
2000-09-23 02:32:26 -03:00
|
|
|
return apply(ExpatParser, args, kwargs)
|
2000-06-29 16:34:54 -03:00
|
|
|
|
|
|
|
# ---
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
import xml.sax
|
|
|
|
p = create_parser()
|
|
|
|
p.setContentHandler(xml.sax.XMLGenerator())
|
|
|
|
p.setErrorHandler(xml.sax.ErrorHandler())
|
|
|
|
p.parse("../../../hamlet.xml")
|