コード例 #1
0
def json_to_rdflib(json_object):
    """
    Convert a json result entry to an :mod:`rdfLib` type.

    :param dict json_object: the *JSON* object
    :return: the converted value (if possible)
    :rtype: :class:`rdflib.term.Literal` or :class:`rdflib.term.BNode` or :class:`rdflib.term.URIRef` or None
    """
    try:
        type = json_object["type"]
    except KeyError:
        raise ValueError("No type specified")

    if type == 'uri':
        return URIRef(json_object["value"])

    elif type == 'literal':
        if "xml:lang" in json_object:
            return Literal(json_object["value"], lang=json_object['xml:lang'])
        else:
            return Literal(json_object["value"])

    elif type == 'typed-literal':
        return Literal(json_object["value"],
                       datatype=URIRef(json_object['datatype']))

    elif type == 'bnode':
        return BNode(json_object["value"])

    else:
        return None
コード例 #2
0
def toRdfLib(term):
    if type(term) is fURIRef:
        return URIRef(term.getURI())
    elif type(term) is fLiteral:
        try:
            if term.getDatatype() is None:
                return Literal(term.getLabel(), lang=term.getLanguage())
            else:
                dtype = term.getDatatype().getURI()
                if dtype.startswith('<') and dtype.endswith('>'):
                    dtype = dtype.strip('<>')
                    dtype = URIRef(dtype)
                else:
                    dtype = URIRef(dtype)

                return Literal(term.getLabel(), lang=term.getLanguage(),
                                datatype=dtype)

        except Exception as e:
            print(e)
    elif type(term) is fBNode:
        return BNode(term.getID())
    elif type(term) in [list,tuple]:
        return map(toRdfLib, term)
    return term
コード例 #3
0
ファイル: sesame2.py プロジェクト: zxenia/surfrdf
 def get_binding(node):
     cnt = None
     for cn in node.childNodes:
         if cn.nodeType == cn.ELEMENT_NODE:
             cnt = cn
             break
     if cnt:
         txt = get_text(cnt)
         if cnt.nodeName == 'uri':
             return URIRef(txt)
         elif cnt.nodeName == 'literal':
             return Literal(txt)
         elif cnt.nodeName == 'bnode':
             return BNode(txt)
     else:
         return None
コード例 #4
0
ファイル: util.py プロジェクト: zxenia/surfrdf
def json_to_rdflib(obj):
    """Convert a json result entry to an rdfLib type."""
    try:
        type = obj["type"]
    except KeyError:
        raise ValueError("No type specified")

    if type == 'uri':
        return URIRef(obj["value"])
    elif type == 'literal':
        if "xml:lang" in obj:
            return Literal(obj["value"], lang=obj['xml:lang'])
        else:
            return Literal(obj["value"])
    elif type == 'typed-literal':
        return Literal(obj["value"], datatype=URIRef(obj['datatype']))
    elif type == 'bnode':
        return BNode(obj["value"])
    else:
        return None
コード例 #5
0
ファイル: util.py プロジェクト: zxenia/surfrdf
            else:
                dtype = term.getDatatype().getURI()
                if dtype.startswith('<') and dtype.endswith('>'):
                    dtype = dtype.strip('<>')
                    dtype = URIRef(dtype)
                else:
                    dtype = URIRef(dtype)

                return Literal(term.getLabel(),
                               lang=term.getLanguage(),
                               datatype=dtype)

        except Exception, e:
            print e
    elif type(term) is fBNode:
        return BNode(term.getID())
    elif type(term) in [list, tuple]:
        return map(toRdfLib, term)
    return term


def toSesame(term, factory):
    if type(term) in (URIRef, Namespace):
        return factory.createURI(unicode(term))
    elif type(term) is Literal:
        return factory.createLiteral(unicode(term),
                                     datatype=term.datatype,
                                     language=term.language)
    elif type(term) is BNode:
        return factory.createBNode(unicode(term))
    elif type(term) in [list, tuple]: