use of org.eclipse.rdf4j.model.Statement 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.Statement in project rdf4j by eclipse.
the class RDFXMLParserTest method rdfXmlLoadedFromInsideAJarResolvesRelativeUris.
@Test
public void rdfXmlLoadedFromInsideAJarResolvesRelativeUris() throws Exception {
URL zipfileUrl = this.getClass().getResource("/org/eclipse/rdf4j/rio/rdfxml/sample-with-rdfxml-data.zip");
assertNotNull("The sample-data.zip file must be present for this test", zipfileUrl);
String url = "jar:" + zipfileUrl + "!/index.rdf";
try (final InputStream in = new URL(url).openStream()) {
parser.parse(in, url);
}
Collection<Statement> stmts = sc.getStatements();
assertThat(stmts, Matchers.<Statement>iterableWithSize(3));
Iterator<Statement> iter = stmts.iterator();
Statement stmt1 = iter.next();
Statement stmt2 = iter.next();
assertEquals(vf.createIRI("http://www.example.com/#"), stmt1.getSubject());
assertEquals(vf.createIRI("http://www.example.com/ns/#document-about"), stmt1.getPredicate());
assertTrue(stmt1.getObject() instanceof IRI);
IRI res = (IRI) stmt1.getObject();
String resourceUrl = res.stringValue();
assertThat(resourceUrl, CoreMatchers.startsWith("jar:" + zipfileUrl + "!"));
URL javaUrl = new URL(resourceUrl);
assertEquals("jar", javaUrl.getProtocol());
try (InputStream uc = javaUrl.openStream()) {
assertEquals("The resource stream should be empty", -1, uc.read());
}
assertEquals(res, stmt2.getSubject());
assertEquals(DC.TITLE, stmt2.getPredicate());
assertEquals(vf.createLiteral("Empty File"), stmt2.getObject());
}
use of org.eclipse.rdf4j.model.Statement in project rdf4j by eclipse.
the class ArrangedWriter method nextStatement.
private synchronized Statement nextStatement() {
if (stmtBySubject.isEmpty() && blanks.isEmpty()) {
assert queueSize == 0;
return null;
}
Set<Statement> stmts = null;
while (stmts == null) {
SubjectInContext last = stack.peekLast();
stmts = stmtBySubject.get(last);
if (stmts == null && last != null && blanks.contains(last.getSubject(), null, null, last.getContext())) {
stmts = queueBlankStatements(last);
} else if (stmts == null) {
stack.pollLast();
}
if (stack.isEmpty() && stmtBySubject.isEmpty()) {
Statement st = blanks.iterator().next();
stmts = queueBlankStatements(new SubjectInContext(st));
} else if (stack.isEmpty()) {
stmts = stmtBySubject.values().iterator().next();
}
}
Iterator<Statement> iter = stmts.iterator();
Statement next = iter.next();
queueSize--;
iter.remove();
SubjectInContext key = new SubjectInContext(next);
if (!key.equals(stack.peekLast())) {
stack.addLast(key);
}
if (!iter.hasNext()) {
stmtBySubject.remove(key);
}
Value obj = next.getObject();
if (obj instanceof BNode) {
// follow blank nodes before continuing with this subject
SubjectInContext bkey = new SubjectInContext((BNode) obj, next.getContext());
if (stack.contains(bkey)) {
// cycle detected
if (repeatBlankNodes) {
throw new RDFHandlerException("Blank node cycle detected. Try disabling " + BasicWriterSettings.INLINE_BLANK_NODES.getKey());
}
} else {
stack.addLast(bkey);
}
}
return next;
}
use of org.eclipse.rdf4j.model.Statement in project rdf4j by eclipse.
the class BinaryRDFParser method readStatement.
private void readStatement() throws RDFParseException, IOException, RDFHandlerException {
Value v = readValue();
Resource subj = null;
if (v instanceof Resource) {
subj = (Resource) v;
} else {
reportFatalError("Invalid subject type: " + v);
}
v = readValue();
IRI pred = null;
if (v instanceof IRI) {
pred = (IRI) v;
} else {
reportFatalError("Invalid predicate type: " + v);
}
Value obj = readValue();
if (obj == null) {
reportFatalError("Invalid object type: null");
}
v = readValue();
Resource context = null;
if (v == null || v instanceof Resource) {
context = (Resource) v;
} else {
reportFatalError("Invalid context type: " + v);
}
Statement st = createStatement(subj, pred, obj, context);
if (rdfHandler != null) {
rdfHandler.handleStatement(st);
}
}
use of org.eclipse.rdf4j.model.Statement in project rdf4j by eclipse.
the class BinaryRDFWriter method writeStatement.
/**
* Writes the first statement from the statement queue
*/
private void writeStatement() throws RDFHandlerException, IOException {
Statement st = statementQueue.remove();
int subjId = getValueId(st.getSubject());
int predId = getValueId(st.getPredicate());
int objId = getValueId(st.getObject());
int contextId = getValueId(st.getContext());
decValueFreq(st.getSubject());
decValueFreq(st.getPredicate());
decValueFreq(st.getObject());
decValueFreq(st.getContext());
out.writeByte(STATEMENT);
writeValueOrId(st.getSubject(), subjId);
writeValueOrId(st.getPredicate(), predId);
writeValueOrId(st.getObject(), objId);
writeValueOrId(st.getContext(), contextId);
}
Aggregations