Search in sources :

Example 36 with Literal

use of org.eclipse.rdf4j.model.Literal in project rdf4j by eclipse.

the class SPARQLConnection method createBGP.

private void createBGP(StringBuilder qb, Resource subject, IRI predicate, Value object) {
    if (subject != null) {
        if (subject instanceof BNode) {
            qb.append("_:" + subject.stringValue() + " ");
        } else {
            qb.append("<" + subject.stringValue() + "> ");
        }
    } else {
        qb.append("?subj");
    }
    if (predicate != null) {
        qb.append("<" + predicate.stringValue() + "> ");
    } else {
        qb.append("?pred");
    }
    if (object != null) {
        if (object instanceof Literal) {
            Literal lit = (Literal) object;
            qb.append("\"");
            qb.append(SPARQLUtil.encodeString(lit.getLabel()));
            qb.append("\"");
            if (lit.getLanguage().isPresent()) {
                qb.append("@");
                qb.append(lit.getLanguage().get());
            } else {
                qb.append("^^<" + lit.getDatatype().stringValue() + ">");
            }
            qb.append(" ");
        } else if (object instanceof BNode) {
            qb.append("_:" + object.stringValue() + " ");
        } else {
            qb.append("<" + object.stringValue() + "> ");
        }
    } else {
        qb.append("?obj");
    }
    qb.append(". \n");
}
Also used : BNode(org.eclipse.rdf4j.model.BNode) Literal(org.eclipse.rdf4j.model.Literal)

Example 37 with Literal

use of org.eclipse.rdf4j.model.Literal in project rdf4j by eclipse.

the class HTTPRepositoryConnection method getNamespaces.

public RepositoryResult<Namespace> getNamespaces() throws RepositoryException {
    try {
        List<Namespace> namespaceList = new ArrayList<Namespace>();
        TupleQueryResult namespaces = client.getNamespaces();
        try {
            while (namespaces.hasNext()) {
                BindingSet bindingSet = namespaces.next();
                Value prefix = bindingSet.getValue("prefix");
                Value namespace = bindingSet.getValue("namespace");
                if (prefix instanceof Literal && namespace instanceof Literal) {
                    String prefixStr = ((Literal) prefix).getLabel();
                    String namespaceStr = ((Literal) namespace).getLabel();
                    namespaceList.add(new SimpleNamespace(prefixStr, namespaceStr));
                }
            }
        } finally {
            namespaces.close();
        }
        return createRepositoryResult(namespaceList);
    } catch (QueryEvaluationException e) {
        throw new RepositoryException(e);
    } catch (IOException e) {
        throw new RepositoryException(e);
    }
}
Also used : BindingSet(org.eclipse.rdf4j.query.BindingSet) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) Literal(org.eclipse.rdf4j.model.Literal) ArrayList(java.util.ArrayList) Value(org.eclipse.rdf4j.model.Value) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) IOException(java.io.IOException) TupleQueryResult(org.eclipse.rdf4j.query.TupleQueryResult) Namespace(org.eclipse.rdf4j.model.Namespace) SimpleNamespace(org.eclipse.rdf4j.model.impl.SimpleNamespace) SimpleNamespace(org.eclipse.rdf4j.model.impl.SimpleNamespace)

Example 38 with Literal

use of org.eclipse.rdf4j.model.Literal in project rdf4j by eclipse.

the class RDFXMLParser method text.

void text(String text) throws RDFParseException, RDFHandlerException {
    if (!topIsProperty()) {
        reportError("unexpected literal", XMLParserSettings.FAIL_ON_NON_STANDARD_ATTRIBUTES);
        return;
    }
    PropertyElement propEl = (PropertyElement) peekStack(0);
    IRI datatype = propEl.getDatatype();
    Literal lit = createLiteral(text, xmlLang, datatype);
    NodeElement subject = (NodeElement) peekStack(1);
    PropertyElement predicate = (PropertyElement) peekStack(0);
    reportStatement(subject.getResource(), predicate.getURI(), lit);
    handleReification(lit);
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) Literal(org.eclipse.rdf4j.model.Literal)

Example 39 with Literal

use of org.eclipse.rdf4j.model.Literal in project rdf4j by eclipse.

the class RDFXMLParser method processPropertyElt.

private void processPropertyElt(String namespaceURI, String localName, String qName, Atts atts, boolean isEmptyElt) throws RDFParseException, RDFHandlerException {
    if (getParserConfig().get(XMLParserSettings.FAIL_ON_NON_STANDARD_ATTRIBUTES)) {
        checkPropertyEltName(namespaceURI, localName, qName, XMLParserSettings.FAIL_ON_NON_STANDARD_ATTRIBUTES);
    }
    // Get the URI of the property
    IRI propURI = null;
    if (namespaceURI.equals("")) {
        // no namespace URI
        reportError("unqualified property element <" + qName + "> not allowed", XMLParserSettings.FAIL_ON_INVALID_QNAME);
        // Use base URI as namespace:
        propURI = buildResourceFromLocalName(localName);
    } else {
        propURI = createURI(namespaceURI + localName);
    }
    // List expansion rule
    if (propURI.equals(RDF.LI)) {
        NodeElement subject = (NodeElement) peekStack(0);
        propURI = createURI(RDF.NAMESPACE + "_" + subject.getNextLiCounter());
    }
    // Push the property on the stack.
    PropertyElement predicate = new PropertyElement(propURI);
    elementStack.push(predicate);
    // Check if property has a reification ID
    Att id = atts.removeAtt(RDF.NAMESPACE, "ID");
    if (id != null) {
        IRI reifURI = buildURIFromID(id.getValue());
        predicate.setReificationURI(reifURI);
    }
    // Check for presence of rdf:parseType attribute
    Att parseType = atts.removeAtt(RDF.NAMESPACE, "parseType");
    if (parseType != null) {
        if (getParserConfig().get(XMLParserSettings.FAIL_ON_NON_STANDARD_ATTRIBUTES)) {
            checkNoMoreAtts(atts);
        }
        String parseTypeValue = parseType.getValue();
        if (parseTypeValue.equals("Resource")) {
            Resource objectResource = createNode();
            NodeElement subject = (NodeElement) peekStack(1);
            reportStatement(subject.getResource(), propURI, objectResource);
            if (isEmptyElt) {
                handleReification(objectResource);
            } else {
                NodeElement object = new NodeElement(objectResource);
                object.setIsVolatile(true);
                elementStack.push(object);
            }
        } else if (parseTypeValue.equals("Collection")) {
            if (isEmptyElt) {
                NodeElement subject = (NodeElement) peekStack(1);
                reportStatement(subject.getResource(), propURI, RDF.NIL);
                handleReification(RDF.NIL);
            } else {
                predicate.setParseCollection(true);
            }
        } else {
            // other parseType
            if (!parseTypeValue.equals("Literal")) {
                reportWarning("unknown parseType: " + parseType.getValue());
            }
            if (isEmptyElt) {
                NodeElement subject = (NodeElement) peekStack(1);
                Literal lit = createLiteral("", null, RDF.XMLLITERAL);
                reportStatement(subject.getResource(), propURI, lit);
                handleReification(lit);
            } else {
                // The next string is an rdf:XMLLiteral
                predicate.setDatatype(RDF.XMLLITERAL);
                saxFilter.setParseLiteralMode();
            }
        }
    } else // parseType == null
    if (isEmptyElt) {
        // empty element without an rdf:parseType attribute
        // Note: we handle rdf:datatype attributes here to allow datatyped
        // empty strings in documents. The current spec does have a
        // production rule that matches this, which is likely to be an
        // omission on its part.
        Att datatype = atts.getAtt(RDF.NAMESPACE, "datatype");
        if (atts.size() == 0 || atts.size() == 1 && datatype != null) {
            // element had no attributes, or only the optional
            // rdf:ID and/or rdf:datatype attributes.
            NodeElement subject = (NodeElement) peekStack(1);
            IRI dtURI = null;
            if (datatype != null) {
                dtURI = createURI(datatype.getValue());
            }
            Literal lit = createLiteral("", xmlLang, dtURI);
            reportStatement(subject.getResource(), propURI, lit);
            handleReification(lit);
        } else {
            // Create resource for the statement's object.
            Resource resourceRes = getPropertyResource(atts);
            // All special rdf attributes have been checked/removed.
            if (getParserConfig().get(XMLParserSettings.FAIL_ON_NON_STANDARD_ATTRIBUTES)) {
                checkRDFAtts(atts);
            }
            NodeElement resourceElt = new NodeElement(resourceRes);
            NodeElement subject = (NodeElement) peekStack(1);
            reportStatement(subject.getResource(), propURI, resourceRes);
            handleReification(resourceRes);
            Att type = atts.removeAtt(RDF.NAMESPACE, "type");
            if (type != null) {
                // rdf:type attribute, value is a URI-reference
                IRI className = resolveURI(type.getValue());
                reportStatement(resourceRes, RDF.TYPE, className);
            }
            processSubjectAtts(resourceElt, atts);
        }
    } else {
        // Not an empty element, sub elements will follow.
        // Check for rdf:datatype attribute
        Att datatype = atts.removeAtt(RDF.NAMESPACE, "datatype");
        if (datatype != null) {
            IRI dtURI = resolveURI(datatype.getValue());
            predicate.setDatatype(dtURI);
        }
        // No more attributes are expected.
        if (getParserConfig().get(XMLParserSettings.FAIL_ON_NON_STANDARD_ATTRIBUTES)) {
            checkNoMoreAtts(atts);
        }
    }
    if (isEmptyElt) {
        // Empty element has been pushed on the stack
        // at the start of this method, remove it.
        elementStack.pop();
    }
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) Literal(org.eclipse.rdf4j.model.Literal) Resource(org.eclipse.rdf4j.model.Resource)

Example 40 with Literal

use of org.eclipse.rdf4j.model.Literal in project rdf4j by eclipse.

the class RDFJSONWriter method writeObject.

/**
 * Helper method to reduce complexity of the JSON serialisation algorithm Any null contexts will only be
 * serialised to JSON if there are also non-null contexts in the contexts array
 *
 * @param object
 *        The RDF value to serialise
 * @param contexts
 *        The set of contexts that are relevant to this object, including null contexts as they are found.
 * @param jg
 *        the {@link JsonGenerator} to write to.
 * @throws IOException
 * @throws JsonGenerationException
 * @throws JSONException
 */
public static void writeObject(final Value object, final Set<Resource> contexts, final JsonGenerator jg) throws JsonGenerationException, IOException {
    jg.writeStartObject();
    if (object instanceof Literal) {
        jg.writeObjectField(RDFJSONUtility.VALUE, object.stringValue());
        jg.writeObjectField(RDFJSONUtility.TYPE, RDFJSONUtility.LITERAL);
        final Literal l = (Literal) object;
        if (Literals.isLanguageLiteral(l)) {
            jg.writeObjectField(RDFJSONUtility.LANG, l.getLanguage().orElse(null));
        } else {
            jg.writeObjectField(RDFJSONUtility.DATATYPE, l.getDatatype().stringValue());
        }
    } else if (object instanceof BNode) {
        jg.writeObjectField(RDFJSONUtility.VALUE, resourceToString((BNode) object));
        jg.writeObjectField(RDFJSONUtility.TYPE, RDFJSONUtility.BNODE);
    } else if (object instanceof IRI) {
        jg.writeObjectField(RDFJSONUtility.VALUE, resourceToString((IRI) object));
        jg.writeObjectField(RDFJSONUtility.TYPE, RDFJSONUtility.URI);
    }
    if (contexts != null && !contexts.isEmpty() && !(contexts.size() == 1 && contexts.iterator().next() == null)) {
        jg.writeArrayFieldStart(RDFJSONUtility.GRAPHS);
        for (final Resource nextContext : contexts) {
            if (nextContext == null) {
                jg.writeNull();
            } else {
                jg.writeString(resourceToString(nextContext));
            }
        }
        jg.writeEndArray();
    }
    jg.writeEndObject();
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) BNode(org.eclipse.rdf4j.model.BNode) Literal(org.eclipse.rdf4j.model.Literal) Resource(org.eclipse.rdf4j.model.Resource)

Aggregations

Literal (org.eclipse.rdf4j.model.Literal)98 Test (org.junit.Test)52 IRI (org.eclipse.rdf4j.model.IRI)34 Value (org.eclipse.rdf4j.model.Value)17 BNode (org.eclipse.rdf4j.model.BNode)14 Statement (org.eclipse.rdf4j.model.Statement)14 Model (org.eclipse.rdf4j.model.Model)13 Resource (org.eclipse.rdf4j.model.Resource)9 LinkedHashModel (org.eclipse.rdf4j.model.impl.LinkedHashModel)8 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)7 InputStream (java.io.InputStream)6 Date (java.util.Date)6 BindingSet (org.eclipse.rdf4j.query.BindingSet)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 ValueFactory (org.eclipse.rdf4j.model.ValueFactory)4 SimpleValueFactory (org.eclipse.rdf4j.model.impl.SimpleValueFactory)4 AbstractQueryResultIOTupleTest (org.eclipse.rdf4j.query.resultio.AbstractQueryResultIOTupleTest)4 QueryResultCollector (org.eclipse.rdf4j.query.resultio.helpers.QueryResultCollector)4 SPARQLResultsJSONParser (org.eclipse.rdf4j.query.resultio.sparqljson.SPARQLResultsJSONParser)4