From 034464b7b9955d2aa5655c7271343b9be1662133 Mon Sep 17 00:00:00 2001 From: Carlos Galindo Date: Sun, 12 Jun 2016 16:04:56 +0200 Subject: [PATCH] Import from other formats --- .../kauron/jstudy/controller/Controller.java | 11 +++++++++-- .../java/es/kauron/jstudy/model/TestItem.java | 19 +++++++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/main/java/es/kauron/jstudy/controller/Controller.java b/src/main/java/es/kauron/jstudy/controller/Controller.java index 7bd9419..c4293f0 100644 --- a/src/main/java/es/kauron/jstudy/controller/Controller.java +++ b/src/main/java/es/kauron/jstudy/controller/Controller.java @@ -86,10 +86,17 @@ public class Controller implements Initializable { @FXML private void onImportAction(ActionEvent event) { FileChooser chooser = new FileChooser(); - chooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Data files (.csv)", "*.csv")); + chooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Data files (.csv, .txt)", "*.csv", "*.txt")); File file = chooser.showOpenDialog(root.getScene().getWindow()); if (file == null) return; - List aux = TestItem.loadFrom(file, TestItem.SEMICOLON); + String separator; + if (file.getName().matches(".*txt")) + separator = TestItem.TAB; + else if (file.getName().matches(".*csv")) + separator = TestItem.SEMICOLON; + else + separator = TestItem.COMMA; + List aux = TestItem.loadFrom(file, separator); if (aux != null) { tabPane.getTabs().add(createTableTab(file.getName().substring(0, file.getName().lastIndexOf('.')), aux)); tabPane.getSelectionModel().selectLast(); diff --git a/src/main/java/es/kauron/jstudy/model/TestItem.java b/src/main/java/es/kauron/jstudy/model/TestItem.java index 405a301..563042e 100644 --- a/src/main/java/es/kauron/jstudy/model/TestItem.java +++ b/src/main/java/es/kauron/jstudy/model/TestItem.java @@ -10,7 +10,7 @@ import java.util.List; import java.util.Scanner; public class TestItem { - public static final String COMMA = ",", COLONS = "::", SEMICOLON = ";"; + public static final String TAB = "\t", COLONS = "::", SEMICOLON = ";", COMMA = ","; private StringProperty question, answer; @@ -67,12 +67,27 @@ public class TestItem { public static List loadFrom(File file, String separator) { List list = new ArrayList<>(); try { - Scanner in = new Scanner(file); + Scanner in; + switch (separator) { + case COMMA: + case SEMICOLON: + case TAB: + in = new Scanner(new FileInputStream(file), "ISO-8859-1"); + break; + default: + in = new Scanner(file); + break; + } while (in.hasNextLine()) { String line = in.nextLine(); + if (!line.matches("..*" + separator + "..*")) continue; int index = line.indexOf(separator); String question = line.substring(0, index); String answer = line.substring(index + separator.length()); + index = answer.indexOf(separator); + if (index != -1) + answer = answer.substring(0, index); + if (answer.isEmpty()) continue; list.add(new TestItem(question, answer)); System.err.println(question + " :: " + answer); }