Main.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package r2rml;
  2. import java.io.BufferedWriter;
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.nio.charset.StandardCharsets;
  7. import java.nio.file.Files;
  8. import java.nio.file.Path;
  9. import java.nio.file.Paths;
  10. import java.nio.file.StandardOpenOption;
  11. import java.util.Iterator;
  12. import org.apache.jena.iri.IRIFactory;
  13. import org.apache.jena.query.Dataset;
  14. import org.apache.jena.rdf.model.Model;
  15. import org.apache.jena.riot.Lang;
  16. import org.apache.jena.riot.RDFDataMgr;
  17. import r2rml.engine.Configuration;
  18. import r2rml.engine.R2RMLException;
  19. import r2rml.engine.R2RMLProcessor;
  20. /**
  21. * Main Class.
  22. *
  23. * @author Christophe Debruyne
  24. * @version 0.1
  25. *
  26. */
  27. public class Main {
  28. public static void main(String[] args) {
  29. try {
  30. if(args.length != 1) {
  31. throw new R2RMLException("Only and exactly one config file needs to be passed as an argument", null);
  32. }
  33. Configuration configuration = new Configuration(args[0]);
  34. if(configuration.getConnectionURL() == null) {
  35. throw new R2RMLException("A connection URL is mandatory.", null);
  36. }
  37. if(configuration.getMappingFile() == null) {
  38. throw new R2RMLException("A R2RML mapping file is mandatory.", null);
  39. }
  40. R2RMLProcessor engine = new R2RMLProcessor(configuration);
  41. engine.execute();
  42. String format = configuration.getFormat();
  43. if(format.equals("NQUADS") || format.equals("TRIG")) {
  44. System.out.println("Writing dataset to dataset format. Ignoring irrelevant parameters.");
  45. writeDatasetAsDatasetFile(configuration, engine);
  46. } else if(configuration.isFilePerGraph()) {
  47. System.out.println("Writing dataset as separate files. Ignoring irrelevant parameters.");
  48. writeDatasetAsFiles(configuration, engine);
  49. } else {
  50. System.out.println("Writing dataset to one RDf file. Ignoring irrelevant parameters.");
  51. writeDatasetAsFile(configuration, engine);
  52. }
  53. } catch (Exception e) {
  54. System.err.println(e.getMessage());
  55. System.exit(-1);
  56. }
  57. }
  58. private static void writeDatasetAsFiles(
  59. Configuration configuration,
  60. R2RMLProcessor engine)
  61. throws R2RMLException {
  62. // Create RDF files out of RDF dataset (losing graphs)
  63. try {
  64. File o = new File(configuration.getOutputFile());
  65. if(!o.exists()) o.mkdirs();
  66. Dataset ds = engine.getDataset();
  67. String ext = getExtensionForFormat(configuration.getFormat());
  68. File file = new File(o, "default" + ext);
  69. writeModelToFile(ds.getDefaultModel(), file, configuration.getFormat());
  70. Iterator<String> graphs = ds.listNames();
  71. while(graphs.hasNext()) {
  72. String graph = graphs.next();
  73. String name = IRIFactory.iriImplementation().construct(graph).toASCIIString();
  74. name = name.replace("://", "_").replace("/", "_").replace(".", "_");
  75. file = new File(o, name + ext);
  76. writeModelToFile(ds.getNamedModel(graph), file, configuration.getFormat());
  77. }
  78. } catch (Exception e) {
  79. throw new R2RMLException(e.getMessage(), e);
  80. }
  81. }
  82. private static void writeModelToFile(
  83. Model model,
  84. File file,
  85. String format)
  86. throws IOException {
  87. writeModelToFile(model, file, format, false);
  88. }
  89. private static void writeModelToFile(
  90. Model model,
  91. File file,
  92. String format,
  93. boolean append)
  94. throws IOException {
  95. // Make sure that we write output as UTF-8 as windows machines
  96. // use a different encoding for the writers...
  97. Path path = Paths.get(file.getPath());
  98. if(!file.exists()) file.createNewFile();
  99. BufferedWriter bw = Files.newBufferedWriter(path, StandardCharsets.UTF_8, append ? StandardOpenOption.APPEND : StandardOpenOption.TRUNCATE_EXISTING);
  100. model.write(bw, format);
  101. bw.close();
  102. }
  103. private static void writeDatasetAsFile(
  104. Configuration configuration,
  105. R2RMLProcessor engine)
  106. throws R2RMLException {
  107. // Create RDF file out of RDF dataset (losing graphs)
  108. try {
  109. File o = new File(configuration.getOutputFile());
  110. if(o.exists())
  111. o.delete();
  112. o.createNewFile(); // make sure that it exists for some of the APIs.
  113. Dataset ds = engine.getDataset();
  114. writeModelToFile(ds.getDefaultModel(), o, configuration.getFormat());
  115. Iterator<String> graphs = engine.getDataset().listNames();
  116. while(graphs.hasNext()) {
  117. String graph = graphs.next();
  118. writeModelToFile(ds.getNamedModel(graph), o, configuration.getFormat(), true);
  119. }
  120. } catch (Exception e) {
  121. throw new R2RMLException(e.getMessage(), e);
  122. }
  123. }
  124. private static void writeDatasetAsDatasetFile(
  125. Configuration configuration,
  126. R2RMLProcessor engine)
  127. throws R2RMLException {
  128. try {
  129. File o = new File(configuration.getOutputFile());
  130. if(o.exists())
  131. o.delete();
  132. o.createNewFile(); // Make sure that file exists for APIs.
  133. FileOutputStream out = new FileOutputStream(o);
  134. Lang lang = configuration.getFormat().equals("NQUADS") ? Lang.NQ : Lang.TRIG;
  135. RDFDataMgr.write(out, engine.getDataset(), lang);
  136. out.close();
  137. } catch (Exception e) {
  138. throw new R2RMLException(e.getMessage(), e);
  139. }
  140. }
  141. /**
  142. * Simple method to find the corresponding extension for a format in case graphs
  143. * are written to different tiles
  144. *
  145. * @param format
  146. * @return
  147. */
  148. private static String getExtensionForFormat(String format) {
  149. if(equals(format, new String[]{"turtle", "ttl"}))
  150. return ".ttl";
  151. if(equals(format, new String[]{"n-triples", "n-triple", "nt", "ntriples"}))
  152. return ".nt";
  153. if(equals(format, new String[]{"rdf", "rdf/xml", "rdf/xml-abbrev"}))
  154. return ".rdf";
  155. if(equals(format, new String[]{"n3"}))
  156. return ".n3";
  157. if(equals(format, new String[]{"rdf/json"}))
  158. return ".rj";
  159. if(equals(format, new String[]{"jsonld"}))
  160. return ".jsonld";
  161. return ".unknown";
  162. }
  163. private static boolean equals(String format, String[] strings) {
  164. String t = format == null ? "" : format.toLowerCase();
  165. for(String s : strings) {
  166. if(s.equals(t)) return true;
  167. }
  168. return false;
  169. }
  170. }