Search in sources :

Example 81 with Resource

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

the class TriGWriter method handleStatementInternal.

protected void handleStatementInternal(Statement st, boolean endRDFCalled, boolean canShortenSubject, boolean canShortenObject) {
    // them if they are sent here
    if (prettyPrintModel != null && !endRDFCalled) {
        prettyPrintModel.add(st);
        return;
    }
    try {
        Resource context = st.getContext();
        if (inActiveContext && !contextsEquals(context, currentContext)) {
            closePreviousStatement();
            closeActiveContext();
        }
        if (!inActiveContext) {
            writer.writeEOL();
            if (context != null) {
                boolean canShortenContext = false;
                if (context instanceof BNode) {
                    if (prettyPrintModel != null && !prettyPrintModel.contains(context, null, null) && !prettyPrintModel.contains(null, null, context)) {
                        canShortenContext = true;
                    }
                }
                writeResource(context, canShortenContext);
                writer.write(" ");
            }
            writer.write("{");
            writer.increaseIndentation();
            currentContext = context;
            inActiveContext = true;
        }
    } catch (IOException e) {
        throw new RDFHandlerException(e);
    }
    // If we get to this point, switch endRDFCalled to true so writing occurs
    super.handleStatementInternal(st, true, canShortenSubject, canShortenObject);
}
Also used : BNode(org.eclipse.rdf4j.model.BNode) RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) Resource(org.eclipse.rdf4j.model.Resource) IOException(java.io.IOException)

Example 82 with Resource

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

the class RDFXMLPrettyWriter method handleStatement.

@Override
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();
    try {
        if (!headerWritten) {
            writeHeader();
        }
        if (!nodeStack.isEmpty() && !subj.equals(nodeStack.peek().getValue())) {
            // Different subject than we had before, empty the stack
            // until we find it
            popStacks(subj);
        }
        if (nodeStack.isEmpty()) {
            // Push subject
            nodeStack.push(new Node(subj));
        }
        // Stack now contains at least one element
        Node topSubject = nodeStack.peek();
        // for the type URI
        if (pred.equals(RDF.TYPE) && obj instanceof IRI && !topSubject.hasType() && !topSubject.isWritten()) {
            // Use typed node element
            topSubject.setType((IRI) obj);
        } else {
            if (!nodeStack.isEmpty() && pred.equals(nodeStack.peek().nextLi())) {
                pred = RDF.LI;
                nodeStack.peek().incrementNextLi();
            }
            // Push predicate and object
            predicateStack.push(pred);
            nodeStack.push(new Node(obj));
        }
    } catch (IOException e) {
        throw new RDFHandlerException(e);
    }
}
Also used : ParsedIRI(org.eclipse.rdf4j.common.net.ParsedIRI) IRI(org.eclipse.rdf4j.model.IRI) RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) BNode(org.eclipse.rdf4j.model.BNode) Resource(org.eclipse.rdf4j.model.Resource) Value(org.eclipse.rdf4j.model.Value) IOException(java.io.IOException)

Example 83 with Resource

use of org.eclipse.rdf4j.model.Resource 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();
}
Also used : ParsedIRI(org.eclipse.rdf4j.common.net.ParsedIRI) 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 84 with Resource

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

the class TurtleParser method parseCollection.

/**
 * Parses a collection, e.g. <tt>( item1 item2 item3 )</tt>.
 */
protected Resource parseCollection() throws IOException, RDFParseException, RDFHandlerException {
    verifyCharacterOrFail(readCodePoint(), "(");
    int c = skipWSC();
    if (c == ')') {
        // Empty list
        readCodePoint();
        if (subject != null) {
            reportStatement(subject, predicate, RDF.NIL);
        }
        return RDF.NIL;
    } else {
        Resource listRoot = createNode();
        if (subject != null) {
            reportStatement(subject, predicate, listRoot);
        }
        // Remember current subject and predicate
        Resource oldSubject = subject;
        IRI oldPredicate = predicate;
        // generated bNode becomes subject, predicate becomes rdf:first
        subject = listRoot;
        predicate = RDF.FIRST;
        parseObject();
        Resource bNode = listRoot;
        while (skipWSC() != ')') {
            // Create another list node and link it to the previous
            Resource newNode = createNode();
            reportStatement(bNode, RDF.REST, newNode);
            // New node becomes the current
            subject = bNode = newNode;
            parseObject();
        }
        // Skip ')'
        readCodePoint();
        // Close the list
        reportStatement(bNode, RDF.REST, RDF.NIL);
        // Restore previous subject and predicate
        subject = oldSubject;
        predicate = oldPredicate;
        return listRoot;
    }
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) Resource(org.eclipse.rdf4j.model.Resource)

Example 85 with Resource

use of org.eclipse.rdf4j.model.Resource 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)

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