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