Search in sources :

Example 16 with RDFParseException

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

the class HTTPRepositoryConnection method removeModel.

private void removeModel(Model m) throws RepositoryException {
    RDFFormat format = RDFFormat.BINARY;
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        Rio.write(m, out, format);
        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
        client.removeData(in, null, format);
    } catch (RDFHandlerException e) {
        throw new RepositoryException("error while writing statement", e);
    } catch (RDFParseException e) {
        throw new RepositoryException(e);
    } catch (IOException e) {
        throw new RepositoryException(e);
    }
}
Also used : RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) ByteArrayInputStream(java.io.ByteArrayInputStream) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) RDFFormat(org.eclipse.rdf4j.rio.RDFFormat) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException)

Example 17 with RDFParseException

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

the class SAXFilter method characters.

@Override
public void characters(char[] ch, int start, int length) throws SAXException {
    try {
        if (inRDFContext) {
            // verify if we need to switch to XMLLiteral processing mode immediately.
            if (deferredElement != null && !parseLiteralMode) {
                Att parseType = deferredElement.atts.getAtt(RDF.NAMESPACE, "parseType");
                if (parseType != null && parseType.getValue().equals("Literal")) {
                    setParseLiteralMode();
                }
            }
            if (parseLiteralMode) {
                if (deferredElement != null) {
                    reportDeferredStartElement();
                }
                // Characters like '<', '>', and '&' must be escaped to
                // prevent breaking the XML text.
                String s = new String(ch, start, length);
                s = XMLUtil.escapeCharacterData(s);
                charBuf.append(s);
            } else {
                charBuf.append(ch, start, length);
                // we keep the start element deferred for now.
                if (deferredElement != null && charBuf.toString().trim().length() > 0) {
                    reportDeferredStartElement();
                }
            }
        }
    } catch (RDFParseException e) {
        throw new SAXException(e);
    } catch (RDFHandlerException e) {
        throw new SAXException(e);
    }
}
Also used : RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException) SAXException(org.xml.sax.SAXException)

Example 18 with RDFParseException

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

the class RDFXMLParserTest method testFatalErrorPrologContent.

@Test
public void testFatalErrorPrologContent() throws Exception {
    // Temporarily override System.err to verify that nothing is being
    // printed to it for this test
    PrintStream oldErr = System.err;
    ByteArrayOutputStream tempErr = new ByteArrayOutputStream();
    System.setErr(new PrintStream(tempErr));
    PrintStream oldOut = System.out;
    ByteArrayOutputStream tempOut = new ByteArrayOutputStream();
    System.setOut(new PrintStream(tempOut));
    try (final InputStream in = this.getClass().getResourceAsStream("/org/eclipse/rdf4j/rio/rdfxml/not-an-rdfxml-file.rdf")) {
        parser.parse(in, "");
    } catch (RDFParseException e) {
        assertEquals("Content is not allowed in prolog. [line 1, column 1]", e.getMessage());
    } finally {
        // Reset System Error output to ensure that we don't interfere with
        // other tests
        System.setErr(oldErr);
        // Reset System Out output to ensure that we don't interfere with
        // other tests
        System.setOut(oldOut);
    }
    // Verify nothing was printed to System.err during test
    assertEquals(0, tempErr.size());
    // Verify nothing was printed to System.out during test
    assertEquals(0, tempOut.size());
    assertEquals(0, el.getWarnings().size());
    assertEquals(0, el.getErrors().size());
    assertEquals(1, el.getFatalErrors().size());
    assertEquals("[Rio fatal] Content is not allowed in prolog. (1, 1)", el.getFatalErrors().get(0));
}
Also used : PrintStream(java.io.PrintStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException) Test(org.junit.Test)

Example 19 with RDFParseException

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

the class JSONLDParser method parse.

@Override
public void parse(final InputStream in, final String baseURI) throws IOException, RDFParseException, RDFHandlerException {
    clear();
    try {
        final JSONLDInternalTripleCallback callback = new JSONLDInternalTripleCallback(getRDFHandler(), valueFactory, getParserConfig(), getParseErrorListener(), nodeID -> createNode(nodeID), () -> createNode());
        final JsonLdOptions options = new JsonLdOptions(baseURI);
        options.useNamespaces = true;
        JsonLdProcessor.toRDF(JsonUtils.fromInputStream(in), callback, options);
    } catch (final JsonLdError e) {
        throw new RDFParseException("Could not parse JSONLD", e);
    } catch (final JsonParseException e) {
        throw new RDFParseException("Could not parse JSONLD", e);
    } catch (final RuntimeException e) {
        if (e.getCause() != null && e.getCause() instanceof RDFParseException) {
            throw (RDFParseException) e.getCause();
        }
        throw e;
    } finally {
        clear();
    }
}
Also used : JsonLdOptions(com.github.jsonldjava.core.JsonLdOptions) JsonParseException(com.fasterxml.jackson.core.JsonParseException) JsonLdError(com.github.jsonldjava.core.JsonLdError) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException)

Example 20 with RDFParseException

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

the class JSONLDParser method parse.

@Override
public void parse(final Reader reader, final String baseURI) throws IOException, RDFParseException, RDFHandlerException {
    clear();
    try {
        final JSONLDInternalTripleCallback callback = new JSONLDInternalTripleCallback(getRDFHandler(), valueFactory, getParserConfig(), getParseErrorListener(), nodeID -> createNode(nodeID), () -> createNode());
        final JsonLdOptions options = new JsonLdOptions(baseURI);
        options.useNamespaces = true;
        JsonLdProcessor.toRDF(JsonUtils.fromReader(reader), callback, options);
    } catch (final JsonLdError e) {
        throw new RDFParseException("Could not parse JSONLD", e);
    } catch (final JsonParseException e) {
        throw new RDFParseException("Could not parse JSONLD", e);
    } catch (final RuntimeException e) {
        if (e.getCause() != null && e.getCause() instanceof RDFParseException) {
            throw (RDFParseException) e.getCause();
        }
        throw e;
    } finally {
        clear();
    }
}
Also used : JsonLdOptions(com.github.jsonldjava.core.JsonLdOptions) JsonParseException(com.fasterxml.jackson.core.JsonParseException) JsonLdError(com.github.jsonldjava.core.JsonLdError) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException)

Aggregations

RDFParseException (org.eclipse.rdf4j.rio.RDFParseException)50 Test (org.junit.Test)22 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)19 IOException (java.io.IOException)16 StringReader (java.io.StringReader)13 StatementCollector (org.eclipse.rdf4j.rio.helpers.StatementCollector)9 RDFLoader (org.eclipse.rdf4j.repository.util.RDFLoader)8 Model (org.eclipse.rdf4j.model.Model)7 RDFParser (org.eclipse.rdf4j.rio.RDFParser)7 Statement (org.eclipse.rdf4j.model.Statement)6 LinkedHashModel (org.eclipse.rdf4j.model.impl.LinkedHashModel)6 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)5 RDFFormat (org.eclipse.rdf4j.rio.RDFFormat)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 InputStream (java.io.InputStream)4 Reader (java.io.Reader)4 RDFInserter (org.eclipse.rdf4j.repository.util.RDFInserter)4 SAXException (org.xml.sax.SAXException)4 IRI (org.eclipse.rdf4j.model.IRI)3 Literal (org.eclipse.rdf4j.model.Literal)3