Search in sources :

Example 41 with Resource

use of org.eclipse.rdf4j.model.Resource in project rdf4j by eclipse.

the class RDFXMLParser method processNodeElt.

/*------------------------*
	 * RDF processing methods *
	 *------------------------*/
/* Process a node element (can be both subject and object) */
private void processNodeElt(String namespaceURI, String localName, String qName, Atts atts, boolean isEmptyElt) throws RDFParseException, RDFHandlerException {
    if (getParserConfig().get(XMLParserSettings.FAIL_ON_NON_STANDARD_ATTRIBUTES)) {
        // Check the element name
        checkNodeEltName(namespaceURI, localName, qName);
    }
    Resource nodeResource = getNodeResource(atts);
    NodeElement nodeElement = new NodeElement(nodeResource);
    if (!elementStack.isEmpty()) {
        // node can be object of a statement, or part of an rdf:List
        NodeElement subject = (NodeElement) peekStack(1);
        PropertyElement predicate = (PropertyElement) peekStack(0);
        if (predicate.parseCollection()) {
            Resource lastListRes = predicate.getLastListResource();
            Resource newListRes = createNode();
            if (lastListRes == null) {
                // first element in the list
                reportStatement(subject.getResource(), predicate.getURI(), newListRes);
                handleReification(newListRes);
            } else {
                // not the first element in the list
                reportStatement(lastListRes, RDF.REST, newListRes);
            }
            reportStatement(newListRes, RDF.FIRST, nodeResource);
            predicate.setLastListResource(newListRes);
        } else {
            reportStatement(subject.getResource(), predicate.getURI(), nodeResource);
            handleReification(nodeResource);
        }
    }
    if (!localName.equals("Description") || !namespaceURI.equals(RDF.NAMESPACE)) {
        // element name is uri's type
        IRI className = null;
        if ("".equals(namespaceURI)) {
            // No namespace, use base URI
            className = buildResourceFromLocalName(localName);
        } else {
            className = createURI(namespaceURI + localName);
        }
        reportStatement(nodeResource, RDF.TYPE, className);
    }
    Att type = atts.removeAtt(RDF.NAMESPACE, "type");
    if (type != null) {
        // rdf:type attribute, value is a URI-reference
        IRI className = resolveURI(type.getValue());
        reportStatement(nodeResource, RDF.TYPE, className);
    }
    if (getParserConfig().get(XMLParserSettings.FAIL_ON_NON_STANDARD_ATTRIBUTES)) {
        checkRDFAtts(atts);
    }
    processSubjectAtts(nodeElement, atts);
    if (!isEmptyElt) {
        elementStack.push(nodeElement);
    }
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) Resource(org.eclipse.rdf4j.model.Resource)

Example 42 with Resource

use of org.eclipse.rdf4j.model.Resource in project rdf4j by eclipse.

the class RDFJSONWriter method modelToRdfJsonInternal.

public static void modelToRdfJsonInternal(final Model graph, final WriterConfig writerConfig, final JsonGenerator jg) throws IOException, JsonGenerationException {
    if (writerConfig.get(BasicWriterSettings.PRETTY_PRINT)) {
        // SES-2011: Always use \n for consistency
        Indenter indenter = DefaultIndenter.SYSTEM_LINEFEED_INSTANCE;
        // By default Jackson does not pretty print, so enable this unless
        // PRETTY_PRINT setting is disabled
        DefaultPrettyPrinter pp = new DefaultPrettyPrinter().withArrayIndenter(indenter).withObjectIndenter(indenter);
        jg.setPrettyPrinter(pp);
    }
    jg.writeStartObject();
    for (final Resource nextSubject : graph.subjects()) {
        jg.writeObjectFieldStart(RDFJSONWriter.resourceToString(nextSubject));
        for (final IRI nextPredicate : graph.filter(nextSubject, null, null).predicates()) {
            jg.writeArrayFieldStart(nextPredicate.stringValue());
            for (final Value nextObject : graph.filter(nextSubject, nextPredicate, null).objects()) {
                // contexts are optional, so this may return empty in some
                // scenarios depending on the interpretation of the way contexts
                // work
                final Set<Resource> contexts = graph.filter(nextSubject, nextPredicate, nextObject).contexts();
                RDFJSONWriter.writeObject(nextObject, contexts, jg);
            }
            jg.writeEndArray();
        }
        jg.writeEndObject();
    }
    jg.writeEndObject();
}
Also used : DefaultPrettyPrinter(com.fasterxml.jackson.core.util.DefaultPrettyPrinter) IRI(org.eclipse.rdf4j.model.IRI) Indenter(com.fasterxml.jackson.core.util.DefaultPrettyPrinter.Indenter) DefaultIndenter(com.fasterxml.jackson.core.util.DefaultIndenter) Resource(org.eclipse.rdf4j.model.Resource) Value(org.eclipse.rdf4j.model.Value)

Example 43 with Resource

use of org.eclipse.rdf4j.model.Resource 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();
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) BNode(org.eclipse.rdf4j.model.BNode) Literal(org.eclipse.rdf4j.model.Literal) Resource(org.eclipse.rdf4j.model.Resource)

Example 44 with Resource

use of org.eclipse.rdf4j.model.Resource in project rdf4j by eclipse.

the class TriGParser method parseGraph.

protected void parseGraph() throws IOException, RDFParseException, RDFHandlerException {
    int c = readCodePoint();
    int c2 = peekCodePoint();
    Resource contextOrSubject = null;
    boolean foundContextOrSubject = false;
    if (c == '[') {
        skipWSC();
        c2 = readCodePoint();
        if (c2 == ']') {
            contextOrSubject = createNode();
            foundContextOrSubject = true;
            skipWSC();
        } else {
            unread(c2);
            unread(c);
        }
        c = readCodePoint();
    } else if (c == '<' || TurtleUtil.isPrefixStartChar(c) || (c == ':' && c2 != '-') || (c == '_' && c2 == ':')) {
        unread(c);
        Value value = parseValue();
        if (value instanceof Resource) {
            contextOrSubject = (Resource) value;
            foundContextOrSubject = true;
        } else {
            // NOTE: If a user parses Turtle using TriG, then the following
            // could actually be "Illegal subject name", but it should still
            // hold
            reportFatalError("Illegal graph name: " + value);
        }
        skipWSC();
        c = readCodePoint();
    } else {
        setContext(null);
    }
    if (c == '{') {
        setContext(contextOrSubject);
        c = skipWSC();
        if (c != '}') {
            parseTriples();
            c = skipWSC();
            while (c == '.') {
                readCodePoint();
                c = skipWSC();
                if (c == '}') {
                    break;
                }
                parseTriples();
                c = skipWSC();
            }
            verifyCharacterOrFail(c, "}");
        }
    } else {
        setContext(null);
        // parse from here to triples
        if (foundContextOrSubject) {
            subject = contextOrSubject;
            unread(c);
            parsePredicateObjectList();
        } else // Or if we didn't recognise anything, just parse as Turtle
        {
            unread(c);
            parseTriples();
        }
    }
    readCodePoint();
}
Also used : Resource(org.eclipse.rdf4j.model.Resource) Value(org.eclipse.rdf4j.model.Value)

Example 45 with Resource

use of org.eclipse.rdf4j.model.Resource in project rdf4j by eclipse.

the class JSONLDInternalRDFParser method handleStatement.

public void handleStatement(RDFDataset result, Statement nextStatement) {
    // TODO: from a basic look at the code it seems some of these could be
    // null
    // null values for IRIs will probably break things further down the line
    // and i'm not sure yet if this should be something handled later on, or
    // something that should be checked here
    final String subject = getResourceValue(nextStatement.getSubject());
    final String predicate = getResourceValue(nextStatement.getPredicate());
    final Value object = nextStatement.getObject();
    final String graphName = getResourceValue(nextStatement.getContext());
    if (object instanceof Literal) {
        final Literal literal = (Literal) object;
        final String value = literal.getLabel();
        String datatype = getResourceValue(literal.getDatatype());
        // rdf:langString
        if (literal.getLanguage().isPresent() && datatype == null) {
            datatype = RDF.LANGSTRING.stringValue();
        }
        // type xsd:String
        if (!literal.getLanguage().isPresent() && datatype == null) {
            datatype = XMLSchema.STRING.stringValue();
        }
        result.addQuad(subject, predicate, value, datatype, literal.getLanguage().orElse(null), graphName);
    } else {
        result.addQuad(subject, predicate, getResourceValue((Resource) object), graphName);
    }
}
Also used : Literal(org.eclipse.rdf4j.model.Literal) Value(org.eclipse.rdf4j.model.Value) Resource(org.eclipse.rdf4j.model.Resource)

Aggregations

Resource (org.eclipse.rdf4j.model.Resource)90 IRI (org.eclipse.rdf4j.model.IRI)37 Value (org.eclipse.rdf4j.model.Value)30 Test (org.junit.Test)16 Statement (org.eclipse.rdf4j.model.Statement)15 Model (org.eclipse.rdf4j.model.Model)12 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)12 BNode (org.eclipse.rdf4j.model.BNode)11 IOException (java.io.IOException)9 Literal (org.eclipse.rdf4j.model.Literal)9 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)7 StringWriter (java.io.StringWriter)6 ParsedIRI (org.eclipse.rdf4j.common.net.ParsedIRI)6 ValueFactory (org.eclipse.rdf4j.model.ValueFactory)6 SimpleValueFactory (org.eclipse.rdf4j.model.impl.SimpleValueFactory)6 TreeModel (org.eclipse.rdf4j.model.impl.TreeModel)6 RepositoryConnection (org.eclipse.rdf4j.repository.RepositoryConnection)6 RDFWriter (org.eclipse.rdf4j.rio.RDFWriter)6 LinkedHashModel (org.eclipse.rdf4j.model.impl.LinkedHashModel)5 ArrayList (java.util.ArrayList)4