Skip to content

Commit

Permalink
Ignores invalid startup arguments (fixes #58)
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Håkansson committed Feb 23, 2018
1 parent 0b35590 commit 6c20436
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 37 deletions.
17 changes: 15 additions & 2 deletions src/application/MainFx.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -83,11 +88,19 @@ public void start(Stage primaryStage) throws IOException {
Scene scene = new Scene(root);

MainController controller = fxmlLoader.<MainController>getController();
controller.openArgs(StartupDetails.parse(getParameters().getRaw().toArray(new String[]{})));
Optional<StartupDetails> 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<WindowEvent>() {
public void handle(WindowEvent ev) {
if (!controller.confirmShutdown()) {
Expand Down
1 change: 1 addition & 0 deletions src/application/l10n/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
1 change: 1 addition & 0 deletions src/application/l10n/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/application/l10n/messages_no.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/application/l10n/messages_sv.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 3 additions & 10 deletions src/com/googlecode/e2u/Start.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.googlecode.e2u;

import java.util.Objects;

import com.googlecode.ajui.BrowserUI;
import com.googlecode.e2u.StartupDetails.Mode;

Expand All @@ -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()) {
Expand Down
40 changes: 15 additions & 25 deletions src/com/googlecode/e2u/StartupDetails.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.File;
import java.util.Objects;
import java.util.Optional;

public class StartupDetails {
private final Mode mode;
Expand Down Expand Up @@ -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<StartupDetails> 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();
}
}

Expand Down

0 comments on commit 6c20436

Please sign in to comment.