diff options
author | zaaarf <zaaarf@proton.me> | 2023-08-27 01:05:26 +0200 |
---|---|---|
committer | zaaarf <zaaarf@proton.me> | 2023-08-27 01:05:26 +0200 |
commit | 5bd9f685b1df4e819dcab5ebe4bff0146bb90aa3 (patch) | |
tree | 9d7a2d6341b0a12af8535d0b8956c9696039cc2f /src/main/java |
feat: basic cli application
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/ftbsc/lll/mapper/writer/IWriter.java | 23 | ||||
-rw-r--r-- | src/main/java/ftbsc/lll/mapper/writer/MappingWriter.java | 80 |
2 files changed, 103 insertions, 0 deletions
diff --git a/src/main/java/ftbsc/lll/mapper/writer/IWriter.java b/src/main/java/ftbsc/lll/mapper/writer/IWriter.java new file mode 100644 index 0000000..13244a9 --- /dev/null +++ b/src/main/java/ftbsc/lll/mapper/writer/IWriter.java @@ -0,0 +1,23 @@ +package ftbsc.lll.mapper.writer; + +import ftbsc.lll.mapper.IMapper; + +import java.io.PrintWriter; + +/** + * The common interface for all mapping writers. + */ +public interface IWriter { + + /** + * @return a unique identifier for this writer + */ + String uniqueId(); + + /** + * Writes in a {@link PrintWriter} the contents of a {@link IMapper}. + * @param mapper the mapper + * @param writer the writer + */ + void write(IMapper mapper, PrintWriter writer); +} diff --git a/src/main/java/ftbsc/lll/mapper/writer/MappingWriter.java b/src/main/java/ftbsc/lll/mapper/writer/MappingWriter.java new file mode 100644 index 0000000..f5f7b10 --- /dev/null +++ b/src/main/java/ftbsc/lll/mapper/writer/MappingWriter.java @@ -0,0 +1,80 @@ +package ftbsc.lll.mapper.writer; + +import ftbsc.lll.mapper.IMapper; +import ftbsc.lll.mapper.MapperProvider; +import org.apache.commons.cli.*; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.ServiceLoader; + +/** + * Writes a mapping to a certain format. + */ +public class MappingWriter { + + /** + * The main function, must be passed exactly two arguments + * @param args the command line arguments + * @throws IOException if something goes wrong + */ + public static void main(String[] args) throws IOException, ParseException { + try { + Options options = new Options() + .addOption("r", "reverse", false, "Writes down inverted mappings"); + DefaultParser parser = new DefaultParser(); + CommandLine cmdLine = parser.parse(options, args); + args = cmdLine.getArgs(); + + if(args.length != 3) { + System.err.println("Bad argument count!"); + System.err.println("java -jar mapping-writer.jar [-r] <location> <format> <output>"); + return; + } + + //load the mapper + List<String> lines = MapperProvider.fetchFromLocalOrRemote(args[0]); + IMapper mapper = MapperProvider.getMapper(lines); + mapper.populate(lines, false); + if(cmdLine.hasOption("reverse")) + mapper = mapper.getInverted(); + + //load the writers + Map<String, IWriter> writerMap = new HashMap<>(); + for(IWriter w : ServiceLoader.load(IWriter.class)) + writerMap.put(w.uniqueId(), w); + + //get the one we need + IWriter writer = writerMap.get(args[1]); + if(writer == null) { + System.err.printf("%s was not recognised as a valid format!", args[1]); + return; + } + + //now for the file + File targetFile = new File(args[2]); + + if(!targetFile.createNewFile()) { + System.out.println("File already exists!"); + return; + } + + if(!targetFile.canWrite()) { + System.out.println("Failed to write to file: access denied."); + return; + } + + //call the writer + PrintWriter printWriter = new PrintWriter(new FileWriter(targetFile)); + writer.write(mapper, printWriter); + printWriter.close(); + } catch(ParseException ex) { + ex.printStackTrace(); + } + } +} |