jpayne@69: """ jpayne@69: SAX driver for the pyexpat C module. This driver works with jpayne@69: pyexpat.__version__ == '2.22'. jpayne@69: """ jpayne@69: jpayne@69: version = "0.20" jpayne@69: jpayne@69: from xml.sax._exceptions import * jpayne@69: from xml.sax.handler import feature_validation, feature_namespaces jpayne@69: from xml.sax.handler import feature_namespace_prefixes jpayne@69: from xml.sax.handler import feature_external_ges, feature_external_pes jpayne@69: from xml.sax.handler import feature_string_interning jpayne@69: from xml.sax.handler import property_xml_string, property_interning_dict jpayne@69: jpayne@69: # xml.parsers.expat does not raise ImportError in Jython jpayne@69: import sys jpayne@69: if sys.platform[:4] == "java": jpayne@69: raise SAXReaderNotAvailable("expat not available in Java", None) jpayne@69: del sys jpayne@69: jpayne@69: try: jpayne@69: from xml.parsers import expat jpayne@69: except ImportError: jpayne@69: raise SAXReaderNotAvailable("expat not supported", None) jpayne@69: else: jpayne@69: if not hasattr(expat, "ParserCreate"): jpayne@69: raise SAXReaderNotAvailable("expat not supported", None) jpayne@69: from xml.sax import xmlreader, saxutils, handler jpayne@69: jpayne@69: AttributesImpl = xmlreader.AttributesImpl jpayne@69: AttributesNSImpl = xmlreader.AttributesNSImpl jpayne@69: jpayne@69: # If we're using a sufficiently recent version of Python, we can use jpayne@69: # weak references to avoid cycles between the parser and content jpayne@69: # handler, otherwise we'll just have to pretend. jpayne@69: try: jpayne@69: import _weakref jpayne@69: except ImportError: jpayne@69: def _mkproxy(o): jpayne@69: return o jpayne@69: else: jpayne@69: import weakref jpayne@69: _mkproxy = weakref.proxy jpayne@69: del weakref, _weakref jpayne@69: jpayne@69: class _ClosedParser: jpayne@69: pass jpayne@69: jpayne@69: # --- ExpatLocator jpayne@69: jpayne@69: class ExpatLocator(xmlreader.Locator): jpayne@69: """Locator for use with the ExpatParser class. jpayne@69: jpayne@69: This uses a weak reference to the parser object to avoid creating jpayne@69: a circular reference between the parser and the content handler. jpayne@69: """ jpayne@69: def __init__(self, parser): jpayne@69: self._ref = _mkproxy(parser) jpayne@69: jpayne@69: def getColumnNumber(self): jpayne@69: parser = self._ref jpayne@69: if parser._parser is None: jpayne@69: return None jpayne@69: return parser._parser.ErrorColumnNumber jpayne@69: jpayne@69: def getLineNumber(self): jpayne@69: parser = self._ref jpayne@69: if parser._parser is None: jpayne@69: return 1 jpayne@69: return parser._parser.ErrorLineNumber jpayne@69: jpayne@69: def getPublicId(self): jpayne@69: parser = self._ref jpayne@69: if parser is None: jpayne@69: return None jpayne@69: return parser._source.getPublicId() jpayne@69: jpayne@69: def getSystemId(self): jpayne@69: parser = self._ref jpayne@69: if parser is None: jpayne@69: return None jpayne@69: return parser._source.getSystemId() jpayne@69: jpayne@69: jpayne@69: # --- ExpatParser jpayne@69: jpayne@69: class ExpatParser(xmlreader.IncrementalParser, xmlreader.Locator): jpayne@69: """SAX driver for the pyexpat C module.""" jpayne@69: jpayne@69: def __init__(self, namespaceHandling=0, bufsize=2**16-20): jpayne@69: xmlreader.IncrementalParser.__init__(self, bufsize) jpayne@69: self._source = xmlreader.InputSource() jpayne@69: self._parser = None jpayne@69: self._namespaces = namespaceHandling jpayne@69: self._lex_handler_prop = None jpayne@69: self._parsing = 0 jpayne@69: self._entity_stack = [] jpayne@69: self._external_ges = 0 jpayne@69: self._interning = None jpayne@69: jpayne@69: # XMLReader methods jpayne@69: jpayne@69: def parse(self, source): jpayne@69: "Parse an XML document from a URL or an InputSource." jpayne@69: source = saxutils.prepare_input_source(source) jpayne@69: jpayne@69: self._source = source jpayne@69: try: jpayne@69: self.reset() jpayne@69: self._cont_handler.setDocumentLocator(ExpatLocator(self)) jpayne@69: xmlreader.IncrementalParser.parse(self, source) jpayne@69: except: jpayne@69: # bpo-30264: Close the source on error to not leak resources: jpayne@69: # xml.sax.parse() doesn't give access to the underlying parser jpayne@69: # to the caller jpayne@69: self._close_source() jpayne@69: raise jpayne@69: jpayne@69: def prepareParser(self, source): jpayne@69: if source.getSystemId() is not None: jpayne@69: self._parser.SetBase(source.getSystemId()) jpayne@69: jpayne@69: # Redefined setContentHandler to allow changing handlers during parsing jpayne@69: jpayne@69: def setContentHandler(self, handler): jpayne@69: xmlreader.IncrementalParser.setContentHandler(self, handler) jpayne@69: if self._parsing: jpayne@69: self._reset_cont_handler() jpayne@69: jpayne@69: def getFeature(self, name): jpayne@69: if name == feature_namespaces: jpayne@69: return self._namespaces jpayne@69: elif name == feature_string_interning: jpayne@69: return self._interning is not None jpayne@69: elif name in (feature_validation, feature_external_pes, jpayne@69: feature_namespace_prefixes): jpayne@69: return 0 jpayne@69: elif name == feature_external_ges: jpayne@69: return self._external_ges jpayne@69: raise SAXNotRecognizedException("Feature '%s' not recognized" % name) jpayne@69: jpayne@69: def setFeature(self, name, state): jpayne@69: if self._parsing: jpayne@69: raise SAXNotSupportedException("Cannot set features while parsing") jpayne@69: jpayne@69: if name == feature_namespaces: jpayne@69: self._namespaces = state jpayne@69: elif name == feature_external_ges: jpayne@69: self._external_ges = state jpayne@69: elif name == feature_string_interning: jpayne@69: if state: jpayne@69: if self._interning is None: jpayne@69: self._interning = {} jpayne@69: else: jpayne@69: self._interning = None jpayne@69: elif name == feature_validation: jpayne@69: if state: jpayne@69: raise SAXNotSupportedException( jpayne@69: "expat does not support validation") jpayne@69: elif name == feature_external_pes: jpayne@69: if state: jpayne@69: raise SAXNotSupportedException( jpayne@69: "expat does not read external parameter entities") jpayne@69: elif name == feature_namespace_prefixes: jpayne@69: if state: jpayne@69: raise SAXNotSupportedException( jpayne@69: "expat does not report namespace prefixes") jpayne@69: else: jpayne@69: raise SAXNotRecognizedException( jpayne@69: "Feature '%s' not recognized" % name) jpayne@69: jpayne@69: def getProperty(self, name): jpayne@69: if name == handler.property_lexical_handler: jpayne@69: return self._lex_handler_prop jpayne@69: elif name == property_interning_dict: jpayne@69: return self._interning jpayne@69: elif name == property_xml_string: jpayne@69: if self._parser: jpayne@69: if hasattr(self._parser, "GetInputContext"): jpayne@69: return self._parser.GetInputContext() jpayne@69: else: jpayne@69: raise SAXNotRecognizedException( jpayne@69: "This version of expat does not support getting" jpayne@69: " the XML string") jpayne@69: else: jpayne@69: raise SAXNotSupportedException( jpayne@69: "XML string cannot be returned when not parsing") jpayne@69: raise SAXNotRecognizedException("Property '%s' not recognized" % name) jpayne@69: jpayne@69: def setProperty(self, name, value): jpayne@69: if name == handler.property_lexical_handler: jpayne@69: self._lex_handler_prop = value jpayne@69: if self._parsing: jpayne@69: self._reset_lex_handler_prop() jpayne@69: elif name == property_interning_dict: jpayne@69: self._interning = value jpayne@69: elif name == property_xml_string: jpayne@69: raise SAXNotSupportedException("Property '%s' cannot be set" % jpayne@69: name) jpayne@69: else: jpayne@69: raise SAXNotRecognizedException("Property '%s' not recognized" % jpayne@69: name) jpayne@69: jpayne@69: # IncrementalParser methods jpayne@69: jpayne@69: def feed(self, data, isFinal = 0): jpayne@69: if not self._parsing: jpayne@69: self.reset() jpayne@69: self._parsing = 1 jpayne@69: self._cont_handler.startDocument() jpayne@69: jpayne@69: try: jpayne@69: # The isFinal parameter is internal to the expat reader. jpayne@69: # If it is set to true, expat will check validity of the entire jpayne@69: # document. When feeding chunks, they are not normally final - jpayne@69: # except when invoked from close. jpayne@69: self._parser.Parse(data, isFinal) jpayne@69: except expat.error as e: jpayne@69: exc = SAXParseException(expat.ErrorString(e.code), e, self) jpayne@69: # FIXME: when to invoke error()? jpayne@69: self._err_handler.fatalError(exc) jpayne@69: jpayne@69: def _close_source(self): jpayne@69: source = self._source jpayne@69: try: jpayne@69: file = source.getCharacterStream() jpayne@69: if file is not None: jpayne@69: file.close() jpayne@69: finally: jpayne@69: file = source.getByteStream() jpayne@69: if file is not None: jpayne@69: file.close() jpayne@69: jpayne@69: def close(self): jpayne@69: if (self._entity_stack or self._parser is None or jpayne@69: isinstance(self._parser, _ClosedParser)): jpayne@69: # If we are completing an external entity, do nothing here jpayne@69: return jpayne@69: try: jpayne@69: self.feed("", isFinal = 1) jpayne@69: self._cont_handler.endDocument() jpayne@69: self._parsing = 0 jpayne@69: # break cycle created by expat handlers pointing to our methods jpayne@69: self._parser = None jpayne@69: finally: jpayne@69: self._parsing = 0 jpayne@69: if self._parser is not None: jpayne@69: # Keep ErrorColumnNumber and ErrorLineNumber after closing. jpayne@69: parser = _ClosedParser() jpayne@69: parser.ErrorColumnNumber = self._parser.ErrorColumnNumber jpayne@69: parser.ErrorLineNumber = self._parser.ErrorLineNumber jpayne@69: self._parser = parser jpayne@69: self._close_source() jpayne@69: jpayne@69: def _reset_cont_handler(self): jpayne@69: self._parser.ProcessingInstructionHandler = \ jpayne@69: self._cont_handler.processingInstruction jpayne@69: self._parser.CharacterDataHandler = self._cont_handler.characters jpayne@69: jpayne@69: def _reset_lex_handler_prop(self): jpayne@69: lex = self._lex_handler_prop jpayne@69: parser = self._parser jpayne@69: if lex is None: jpayne@69: parser.CommentHandler = None jpayne@69: parser.StartCdataSectionHandler = None jpayne@69: parser.EndCdataSectionHandler = None jpayne@69: parser.StartDoctypeDeclHandler = None jpayne@69: parser.EndDoctypeDeclHandler = None jpayne@69: else: jpayne@69: parser.CommentHandler = lex.comment jpayne@69: parser.StartCdataSectionHandler = lex.startCDATA jpayne@69: parser.EndCdataSectionHandler = lex.endCDATA jpayne@69: parser.StartDoctypeDeclHandler = self.start_doctype_decl jpayne@69: parser.EndDoctypeDeclHandler = lex.endDTD jpayne@69: jpayne@69: def reset(self): jpayne@69: if self._namespaces: jpayne@69: self._parser = expat.ParserCreate(self._source.getEncoding(), " ", jpayne@69: intern=self._interning) jpayne@69: self._parser.namespace_prefixes = 1 jpayne@69: self._parser.StartElementHandler = self.start_element_ns jpayne@69: self._parser.EndElementHandler = self.end_element_ns jpayne@69: else: jpayne@69: self._parser = expat.ParserCreate(self._source.getEncoding(), jpayne@69: intern = self._interning) jpayne@69: self._parser.StartElementHandler = self.start_element jpayne@69: self._parser.EndElementHandler = self.end_element jpayne@69: jpayne@69: self._reset_cont_handler() jpayne@69: self._parser.UnparsedEntityDeclHandler = self.unparsed_entity_decl jpayne@69: self._parser.NotationDeclHandler = self.notation_decl jpayne@69: self._parser.StartNamespaceDeclHandler = self.start_namespace_decl jpayne@69: self._parser.EndNamespaceDeclHandler = self.end_namespace_decl jpayne@69: jpayne@69: self._decl_handler_prop = None jpayne@69: if self._lex_handler_prop: jpayne@69: self._reset_lex_handler_prop() jpayne@69: # self._parser.DefaultHandler = jpayne@69: # self._parser.DefaultHandlerExpand = jpayne@69: # self._parser.NotStandaloneHandler = jpayne@69: self._parser.ExternalEntityRefHandler = self.external_entity_ref jpayne@69: try: jpayne@69: self._parser.SkippedEntityHandler = self.skipped_entity_handler jpayne@69: except AttributeError: jpayne@69: # This pyexpat does not support SkippedEntity jpayne@69: pass jpayne@69: self._parser.SetParamEntityParsing( jpayne@69: expat.XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE) jpayne@69: jpayne@69: self._parsing = 0 jpayne@69: self._entity_stack = [] jpayne@69: jpayne@69: # Locator methods jpayne@69: jpayne@69: def getColumnNumber(self): jpayne@69: if self._parser is None: jpayne@69: return None jpayne@69: return self._parser.ErrorColumnNumber jpayne@69: jpayne@69: def getLineNumber(self): jpayne@69: if self._parser is None: jpayne@69: return 1 jpayne@69: return self._parser.ErrorLineNumber jpayne@69: jpayne@69: def getPublicId(self): jpayne@69: return self._source.getPublicId() jpayne@69: jpayne@69: def getSystemId(self): jpayne@69: return self._source.getSystemId() jpayne@69: jpayne@69: # event handlers jpayne@69: def start_element(self, name, attrs): jpayne@69: self._cont_handler.startElement(name, AttributesImpl(attrs)) jpayne@69: jpayne@69: def end_element(self, name): jpayne@69: self._cont_handler.endElement(name) jpayne@69: jpayne@69: def start_element_ns(self, name, attrs): jpayne@69: pair = name.split() jpayne@69: if len(pair) == 1: jpayne@69: # no namespace jpayne@69: pair = (None, name) jpayne@69: elif len(pair) == 3: jpayne@69: pair = pair[0], pair[1] jpayne@69: else: jpayne@69: # default namespace jpayne@69: pair = tuple(pair) jpayne@69: jpayne@69: newattrs = {} jpayne@69: qnames = {} jpayne@69: for (aname, value) in attrs.items(): jpayne@69: parts = aname.split() jpayne@69: length = len(parts) jpayne@69: if length == 1: jpayne@69: # no namespace jpayne@69: qname = aname jpayne@69: apair = (None, aname) jpayne@69: elif length == 3: jpayne@69: qname = "%s:%s" % (parts[2], parts[1]) jpayne@69: apair = parts[0], parts[1] jpayne@69: else: jpayne@69: # default namespace jpayne@69: qname = parts[1] jpayne@69: apair = tuple(parts) jpayne@69: jpayne@69: newattrs[apair] = value jpayne@69: qnames[apair] = qname jpayne@69: jpayne@69: self._cont_handler.startElementNS(pair, None, jpayne@69: AttributesNSImpl(newattrs, qnames)) jpayne@69: jpayne@69: def end_element_ns(self, name): jpayne@69: pair = name.split() jpayne@69: if len(pair) == 1: jpayne@69: pair = (None, name) jpayne@69: elif len(pair) == 3: jpayne@69: pair = pair[0], pair[1] jpayne@69: else: jpayne@69: pair = tuple(pair) jpayne@69: jpayne@69: self._cont_handler.endElementNS(pair, None) jpayne@69: jpayne@69: # this is not used (call directly to ContentHandler) jpayne@69: def processing_instruction(self, target, data): jpayne@69: self._cont_handler.processingInstruction(target, data) jpayne@69: jpayne@69: # this is not used (call directly to ContentHandler) jpayne@69: def character_data(self, data): jpayne@69: self._cont_handler.characters(data) jpayne@69: jpayne@69: def start_namespace_decl(self, prefix, uri): jpayne@69: self._cont_handler.startPrefixMapping(prefix, uri) jpayne@69: jpayne@69: def end_namespace_decl(self, prefix): jpayne@69: self._cont_handler.endPrefixMapping(prefix) jpayne@69: jpayne@69: def start_doctype_decl(self, name, sysid, pubid, has_internal_subset): jpayne@69: self._lex_handler_prop.startDTD(name, pubid, sysid) jpayne@69: jpayne@69: def unparsed_entity_decl(self, name, base, sysid, pubid, notation_name): jpayne@69: self._dtd_handler.unparsedEntityDecl(name, pubid, sysid, notation_name) jpayne@69: jpayne@69: def notation_decl(self, name, base, sysid, pubid): jpayne@69: self._dtd_handler.notationDecl(name, pubid, sysid) jpayne@69: jpayne@69: def external_entity_ref(self, context, base, sysid, pubid): jpayne@69: if not self._external_ges: jpayne@69: return 1 jpayne@69: jpayne@69: source = self._ent_handler.resolveEntity(pubid, sysid) jpayne@69: source = saxutils.prepare_input_source(source, jpayne@69: self._source.getSystemId() or jpayne@69: "") jpayne@69: jpayne@69: self._entity_stack.append((self._parser, self._source)) jpayne@69: self._parser = self._parser.ExternalEntityParserCreate(context) jpayne@69: self._source = source jpayne@69: jpayne@69: try: jpayne@69: xmlreader.IncrementalParser.parse(self, source) jpayne@69: except: jpayne@69: return 0 # FIXME: save error info here? jpayne@69: jpayne@69: (self._parser, self._source) = self._entity_stack[-1] jpayne@69: del self._entity_stack[-1] jpayne@69: return 1 jpayne@69: jpayne@69: def skipped_entity_handler(self, name, is_pe): jpayne@69: if is_pe: jpayne@69: # The SAX spec requires to report skipped PEs with a '%' jpayne@69: name = '%'+name jpayne@69: self._cont_handler.skippedEntity(name) jpayne@69: jpayne@69: # --- jpayne@69: jpayne@69: def create_parser(*args, **kwargs): jpayne@69: return ExpatParser(*args, **kwargs) jpayne@69: jpayne@69: # --- jpayne@69: jpayne@69: if __name__ == "__main__": jpayne@69: import xml.sax.saxutils jpayne@69: p = create_parser() jpayne@69: p.setContentHandler(xml.sax.saxutils.XMLGenerator()) jpayne@69: p.setErrorHandler(xml.sax.ErrorHandler()) jpayne@69: p.parse("http://www.ibiblio.org/xml/examples/shakespeare/hamlet.xml")