Esempio n. 1
0
    def __init__(self, source):

        xmlstring = source.read()

        if isinstance(xmlstring, text_type):
            xmlstring = xmlstring.encode('utf-8')
        try:
            parser = etree.XMLParser(huge_tree=True)
            tree = etree.parse(BytesIO(xmlstring), parser)
        except TypeError:
            tree = etree.fromstring(xmlstring)
        except Exception as e:
            log.exception("Error parsing XML results: %s" % xmlstring)
            raise e

        boolean = tree.find(RESULTS_NS_ET + 'boolean')
        results = tree.find(RESULTS_NS_ET + 'results')

        if boolean is not None:
            type_ = 'ASK'
        elif results is not None:
            type_ = 'SELECT'
        else:
            g = Graph()
            try:
                g.parse(data=xmlstring)
                if len(g) == 0:
                    raise
                type_ = 'CONSTRUCT'

            except:
                raise ResultException(
                    "No RDF Graph, result-bindings or boolean answer found!")

        Result.__init__(self, type_)
        if type_ == 'SELECT':
            self.bindings = []
            for result in results:
                r = {}
                for binding in result:
                    r[Variable(binding.get('name'))] = parseTerm(binding[0])
                self.bindings.append(r)

            self.vars = [
                Variable(x.get("name"))
                for x in tree.findall('./%shead/%svariable' %
                                      (RESULTS_NS_ET, RESULTS_NS_ET))
            ]

        elif type_ == 'ASK':
            self.askAnswer = boolean.text.lower().strip() == "true"
        elif type_ == 'CONSTRUCT':
            self.graph = g
Esempio n. 2
0
    def __init__(self, source):

        xmlstring = source.read()

        if isinstance(xmlstring, text_type):
            xmlstring = xmlstring.encode('utf-8')
        try:
            parser = etree.XMLParser(huge_tree=True)
            tree = etree.parse(BytesIO(xmlstring), parser)
        except TypeError:
            tree = etree.fromstring(xmlstring)
        except Exception as e:
            log.exception("Error parsing XML results: %s"%xmlstring)
            raise e

        boolean = tree.find(RESULTS_NS_ET + 'boolean')
        results = tree.find(RESULTS_NS_ET + 'results')

        if boolean is not None:
            type_ = 'ASK'
        elif results is not None:
            type_ = 'SELECT'
        else:
            g = Graph()
            try:
                g.parse(data=xmlstring)
                if len(g) == 0:
                    raise
                type_ = 'CONSTRUCT'

            except:
                raise ResultException(
                    "No RDF Graph, result-bindings or boolean answer found!")

        Result.__init__(self, type_)
        if type_ == 'SELECT':
            self.bindings = []
            for result in results:
                r = {}
                for binding in result:
                    r[Variable(binding.get('name'))] = parseTerm(binding[0])
                self.bindings.append(r)

            self.vars = [Variable(x.get("name"))
                         for x in tree.findall(
                         './%shead/%svariable' % (
                             RESULTS_NS_ET, RESULTS_NS_ET))]

        elif type_ == 'ASK':
            self.askAnswer = boolean.text.lower().strip() == "true"
        elif type_ == 'CONSTRUCT':
            self.graph = g
Esempio n. 3
0
    def __init__(self, source):

        xmlstring = source.read()

        if isinstance(xmlstring, unicode):
            xmlstring = xmlstring.encode('utf-8')
        try:
            tree = etree.fromstring(xmlstring)
        except Exception, e:
            try:
                raise e.__class__("error parsing %r: %s" % (xmlstring, e))
            except:
                raise e