From 6c2043698d37774f77d5ae1a6c067b72c2a3ea14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joel=20H=C3=A5kansson?= Date: Fri, 23 Feb 2018 10:53:44 +0100 Subject: [PATCH] Ignores invalid startup arguments (fixes #58) --- src/application/MainFx.java | 17 +++++++-- src/application/l10n/Messages.java | 1 + src/application/l10n/messages.properties | 1 + src/application/l10n/messages_no.properties | 1 + src/application/l10n/messages_sv.properties | 1 + src/com/googlecode/e2u/Start.java | 13 ++----- src/com/googlecode/e2u/StartupDetails.java | 40 ++++++++------------- 7 files changed, 37 insertions(+), 37 deletions(-) diff --git a/src/application/MainFx.java b/src/application/MainFx.java index 12f3fd2..2495e20 100644 --- a/src/application/MainFx.java +++ b/src/application/MainFx.java @@ -7,6 +7,7 @@ import java.net.URLClassLoader; import java.util.ArrayList; import java.util.List; +import java.util.Optional; import java.util.logging.Level; import java.util.logging.Logger; @@ -16,11 +17,15 @@ import application.l10n.Messages; import javafx.application.Application; +import javafx.application.Platform; import javafx.event.EventHandler; import javafx.fxml.FXMLLoader; import javafx.geometry.Rectangle2D; import javafx.scene.Parent; import javafx.scene.Scene; +import javafx.scene.control.Alert; +import javafx.scene.control.Alert.AlertType; +import javafx.scene.control.ButtonType; import javafx.scene.image.Image; import javafx.stage.Screen; import javafx.stage.Stage; @@ -83,11 +88,19 @@ public void start(Stage primaryStage) throws IOException { Scene scene = new Scene(root); MainController controller = fxmlLoader.getController(); - controller.openArgs(StartupDetails.parse(getParameters().getRaw().toArray(new String[]{}))); + Optional args = StartupDetails.parse(getParameters().getRaw().toArray(new String[]{})); + args.ifPresent(a->controller.openArgs(a)); primaryStage.setScene(scene); primaryStage.show(); - + + if (!args.isPresent()) { + Platform.runLater(()->{ + Alert alert = new Alert(AlertType.ERROR, Messages.ERROR_INVALID_ARGUMENTS.localize(getParameters().getRaw()), ButtonType.OK); + alert.showAndWait(); + }); + } + scene.getWindow().setOnCloseRequest(new EventHandler() { public void handle(WindowEvent ev) { if (!controller.confirmShutdown()) { diff --git a/src/application/l10n/Messages.java b/src/application/l10n/Messages.java index 677d63a..a7301f0 100644 --- a/src/application/l10n/Messages.java +++ b/src/application/l10n/Messages.java @@ -73,6 +73,7 @@ public enum Messages { ERROR_FAILED_TO_LOAD_HELP("error-failed-to-load-help"), ERROR_UNSUPPORTED_XML_ENCODING("error-unsupported-xml-encoding"), ERROR_FAILED_TO_WRITE_TO_FILE("error-failed-to-write-to-file"), + ERROR_INVALID_ARGUMENTS("error-invalid-arguments"), TITLE_SET_SEARCH_FOLDER("title-set-search-folder"), TITLE_OPEN_DIALOG("title-open-dialog"), TITLE_IMPORT_SOURCE_DOCUMENT_DIALOG("title-import-source-document-dialog"), diff --git a/src/application/l10n/messages.properties b/src/application/l10n/messages.properties index 09c80b6..e90ad7a 100644 --- a/src/application/l10n/messages.properties +++ b/src/application/l10n/messages.properties @@ -26,6 +26,7 @@ error-no-alignment-specified=No alignment specified error-failed-to-load-help=Failed to load help contents error-unsupported-xml-encoding=Unsupported XML encoding: {0} error-failed-to-write-to-file=Failed to write to file: {0} +error-invalid-arguments=Invalid arguments: {0} checkbox-watch-source=Watch source title-set-search-folder=Set search folder title-open-dialog=Open file diff --git a/src/application/l10n/messages_no.properties b/src/application/l10n/messages_no.properties index e932608..db89b2e 100644 --- a/src/application/l10n/messages_no.properties +++ b/src/application/l10n/messages_no.properties @@ -26,6 +26,7 @@ error-no-alignment-specified=Ingen sidejustering angitt error-failed-to-load-help=Klarte ikke å laste hjelpetekst error-unsupported-xml-encoding=Unsupported XML encoding: {0} error-failed-to-write-to-file=Failed to write to file: {0} +error-invalid-arguments=Invalid arguments: {0} checkbox-watch-source=Overvåk kilde title-set-search-folder=Velg søkemappe title-open-dialog=Open file diff --git a/src/application/l10n/messages_sv.properties b/src/application/l10n/messages_sv.properties index 2f9063f..8efb9c6 100644 --- a/src/application/l10n/messages_sv.properties +++ b/src/application/l10n/messages_sv.properties @@ -26,6 +26,7 @@ error-no-alignment-specified=Ingen sidjustering angiven error-failed-to-load-help=Kunde inte ladda hjälptexten error-unsupported-xml-encoding=Teckenkodningen stöds inte: {0} error-failed-to-write-to-file=Kunde inte skriva till fil: {0} +error-invalid-arguments=Ogiltiga argument: {0} checkbox-watch-source=Övervaka källa title-set-search-folder=Välj sökmapp title-open-dialog=Öppna fil diff --git a/src/com/googlecode/e2u/Start.java b/src/com/googlecode/e2u/Start.java index d04a0a6..ee69dc5 100644 --- a/src/com/googlecode/e2u/Start.java +++ b/src/com/googlecode/e2u/Start.java @@ -1,5 +1,7 @@ package com.googlecode.e2u; +import java.util.Objects; + import com.googlecode.ajui.BrowserUI; import com.googlecode.e2u.StartupDetails.Mode; @@ -8,16 +10,7 @@ public class Start { private BrowserUI ui; public String start(StartupDetails args) throws Exception { - if (args==null) { - System.out.println("Supplied arguments do not match any of the following:"); - System.out.println("\t-setup"); - System.out.println("\t-open path-to-file"); - System.out.println("\t-print path-to-file"); - //System.out.println("\t-emboss path-to-file"); - //System.out.println("\t-view path-to-file"); - content = new MainPage(null); - System.exit(-1); - } + Objects.requireNonNull(args); BrowserUI.Builder buildUi = new BrowserUI.Builder("com/googlecode/e2u/resource-files"); buildUi.timeout(5000); if (!args.shouldLog()) { diff --git a/src/com/googlecode/e2u/StartupDetails.java b/src/com/googlecode/e2u/StartupDetails.java index 64431ec..c362e60 100644 --- a/src/com/googlecode/e2u/StartupDetails.java +++ b/src/com/googlecode/e2u/StartupDetails.java @@ -2,6 +2,7 @@ import java.io.File; import java.util.Objects; +import java.util.Optional; public class StartupDetails { private final Mode mode; @@ -71,38 +72,27 @@ private StartupDetails(Builder builder) { this.log = builder.log; } - public static StartupDetails parse(String[] args) { + /** + * Parses the supplied string array for startup commands. + * @param args the arguments + * @return the startup details + */ + public static Optional parse(String[] args) { if (args.length==0) { - return new StartupDetails.Builder().build(); - /* - page = ""; - content = new MainPage(null);*/ + return Optional.of(new StartupDetails.Builder().build()); } else if (args.length==1 && args[0].equalsIgnoreCase("-setup")) { - return new StartupDetails.Builder().mode(Mode.SETUP).build(); - /* - page = "index.html?method=setup"; - content = new MainPage(null); - */ + return Optional.of(new StartupDetails.Builder().mode(Mode.SETUP).build()); } else if (args.length==2 && args[0].equalsIgnoreCase("-emboss")) { - return new StartupDetails.Builder().mode(Mode.EMBOSS) - .file(new File(args[1])).build(); - /* - content = new MainPage(new File(args[1])); - page = "index.html?method=do";*/ + return Optional.of(new StartupDetails.Builder().mode(Mode.EMBOSS) + .file(new File(args[1])).build()); } else if (args.length==2 && args[0].equalsIgnoreCase("-open")) { - return new StartupDetails.Builder().mode(Mode.OPEN) + return Optional.of(new StartupDetails.Builder().mode(Mode.OPEN) .file(new File(args[1])) - .build(); - /* - content = new MainPage(new File(args[1])); - page = "view.html";*/ + .build()); } else if (args.length==2 && (args[0].equalsIgnoreCase("-view") || args[0].equalsIgnoreCase("-print"))) { - return new StartupDetails.Builder().mode(Mode.VIEW).file(new File(args[1])).build(); - /* - content = new MainPage(new File(args[1])); - page = "";*/ + return Optional.of(new StartupDetails.Builder().mode(Mode.VIEW).file(new File(args[1])).build()); } else { - return null; + return Optional.empty(); } }