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));
}
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);
}
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));
}
}
}
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");
}
}
}
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();
}
}
Aggregations