Python xml.sax.handler.ErrorHandler() Examples

The following are 9 code examples of xml.sax.handler.ErrorHandler(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module xml.sax.handler , or try the search function .
Example #1
Source File: config.py    From uniconvertor with GNU Affero General Public License v3.0 6 votes vote down vote up
def load(self, filename=None):
        self.filename = filename
        if fsutils.exists(filename):
            content_handler = XMLPrefReader(pref=self)
            error_handler = ErrorHandler()
            entity_resolver = EntityResolver()
            dtd_handler = DTDHandler()
            try:
                input_file = get_fileptr(filename)
                input_source = InputSource()
                input_source.setByteStream(input_file)
                xml_reader = xml.sax.make_parser()
                xml_reader.setContentHandler(content_handler)
                xml_reader.setErrorHandler(error_handler)
                xml_reader.setEntityResolver(entity_resolver)
                xml_reader.setDTDHandler(dtd_handler)
                xml_reader.parse(input_source)
                input_file.close()
            except Exception as e:
                LOG.error('Cannot read preferences from %s %s', filename, e) 
Example #2
Source File: recipe-149284.py    From code with MIT License 5 votes vote down vote up
def filter_rdf (input, output):
    """filter_rdf(input:file, output:file)

    Parses the XML input from the input stream, filtering out all
    elements and attributes that are in the RDF namespace.
    """

    output_gen = saxutils.XMLGenerator(output)
    parser = sax.make_parser()
    filter = RDFFilter(parser)
    filter.setFeature(handler.feature_namespaces, True)
    filter.setContentHandler(output_gen)
    filter.setErrorHandler(handler.ErrorHandler())
    filter.parse(input) 
Example #3
Source File: __init__.py    From qpid-python with Apache License 2.0 5 votes vote down vote up
def xml_parse(filename, path=()):
  source = "file://%s" % os.path.abspath(filename)
  h = parsers.XMLParser()
  p = xml.sax.make_parser()
  p.setContentHandler(h)
  p.setErrorHandler(ErrorHandler())
  p.setEntityResolver(Resolver(path))
  p.parse(source)
  return h.parser.tree 
Example #4
Source File: odfmanifest.py    From lambda-text-extractor with Apache License 2.0 5 votes vote down vote up
def manifestlist(manifestxml):
    odhandler = ODFManifestHandler()
    parser = make_parser()
    parser.setFeature(handler.feature_namespaces, 1)
    parser.setContentHandler(odhandler)
    parser.setErrorHandler(handler.ErrorHandler())

    inpsrc = InputSource()
    if not isinstance(manifestxml, str):
        manifestxml=manifestxml.decode("utf-8")
    inpsrc.setByteStream(StringIO(manifestxml))
    parser.parse(inpsrc)

    return odhandler.manifest 
Example #5
Source File: odfmanifest.py    From lambda-text-extractor with Apache License 2.0 5 votes vote down vote up
def manifestlist(manifestxml):
    odhandler = ODFManifestHandler()
    parser = make_parser()
    parser.setFeature(handler.feature_namespaces, 1)
    parser.setContentHandler(odhandler)
    parser.setErrorHandler(handler.ErrorHandler())

    inpsrc = InputSource()
    if not isinstance(manifestxml, str):
        manifestxml=manifestxml.decode("utf-8")
    inpsrc.setByteStream(StringIO(manifestxml))
    parser.parse(inpsrc)

    return odhandler.manifest 
Example #6
Source File: generic_filters.py    From uniconvertor with GNU Affero General Public License v3.0 5 votes vote down vote up
def init_load(self):
        self.input_source = InputSource()
        self.input_source.setByteStream(self.fileptr)
        self.xml_reader = xml.sax.make_parser()
        self.xml_reader.setContentHandler(self)
        self.xml_reader.setErrorHandler(handler.ErrorHandler())
        self.xml_reader.setEntityResolver(handler.EntityResolver())
        self.xml_reader.setDTDHandler(handler.DTDHandler())
        self.xml_reader.setFeature(handler.feature_external_ges, False)
        self.do_load() 
Example #7
Source File: pdxf_filters.py    From uniconvertor with GNU Affero General Public License v3.0 5 votes vote down vote up
def _build_model(self):
        content_handler = XMLDocReader(self.presenter)
        error_handler = ErrorHandler()
        entity_resolver = EntityResolver()
        dtd_handler = DTDHandler()
        try:
            filename = os.path.join(self.presenter.doc_dir, 'content.xml')
            handler = open(filename, 'r')
            lines = float(sum(1 for l in handler))
            handler.close()
            self.file_handler = open(filename, "r")
            input_source = InputSource()
            input_source.setByteStream(self.file_handler)
            content_handler.lines = lines
            xml_reader = xml.sax.make_parser()
            xml_reader.setContentHandler(content_handler)
            xml_reader.setErrorHandler(error_handler)
            xml_reader.setEntityResolver(entity_resolver)
            xml_reader.setDTDHandler(dtd_handler)
            xml_reader.parse(input_source)
            self.file_handler.close()
            content_handler.file = None
        except:
            errtype, value, traceback = sys.exc_info()
            msg = 'It seems content.xml is corrupted' + '\n' + value
            events.emit(events.MESSAGES, msgconst.ERROR, msg)
            raise IOError(errtype, msg, traceback)
        self.model = content_handler.model

        msg = 'Content.xml is parsed successfully'
        events.emit(events.MESSAGES, msgconst.OK, msg) 
Example #8
Source File: opendocument.py    From lambda-text-extractor with Apache License 2.0 4 votes vote down vote up
def __loadxmlparts(z, manifest, doc, objectpath):
    """
    Parses a document from its zipfile
    @param z an instance of zipfile.ZipFile
    @param manifest Manifest data structured in a dictionary
    @param doc instance of OpenDocument to feed in
    @param objectpath unicode string: path to an object
    """
    assert(isinstance(z, zipfile.ZipFile))
    assert(type(manifest)==type(dict()))
    assert(isinstance(doc, OpenDocument))
    assert(type(objectpath)==type(u""))

    from load import LoadParser
    from xml.sax import make_parser, handler

    for xmlfile in (objectpath+u'settings.xml', objectpath+u'meta.xml', objectpath+u'content.xml', objectpath+u'styles.xml'):
        if xmlfile not in manifest:
            continue
        ##########################################################
        # this one is added to debug the bad behavior with Python2
        # which raises exceptions of type SAXParseException
        from xml.sax._exceptions import SAXParseException
        ##########################################################
        try:
            xmlpart = z.read(xmlfile).decode("utf-8")
            doc._parsing = xmlfile

            parser = make_parser()
            parser.setFeature(handler.feature_namespaces, 1)
            parser.setFeature(handler.feature_external_ges, 0)
            parser.setContentHandler(LoadParser(doc))
            parser.setErrorHandler(handler.ErrorHandler())

            inpsrc = InputSource()
            #################
            # There may be a SAXParseException triggered because of
            # a missing xmlns prefix like meta, config, etc.
            # So i add such declarations when needed (GK, 2014/10/21).
            # Is there any option to prevent xmlns checks by SAX?
            xmlpart=__fixXmlPart(xmlpart)

            inpsrc.setByteStream(BytesIO(xmlpart.encode("utf-8")))
            parser.parse(inpsrc)
            del doc._parsing
        except KeyError as v: pass
        except SAXParseException:
            print (u"====== SAX FAILED TO PARSE ==========\n", xmlpart) 
Example #9
Source File: opendocument.py    From lambda-text-extractor with Apache License 2.0 4 votes vote down vote up
def __loadxmlparts(z, manifest, doc, objectpath):
    """
    Parses a document from its zipfile
    @param z an instance of zipfile.ZipFile
    @param manifest Manifest data structured in a dictionary
    @param doc instance of OpenDocument to feed in
    @param objectpath unicode string: path to an object
    """
    assert(isinstance(z, zipfile.ZipFile))
    assert(type(manifest)==type(dict()))
    assert(isinstance(doc, OpenDocument))
    assert(type(objectpath)==type(u""))

    from load import LoadParser
    from xml.sax import make_parser, handler

    for xmlfile in (objectpath+u'settings.xml', objectpath+u'meta.xml', objectpath+u'content.xml', objectpath+u'styles.xml'):
        if xmlfile not in manifest:
            continue
        ##########################################################
        # this one is added to debug the bad behavior with Python2
        # which raises exceptions of type SAXParseException
        from xml.sax._exceptions import SAXParseException
        ##########################################################
        try:
            xmlpart = z.read(xmlfile).decode("utf-8")
            doc._parsing = xmlfile

            parser = make_parser()
            parser.setFeature(handler.feature_namespaces, 1)
            parser.setFeature(handler.feature_external_ges, 0)
            parser.setContentHandler(LoadParser(doc))
            parser.setErrorHandler(handler.ErrorHandler())

            inpsrc = InputSource()
            #################
            # There may be a SAXParseException triggered because of
            # a missing xmlns prefix like meta, config, etc.
            # So i add such declarations when needed (GK, 2014/10/21).
            # Is there any option to prevent xmlns checks by SAX?
            xmlpart=__fixXmlPart(xmlpart)

            inpsrc.setByteStream(BytesIO(xmlpart.encode("utf-8")))
            parser.parse(inpsrc)
            del doc._parsing
        except KeyError as v: pass
        except SAXParseException:
            print (u"====== SAX FAILED TO PARSE ==========\n", xmlpart)