Search in sources :

Example 56 with Literal

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

the class CustomTurtleParserTest method testLiteralWithNewlines.

@Test
public void testLiteralWithNewlines() throws Exception {
    String namespace = "http://www.foo.com/bar#";
    String okLiteralString = "Literal \n without \n new line at the beginning. \n ";
    String errLiteralString = "\n Literal \n with \n new line at the beginning. \n ";
    IRI mySubject = vf.createIRI(namespace, "Subject");
    IRI myPredicate = vf.createIRI(namespace, "Predicate");
    Literal myOkObject = vf.createLiteral(okLiteralString);
    Literal myErrObject = vf.createLiteral(errLiteralString);
    StringWriter out = new StringWriter();
    Model model = new LinkedHashModel();
    model.add(mySubject, myPredicate, myOkObject);
    model.add(mySubject, myPredicate, myErrObject);
    Rio.write(model, out, RDFFormat.TURTLE);
    String str = out.toString();
    System.err.println(str);
    assertTrue("okLiteralString not found", str.contains(okLiteralString));
    assertTrue("errLiteralString not found", str.contains(errLiteralString));
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) StringWriter(java.io.StringWriter) Literal(org.eclipse.rdf4j.model.Literal) Model(org.eclipse.rdf4j.model.Model) LinkedHashModel(org.eclipse.rdf4j.model.impl.LinkedHashModel) LinkedHashModel(org.eclipse.rdf4j.model.impl.LinkedHashModel) Test(org.junit.Test)

Example 57 with Literal

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

the class TransactionWriter method serialize.

protected void serialize(SPARQLUpdateOperation op, XMLWriter xmlWriter) throws IOException {
    String baseURI = op.getBaseURI();
    if (baseURI != null) {
        xmlWriter.setAttribute(TransactionXMLConstants.BASE_URI_ATT, baseURI);
    }
    xmlWriter.setAttribute(TransactionXMLConstants.INCLUDE_INFERRED_ATT, op.isIncludeInferred());
    xmlWriter.startTag(TransactionXMLConstants.SPARQL_UPDATE_TAG);
    // serialize update string
    String updateString = op.getUpdateString();
    xmlWriter.textElement(TransactionXMLConstants.UPDATE_STRING_TAG, updateString);
    // serialize dataset definition (if any)
    Dataset dataset = op.getDataset();
    if (dataset != null) {
        xmlWriter.startTag(TransactionXMLConstants.DATASET_TAG);
        xmlWriter.startTag(TransactionXMLConstants.DEFAULT_GRAPHS_TAG);
        for (IRI defaultGraph : dataset.getDefaultGraphs()) {
            xmlWriter.textElement(TransactionXMLConstants.GRAPH_TAG, defaultGraph.stringValue());
        }
        xmlWriter.endTag(TransactionXMLConstants.DEFAULT_GRAPHS_TAG);
        xmlWriter.startTag(TransactionXMLConstants.NAMED_GRAPHS_TAG);
        for (IRI namedGraph : dataset.getNamedGraphs()) {
            xmlWriter.textElement(TransactionXMLConstants.GRAPH_TAG, namedGraph.stringValue());
        }
        xmlWriter.endTag(TransactionXMLConstants.NAMED_GRAPHS_TAG);
        xmlWriter.startTag(TransactionXMLConstants.DEFAULT_REMOVE_GRAPHS_TAG);
        for (IRI defaultRemoveGraph : dataset.getDefaultRemoveGraphs()) {
            xmlWriter.textElement(TransactionXMLConstants.GRAPH_TAG, defaultRemoveGraph.stringValue());
        }
        xmlWriter.endTag(TransactionXMLConstants.DEFAULT_REMOVE_GRAPHS_TAG);
        if (dataset.getDefaultInsertGraph() != null) {
            xmlWriter.textElement(TransactionXMLConstants.DEFAULT_INSERT_GRAPH, dataset.getDefaultInsertGraph().stringValue());
        }
        xmlWriter.endTag(TransactionXMLConstants.DATASET_TAG);
    }
    if (op.getBindings() != null && op.getBindings().length > 0) {
        xmlWriter.startTag(TransactionXMLConstants.BINDINGS);
        for (Binding binding : op.getBindings()) {
            if (binding.getName() != null && binding.getValue() != null && binding.getValue().stringValue() != null) {
                if (binding.getValue() instanceof IRI) {
                    xmlWriter.setAttribute(TransactionXMLConstants.NAME_ATT, binding.getName());
                    xmlWriter.textElement(TransactionXMLConstants.BINDING_URI, binding.getValue().stringValue());
                }
                if (binding.getValue() instanceof BNode) {
                    xmlWriter.setAttribute(TransactionXMLConstants.NAME_ATT, binding.getName());
                    xmlWriter.textElement(TransactionXMLConstants.BINDING_BNODE, binding.getValue().stringValue());
                }
                if (binding.getValue() instanceof Literal) {
                    xmlWriter.setAttribute(TransactionXMLConstants.NAME_ATT, binding.getName());
                    Literal literal = (Literal) binding.getValue();
                    if (Literals.isLanguageLiteral(literal)) {
                        xmlWriter.setAttribute(TransactionXMLConstants.LANGUAGE_ATT, literal.getLanguage().get());
                    } else {
                        xmlWriter.setAttribute(TransactionXMLConstants.DATA_TYPE_ATT, literal.getDatatype().stringValue());
                    }
                    xmlWriter.textElement(TransactionXMLConstants.BINDING_LITERAL, binding.getValue().stringValue());
                }
            }
        }
        xmlWriter.endTag(TransactionXMLConstants.BINDINGS);
    }
    xmlWriter.endTag(TransactionXMLConstants.SPARQL_UPDATE_TAG);
}
Also used : Binding(org.eclipse.rdf4j.query.Binding) IRI(org.eclipse.rdf4j.model.IRI) BNode(org.eclipse.rdf4j.model.BNode) Dataset(org.eclipse.rdf4j.query.Dataset) Literal(org.eclipse.rdf4j.model.Literal)

Example 58 with Literal

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

the class TransactionSAXParser method startTag.

@Override
public void startTag(String tagName, Map<String, String> atts, String text) throws SAXException {
    if (TransactionXMLConstants.URI_TAG.equals(tagName)) {
        parsedValues.add(valueFactory.createIRI(text));
    } else if (TransactionXMLConstants.BNODE_TAG.equals(tagName)) {
        parsedValues.add(valueFactory.createBNode(text));
    } else if (TransactionXMLConstants.LITERAL_TAG.equals(tagName)) {
        String lang = atts.get(TransactionXMLConstants.LANG_ATT);
        String datatype = atts.get(TransactionXMLConstants.DATATYPE_ATT);
        String encoding = atts.get(TransactionXMLConstants.ENCODING_ATT);
        if (encoding != null && "base64".equalsIgnoreCase(encoding)) {
            text = new String(javax.xml.bind.DatatypeConverter.parseBase64Binary(text));
        }
        Literal lit;
        if (lang != null) {
            lit = valueFactory.createLiteral(text, lang);
        } else if (datatype != null) {
            IRI dtURI = valueFactory.createIRI(datatype);
            lit = valueFactory.createLiteral(text, dtURI);
        } else {
            lit = valueFactory.createLiteral(text);
        }
        parsedValues.add(lit);
    } else if (TransactionXMLConstants.NULL_TAG.equals(tagName)) {
        parsedValues.add(null);
    } else if (TransactionXMLConstants.SET_NAMESPACE_TAG.equals(tagName)) {
        String prefix = atts.get(TransactionXMLConstants.PREFIX_ATT);
        String name = atts.get(TransactionXMLConstants.NAME_ATT);
        txn.add(new SetNamespaceOperation(prefix, name));
    } else if (TransactionXMLConstants.REMOVE_NAMESPACE_TAG.equals(tagName)) {
        String prefix = atts.get(TransactionXMLConstants.PREFIX_ATT);
        txn.add(new RemoveNamespaceOperation(prefix));
    } else if (TransactionXMLConstants.CLEAR_NAMESPACES_TAG.equals(tagName)) {
        txn.add(new ClearNamespacesOperation());
    } else if (TransactionXMLConstants.SPARQL_UPDATE_TAG.equals(tagName)) {
        if (currentSPARQLUpdate != null) {
            throw new SAXException("unexpected start of SPARQL Update operation");
        }
        currentSPARQLUpdate = new SPARQLUpdateOperation();
        String baseURI = atts.get(TransactionXMLConstants.BASE_URI_ATT);
        boolean includeInferred = Boolean.parseBoolean(atts.get(TransactionXMLConstants.INCLUDE_INFERRED_ATT));
        currentSPARQLUpdate.setIncludeInferred(includeInferred);
        currentSPARQLUpdate.setBaseURI(baseURI);
    } else if (TransactionXMLConstants.UPDATE_STRING_TAG.equals(tagName)) {
        currentSPARQLUpdate.setUpdateString(text);
    } else if (TransactionXMLConstants.DATASET_TAG.equals(tagName)) {
        currentDataset = new SimpleDataset();
    } else if (TransactionXMLConstants.DEFAULT_INSERT_GRAPH.equals(tagName)) {
        currentDataset.setDefaultInsertGraph(valueFactory.createIRI(text));
    } else if (TransactionXMLConstants.GRAPH_TAG.equals(tagName)) {
        parsedValues.add(valueFactory.createIRI(text));
    } else if (TransactionXMLConstants.BINDINGS.equals(tagName)) {
        if (bindings != null) {
            throw new SAXException("unexpected start of SPARQL Update operation bindings");
        }
        bindings = new ArrayList<Binding>();
    } else if (TransactionXMLConstants.BINDING_URI.equals(tagName) || TransactionXMLConstants.BINDING_BNODE.equals(tagName) || TransactionXMLConstants.BINDING_LITERAL.equals(tagName)) {
        if (bindings == null) {
            throw new SAXException("unexpected start of SPARQL Update operation binding (without <bindings>)");
        }
        String value = text;
        String name = atts.get(TransactionXMLConstants.NAME_ATT);
        if (name != null && value != null) {
            Value v;
            if (TransactionXMLConstants.BINDING_URI.equals(tagName)) {
                v = valueFactory.createIRI(value);
            } else if (TransactionXMLConstants.BINDING_BNODE.equals(tagName)) {
                v = valueFactory.createBNode(value);
            } else {
                String language = atts.get(TransactionXMLConstants.LANGUAGE_ATT);
                String dataType = atts.get(TransactionXMLConstants.DATA_TYPE_ATT);
                if (language != null) {
                    v = valueFactory.createLiteral(value, language);
                } else if (dataType != null) {
                    v = valueFactory.createLiteral(value, valueFactory.createIRI(dataType));
                } else {
                    v = valueFactory.createLiteral(value);
                }
            }
            bindings.add(new SimpleBinding(name, v));
        }
    }
}
Also used : Binding(org.eclipse.rdf4j.query.Binding) SimpleBinding(org.eclipse.rdf4j.query.impl.SimpleBinding) IRI(org.eclipse.rdf4j.model.IRI) ClearNamespacesOperation(org.eclipse.rdf4j.http.protocol.transaction.operations.ClearNamespacesOperation) SimpleBinding(org.eclipse.rdf4j.query.impl.SimpleBinding) SPARQLUpdateOperation(org.eclipse.rdf4j.http.protocol.transaction.operations.SPARQLUpdateOperation) SimpleDataset(org.eclipse.rdf4j.query.impl.SimpleDataset) RemoveNamespaceOperation(org.eclipse.rdf4j.http.protocol.transaction.operations.RemoveNamespaceOperation) SAXException(org.xml.sax.SAXException) SetNamespaceOperation(org.eclipse.rdf4j.http.protocol.transaction.operations.SetNamespaceOperation) Literal(org.eclipse.rdf4j.model.Literal) Value(org.eclipse.rdf4j.model.Value)

Example 59 with Literal

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

the class SPARQLConnection method createDataBody.

private void createDataBody(StringBuilder qb, Iterable<? extends Statement> statements, boolean ignoreContext) {
    for (Statement st : statements) {
        final Resource context = st.getContext();
        if (!ignoreContext) {
            if (context != null) {
                String namedGraph = context.stringValue();
                if (context instanceof BNode) {
                    // SPARQL does not allow blank nodes as named graph
                    // identifiers, so we need to skolemize
                    // the blank node id.
                    namedGraph = "urn:nodeid:" + context.stringValue();
                }
                qb.append("    GRAPH <" + namedGraph + "> { \n");
            }
        }
        if (st.getSubject() instanceof BNode) {
            qb.append("_:" + st.getSubject().stringValue() + " ");
        } else {
            qb.append("<" + st.getSubject().stringValue() + "> ");
        }
        qb.append("<" + st.getPredicate().stringValue() + "> ");
        if (st.getObject() instanceof Literal) {
            Literal lit = (Literal) st.getObject();
            qb.append("\"");
            qb.append(SPARQLUtil.encodeString(lit.getLabel()));
            qb.append("\"");
            if (Literals.isLanguageLiteral(lit)) {
                qb.append("@");
                qb.append(lit.getLanguage().get());
            } else {
                qb.append("^^<" + lit.getDatatype().stringValue() + ">");
            }
            qb.append(" ");
        } else if (st.getObject() instanceof BNode) {
            qb.append("_:" + st.getObject().stringValue() + " ");
        } else {
            qb.append("<" + st.getObject().stringValue() + "> ");
        }
        qb.append(". \n");
        if (!ignoreContext && context != null) {
            qb.append("    }\n");
        }
    }
}
Also used : BNode(org.eclipse.rdf4j.model.BNode) Statement(org.eclipse.rdf4j.model.Statement) Literal(org.eclipse.rdf4j.model.Literal) Resource(org.eclipse.rdf4j.model.Resource)

Example 60 with Literal

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

the class RepositoryConfigUtil method getRepositoryIDs.

@Deprecated
public static Set<String> getRepositoryIDs(Repository repository) throws RepositoryException {
    RepositoryConnection con = repository.getConnection();
    try {
        Set<String> idSet = new LinkedHashSet<String>();
        RepositoryResult<Statement> idStatementIter = con.getStatements(null, REPOSITORYID, null, true);
        try {
            while (idStatementIter.hasNext()) {
                Statement idStatement = idStatementIter.next();
                if (idStatement.getObject() instanceof Literal) {
                    Literal idLiteral = (Literal) idStatement.getObject();
                    idSet.add(idLiteral.getLabel());
                }
            }
        } finally {
            idStatementIter.close();
        }
        return idSet;
    } finally {
        con.close();
    }
}
Also used : RepositoryConnection(org.eclipse.rdf4j.repository.RepositoryConnection) LinkedHashSet(java.util.LinkedHashSet) Statement(org.eclipse.rdf4j.model.Statement) Literal(org.eclipse.rdf4j.model.Literal)

Aggregations

Literal (org.eclipse.rdf4j.model.Literal)96 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 Model (org.eclipse.rdf4j.model.Model)13 Statement (org.eclipse.rdf4j.model.Statement)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 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 ArrayList (java.util.ArrayList)3