Search in sources :

Example 31 with Literal

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

the class LiteralsTest method testCreateLiteralObjectXMLGregorianCalendar.

/**
 * Test method for
 * {@link org.eclipse.rdf4j.model.util.Literals#createLiteral(org.eclipse.rdf4j.model.ValueFactory, java.lang.Object)}
 * .
 */
@Test
public void testCreateLiteralObjectXMLGregorianCalendar() throws Exception {
    GregorianCalendar c = new GregorianCalendar();
    c.setTime(new Date());
    try {
        Object obj = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
        Literal l = Literals.createLiteral(SimpleValueFactory.getInstance(), obj);
        assertNotNull(l);
        assertEquals(l.getDatatype(), XMLSchema.DATETIME);
    // TODO check lexical value?
    } catch (DatatypeConfigurationException e) {
        e.printStackTrace();
        fail("Could not instantiate javax.xml.datatype.DatatypeFactory");
    }
}
Also used : DatatypeConfigurationException(javax.xml.datatype.DatatypeConfigurationException) Literal(org.eclipse.rdf4j.model.Literal) GregorianCalendar(java.util.GregorianCalendar) Date(java.util.Date) Test(org.junit.Test)

Example 32 with Literal

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

the class DAWGTestResultSetParser method getBinding.

private Binding getBinding(Resource bindingNode) throws GraphUtilException {
    Literal name = GraphUtil.getUniqueObjectLiteral(graph, bindingNode, VARIABLE);
    Value value = GraphUtil.getUniqueObject(graph, bindingNode, VALUE);
    return new SimpleBinding(name.getLabel(), value);
}
Also used : Literal(org.eclipse.rdf4j.model.Literal) SimpleBinding(org.eclipse.rdf4j.query.impl.SimpleBinding) Value(org.eclipse.rdf4j.model.Value)

Example 33 with Literal

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

the class ContextAwareConfig method export.

@Override
public Resource export(Model model) {
    Resource repImplNode = super.export(model);
    ValueFactory vf = SimpleValueFactory.getInstance();
    if (includeInferred != null) {
        Literal bool = vf.createLiteral(includeInferred);
        model.add(repImplNode, INCLUDE_INFERRED, bool);
    }
    if (maxQueryTime > 0) {
        model.add(repImplNode, MAX_QUERY_TIME, vf.createLiteral(maxQueryTime));
    }
    if (queryLanguage != null) {
        model.add(repImplNode, QUERY_LANGUAGE, vf.createLiteral(queryLanguage.getName()));
    }
    if (baseURI != null) {
        model.add(repImplNode, BASE_URI, vf.createIRI(baseURI));
    }
    for (IRI uri : readContexts) {
        model.add(repImplNode, READ_CONTEXT, uri);
    }
    for (IRI resource : addContexts) {
        model.add(repImplNode, ADD_CONTEXT, resource);
    }
    for (IRI resource : removeContexts) {
        model.add(repImplNode, REMOVE_CONTEXT, resource);
    }
    for (IRI resource : archiveContexts) {
        model.add(repImplNode, ARCHIVE_CONTEXT, resource);
    }
    if (insertContext != null) {
        model.add(repImplNode, INSERT_CONTEXT, insertContext);
    }
    return repImplNode;
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) Literal(org.eclipse.rdf4j.model.Literal) Resource(org.eclipse.rdf4j.model.Resource) ValueFactory(org.eclipse.rdf4j.model.ValueFactory) SimpleValueFactory(org.eclipse.rdf4j.model.impl.SimpleValueFactory)

Example 34 with Literal

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

the class AbstractSPARQLJSONWriter method writeValue.

protected void writeValue(Value value) throws IOException, QueryResultHandlerException {
    jg.writeStartObject();
    if (value instanceof IRI) {
        jg.writeStringField("type", "uri");
        jg.writeStringField("value", ((IRI) value).toString());
    } else if (value instanceof BNode) {
        jg.writeStringField("type", "bnode");
        jg.writeStringField("value", ((BNode) value).getID());
    } else if (value instanceof Literal) {
        Literal lit = (Literal) value;
        if (Literals.isLanguageLiteral(lit)) {
            jg.writeObjectField("xml:lang", lit.getLanguage().orElse(null));
        } else {
            IRI datatype = lit.getDatatype();
            boolean ignoreDatatype = datatype.equals(XMLSchema.STRING) && xsdStringToPlainLiteral();
            if (!ignoreDatatype) {
                jg.writeObjectField("datatype", lit.getDatatype().stringValue());
            }
        }
        jg.writeObjectField("type", "literal");
        jg.writeObjectField("value", lit.getLabel());
    } else {
        throw new TupleQueryResultHandlerException("Unknown Value object type: " + value.getClass());
    }
    jg.writeEndObject();
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) BNode(org.eclipse.rdf4j.model.BNode) TupleQueryResultHandlerException(org.eclipse.rdf4j.query.TupleQueryResultHandlerException) Literal(org.eclipse.rdf4j.model.Literal)

Example 35 with Literal

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

the class AbstractRepositoryImplConfig method create.

/**
 * Utility method to create a new {@link RepositoryImplConfig} by reading data from the supplied
 * {@link Model}.
 *
 * @param model
 *        the {@link Model} to read configuration data from.
 * @param implNode
 *        the subject {@link Resource} identifying the configuration data in the Model.
 * @return a new {@link RepositoryImplConfig} initialized with the configuration from the input Model, or
 *         {@code null} if no {@link RepositoryConfigSchema#REPOSITORYTYPE} property was found in the
 *         configuration data..
 * @throws RepositoryConfigException
 *         if an error occurred reading the configuration data from the model.
 */
public static RepositoryImplConfig create(Model model, Resource resource) throws RepositoryConfigException {
    try {
        // Literal typeLit = GraphUtil.getOptionalObjectLiteral(graph,
        // implNode, REPOSITORYTYPE);
        final Literal typeLit = Models.objectLiteral(model.filter(resource, REPOSITORYTYPE, null)).orElse(null);
        if (typeLit != null) {
            RepositoryFactory factory = RepositoryRegistry.getInstance().get(typeLit.getLabel()).orElseThrow(() -> new RepositoryConfigException("Unsupported repository type: " + typeLit.getLabel()));
            RepositoryImplConfig implConfig = factory.getConfig();
            implConfig.parse(model, resource);
            return implConfig;
        }
        return null;
    } catch (ModelException e) {
        throw new RepositoryConfigException(e.getMessage(), e);
    }
}
Also used : ModelException(org.eclipse.rdf4j.model.util.ModelException) Literal(org.eclipse.rdf4j.model.Literal)

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