use of org.eclipse.rdf4j.model.Resource in project rdf4j by eclipse.
the class RDFXMLPrettyWriterBackgroundTest method sequenceItemsAreAbbreviated.
@Test
public void sequenceItemsAreAbbreviated() throws RDFHandlerException, IOException {
StringWriter writer = new StringWriter();
RDFWriter rdfWriter = rdfWriterFactory.getWriter(writer);
rdfWriter.startRDF();
Resource res = vf.createIRI("http://example.com/#");
rdfWriter.handleStatement(vf.createStatement(res, RDF.TYPE, RDF.BAG));
rdfWriter.handleStatement(vf.createStatement(res, vf.createIRI(RDF.NAMESPACE + "_1"), vf.createIRI("http://example.com/#1")));
rdfWriter.handleStatement(vf.createStatement(res, vf.createIRI(RDF.NAMESPACE + "_2"), vf.createIRI("http://example.com/#2")));
rdfWriter.endRDF();
List<String> rdfLines = rdfOpenTags(writer.toString());
assertEquals(Arrays.asList("<rdf:RDF", "<rdf:Bag", "<rdf:li", "<rdf:li"), rdfLines);
}
use of org.eclipse.rdf4j.model.Resource in project rdf4j by eclipse.
the class RDFXMLPrettyWriterBackgroundTest method inSequenceItemsMixedWithOtherElementsAreAbbreviated.
@Test
public void inSequenceItemsMixedWithOtherElementsAreAbbreviated() throws RDFHandlerException, IOException {
StringWriter writer = new StringWriter();
RDFWriter rdfWriter = rdfWriterFactory.getWriter(writer);
rdfWriter.startRDF();
Resource res = vf.createIRI("http://example.com/#");
rdfWriter.handleStatement(vf.createStatement(res, RDF.TYPE, RDF.BAG));
rdfWriter.handleStatement(vf.createStatement(res, vf.createIRI(RDF.NAMESPACE + "_2"), vf.createIRI("http://example.com/#2")));
rdfWriter.handleStatement(vf.createStatement(res, vf.createIRI(RDF.NAMESPACE + "_1"), vf.createIRI("http://example.com/#1")));
rdfWriter.handleStatement(vf.createStatement(res, vf.createIRI(RDF.NAMESPACE + "_3"), vf.createIRI("http://example.com/#3")));
rdfWriter.handleStatement(vf.createStatement(res, vf.createIRI(RDF.NAMESPACE + "_2"), vf.createIRI("http://example.com/#2")));
rdfWriter.endRDF();
List<String> rdfLines = rdfOpenTags(writer.toString());
assertEquals(Arrays.asList("<rdf:RDF", "<rdf:Bag", "<rdf:_2", "<rdf:li", "<rdf:_3", "<rdf:li"), rdfLines);
}
use of org.eclipse.rdf4j.model.Resource in project rdf4j by eclipse.
the class RDFJSONParser method rdfJsonToHandlerInternal.
private void rdfJsonToHandlerInternal(final RDFHandler handler, final ValueFactory vf, final JsonParser jp) throws IOException, JsonParseException, RDFParseException, RDFHandlerException {
if (jp.nextToken() != JsonToken.START_OBJECT) {
reportFatalError("Expected RDF/JSON document to start with an Object", jp.getCurrentLocation());
}
while (jp.nextToken() != JsonToken.END_OBJECT) {
final String subjStr = jp.getCurrentName();
Resource subject = null;
subject = subjStr.startsWith("_:") ? createNode(subjStr.substring(2)) : vf.createIRI(subjStr);
if (jp.nextToken() != JsonToken.START_OBJECT) {
reportFatalError("Expected subject value to start with an Object", jp.getCurrentLocation());
}
boolean foundPredicate = false;
while (jp.nextToken() != JsonToken.END_OBJECT) {
final String predStr = jp.getCurrentName();
final IRI predicate = vf.createIRI(predStr);
foundPredicate = true;
if (jp.nextToken() != JsonToken.START_ARRAY) {
reportFatalError("Expected predicate value to start with an array", jp.getCurrentLocation());
}
boolean foundObject = false;
while (jp.nextToken() != JsonToken.END_ARRAY) {
if (jp.getCurrentToken() != JsonToken.START_OBJECT) {
reportFatalError("Expected object value to start with an Object: subject=<" + subjStr + "> predicate=<" + predStr + ">", jp.getCurrentLocation());
}
String nextValue = null;
String nextType = null;
String nextDatatype = null;
String nextLanguage = null;
final Set<String> nextContexts = new HashSet<String>(2);
while (jp.nextToken() != JsonToken.END_OBJECT) {
final String fieldName = jp.getCurrentName();
if (RDFJSONUtility.VALUE.equals(fieldName)) {
if (nextValue != null) {
reportError("Multiple values found for a single object: subject=" + subjStr + " predicate=" + predStr, jp.getCurrentLocation(), RDFJSONParserSettings.FAIL_ON_MULTIPLE_OBJECT_VALUES);
}
jp.nextToken();
nextValue = jp.getText();
} else if (RDFJSONUtility.TYPE.equals(fieldName)) {
if (nextType != null) {
reportError("Multiple types found for a single object: subject=" + subjStr + " predicate=" + predStr, jp.getCurrentLocation(), RDFJSONParserSettings.FAIL_ON_MULTIPLE_OBJECT_TYPES);
}
jp.nextToken();
nextType = jp.getText();
} else if (RDFJSONUtility.LANG.equals(fieldName)) {
if (nextLanguage != null) {
reportError("Multiple languages found for a single object: subject=" + subjStr + " predicate=" + predStr, jp.getCurrentLocation(), RDFJSONParserSettings.FAIL_ON_MULTIPLE_OBJECT_LANGUAGES);
}
jp.nextToken();
nextLanguage = jp.getText();
} else if (RDFJSONUtility.DATATYPE.equals(fieldName)) {
if (nextDatatype != null) {
reportError("Multiple datatypes found for a single object: subject=" + subjStr + " predicate=" + predStr, jp.getCurrentLocation(), RDFJSONParserSettings.FAIL_ON_MULTIPLE_OBJECT_DATATYPES);
}
jp.nextToken();
nextDatatype = jp.getText();
} else if (RDFJSONUtility.GRAPHS.equals(fieldName)) {
if (jp.nextToken() != JsonToken.START_ARRAY) {
reportError("Expected graphs to start with an array", jp.getCurrentLocation(), RDFJSONParserSettings.SUPPORT_GRAPHS_EXTENSION);
}
while (jp.nextToken() != JsonToken.END_ARRAY) {
final String nextGraph = jp.getText();
nextContexts.add(nextGraph);
}
} else {
reportError("Unrecognised JSON field name for object: subject=" + subjStr + " predicate=" + predStr + " fieldname=" + fieldName, jp.getCurrentLocation(), RDFJSONParserSettings.FAIL_ON_UNKNOWN_PROPERTY);
}
}
Value object = null;
if (nextType == null) {
reportFatalError("No type for object: subject=" + subjStr + " predicate=" + predStr, jp.getCurrentLocation());
}
if (nextValue == null) {
reportFatalError("No value for object: subject=" + subjStr + " predicate=" + predStr, jp.getCurrentLocation());
}
if (RDFJSONUtility.LITERAL.equals(nextType)) {
if (nextLanguage != null) {
object = this.createLiteral(nextValue, nextLanguage, null, jp.getCurrentLocation());
} else if (nextDatatype != null) {
object = this.createLiteral(nextValue, null, this.createURI(nextDatatype), jp.getCurrentLocation());
} else {
object = this.createLiteral(nextValue, null, null, jp.getCurrentLocation());
}
} else if (RDFJSONUtility.BNODE.equals(nextType)) {
if (nextLanguage != null) {
reportFatalError("Language was attached to a blank node object: subject=" + subjStr + " predicate=" + predStr, jp.getCurrentLocation());
}
if (nextDatatype != null) {
reportFatalError("Datatype was attached to a blank node object: subject=" + subjStr + " predicate=" + predStr, jp.getCurrentLocation());
}
object = createNode(nextValue.substring(2));
} else if (RDFJSONUtility.URI.equals(nextType)) {
if (nextLanguage != null) {
reportFatalError("Language was attached to a uri object: subject=" + subjStr + " predicate=" + predStr, jp.getCurrentLocation());
}
if (nextDatatype != null) {
reportFatalError("Datatype was attached to a uri object: subject=" + subjStr + " predicate=" + predStr, jp.getCurrentLocation());
}
object = vf.createIRI(nextValue);
}
foundObject = true;
if (!nextContexts.isEmpty()) {
for (final String nextContext : nextContexts) {
final Resource context;
if (nextContext.equals(RDFJSONUtility.NULL)) {
context = null;
} else if (nextContext.startsWith("_:")) {
context = createNode(nextContext.substring(2));
} else {
context = vf.createIRI(nextContext);
}
Statement st = vf.createStatement(subject, predicate, object, context);
if (handler != null) {
handler.handleStatement(st);
}
}
} else {
Statement st = vf.createStatement(subject, predicate, object);
if (handler != null) {
handler.handleStatement(st);
}
}
}
if (!foundObject) {
reportFatalError("No object for predicate: subject=" + subjStr + " predicate=" + predStr, jp.getCurrentLocation());
}
}
if (!foundPredicate) {
reportFatalError("No predicate for object: subject=" + subjStr, jp.getCurrentLocation());
}
}
}
use of org.eclipse.rdf4j.model.Resource 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.Resource 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);
}
}
Aggregations