Search code examples
javafilerdfconvertersturtle-rdf

File/ontology (turtle, N3, JSON, RDF-XML) etc to .ttl file conversions in java


I wanted to ask if there is a way in Java where I can read in, basically any file format (N3, JSON, RDF-XML) etc and then convert it into turtle(.ttl). I have searched on Google to get some idea, but they mainly just explain for specific file types and how a file type can be converted to RDF whereas I want it the other way.

EDIT (following the code example given in the answer):

if(FilePath.getText().equals("")){
FilePath.setText("Cannot be empty");

}else{

try {
    // get the inputFile from file chooser and setting a text field with
    // the path (FilePath is the variable name fo the textField in which the 
    // path to the selected file from file chooser is done earlier)
    FileInputStream fis = new FileInputStream(FilePath.getText());
    // guess the format of the input file (default set to RDF/XML)
    // when clicking on the error I get take to this line.
    RDFFormat inputFormat = Rio.getParserFormatForFileName(fis.toString()).orElse(RDFFormat.RDFXML);
    //create a parser for the input file and a writer for Turtle
    RDFParser rdfParser = Rio.createParser(inputFormat);
    RDFWriter rdfWriter = Rio.createWriter(RDFFormat.TURTLE, 
            new FileOutputStream("./" + fileName + ".ttl"));

    //link parser to the writer
    rdfParser.setRDFHandler(rdfWriter);
    //start the conversion
    InputStream inputStream = fis;
    rdfParser.parse(inputStream, fis.toString());

    //exception handling
} catch (FileNotFoundException ex) {
    Logger.getLogger(FileConverter.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
    Logger.getLogger(FileConverter.class.getName()).log(Level.SEVERE, null, ex);
 }
}

I have added "eclipse-rdf4j-3.0.3-onejar.jar" to the Libraries folder in NetBeans and now when I run the program I keep getting this error:

Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory at org.eclipse.rdf4j.common.lang.service.ServiceRegistry.(ServiceRegistry.java:31)

Any help or advice would be highly appreciated. Thank you.


Solution

  • Yes, this is possible. One option is to use Eclipse RDF4J for this purpose, or more specifically, its Rio parser/writer toolkit.

    Here's a code example using RDF4J Rio. It detects the syntax format of the input file based on the file extension, and directly writes the data to a new file, in Turtle syntax:

    // the input file
    java.net.URL url = new URL(“http://example.org/example.rdf”);
    
    // guess the format of the input file (default to RDF/XML)
    RDFFormat inputFormat = Rio.getParserFormatForFileName(url.toString()).orElse(RDFFormat.RDFXML);
    
    // create a parser for the input file and a writer for Turtle format
    RDFParser rdfParser = Rio.createParser(inputFormat);
    RDFWriter rdfWriter = Rio.createWriter(RDFFormat.TURTLE,
                   new FileOutputStream("/path/to/example-output.ttl"));
    
    // link the parser to the writer
    rdfParser.setRDFHandler(rdfWriter);
    
    // start the conversion
    try(InputStream inputStream = url.openStream()) {
       rdfParser.parse(inputStream, url.toString());
    }
    catch (IOException | RDFParseException | RDFHandlerException e) { ... }
    

    For more examples, see the RDF4J documentation.

    edit Regarding your NoClassDefFoundError: you're missing a necessary third party library on your classpath (in this particular case, a logging library).

    Instead of using the onejar, it's probably better to use Maven (or Gradle) to set up your project. See the development environment setup notes, or for a more step by step guide, see this tutorial (the tutorial uses Eclipse rather than Netbeans, but the points about how to set up your maven project will be very similar in Netbeans).

    If you really don't want to use Maven, what you can also do is just download the RDF4J SDK, which is a ZIP file. Unpack it, and just add all jar files in the lib/ directory to Netbeans.