1 package com.ontotext.ordi.mapper.console; 2 3 import java.io.File; 4 import java.io.FileFilter; 5 import java.io.FileWriter; 6 7 import org.openrdf.model.impl.StatementImpl; 8 import org.openrdf.rio.RDFWriter; 9 import org.openrdf.rio.turtle.TurtleWriter; 10 11 import com.ontotext.ordi.mapper.model.MapperDescriptor; 12 import com.ontotext.ordi.mapper.model.RDFResultSet; 13 import com.ontotext.ordi.mapper.processor.N3MapperDescriptorImpl; 14 import com.ontotext.ordi.mapper.processor.Processor; 15 16 public class CommandLine { 17 18 public static void main(String[] args) { 19 File input = null; 20 File output = null; 21 if (args.length == 2) { 22 input = new File(args[0]); 23 output = new File(args[1]); 24 } 25 if (input == null || input.exists() == false 26 || input.isDirectory() == false) { 27 System.out.println("Invalid descriptor_path!\n"); 28 printUsage(); 29 System.exit(1); 30 } 31 if (output.isFile() == true) { 32 System.out 33 .println("Invalid output_path! Specify a valid directory name!\n"); 34 printUsage(); 35 System.exit(1); 36 } 37 if (output.exists() == false) { 38 output.mkdir(); 39 } 40 try { 41 42 for (File descriptor : input.listFiles()) { 43 System.out.println(descriptor); 44 } 45 46 for (File descriptor : input.listFiles(new Filter())) { 47 long start = System.currentTimeMillis(); 48 System.out.println(String.format("Processing %s...", descriptor 49 .getAbsoluteFile())); 50 File result = new File(output + File.separator 51 + descriptor.getName().replaceAll(".n3", ".nt")); 52 if (result.exists()) { 53 System.out.println(String.format( 54 "Processed %s! (already completed)", descriptor 55 .getAbsoluteFile())); 56 continue; 57 } 58 MapperDescriptor mapper = new N3MapperDescriptorImpl(descriptor); 59 Processor processor = new Processor(mapper); 60 RDFResultSet rdfresult = processor.evaluate(null, null, null, 61 null); 62 RDFWriter writer = new TurtleWriter(new FileWriter(result)); 63 writer.startRDF(); 64 int counter = 0; 65 66 while (rdfresult.next()) { 67 writer.handleStatement(new StatementImpl(rdfresult 68 .getSubject(), rdfresult.getPredicate(), rdfresult 69 .getObject())); 70 counter++; 71 } 72 writer.endRDF(); 73 System.out.println(String.format("Processed %s! (%d ms) %d statemets", 74 descriptor.getAbsoluteFile(), System 75 .currentTimeMillis() 76 - start, counter)); 77 } 78 } catch (Exception e) { 79 e.printStackTrace(); 80 } 81 } 82 83 private static void printUsage() { 84 System.out.println("Converts RDBMS to RDF data model."); 85 System.out.println(); 86 System.out.println("rdbmsconverter descriptor_path output_path"); 87 System.out.println(); 88 System.out.println(" descriptor_path Specified a path to descriptors with n3"); 89 System.out.println(" extension to be converted to NTriple. Each"); 90 System.out.println(" descriptor generetase a new file with rdf in"); 91 System.out.println(" the current directory."); 92 System.out.println(" output_path Specified a path for the output files"); 93 System.out.println(); 94 System.out.println("Example usage:"); 95 System.out.println(); 96 System.out.println(" rdbmsconverter /myDescriptorDir/ -d /myDescriptorDir/output/"); 97 System.out.println(); 98 } 99 100 private static class Filter implements FileFilter { 101 public boolean accept(File file) { 102 return file.getAbsolutePath().endsWith(".n3") ? true : false; 103 } 104 } 105 }