Search in sources :

Example 26 with RDFParseException

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

the class ReadRdfFileTest method test4.

@Test
public void test4() {
    // read the file 'example-data-artists.ttl' as an InputStream.
    InputStream is = null;
    try {
        is = new FileInputStream("test_unesco - Copie.rdf");
    } catch (FileNotFoundException ex) {
        Logger.getLogger(ReadRdfFileTest.class.getName()).log(Level.SEVERE, null, ex);
    }
    Model model = null;
    try {
        model = Rio.parse(is, "", RDFFormat.RDFXML);
    } catch (IOException ex) {
        Logger.getLogger(ReadRdfFileTest.class.getName()).log(Level.SEVERE, null, ex);
    } catch (RDFParseException ex) {
        Logger.getLogger(ReadRdfFileTest.class.getName()).log(Level.SEVERE, null, ex);
    } catch (UnsupportedRDFormatException ex) {
        Logger.getLogger(ReadRdfFileTest.class.getName()).log(Level.SEVERE, null, ex);
    }
    for (Statement statement : model) {
        System.out.println(statement);
    }
    System.out.println("\n----------------");
    for (Statement st : model) {
        // we want to see the object values of each statement
        Value value = st.getObject();
        IRI property = st.getPredicate();
        if (property.getLocalName().equals("type"))
            System.out.println("\n......\n");
        if (property.getLocalName().equals("notation")) {
            Literal title = (Literal) value;
            System.out.println("notation: " + title.getLabel());
        } else if (property.getLocalName().equals("lat")) {
            Literal title = (Literal) value;
            System.out.println("latitude : " + title.getLabel());
        } else if (property.getLocalName().equals("long")) {
            Literal title = (Literal) value;
            System.out.println("longitude : " + title.getLabel());
        } else if (property.getLocalName().equals("created")) {
            Literal title = (Literal) value;
            System.out.println("created : " + title.getLabel());
        } else if (property.getLocalName().equals("modified")) {
            Literal title = (Literal) value;
            System.out.println("modified : " + title.getLabel());
        } else if (value instanceof Literal) {
            String pref = "";
            if (property.getLocalName().equals("prefLabel"))
                pref = " // pref label";
            Literal title = (Literal) value;
            System.out.println("language: " + title.getLanguage().orElse("unknown"));
            System.out.println(" title: " + title.getLabel() + pref);
        } else {
            System.out.println("   ****  " + value + " //// " + property.getLocalName());
            if (property.getLocalName().equals("type")) {
                System.out.println("        URL: " + st.getSubject());
            }
        }
    }
    Rio.write(model, System.out, RDFFormat.RDFXML);
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) UnsupportedRDFormatException(org.eclipse.rdf4j.rio.UnsupportedRDFormatException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Statement(org.eclipse.rdf4j.model.Statement) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Literal(org.eclipse.rdf4j.model.Literal) Model(org.eclipse.rdf4j.model.Model) Value(org.eclipse.rdf4j.model.Value) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException) Test(org.junit.Test)

Example 27 with RDFParseException

use of org.eclipse.rdf4j.rio.RDFParseException in project graal by graphik-team.

the class Producer method run.

@Override
public void run() {
    org.eclipse.rdf4j.rio.RDFParser rdfParser = Rio.createParser(format);
    if (this.config != null) {
        rdfParser.setParserConfig(config);
    }
    rdfParser.setRDFHandler(new RDFListener(buffer));
    try {
        rdfParser.parse(this.reader, "");
    } catch (RDFParseException e) {
        throw new ParseError("An error occured while parsing", e);
    } catch (RDFHandlerException e) {
        throw new ParseError("An error occured while parsing", e);
    } catch (IOException e) {
        throw new ParseError("An error occured while parsing", e);
    }
    buffer.close();
    try {
        this.reader.close();
    } catch (IOException e) {
    }
}
Also used : RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) ParseError(fr.lirmm.graphik.graal.api.io.ParseError) IOException(java.io.IOException) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException)

Example 28 with RDFParseException

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

the class TestTurtleParser method testParseBooleanLiteralWhitespaceSemicolumn.

@Test
public void testParseBooleanLiteralWhitespaceSemicolumn() throws Exception {
    String data = "<urn:a> <urn:b> true ; <urn:c> false .";
    Reader r = new StringReader(data);
    try {
        parser.parse(r, baseURI);
        assertTrue(statementCollector.getStatements().size() == 2);
    } catch (RDFParseException e) {
        fail("parse error on correct data: " + e.getMessage());
    }
}
Also used : StringReader(java.io.StringReader) Reader(java.io.Reader) StringReader(java.io.StringReader) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException) Test(org.junit.Test)

Example 29 with RDFParseException

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

the class TestTurtleParser method testLineNumberReporting.

@Test
public void testLineNumberReporting() throws Exception {
    InputStream in = this.getClass().getResourceAsStream("/test-newlines.ttl");
    try {
        parser.parse(in, baseURI);
        fail("expected to fail parsing input file");
    } catch (RDFParseException e) {
        // expected
        assertFalse(errorCollector.getFatalErrors().isEmpty());
        final String error = errorCollector.getFatalErrors().get(0);
        // expected to fail at line 9.
        assertTrue(error.contains("(9,"));
        assertEquals(9, locationListener.getLineNo());
        assertEquals(-1, locationListener.getColumnNo());
    }
}
Also used : InputStream(java.io.InputStream) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException) Test(org.junit.Test)

Example 30 with RDFParseException

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

the class TestTurtleParser method testParseBooleanLiteralComma.

@Test
public void testParseBooleanLiteralComma() throws Exception {
    String data = "<urn:a> <urn:b> true, false .";
    Reader r = new StringReader(data);
    try {
        parser.parse(r, baseURI);
        assertTrue(statementCollector.getStatements().size() == 2);
    } catch (RDFParseException e) {
        fail("parse error on correct data: " + e.getMessage());
    }
}
Also used : StringReader(java.io.StringReader) Reader(java.io.Reader) StringReader(java.io.StringReader) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException) Test(org.junit.Test)

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