Search in sources :

Example 51 with RDFHandlerException

use of org.eclipse.rdf4j.rio.RDFHandlerException in project rdf4j by eclipse.

the class BinaryRDFWriter method handleNamespace.

public void handleNamespace(String prefix, String uri) throws RDFHandlerException {
    startRDF();
    try {
        out.writeByte(NAMESPACE_DECL);
        writeString(prefix);
        writeString(uri);
    } catch (IOException e) {
        throw new RDFHandlerException(e);
    }
}
Also used : RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) IOException(java.io.IOException)

Example 52 with RDFHandlerException

use of org.eclipse.rdf4j.rio.RDFHandlerException in project rdf4j by eclipse.

the class BinaryRDFWriter method endRDF.

public void endRDF() throws RDFHandlerException {
    startRDF();
    try {
        while (!statementQueue.isEmpty()) {
            writeStatement();
        }
        out.writeByte(END_OF_DATA);
        out.flush();
        writingStarted = false;
    } catch (IOException e) {
        throw new RDFHandlerException(e);
    }
}
Also used : RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) IOException(java.io.IOException)

Example 53 with RDFHandlerException

use of org.eclipse.rdf4j.rio.RDFHandlerException in project rdf4j by eclipse.

the class TurtleWriter method handleNamespace.

@Override
public void handleNamespace(String prefix, String name) throws RDFHandlerException {
    try {
        if (!namespaceTable.containsKey(name)) {
            // Namespace not yet mapped to a prefix, try to give it the
            // specified prefix
            boolean isLegalPrefix = prefix.length() == 0 || TurtleUtil.isPN_PREFIX(prefix);
            if (!isLegalPrefix || namespaceTable.containsValue(prefix)) {
                if (prefix.length() == 0 || !isLegalPrefix) {
                    prefix = "ns";
                }
                int number = 1;
                while (namespaceTable.containsValue(prefix + number)) {
                    number++;
                }
                prefix += number;
            }
            namespaceTable.put(name, prefix);
            if (writingStarted) {
                closePreviousStatement();
                writeNamespace(prefix, name);
            }
        }
    } catch (IOException e) {
        throw new RDFHandlerException(e);
    }
}
Also used : RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) IOException(java.io.IOException)

Example 54 with RDFHandlerException

use of org.eclipse.rdf4j.rio.RDFHandlerException in project rdf4j by eclipse.

the class TurtleWriter method handleStatement.

@Override
public void handleStatement(Statement st) throws RDFHandlerException {
    if (!writingStarted) {
        throw new RuntimeException("Document writing has not yet been started");
    }
    try {
        Resource subj = st.getSubject();
        IRI pred = st.getPredicate();
        if (inlineBNodes && (pred.equals(RDF.FIRST) || pred.equals(RDF.REST))) {
            handleList(st);
        } else if (inlineBNodes && !subj.equals(lastWrittenSubject) && stack.contains(subj)) {
            handleInlineNode(st);
        } else {
            closeHangingResource();
            handleStatementInternal(st, false, inlineBNodes, inlineBNodes);
        }
    } catch (IOException e) {
        throw new RDFHandlerException(e);
    }
}
Also used : SimpleIRI(org.eclipse.rdf4j.model.impl.SimpleIRI) ParsedIRI(org.eclipse.rdf4j.common.net.ParsedIRI) IRI(org.eclipse.rdf4j.model.IRI) RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) Resource(org.eclipse.rdf4j.model.Resource) IOException(java.io.IOException)

Example 55 with RDFHandlerException

use of org.eclipse.rdf4j.rio.RDFHandlerException in project rdf4j by eclipse.

the class TurtleWriter method handleStatementInternal.

/**
 * Internal method that differentiates between the pretty-print and streaming writer cases.
 *
 * @param st
 *        The next statement to write
 * @param endRDFCalled
 *        True if endRDF has been called before this method is called. This is used to buffer statements
 *        for pretty-printing before dumping them when all statements have been delivered to us.
 * @param canShortenSubjectBNode
 *        True if, in the current context, we may be able to shorten the subject of this statement iff it
 *        is an instance of {@link BNode}.
 * @param canShortenObjectBNode
 *        True if, in the current context, we may be able to shorten the object of this statement iff it
 *        is an instance of {@link BNode}.
 */
protected void handleStatementInternal(Statement st, boolean endRDFCalled, boolean canShortenSubjectBNode, boolean canShortenObjectBNode) {
    Resource subj = st.getSubject();
    IRI pred = st.getPredicate();
    Value obj = st.getObject();
    try {
        if (subj.equals(lastWrittenSubject)) {
            if (pred.equals(lastWrittenPredicate)) {
                // Identical subject and predicate
                writer.write(",");
                wrapLine(prettyPrint);
            } else {
                // Identical subject, new predicate
                writer.write(";");
                writer.writeEOL();
                // Write new predicate
                writer.decreaseIndentation();
                writePredicate(pred);
                writer.increaseIndentation();
                wrapLine(true);
                path.removeLast();
                path.addLast(pred);
                lastWrittenPredicate = pred;
            }
        } else {
            // New subject
            closePreviousStatement();
            stack.addLast(subj);
            // Write new subject:
            if (prettyPrint) {
                writer.writeEOL();
            }
            writeResource(subj, canShortenSubjectBNode);
            wrapLine(true);
            writer.increaseIndentation();
            lastWrittenSubject = subj;
            // Write new predicate
            writePredicate(pred);
            wrapLine(true);
            path.addLast(pred);
            lastWrittenPredicate = pred;
            statementClosed = false;
            writer.increaseIndentation();
        }
        writeValue(obj, canShortenObjectBNode);
    // Don't close the line just yet. Maybe the next
    // statement has the same subject and/or predicate.
    } catch (IOException e) {
        throw new RDFHandlerException(e);
    }
}
Also used : SimpleIRI(org.eclipse.rdf4j.model.impl.SimpleIRI) ParsedIRI(org.eclipse.rdf4j.common.net.ParsedIRI) IRI(org.eclipse.rdf4j.model.IRI) RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) Resource(org.eclipse.rdf4j.model.Resource) Value(org.eclipse.rdf4j.model.Value) IOException(java.io.IOException)

Aggregations

RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)59 IOException (java.io.IOException)41 RDFParseException (org.eclipse.rdf4j.rio.RDFParseException)19 Resource (org.eclipse.rdf4j.model.Resource)11 Value (org.eclipse.rdf4j.model.Value)10 RDFLoader (org.eclipse.rdf4j.repository.util.RDFLoader)8 BNode (org.eclipse.rdf4j.model.BNode)7 IRI (org.eclipse.rdf4j.model.IRI)7 Literal (org.eclipse.rdf4j.model.Literal)7 Statement (org.eclipse.rdf4j.model.Statement)6 StatementCollector (org.eclipse.rdf4j.rio.helpers.StatementCollector)6 ParsedIRI (org.eclipse.rdf4j.common.net.ParsedIRI)4 TupleQueryResultHandlerException (org.eclipse.rdf4j.query.TupleQueryResultHandlerException)4 RDFInserter (org.eclipse.rdf4j.repository.util.RDFInserter)4 SAXException (org.xml.sax.SAXException)4 RDFFormat (org.eclipse.rdf4j.rio.RDFFormat)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2