Added RDFTranslator code
[smartapi.git] / Utilities / RDFTranslator / src / rdftranslator / CmdLineTranslator.java
1 import java.io.BufferedReader;
2 import java.io.BufferedWriter;
3 import java.io.ByteArrayInputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.InputStreamReader;
7 import java.io.OutputStreamWriter;
8 import java.io.StringWriter;
9 import java.nio.charset.StandardCharsets;
10
11 import org.apache.jena.rdf.model.Model;
12 import org.apache.jena.rdf.model.ModelFactory;
13
14 public class CmdLineTranslator
15 {
16         public static void main(String [] args)
17         {
18                 String inputFormat = "";
19                 String outputFormat = "";
20
21                 if (args.length > 1) {
22                         inputFormat = args[0];
23                         outputFormat = args[1];
24                         
25                         try
26                         {
27                                 BufferedReader cmdIn = new BufferedReader(new InputStreamReader(System.in));    
28                                 StringBuffer buffer = new StringBuffer();
29                                 String tmp; 
30                                 while ((tmp = cmdIn.readLine()) != null) {
31                                         buffer.append(tmp);
32                                 }
33                         
34                                 Model model = ModelFactory.createDefaultModel();
35                                 System.out.println("model created " + buffer.toString().getBytes(StandardCharsets.UTF_8));
36                                 // Read input RDF
37                                 try {
38                                         model.read(new ByteArrayInputStream(buffer.toString().getBytes(StandardCharsets.UTF_8)), null, inputFormat);
39                                         System.out.println("model read");
40                                 } catch(Exception e) {
41                                         System.out.println("Error while reading in (parsing) the RDF document: " + e);
42                                 }
43
44                                 // Write output RDF
45                                 try {
46                                         BufferedWriter cmdOut = new BufferedWriter(new OutputStreamWriter(System.out));
47                                         StringWriter writer = new StringWriter();
48
49                                         // write model to writer with given RDF serialization (e.g. TURTLE or JSON-LD)
50                                         model.write(writer, outputFormat);
51                                         
52                                         // and output it
53                                         cmdOut.write(writer.toString());
54                                         cmdOut.flush();
55
56                                 } catch(Exception e) {
57                                         System.out.println("Error while writing out (serializing) the RDF document: " + e);
58                                 }
59
60                         } catch(IOException e) {
61                                 e.printStackTrace();
62                         }
63                 }
64         }
65 }