Search in sources :

Example 46 with RDFHandlerException

use of org.eclipse.rdf4j.rio.RDFHandlerException 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 47 with RDFHandlerException

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

the class TriXParser method parse.

private void parse(InputSource inputStreamOrReader) throws IOException, RDFParseException, RDFHandlerException {
    clear();
    try {
        if (rdfHandler != null) {
            rdfHandler.startRDF();
        }
        XMLReader xmlReader;
        if (getParserConfig().isSet(XMLParserSettings.CUSTOM_XML_READER)) {
            xmlReader = getParserConfig().get(XMLParserSettings.CUSTOM_XML_READER);
        } else {
            xmlReader = XMLReaderFactory.createXMLReader();
        }
        xmlReader.setErrorHandler(this);
        saxParser = new SimpleSAXParser(xmlReader);
        saxParser.setPreserveWhitespace(true);
        saxParser.setListener(new TriXSAXHandler());
        saxParser.parse(inputStreamOrReader);
    } catch (SAXParseException e) {
        Exception wrappedExc = e.getException();
        if (wrappedExc == null) {
            reportFatalError(e, e.getLineNumber(), e.getColumnNumber());
        } else {
            reportFatalError(wrappedExc, e.getLineNumber(), e.getColumnNumber());
        }
    } catch (SAXException e) {
        Exception wrappedExc = e.getException();
        if (wrappedExc == null) {
            reportFatalError(e);
        } else if (wrappedExc instanceof RDFParseException) {
            throw (RDFParseException) wrappedExc;
        } else if (wrappedExc instanceof RDFHandlerException) {
            throw (RDFHandlerException) wrappedExc;
        } else {
            reportFatalError(wrappedExc);
        }
    } finally {
        clear();
    }
    if (rdfHandler != null) {
        rdfHandler.endRDF();
    }
}
Also used : SimpleSAXParser(org.eclipse.rdf4j.common.xml.SimpleSAXParser) RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) SAXParseException(org.xml.sax.SAXParseException) XMLReader(org.xml.sax.XMLReader) SAXNotSupportedException(org.xml.sax.SAXNotSupportedException) SAXNotRecognizedException(org.xml.sax.SAXNotRecognizedException) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException) RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException)

Example 48 with RDFHandlerException

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

the class TriXWriter method startRDF.

public void startRDF() throws RDFHandlerException {
    if (writingStarted) {
        throw new RDFHandlerException("Document writing has already started");
    }
    try {
        if (getWriterConfig().get(XMLWriterSettings.INCLUDE_XML_PI)) {
            xmlWriter.startDocument();
        }
        xmlWriter.setAttribute("xmlns", NAMESPACE);
        xmlWriter.startTag(ROOT_TAG);
    } catch (IOException e) {
        throw new RDFHandlerException(e);
    } finally {
        writingStarted = true;
    }
}
Also used : RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) IOException(java.io.IOException)

Example 49 with RDFHandlerException

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

the class BinaryRDFWriter method handleComment.

public void handleComment(String comment) throws RDFHandlerException {
    startRDF();
    try {
        out.writeByte(COMMENT);
        writeString(comment);
    } catch (IOException e) {
        throw new RDFHandlerException(e);
    }
}
Also used : RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) IOException(java.io.IOException)

Example 50 with RDFHandlerException

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

the class BinaryRDFWriter method startRDF.

public void startRDF() throws RDFHandlerException {
    if (!writingStarted) {
        writingStarted = true;
        try {
            out.write(MAGIC_NUMBER);
            out.writeInt(FORMAT_VERSION);
        } catch (IOException e) {
            throw new RDFHandlerException(e);
        }
    }
}
Also used : RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) 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