Search in sources :

Example 66 with Statement

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());
        }
    }
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) Statement(org.eclipse.rdf4j.model.Statement) Resource(org.eclipse.rdf4j.model.Resource) Value(org.eclipse.rdf4j.model.Value) HashSet(java.util.HashSet)

Example 67 with Statement

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());
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) InputStream(java.io.InputStream) Statement(org.eclipse.rdf4j.model.Statement) URL(java.net.URL) Test(org.junit.Test)

Example 68 with Statement

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;
}
Also used : BNode(org.eclipse.rdf4j.model.BNode) RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) Statement(org.eclipse.rdf4j.model.Statement) Value(org.eclipse.rdf4j.model.Value)

Example 69 with Statement

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);
    }
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) Statement(org.eclipse.rdf4j.model.Statement) Value(org.eclipse.rdf4j.model.Value) Resource(org.eclipse.rdf4j.model.Resource)

Example 70 with Statement

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);
}
Also used : Statement(org.eclipse.rdf4j.model.Statement)

Aggregations

Statement (org.eclipse.rdf4j.model.Statement)74 IRI (org.eclipse.rdf4j.model.IRI)17 Test (org.junit.Test)17 Model (org.eclipse.rdf4j.model.Model)16 Literal (org.eclipse.rdf4j.model.Literal)15 Resource (org.eclipse.rdf4j.model.Resource)15 Value (org.eclipse.rdf4j.model.Value)13 RDFWriter (org.eclipse.rdf4j.rio.RDFWriter)11 ByteArrayInputStream (java.io.ByteArrayInputStream)10 ArrayList (java.util.ArrayList)10 LinkedHashModel (org.eclipse.rdf4j.model.impl.LinkedHashModel)8 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)8 RDFParseException (org.eclipse.rdf4j.rio.RDFParseException)8 StringWriter (java.io.StringWriter)7 BNode (org.eclipse.rdf4j.model.BNode)6 ValueFactory (org.eclipse.rdf4j.model.ValueFactory)6 SimpleValueFactory (org.eclipse.rdf4j.model.impl.SimpleValueFactory)6 RepositoryConnection (org.eclipse.rdf4j.repository.RepositoryConnection)6 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)6 IOException (java.io.IOException)5