Search in sources :

Example 1 with StatementCollector

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

the class ReadRdfFile_rdf4j method readFile.

// TODO add test methods here.
// The methods must be annotated with annotation @Test. For example:
// 
@Test
public void readFile() {
    File file = new File("/Users/Miled/Desktop/Bureau2/test_unesco.rdf");
    RDFParser rdfParser = Rio.createParser(RDFFormat.RDFXML);
    Model model = new LinkedHashModel();
    rdfParser.setRDFHandler(new StatementCollector(model));
    Rio.write(model, System.out, RDFFormat.TURTLE);
    try {
        rdfParser.parse(new FileReader(file), "http://example.org");
        /*       for (Statement statement : model) {
                
        //        writeLine(statement.getObject().stringValue(), statement.getSubject().stringValue());

                System.out.println("LocalName = " + statement.getPredicate().getLocalName());

                System.out.println("objet = " + statement.getObject().stringValue());
                System.out.println("predicat = " + statement.getPredicate());
                System.out.println("URI = " + statement.getSubject());
                System.out.println("");
               
           //     model.getNamespace(statement.getClass().getgetObject().stringValue());

            } */
        for (Statement statement2 : model) {
            Literal literal = (SimpleLiteral) statement2;
            System.out.println(literal.getLabel());
            // System.out.println(literal.getLanguage());
            System.out.println(literal.getDatatype());
        }
    } catch (IOException e) {
    // handle IO problems (e.g. the file could not be read)
    } catch (RDFParseException e) {
    // handle unrecoverable parse error
    } catch (RDFHandlerException e) {
    // handle a problem encountered by the RDFHandler
    }
}
Also used : RDFHandlerException(org.eclipse.rdf4j.rio.RDFHandlerException) StatementCollector(org.eclipse.rdf4j.rio.helpers.StatementCollector) Statement(org.eclipse.rdf4j.model.Statement) Literal(org.eclipse.rdf4j.model.Literal) SimpleLiteral(org.eclipse.rdf4j.model.impl.SimpleLiteral) LinkedHashModel(org.eclipse.rdf4j.model.impl.LinkedHashModel) Model(org.eclipse.rdf4j.model.Model) FileReader(java.io.FileReader) SimpleLiteral(org.eclipse.rdf4j.model.impl.SimpleLiteral) LinkedHashModel(org.eclipse.rdf4j.model.impl.LinkedHashModel) IOException(java.io.IOException) RDFParser(org.eclipse.rdf4j.rio.RDFParser) File(java.io.File) RDFParseException(org.eclipse.rdf4j.rio.RDFParseException) Test(org.junit.Test)

Aggregations

File (java.io.File)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 Literal (org.eclipse.rdf4j.model.Literal)1 Model (org.eclipse.rdf4j.model.Model)1 Statement (org.eclipse.rdf4j.model.Statement)1 LinkedHashModel (org.eclipse.rdf4j.model.impl.LinkedHashModel)1 SimpleLiteral (org.eclipse.rdf4j.model.impl.SimpleLiteral)1 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)1 RDFParseException (org.eclipse.rdf4j.rio.RDFParseException)1 RDFParser (org.eclipse.rdf4j.rio.RDFParser)1 StatementCollector (org.eclipse.rdf4j.rio.helpers.StatementCollector)1 Test (org.junit.Test)1