From 7b937cc754aa96f314a3ac4b725267fedba08d23 Mon Sep 17 00:00:00 2001 From: VladimirEliTokarev Date: Sat, 12 Nov 2016 17:05:53 +0200 Subject: [PATCH] Added the creator of the jar to compile and create jar The jar converts json configuration into normal --- .../main/java/meerkat/voting/gui/Main.java | 9 ++-- .../voting/gui/configuration/Converter.java | 44 ++++++++++++++++--- .../VotingBoothConfigurationCreator.java | 2 +- 3 files changed, 42 insertions(+), 13 deletions(-) diff --git a/voting-booth-gui/src/main/java/meerkat/voting/gui/Main.java b/voting-booth-gui/src/main/java/meerkat/voting/gui/Main.java index d955cc3..0cab1fd 100644 --- a/voting-booth-gui/src/main/java/meerkat/voting/gui/Main.java +++ b/voting-booth-gui/src/main/java/meerkat/voting/gui/Main.java @@ -13,7 +13,6 @@ import java.io.FileInputStream; import java.io.IOException; import java.net.URISyntaxException; - /** * Created by Vladimir Eliezer Tokarev on 8/27/2016. * The current voting-booth intialization process is next: @@ -22,7 +21,7 @@ import java.net.URISyntaxException; public class Main extends Application { private final Logger logger = LoggerFactory.getLogger(Main.class); - private final String CONFIGURATION_PATH = "/configuration/configuration.bin"; + private final String DEFAULT_CONFIGURATION_PATH = "/configuration/configuration.bin"; /** * Creates the VotingBoothConfigurationCreator and creates configuration based on given path * @return VotingBoothVonfiguration parsed from json file @@ -30,9 +29,7 @@ public class Main extends Application { * @throws JSONException will be thrown if the json is unvalid */ private VotingBoothConfiguration GetConfig() throws IOException, JSONException, URISyntaxException { - - - FileInputStream fis = new FileInputStream(getClass().getResource(CONFIGURATION_PATH).getPath()); + FileInputStream fis = new FileInputStream(getClass().getResource(DEFAULT_CONFIGURATION_PATH).getPath()); BallotQuestionUIElementOuterClass.BallotQuestionsUIElements read_questions = BallotQuestionUIElementOuterClass.BallotQuestionsUIElements.parseFrom(fis); VotingBoothConfiguration votingBoothConfiguration = new VotingBoothConfiguration(); votingBoothConfiguration.NameSelectionQuestion = read_questions.getQuestions(1); @@ -43,7 +40,7 @@ public class Main extends Application { @Override public void start(Stage primaryStage) throws Exception { - this.logger.debug("Running the app."); + this.logger.debug("Started Meerkat Voting GUI Application"); ChainBuilder.Build(primaryStage, GetConfig()); primaryStage.setTitle("Meerkat Polling Station"); primaryStage.show(); diff --git a/voting-booth-gui/src/main/java/meerkat/voting/gui/configuration/Converter.java b/voting-booth-gui/src/main/java/meerkat/voting/gui/configuration/Converter.java index 6913b65..b802e80 100644 --- a/voting-booth-gui/src/main/java/meerkat/voting/gui/configuration/Converter.java +++ b/voting-booth-gui/src/main/java/meerkat/voting/gui/configuration/Converter.java @@ -1,5 +1,7 @@ package meerkat.voting.gui.configuration; +import joptsimple.OptionParser; +import joptsimple.OptionSet; import meerkat.protobuf.BallotQuestionUIElementOuterClass; import org.json.JSONException; @@ -11,7 +13,13 @@ import java.io.IOException; * This class uses the configuration creator which converts json configuration into normal binary proto file */ public class Converter { - private final static String OUTPUTNAME = "configuration.bin"; + private final static String DEFAULT_OUTPUT_PATH = "configuration.bin"; + private final static String DEFAULT_CONFIGURATION_PATH = "VotingBoothConfiguration.json"; + private final static String CONVERTER_DESCRIPTION = "Welcome to simple voting booth " + + "configuration converter (which converts json voting booth configuration into " + + "binary configuration).\n-p (path) stands for the path to the json file configuration " + + "which we want to convert into binary file.\n-o (output) stands for the path to which we " + + "want to write the output binary file.\n-?,/?,help (help) stands for this description.\n"; /** * Reads the json configuration from given path @@ -27,9 +35,10 @@ public class Converter { /** * Converts the configuration file into single binary proto file and writes it to the disc * @param configuration the configuration object + * @param outputPath the path to the new configuraiton file */ - private static void ConvertJsonIntoBinaryFile(VotingBoothConfiguration configuration) throws IOException { - FileOutputStream fileOutputStream = new FileOutputStream(OUTPUTNAME); + private static void ConvertJsonIntoBinaryFile(VotingBoothConfiguration configuration, String outputPath) throws IOException { + FileOutputStream fileOutputStream = new FileOutputStream(outputPath); BallotQuestionUIElementOuterClass.BallotQuestionsUIElements read_questions = BallotQuestionUIElementOuterClass .BallotQuestionsUIElements @@ -42,12 +51,35 @@ public class Converter { } public static void main(String[] args) throws InterruptedException { - System.out.println("args are: " + args); try { - ConvertJsonIntoBinaryFile(GetVotingBoothJsonConfiguration(args[0])); + OptionParser parser = new OptionParser( "p:o:?*.::" ); + String configurationPath = ""; + String outputPath = ""; + OptionSet options = parser.parse(args); + + if (options.has("?") || options.has("help")){ + System.out.println(CONVERTER_DESCRIPTION); + } + if (!options.has("p")){ + configurationPath = DEFAULT_CONFIGURATION_PATH; + } + if (options.has("p")){ + configurationPath = (String) options.valueOf("p"); + System.out.println("Configuraiton read from " + configurationPath + ".\n"); + } + if (!options.has("o")){ + outputPath = DEFAULT_OUTPUT_PATH; + } + if (options.has("o")){ + outputPath = (String) options.valueOf("o"); + } + + VotingBoothConfiguration config = GetVotingBoothJsonConfiguration(configurationPath); + System.out.print("yeyyy"); + ConvertJsonIntoBinaryFile(config, outputPath); } catch (Exception e) { - System.out.println("Exception thrown: " + e); + System.out.println("Exception thrown: " + e + ", " + e.getMessage()); } } diff --git a/voting-booth-gui/src/main/java/meerkat/voting/gui/configuration/VotingBoothConfigurationCreator.java b/voting-booth-gui/src/main/java/meerkat/voting/gui/configuration/VotingBoothConfigurationCreator.java index 2fd1ce0..6950cbb 100644 --- a/voting-booth-gui/src/main/java/meerkat/voting/gui/configuration/VotingBoothConfigurationCreator.java +++ b/voting-booth-gui/src/main/java/meerkat/voting/gui/configuration/VotingBoothConfigurationCreator.java @@ -70,7 +70,7 @@ public class VotingBoothConfigurationCreator { * @throws JSONException will be thrown if the parsing will fail */ private JSONArray getJsonFromPath(String path) throws FileNotFoundException, JSONException { - String content = new Scanner(new File(getClass().getResource(path).getPath())).useDelimiter("\\Z").next(); + String content = new Scanner(new File(path)).next(); JSONObject jsonObject = new JSONObject(content); this.logger.debug("Created json object from from configuration path."); return (JSONArray)jsonObject.get("questions");