Esempio n. 1
0
	def setUp(self):
		self._URL_="http://127.0.0.1:5000"

		self.parser = _SAX.make_parser()


		# Default Configuration

		self.assertTrue(self.parser.getFeature("http://xml.org/sax/features/external-general-entities"))
		self.assertFalse(self.parser.getFeature("http://xml.org/sax/features/external-parameter-entities"))
		# Not recognized      
		#        self.assertFalse(self.parser.getFeature("http://xml.org/sax/features/is-standalone"))
		#        self.assertFalse(self.parser.getFeature("http://xml.org/sax/features/lexical-handler/parameter-entities"))
		self.assertFalse(self.parser.getFeature("http://xml.org/sax/features/namespaces"))
		self.assertFalse(self.parser.getFeature("http://xml.org/sax/features/namespace-prefixes"))
		# Not recognized
		#        self.assertFalse(self.parser.getFeature("http://xml.org/sax/features/resolve-dtd-uris"))
		self.assertFalse(self.parser.getFeature("http://xml.org/sax/features/string-interning"))
		#Not recognized
		#        self.assertFalse(self.parser.getFeature("http://xml.org/sax/features/unicode-normalization-checking"))
		#        self.assertFalse(self.parser.getFeature("http://xml.org/sax/features/use-attributes2"))
		#        self.assertFalse(self.parser.getFeature("http://xml.org/sax/features/use-locator2"))
		#        self.assertFalse(self.parser.getFeature("http://xml.org/sax/features/use-entity-resolver2"))
		self.assertFalse(self.parser.getFeature("http://xml.org/sax/features/validation"))
		#Not recognized
		#        self.assertFalse(self.parser.getFeature("http://xml.org/sax/features/xmlns-uris"))
		#        self.assertFalse(self.parser.getFeature("http://xml.org/sax/features/xmlns-1.1"))      

		self.myHandler = MyContentHandler()
		self.parser.setContentHandler(self.myHandler)
Esempio n. 2
0
def seed(fileobj):
    seeder = Seeder()
    parser = make_parser()
    parser.setFeature(feature_namespaces, 1)
    parser.setContentHandler(seeder)
    parser.parse(fileobj)
    return seeder.result
Esempio n. 3
0
    def __init__(self, file, callback=None):
        """
        Initialize XMLParser

        The callback-method is called when a osm object is found

        def callback(element):
            pass

        :param file: Path to file
        :param callback: Callback for read osm objects
        """
        super().__init__(file, callback)
        self.__file = file
        if not os.path.isfile(file):
            raise Exception('is not a file')

        self.__callback = self.__default_callback \
            if callback is None else callback
        self.__parser = defusedxml_sax.make_parser()
        self.__handler = OSMContentHandler()

        self.__parser.setContentHandler(self.__handler)

        self.__in_node = False
        self.__in_way = False
        self.__in_relation = False
        self.__in_osm = False
        self.__last_event = None

        self.__osm_object = None
        self.__current_object = None
Esempio n. 4
0
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
Esempio n. 5
0
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 odf.load import LoadParser
    from defusedxml.sax import make_parser
    from xml.sax import 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)
Esempio n. 6
0
    def setUp(self):
        self._URL_ = "http://127.0.0.1:5000"

        self.parser = _SAX.make_parser()

        # Default Configuration

        self.assertTrue(
            self.parser.getFeature(
                "http://xml.org/sax/features/external-general-entities"))
        self.assertFalse(
            self.parser.getFeature(
                "http://xml.org/sax/features/external-parameter-entities"))
        # Not recognized
        #        self.assertFalse(self.parser.getFeature("http://xml.org/sax/features/is-standalone"))
        #        self.assertFalse(self.parser.getFeature("http://xml.org/sax/features/lexical-handler/parameter-entities"))
        self.assertFalse(
            self.parser.getFeature("http://xml.org/sax/features/namespaces"))
        self.assertFalse(
            self.parser.getFeature(
                "http://xml.org/sax/features/namespace-prefixes"))
        # Not recognized
        #        self.assertFalse(self.parser.getFeature("http://xml.org/sax/features/resolve-dtd-uris"))
        self.assertFalse(
            self.parser.getFeature(
                "http://xml.org/sax/features/string-interning"))
        #Not recognized
        #        self.assertFalse(self.parser.getFeature("http://xml.org/sax/features/unicode-normalization-checking"))
        #        self.assertFalse(self.parser.getFeature("http://xml.org/sax/features/use-attributes2"))
        #        self.assertFalse(self.parser.getFeature("http://xml.org/sax/features/use-locator2"))
        #        self.assertFalse(self.parser.getFeature("http://xml.org/sax/features/use-entity-resolver2"))
        self.assertFalse(
            self.parser.getFeature("http://xml.org/sax/features/validation"))
        #Not recognized
        #        self.assertFalse(self.parser.getFeature("http://xml.org/sax/features/xmlns-uris"))
        #        self.assertFalse(self.parser.getFeature("http://xml.org/sax/features/xmlns-1.1"))

        self.myHandler = MyContentHandler()
        self.parser.setContentHandler(self.myHandler)
Esempio n. 7
0
from defusedxml.sax import make_parser, parse
from MyContentHandler import MyContentHandler

parser = make_parser()
myHandler = MyContentHandler()
parser.setContentHandler(myHandler)
parser.parse('../../xml_files_windows/standard.xml')
print myHandler.getElementContent("data")