use of org.eclipse.rdf4j.model.Literal in project rdf4j by eclipse.
the class RDFXMLParser method processSubjectAtts.
/**
* processes subject attributes.
*/
private void processSubjectAtts(NodeElement nodeElt, Atts atts) throws RDFParseException, RDFHandlerException {
Resource subject = nodeElt.getResource();
Iterator<Att> iter = atts.iterator();
while (iter.hasNext()) {
Att att = iter.next();
IRI predicate = createURI(att.getURI());
Literal lit = createLiteral(att.getValue(), xmlLang, null);
reportStatement(subject, predicate, lit);
}
}
use of org.eclipse.rdf4j.model.Literal in project rdf4j by eclipse.
the class RDFXMLWriter method handleStatement.
public void handleStatement(Statement st) throws RDFHandlerException {
if (!writingStarted) {
throw new RDFHandlerException("Document writing has not yet been started");
}
Resource subj = st.getSubject();
IRI pred = st.getPredicate();
Value obj = st.getObject();
// Verify that an XML namespace-qualified name can be created for the
// predicate
String predString = pred.toString();
int predSplitIdx = XMLUtil.findURISplitIndex(predString);
if (predSplitIdx == -1) {
throw new RDFHandlerException("Unable to create XML namespace-qualified name for predicate: " + predString);
}
String predNamespace = predString.substring(0, predSplitIdx);
String predLocalName = predString.substring(predSplitIdx);
try {
if (!headerWritten) {
writeHeader();
}
// SUBJECT
if (!subj.equals(lastWrittenSubject)) {
flushPendingStatements();
// Write new subject:
writeNewLine();
writeStartOfStartTag(RDF.NAMESPACE, "Description");
if (subj instanceof BNode) {
BNode bNode = (BNode) subj;
writeAttribute(RDF.NAMESPACE, "nodeID", getValidNodeId(bNode));
} else if (baseIRI != null) {
writeAttribute(RDF.NAMESPACE, "about", baseIRI.relativize(subj.stringValue()));
} else {
IRI uri = (IRI) subj;
writeAttribute(RDF.NAMESPACE, "about", uri.toString());
}
writeEndOfStartTag();
writeNewLine();
lastWrittenSubject = subj;
}
// PREDICATE
writeIndent();
writeStartOfStartTag(predNamespace, predLocalName);
// OBJECT
if (obj instanceof Resource) {
Resource objRes = (Resource) obj;
if (objRes instanceof BNode) {
BNode bNode = (BNode) objRes;
writeAttribute(RDF.NAMESPACE, "nodeID", getValidNodeId(bNode));
} else if (baseIRI != null) {
writeAttribute(RDF.NAMESPACE, "resource", baseIRI.relativize(objRes.stringValue()));
} else {
IRI uri = (IRI) objRes;
writeAttribute(RDF.NAMESPACE, "resource", uri.toString());
}
writeEndOfEmptyTag();
} else if (obj instanceof Literal) {
Literal objLit = (Literal) obj;
// datatype attribute
boolean isXMLLiteral = false;
// language attribute
if (Literals.isLanguageLiteral(objLit)) {
writeAttribute("xml:lang", objLit.getLanguage().get());
} else {
IRI datatype = objLit.getDatatype();
// Check if datatype is rdf:XMLLiteral
isXMLLiteral = datatype.equals(RDF.XMLLITERAL);
if (isXMLLiteral) {
writeAttribute(RDF.NAMESPACE, "parseType", "Literal");
} else if (!datatype.equals(XMLSchema.STRING)) {
writeAttribute(RDF.NAMESPACE, "datatype", datatype.toString());
}
}
writeEndOfStartTag();
// label
if (isXMLLiteral) {
// Write XML literal as plain XML
writer.write(objLit.getLabel());
} else {
writeCharacterData(objLit.getLabel());
}
writeEndTag(predNamespace, predLocalName);
}
writeNewLine();
// Don't write </rdf:Description> yet, maybe the next statement
// has the same subject.
} catch (IOException e) {
throw new RDFHandlerException(e);
}
}
use of org.eclipse.rdf4j.model.Literal in project rdf4j by eclipse.
the class RDFXMLPrettyWriter method writeAbbreviatedPredicate.
/**
* Write out an empty property element.
*/
private void writeAbbreviatedPredicate(IRI pred, Value obj) throws IOException, RDFHandlerException {
writeStartOfStartTag(pred.getNamespace(), pred.getLocalName());
if (obj instanceof Resource) {
Resource objRes = (Resource) obj;
if (objRes instanceof IRI) {
IRI uri = (IRI) objRes;
writeAttribute(RDF.NAMESPACE, "resource", uri.toString());
} else {
BNode bNode = (BNode) objRes;
writeAttribute(RDF.NAMESPACE, "nodeID", getValidNodeId(bNode));
}
writeEndOfEmptyTag();
} else if (obj instanceof Literal) {
Literal objLit = (Literal) obj;
// datatype attribute
IRI datatype = objLit.getDatatype();
// Check if datatype is rdf:XMLLiteral
boolean isXmlLiteral = datatype.equals(RDF.XMLLITERAL);
// language attribute
if (Literals.isLanguageLiteral(objLit)) {
writeAttribute("xml:lang", objLit.getLanguage().get());
} else {
if (isXmlLiteral) {
writeAttribute(RDF.NAMESPACE, "parseType", "Literal");
} else {
writeAttribute(RDF.NAMESPACE, "datatype", datatype.toString());
}
}
writeEndOfStartTag();
// label
if (isXmlLiteral) {
// Write XML literal as plain XML
writer.write(objLit.getLabel());
} else {
writeCharacterData(objLit.getLabel());
}
writeEndTag(pred.getNamespace(), pred.getLocalName());
}
writeNewLine();
}
use of org.eclipse.rdf4j.model.Literal in project rdf4j by eclipse.
the class QueryStringUtilTest method testSimpleLiteral.
@Test
public void testSimpleLiteral() {
Literal literal = VF.createLiteral("simple \"literal\"");
assertEquals("\"simple \\\"literal\\\"\"", QueryStringUtil.valueToString(literal));
assertEquals("\"simple \\\"literal\\\"\"", valueToStringWithStringBuilder(literal));
}
use of org.eclipse.rdf4j.model.Literal in project rdf4j by eclipse.
the class RenderUtils method toSPARQL.
/**
* Append the SPARQL query string rendering of the {@link org.eclipse.rdf4j.model.Value} to the supplied
* {@link StringBuilder}.
*
* @param value
* the value to render
* @param builder
* the {@link StringBuilder} to append to
* @return the original {@link StringBuilder} with the value appended.
*/
public static StringBuilder toSPARQL(Value value, StringBuilder builder) {
if (value instanceof IRI) {
IRI aURI = (IRI) value;
builder.append("<").append(aURI.toString()).append(">");
} else if (value instanceof BNode) {
builder.append("_:").append(((BNode) value).getID());
} else if (value instanceof Literal) {
Literal aLit = (Literal) value;
builder.append("\"\"\"").append(escape(aLit.getLabel())).append("\"\"\"");
if (Literals.isLanguageLiteral(aLit)) {
aLit.getLanguage().ifPresent(lang -> builder.append("@").append(lang));
} else {
builder.append("^^<").append(aLit.getDatatype().toString()).append(">");
}
}
return builder;
}
Aggregations