diff --git a/.gitignore b/.gitignore index fd2d5e0..763c2b1 100644 --- a/.gitignore +++ b/.gitignore @@ -74,3 +74,4 @@ crashlytics.properties crashlytics-build.properties +smartcsv.log diff --git a/README.md b/README.md index 5f78f43..0f24a2d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # SmartCSV.fx ##Description -A simple JavaFX application to load, save and edit a CSV file and provide a JSON configuration for columns to check the values in the columns. +A simple JavaFX application to load, save and edit a CSV file and provide a [JSON Table Schema](http://specs.frictionlessdata.io/json-table-schema/) for columns to check the values in the columns. ##Motivation At work I have the need to fix wrong CSV files from customers. It is hard to find the errors and fix them in a texteditor, @@ -14,7 +14,7 @@ even in a "normal" CSV editor. So I decided to write this simple JavaFX applicat [Wiki & Documentation](https://github.com/frosch95/SmartCSV.fx/wiki) -binary distribution of the [latest release (0.7)](https://github.com/frosch95/SmartCSV.fx/releases/download/0.7/SmartCSV.fx-0.7-SNAPSHOT.zip) +binary distribution of the [latest release (0.8)](https://github.com/frosch95/SmartCSV.fx/releases/download/0.8/SmartCSV.fx-0.8-SNAPSHOT.zip) ##Talks [Introduction](http://javafx.ninja/talks/introduction/) @@ -23,7 +23,7 @@ binary distribution of the [latest release (0.7)](https://github.com/frosch95/Sm ###The MIT License (MIT) -Copyright (c) 2015 Andreas Billmann +Copyright (c) 2015-2016 Andreas Billmann diff --git a/build.gradle b/build.gradle index 6614bb2..25aa6d7 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ group 'ninja.javafx' -version '0.7-SNAPSHOT' +version '0.8-SNAPSHOT' apply plugin: 'java' apply plugin: 'groovy' diff --git a/license b/license index a57e612..acf9c21 100644 --- a/license +++ b/license @@ -1,7 +1,7 @@ The MIT License (MIT) ------------------------------------------------------------------------------------------------------------------------ -Copyright (c) 2015 Andreas Billmann +Copyright (c) 2015-2016 Andreas Billmann diff --git a/src/main/java/ninja/javafx/smartcsv/FileReader.java b/src/main/java/ninja/javafx/smartcsv/FileReader.java index b320a03..743814e 100644 --- a/src/main/java/ninja/javafx/smartcsv/FileReader.java +++ b/src/main/java/ninja/javafx/smartcsv/FileReader.java @@ -2,7 +2,7 @@ The MIT License (MIT) ----------------------------------------------------------------------------- - Copyright (c) 2015 javafx.ninja + Copyright (c) 2015-2016 javafx.ninja Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/src/main/java/ninja/javafx/smartcsv/FileWriter.java b/src/main/java/ninja/javafx/smartcsv/FileWriter.java index 42241f5..18e6979 100644 --- a/src/main/java/ninja/javafx/smartcsv/FileWriter.java +++ b/src/main/java/ninja/javafx/smartcsv/FileWriter.java @@ -2,7 +2,7 @@ The MIT License (MIT) ----------------------------------------------------------------------------- - Copyright (c) 2015 javafx.ninja + Copyright (c) 2015-2016 javafx.ninja Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/src/main/java/ninja/javafx/smartcsv/csv/CSVConfigurable.java b/src/main/java/ninja/javafx/smartcsv/csv/CSVConfigurable.java index 51480cc..43c0830 100644 --- a/src/main/java/ninja/javafx/smartcsv/csv/CSVConfigurable.java +++ b/src/main/java/ninja/javafx/smartcsv/csv/CSVConfigurable.java @@ -2,7 +2,7 @@ The MIT License (MIT) ----------------------------------------------------------------------------- - Copyright (c) 2015 javafx.ninja + Copyright (c) 2015-2016 javafx.ninja Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/src/main/java/ninja/javafx/smartcsv/csv/CSVFileReader.java b/src/main/java/ninja/javafx/smartcsv/csv/CSVFileReader.java index 99f077f..4ccfac3 100644 --- a/src/main/java/ninja/javafx/smartcsv/csv/CSVFileReader.java +++ b/src/main/java/ninja/javafx/smartcsv/csv/CSVFileReader.java @@ -2,7 +2,7 @@ The MIT License (MIT) ----------------------------------------------------------------------------- - Copyright (c) 2015 javafx.ninja + Copyright (c) 2015-2016 javafx.ninja Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/src/main/java/ninja/javafx/smartcsv/csv/CSVFileWriter.java b/src/main/java/ninja/javafx/smartcsv/csv/CSVFileWriter.java index 0fe6d9f..9115e66 100644 --- a/src/main/java/ninja/javafx/smartcsv/csv/CSVFileWriter.java +++ b/src/main/java/ninja/javafx/smartcsv/csv/CSVFileWriter.java @@ -2,7 +2,7 @@ The MIT License (MIT) ----------------------------------------------------------------------------- - Copyright (c) 2015 javafx.ninja + Copyright (c) 2015-2016 javafx.ninja Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/src/main/java/ninja/javafx/smartcsv/export/ErrorExport.java b/src/main/java/ninja/javafx/smartcsv/export/ErrorExport.java index 6ec2107..6837a52 100644 --- a/src/main/java/ninja/javafx/smartcsv/export/ErrorExport.java +++ b/src/main/java/ninja/javafx/smartcsv/export/ErrorExport.java @@ -51,13 +51,13 @@ public class ErrorExport extends Service { format(resourceBundle.getString("log.header.message"), csvFilename, Integer.toString(model.getValidationError().size()))).append("\n\n"); - for (ValidationError error:model.getValidationError()) { + model.getValidationError().forEach(error -> log.append( format(resourceBundle.getString("log.message"), error.getLineNumber().toString(), error.getColumn(), - getI18nValidatioMessage(resourceBundle, error))).append("\n"); - } + getI18nValidatioMessage(resourceBundle, error))).append("\n") + ); Files.write(file.toPath(), log.toString().getBytes()); } catch (Throwable ex) { diff --git a/src/main/java/ninja/javafx/smartcsv/fx/FXMLController.java b/src/main/java/ninja/javafx/smartcsv/fx/FXMLController.java index cad8fb9..4ae0f3e 100644 --- a/src/main/java/ninja/javafx/smartcsv/fx/FXMLController.java +++ b/src/main/java/ninja/javafx/smartcsv/fx/FXMLController.java @@ -2,7 +2,7 @@ The MIT License (MIT) ----------------------------------------------------------------------------- - Copyright (c) 2015 javafx.ninja + Copyright (c) 2015-2016 javafx.ninja Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/src/main/java/ninja/javafx/smartcsv/fx/SmartCSV.java b/src/main/java/ninja/javafx/smartcsv/fx/SmartCSV.java index 881c3f5..2e8404c 100644 --- a/src/main/java/ninja/javafx/smartcsv/fx/SmartCSV.java +++ b/src/main/java/ninja/javafx/smartcsv/fx/SmartCSV.java @@ -2,7 +2,7 @@ The MIT License (MIT) ----------------------------------------------------------------------------- - Copyright (c) 2015 javafx.ninja + Copyright (c) 2015-2016 javafx.ninja Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/src/main/java/ninja/javafx/smartcsv/fx/SmartCSVController.java b/src/main/java/ninja/javafx/smartcsv/fx/SmartCSVController.java index 30af0ff..9163350 100644 --- a/src/main/java/ninja/javafx/smartcsv/fx/SmartCSVController.java +++ b/src/main/java/ninja/javafx/smartcsv/fx/SmartCSVController.java @@ -2,7 +2,7 @@ The MIT License (MIT) ----------------------------------------------------------------------------- - Copyright (c) 2015 javafx.ninja + Copyright (c) 2015-2016 javafx.ninja Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -56,7 +56,7 @@ import ninja.javafx.smartcsv.fx.util.SaveFileService; import ninja.javafx.smartcsv.fx.validation.ValidationEditorController; import ninja.javafx.smartcsv.preferences.PreferencesFileReader; import ninja.javafx.smartcsv.preferences.PreferencesFileWriter; -import ninja.javafx.smartcsv.validation.ValidationConfiguration; +import ninja.javafx.smartcsv.validation.configuration.ValidationConfiguration; import ninja.javafx.smartcsv.validation.ValidationError; import ninja.javafx.smartcsv.validation.ValidationFileReader; import ninja.javafx.smartcsv.validation.ValidationFileWriter; @@ -425,6 +425,7 @@ public class SmartCSVController extends FXMLController { public void showValidationEditor(String column) { validationEditorController.setSelectedColumn(column); + validationEditorController.updateForm(); Alert alert = new Alert(Alert.AlertType.CONFIRMATION); alert.setGraphic(null); diff --git a/src/main/java/ninja/javafx/smartcsv/fx/about/AboutController.java b/src/main/java/ninja/javafx/smartcsv/fx/about/AboutController.java index 1b11450..6697296 100644 --- a/src/main/java/ninja/javafx/smartcsv/fx/about/AboutController.java +++ b/src/main/java/ninja/javafx/smartcsv/fx/about/AboutController.java @@ -2,7 +2,7 @@ The MIT License (MIT) ----------------------------------------------------------------------------- - Copyright (c) 2015 javafx.ninja + Copyright (c) 2015-2016 javafx.ninja Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/src/main/java/ninja/javafx/smartcsv/fx/list/ErrorSideBar.java b/src/main/java/ninja/javafx/smartcsv/fx/list/ErrorSideBar.java index 9d89011..d8b88af 100644 --- a/src/main/java/ninja/javafx/smartcsv/fx/list/ErrorSideBar.java +++ b/src/main/java/ninja/javafx/smartcsv/fx/list/ErrorSideBar.java @@ -2,7 +2,7 @@ The MIT License (MIT) ----------------------------------------------------------------------------- - Copyright (c) 2015 javafx.ninja + Copyright (c) 2015-2016 javafx.ninja Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -137,9 +137,7 @@ public class ErrorSideBar extends Region { int rows = model.get().getRows().size(); double space = (double)heightWithoutStatusBlock() / rows; - for (ValidationError error : errorList) { - errorMarkerList.add(generateErrorMarker(space, error)); - } + errorList.forEach(error -> errorMarkerList.add(generateErrorMarker(space, error))); } } getChildren().setAll(errorMarkerList); diff --git a/src/main/java/ninja/javafx/smartcsv/fx/preferences/PreferencesController.java b/src/main/java/ninja/javafx/smartcsv/fx/preferences/PreferencesController.java index d5f6304..b5ac7b9 100644 --- a/src/main/java/ninja/javafx/smartcsv/fx/preferences/PreferencesController.java +++ b/src/main/java/ninja/javafx/smartcsv/fx/preferences/PreferencesController.java @@ -2,7 +2,7 @@ The MIT License (MIT) ----------------------------------------------------------------------------- - Copyright (c) 2015 javafx.ninja + Copyright (c) 2015-2016 javafx.ninja Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/src/main/java/ninja/javafx/smartcsv/fx/table/EditableValidationCell.java b/src/main/java/ninja/javafx/smartcsv/fx/table/EditableValidationCell.java index c4054cf..c250add 100644 --- a/src/main/java/ninja/javafx/smartcsv/fx/table/EditableValidationCell.java +++ b/src/main/java/ninja/javafx/smartcsv/fx/table/EditableValidationCell.java @@ -2,7 +2,7 @@ The MIT License (MIT) ----------------------------------------------------------------------------- - Copyright (c) 2015 javafx.ninja + Copyright (c) 2015-2016 javafx.ninja Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/src/main/java/ninja/javafx/smartcsv/fx/table/ObservableMapValueFactory.java b/src/main/java/ninja/javafx/smartcsv/fx/table/ObservableMapValueFactory.java index bd56c14..a3057ba 100644 --- a/src/main/java/ninja/javafx/smartcsv/fx/table/ObservableMapValueFactory.java +++ b/src/main/java/ninja/javafx/smartcsv/fx/table/ObservableMapValueFactory.java @@ -2,7 +2,7 @@ The MIT License (MIT) ----------------------------------------------------------------------------- - Copyright (c) 2015 javafx.ninja + Copyright (c) 2015-2016 javafx.ninja Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/src/main/java/ninja/javafx/smartcsv/fx/table/ValidationCellFactory.java b/src/main/java/ninja/javafx/smartcsv/fx/table/ValidationCellFactory.java index abc13e0..f9f7325 100644 --- a/src/main/java/ninja/javafx/smartcsv/fx/table/ValidationCellFactory.java +++ b/src/main/java/ninja/javafx/smartcsv/fx/table/ValidationCellFactory.java @@ -2,7 +2,7 @@ The MIT License (MIT) ----------------------------------------------------------------------------- - Copyright (c) 2015 javafx.ninja + Copyright (c) 2015-2016 javafx.ninja Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/src/main/java/ninja/javafx/smartcsv/fx/table/model/CSVModel.java b/src/main/java/ninja/javafx/smartcsv/fx/table/model/CSVModel.java index 9f15502..c9fef1e 100644 --- a/src/main/java/ninja/javafx/smartcsv/fx/table/model/CSVModel.java +++ b/src/main/java/ninja/javafx/smartcsv/fx/table/model/CSVModel.java @@ -2,7 +2,7 @@ The MIT License (MIT) ----------------------------------------------------------------------------- - Copyright (c) 2015 javafx.ninja + Copyright (c) 2015-2016 javafx.ninja Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -29,7 +29,7 @@ package ninja.javafx.smartcsv.fx.table.model; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import ninja.javafx.smartcsv.validation.RevalidationService; -import ninja.javafx.smartcsv.validation.ValidationConfiguration; +import ninja.javafx.smartcsv.validation.configuration.ValidationConfiguration; import ninja.javafx.smartcsv.validation.ValidationError; import ninja.javafx.smartcsv.validation.Validator; import org.apache.logging.log4j.LogManager; diff --git a/src/main/java/ninja/javafx/smartcsv/fx/table/model/CSVRow.java b/src/main/java/ninja/javafx/smartcsv/fx/table/model/CSVRow.java index fcd0429..8c54513 100644 --- a/src/main/java/ninja/javafx/smartcsv/fx/table/model/CSVRow.java +++ b/src/main/java/ninja/javafx/smartcsv/fx/table/model/CSVRow.java @@ -2,7 +2,7 @@ The MIT License (MIT) ----------------------------------------------------------------------------- - Copyright (c) 2015 javafx.ninja + Copyright (c) 2015-2016 javafx.ninja Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/src/main/java/ninja/javafx/smartcsv/fx/table/model/CSVValue.java b/src/main/java/ninja/javafx/smartcsv/fx/table/model/CSVValue.java index 7903655..41aa180 100644 --- a/src/main/java/ninja/javafx/smartcsv/fx/table/model/CSVValue.java +++ b/src/main/java/ninja/javafx/smartcsv/fx/table/model/CSVValue.java @@ -2,7 +2,7 @@ The MIT License (MIT) ----------------------------------------------------------------------------- - Copyright (c) 2015 javafx.ninja + Copyright (c) 2015-2016 javafx.ninja Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/src/main/java/ninja/javafx/smartcsv/fx/util/ColorConstants.java b/src/main/java/ninja/javafx/smartcsv/fx/util/ColorConstants.java index 3479e64..c949aba 100644 --- a/src/main/java/ninja/javafx/smartcsv/fx/util/ColorConstants.java +++ b/src/main/java/ninja/javafx/smartcsv/fx/util/ColorConstants.java @@ -2,7 +2,7 @@ The MIT License (MIT) ----------------------------------------------------------------------------- - Copyright (c) 2015 javafx.ninja + Copyright (c) 2015-2016 javafx.ninja Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/src/main/java/ninja/javafx/smartcsv/fx/util/I18nValidationUtil.java b/src/main/java/ninja/javafx/smartcsv/fx/util/I18nValidationUtil.java index 5fa81fe..d7f9bbe 100644 --- a/src/main/java/ninja/javafx/smartcsv/fx/util/I18nValidationUtil.java +++ b/src/main/java/ninja/javafx/smartcsv/fx/util/I18nValidationUtil.java @@ -2,7 +2,7 @@ The MIT License (MIT) ----------------------------------------------------------------------------- - Copyright (c) 2015 javafx.ninja + Copyright (c) 2015-2016 javafx.ninja Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -43,9 +43,8 @@ public class I18nValidationUtil { public static String getI18nValidatioMessage(ResourceBundle resourceBundle, List errors) { StringWriter message = new StringWriter(); - for (ValidationError validationError: errors) { - message.append(getI18nValidatioMessage(resourceBundle, validationError)).append("\n"); - } + errors.forEach(error -> message.append(getI18nValidatioMessage(resourceBundle, error)).append("\n")); + if (message.toString().length() != 0) { return cutOffLastLineBreak(message.toString()); @@ -66,7 +65,7 @@ public class I18nValidationUtil { List validationMessages = error.getMessages(); StringWriter message = new StringWriter(); - for (ValidationMessage validationMessage: validationMessages) { + validationMessages.forEach(validationMessage -> { message.append(prefix); if (resourceBundle.containsKey(validationMessage.getKey())) { String resourceText = resourceBundle.getString(validationMessage.getKey()); @@ -78,7 +77,7 @@ public class I18nValidationUtil { } else { message.append(validationMessage.getKey()).append("\n"); } - } + }); if (!validationMessages.isEmpty()) { return cutOffLastLineBreak(message.toString()); diff --git a/src/main/java/ninja/javafx/smartcsv/fx/util/LoadFileService.java b/src/main/java/ninja/javafx/smartcsv/fx/util/LoadFileService.java index 49606f0..468af45 100644 --- a/src/main/java/ninja/javafx/smartcsv/fx/util/LoadFileService.java +++ b/src/main/java/ninja/javafx/smartcsv/fx/util/LoadFileService.java @@ -2,7 +2,7 @@ The MIT License (MIT) ----------------------------------------------------------------------------- - Copyright (c) 2015 javafx.ninja + Copyright (c) 2015-2016 javafx.ninja Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/src/main/java/ninja/javafx/smartcsv/fx/util/SaveFileService.java b/src/main/java/ninja/javafx/smartcsv/fx/util/SaveFileService.java index 03c943b..6b4ee1b 100644 --- a/src/main/java/ninja/javafx/smartcsv/fx/util/SaveFileService.java +++ b/src/main/java/ninja/javafx/smartcsv/fx/util/SaveFileService.java @@ -2,7 +2,7 @@ The MIT License (MIT) ----------------------------------------------------------------------------- - Copyright (c) 2015 javafx.ninja + Copyright (c) 2015-2016 javafx.ninja Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/src/main/java/ninja/javafx/smartcsv/fx/validation/StringFormatEditorCellFactory.java b/src/main/java/ninja/javafx/smartcsv/fx/validation/StringFormatEditorCellFactory.java new file mode 100644 index 0000000..dd1eaf5 --- /dev/null +++ b/src/main/java/ninja/javafx/smartcsv/fx/validation/StringFormatEditorCellFactory.java @@ -0,0 +1,61 @@ +/* + The MIT License (MIT) + ----------------------------------------------------------------------------- + + Copyright (c) 2015-2016 javafx.ninja + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + +*/ + +package ninja.javafx.smartcsv.fx.validation; + +import javafx.scene.control.ListCell; +import javafx.scene.control.ListView; +import javafx.util.Callback; +import ninja.javafx.smartcsv.validation.configuration.StringFormat; + +import java.util.ResourceBundle; + +/** + * cell factory for string formats + */ +public class StringFormatEditorCellFactory implements Callback, ListCell> { + + private ResourceBundle resourceBundle; + + public StringFormatEditorCellFactory(ResourceBundle resourceBundle) { + this.resourceBundle = resourceBundle; + } + + @Override + public ListCell call(ListView param) { + return new ListCell(){ + @Override + protected void updateItem(StringFormat item, boolean empty) { + super.updateItem(item, empty); + if (item == null || empty) { + setGraphic(null); + } else { + setText(resourceBundle.getString("format.type."+item)); + } + } + } ; + } +} diff --git a/src/main/java/ninja/javafx/smartcsv/fx/validation/StringFormatStringConverter.java b/src/main/java/ninja/javafx/smartcsv/fx/validation/StringFormatStringConverter.java new file mode 100644 index 0000000..e1db857 --- /dev/null +++ b/src/main/java/ninja/javafx/smartcsv/fx/validation/StringFormatStringConverter.java @@ -0,0 +1,54 @@ +/* + The MIT License (MIT) + ----------------------------------------------------------------------------- + + Copyright (c) 2015-2016 javafx.ninja + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + +*/ + +package ninja.javafx.smartcsv.fx.validation; + +import javafx.util.StringConverter; +import ninja.javafx.smartcsv.validation.configuration.StringFormat; + +import java.util.ResourceBundle; + +/** + * converter for string formats + */ +public class StringFormatStringConverter extends StringConverter { + + private ResourceBundle resourceBundle; + + public StringFormatStringConverter(ResourceBundle resourceBundle) { + this.resourceBundle = resourceBundle; + } + + @Override + public String toString(StringFormat item) { + return resourceBundle.getString("format.type."+item); + } + + @Override + public StringFormat fromString(String string) { + return StringFormat.fromExternalValue(string); + } +} diff --git a/src/main/java/ninja/javafx/smartcsv/fx/validation/ValidationEditorController.java b/src/main/java/ninja/javafx/smartcsv/fx/validation/ValidationEditorController.java index aaae261..46dba1a 100644 --- a/src/main/java/ninja/javafx/smartcsv/fx/validation/ValidationEditorController.java +++ b/src/main/java/ninja/javafx/smartcsv/fx/validation/ValidationEditorController.java @@ -2,7 +2,7 @@ The MIT License (MIT) ----------------------------------------------------------------------------- - Copyright (c) 2015 javafx.ninja + Copyright (c) 2015-2016 javafx.ninja Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -26,16 +26,13 @@ package ninja.javafx.smartcsv.fx.validation; -import javafx.beans.binding.Bindings; import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.StringProperty; -import javafx.beans.value.ChangeListener; -import javafx.beans.value.ObservableValue; import javafx.fxml.FXML; import javafx.scene.control.*; import javafx.scene.control.SpinnerValueFactory.IntegerSpinnerValueFactory; import ninja.javafx.smartcsv.fx.FXMLController; -import ninja.javafx.smartcsv.validation.ValidationConfiguration; +import ninja.javafx.smartcsv.validation.configuration.*; import org.fxmisc.richtext.CodeArea; import org.fxmisc.richtext.LineNumberFactory; import org.fxmisc.richtext.StyleSpans; @@ -51,9 +48,11 @@ import java.util.ResourceBundle; import java.util.regex.Matcher; import java.util.regex.Pattern; +import static java.lang.Boolean.FALSE; import static java.util.Arrays.asList; import static java.util.stream.Collectors.joining; import static javafx.beans.binding.Bindings.when; +import static ninja.javafx.smartcsv.validation.configuration.Type.STRING; /** * controller for editing column validations @@ -80,7 +79,7 @@ public class ValidationEditorController extends FXMLController { "transient", "true", "try", "void", "volatile", "while" }; - private static final String KEYWORD_PATTERN = "\\b(" + String.join("|", KEYWORDS) + ")\\b"; + private static final String KEYWORD_PATTERN = "\\b(" + String.join("|", (CharSequence[]) KEYWORDS) + ")\\b"; private static final String PAREN_PATTERN = "\\(|\\)"; private static final String BRACE_PATTERN = "\\{|\\}"; private static final String BRACKET_PATTERN = "\\[|\\]"; @@ -100,20 +99,14 @@ public class ValidationEditorController extends FXMLController { + "|(?" + COMMENT_PATTERN + ")" ); -// @FXML -// private CheckBox notEmptyRuleCheckBox; -// -// @FXML -// private CheckBox integerRuleCheckBox; -// -// @FXML -// private CheckBox doublerRuleCheckBox; -// -// @FXML -// private CheckBox alphanumericRuleCheckBox; -// -// @FXML -// private CheckBox uniqueRuleCheckBox; + @FXML + private ComboBox typeComboBox; + + @FXML + private ComboBox formatComboBox; + + @FXML + private TextField formatTextField; @FXML private Spinner minLengthSpinner; @@ -121,9 +114,6 @@ public class ValidationEditorController extends FXMLController { @FXML private Spinner maxLengthSpinner; - @FXML - private TextField dateformatRuleTextField; - @FXML private TextField regexpRuleTextField; @@ -136,24 +126,12 @@ public class ValidationEditorController extends FXMLController { @FXML private CheckBox enableNotEmptyRule; - @FXML - private CheckBox enableIntegerRule; - - @FXML - private CheckBox enableDoubleRule; - - @FXML - private CheckBox enableAlphanumericRule; - @FXML private CheckBox enableMinLengthRule; @FXML private CheckBox enableMaxLengthRule; - @FXML - private CheckBox enableDateRule; - @FXML private CheckBox enableRegexpRule; @@ -175,21 +153,75 @@ public class ValidationEditorController extends FXMLController { @Override public void initialize(URL location, ResourceBundle resources) { - + initTypeAndFormatInput(resources); initMinMaxSpinner(); - initSpinner(minLengthSpinner, enableMinLengthRule); initSpinner(maxLengthSpinner, enableMaxLengthRule); - initTextInputControl(dateformatRuleTextField, enableDateRule); initTextInputControl(regexpRuleTextField, enableRegexpRule); initTextInputControl(valueOfRuleTextField, enableValueOfRule); initCodeAreaControl(groovyRuleTextArea, enableGroovyRule); + } - listenToExcludingRuleCombinations(); + public String getSelectedColumn() { + return selectedColumn.get(); + } - selectedColumn.addListener(observable -> { - updateForm(); - }); + public StringProperty selectedColumnProperty() { + return selectedColumn; + } + + public void setSelectedColumn(String selectedColumn) { + this.selectedColumn.set(selectedColumn); + } + + public void setValidationConfiguration(ValidationConfiguration validationConfiguration) { + this.validationConfiguration = validationConfiguration; + } + + private void initTypeAndFormatInput(ResourceBundle resources) { + typeComboBox.getItems().addAll(STRING, + Type.INTEGER, + Type.NUMBER, + Type.DATE, + Type.DATETIME, + Type.TIME); + formatComboBox.setCellFactory(new StringFormatEditorCellFactory(resources)); + formatComboBox.setConverter(new StringFormatStringConverter(resources)); + formatComboBox.getItems().addAll( + StringFormat.DEFAULT, + StringFormat.EMAIL, + StringFormat.URI, + StringFormat.BINARY, + StringFormat.UUID + ); + typeComboBox.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> changeFormat()); + } + + private void updateFormatTextField() { + switch (typeComboBox.getValue()) { + case DATE: + case DATETIME: + case TIME: + formatTextField.setVisible(true); + formatTextField.setText(getCurrentFieldConfig().getFormat()); + break; + default: + formatTextField.setVisible(false); + formatTextField.setText(null); + break; + } + } + + private void updateFormatComboBox() { + switch (typeComboBox.getValue()) { + case STRING: + formatComboBox.setVisible(true); + formatComboBox.getSelectionModel().select(StringFormat.fromExternalValue(getCurrentFieldConfig().getFormat())); + break; + default: + formatComboBox.setVisible(false); + break; + } } private void initMinMaxSpinner() { @@ -210,97 +242,104 @@ public class ValidationEditorController extends FXMLController { } - public String getSelectedColumn() { - return selectedColumn.get(); - } - - public StringProperty selectedColumnProperty() { - return selectedColumn; - } - - public void setSelectedColumn(String selectedColumn) { - this.selectedColumn.set(selectedColumn); - } - - public void setValidationConfiguration(ValidationConfiguration validationConfiguration) { - this.validationConfiguration = validationConfiguration; + private void changeFormat() { + switch (typeComboBox.getValue()) { + case STRING: + updateFormatComboBox(); + break; + case DATE: + case DATETIME: + case TIME: + updateFormatTextField(); + break; + case INTEGER: + case NUMBER: + default: + // format: no options + formatComboBox.setVisible(false); + formatTextField.setVisible(false); + break; + } } public void updateConfiguration() { - if (enableIntegerRule.isSelected()) { - validationConfiguration.setIntegerRuleFor(selectedColumn.getValue(), enableIntegerRule.isSelected()); - } else { - validationConfiguration.setIntegerRuleFor(selectedColumn.getValue(), null); - } + Field config = getCurrentFieldConfig(); + config.setType(typeComboBox.getValue()); - if (enableNotEmptyRule.isSelected()) { - validationConfiguration.setNotEmptyRuleFor(selectedColumn.getValue(), enableNotEmptyRule.isSelected()); - } else { - validationConfiguration.setNotEmptyRuleFor(selectedColumn.getValue(), null); - } - - if (enableUniqueRule.isSelected()) { - validationConfiguration.setUniqueRuleFor(selectedColumn.getValue(), enableUniqueRule.isSelected()); - } else { - validationConfiguration.setUniqueRuleFor(selectedColumn.getValue(), null); - } - - if (enableDoubleRule.isSelected()) { - validationConfiguration.setDoubleRuleFor(selectedColumn.getValue(), enableDoubleRule.isSelected()); - } else { - validationConfiguration.setDoubleRuleFor(selectedColumn.getValue(), null); - } - - if (enableAlphanumericRule.isSelected()) { - validationConfiguration.setAlphanumericRuleFor(selectedColumn.getValue(), enableAlphanumericRule.isSelected()); - } else { - validationConfiguration.setAlphanumericRuleFor(selectedColumn.getValue(), null); - } - - if (enableDateRule.isSelected()) { - validationConfiguration.setDateRuleFor(selectedColumn.getValue(), dateformatRuleTextField.getText()); - } else { - validationConfiguration.setDateRuleFor(selectedColumn.getValue(), null); + switch (typeComboBox.getValue()) { + case STRING: + config.setFormat(formatComboBox.getValue().getExternalValue()); + break; + case DATE: + case DATETIME: + case TIME: + if (formatTextField.getText().trim().isEmpty()) { + config.setFormat(null); + } else { + // TODO: validate input + config.setFormat(formatTextField.getText()); + } + break; + case INTEGER: + case NUMBER: + default: + // format: no options + config.setFormat(null); + break; } if (enableGroovyRule.isSelected()) { - validationConfiguration.setGroovyRuleFor(selectedColumn.getValue(), groovyRuleTextArea.getText()); + config.setGroovy(groovyRuleTextArea.getText()); } else { - validationConfiguration.setGroovyRuleFor(selectedColumn.getValue(), null); + config.setGroovy(null); + } + + Constraints constraints = config.getConstraints(); + if (constraints == null) { + constraints = new Constraints(); + } + + if (enableNotEmptyRule.isSelected()) { + constraints.setRequired(enableNotEmptyRule.isSelected()); + } else { + constraints.setRequired(null); + } + + if (enableUniqueRule.isSelected()) { + constraints.setUnique(enableUniqueRule.isSelected()); + } else { + constraints.setUnique(null); } if (enableMinLengthRule.isSelected()) { - validationConfiguration.setMinLengthRuleFor(selectedColumn.getValue(), minLengthSpinner.getValue()); + constraints.setMinLength(minLengthSpinner.getValue()); } else { - validationConfiguration.setMinLengthRuleFor(selectedColumn.getValue(), null); + constraints.setMinLength(null); } if (enableMaxLengthRule.isSelected()) { - validationConfiguration.setMaxLengthRuleFor(selectedColumn.getValue(), maxLengthSpinner.getValue()); + constraints.setMaxLength(maxLengthSpinner.getValue()); } else { - validationConfiguration.setMaxLengthRuleFor(selectedColumn.getValue(), null); + constraints.setMaxLength(null); } if (enableRegexpRule.isSelected()) { - validationConfiguration.setRegexpRuleFor(selectedColumn.getValue(), regexpRuleTextField.getText()); + constraints.setPattern(regexpRuleTextField.getText()); } else { - validationConfiguration.setRegexpRuleFor(selectedColumn.getValue(), null); + constraints.setPattern(null); } if (enableValueOfRule.isSelected()) { - validationConfiguration.setValueOfRuleFor(selectedColumn.getValue(), asList(valueOfRuleTextField.getText().split(", "))); + constraints.setEnumeration(asList(valueOfRuleTextField.getText().split(", "))); } else { - validationConfiguration.setValueOfRuleFor(selectedColumn.getValue(), null); + constraints.setEnumeration(null); } } - private void listenToExcludingRuleCombinations() { - addDependencyListener(enableIntegerRule, enableDoubleRule, enableAlphanumericRule, enableDateRule); - addDependencyListener(enableDoubleRule, enableIntegerRule, enableAlphanumericRule, enableDateRule); - addDependencyListener(enableAlphanumericRule, enableIntegerRule, enableDoubleRule, enableDateRule); - addDependencyListener(enableDateRule, enableIntegerRule, enableDoubleRule, enableAlphanumericRule); + private Field getCurrentFieldConfig() { + return validationConfiguration.getFieldConfiguration(getSelectedColumn()); } private void addDependencyListener(CheckBox rule, CheckBox... dependentRules) { @@ -313,68 +352,54 @@ public class ValidationEditorController extends FXMLController { }); } - private void updateForm() { + public void updateForm() { - updateCheckBox( - validationConfiguration.getNotEmptyRuleFor(getSelectedColumn()), - enableNotEmptyRule + Field config = getCurrentFieldConfig(); + + if (config.getType() != null) { + typeComboBox.setValue(config.getType()); + } else { + typeComboBox.setValue(STRING); + } + + updateFormatComboBox(); + updateFormatTextField(); + + updateCodeAreaControl( + groovyRuleTextArea, + config.getGroovy(), + enableGroovyRule ); - updateCheckBox( - validationConfiguration.getIntegerRuleFor(getSelectedColumn()), - enableIntegerRule - ); - - updateCheckBox( - validationConfiguration.getDoubleRuleFor(getSelectedColumn()), - enableDoubleRule - ); - - updateCheckBox( - validationConfiguration.getAlphanumericRuleFor(getSelectedColumn()), - enableAlphanumericRule - ); - - updateCheckBox( - validationConfiguration.getUniqueRuleFor(getSelectedColumn()), - enableUniqueRule - ); + Constraints constraints = config.getConstraints(); + updateCheckBox(constraints != null ? constraints.getRequired() : FALSE, enableNotEmptyRule); + updateCheckBox(constraints != null ? constraints.getUnique() : FALSE, enableUniqueRule); updateSpinner( minLengthSpinner, - validationConfiguration.getMinLengthRuleFor(getSelectedColumn()), + constraints != null ? constraints.getMinLength() : null, enableMinLengthRule ); updateSpinner( maxLengthSpinner, - validationConfiguration.getMaxLengthRuleFor(getSelectedColumn()), + constraints != null ? constraints.getMaxLength() : null, enableMaxLengthRule ); - updateTextInputControl( - dateformatRuleTextField, - validationConfiguration.getDateRuleFor(getSelectedColumn()), - enableDateRule - ); - updateTextInputControl( regexpRuleTextField, - validationConfiguration.getRegexpRuleFor(getSelectedColumn()), + constraints != null ? constraints.getPattern() : null, enableRegexpRule ); updateTextInputControl( valueOfRuleTextField, - validationConfiguration.getValueOfRuleFor(getSelectedColumn()), + constraints != null ? constraints.getEnumeration() : null, enableValueOfRule ); - updateCodeAreaControl( - groovyRuleTextArea, - validationConfiguration.getGroovyRuleFor(getSelectedColumn()), - enableGroovyRule - ); + } private void updateCheckBox(Boolean value, CheckBox ruleEnabled) { diff --git a/src/main/java/ninja/javafx/smartcsv/preferences/PreferencesFileReader.java b/src/main/java/ninja/javafx/smartcsv/preferences/PreferencesFileReader.java index f6258f6..15b12ec 100644 --- a/src/main/java/ninja/javafx/smartcsv/preferences/PreferencesFileReader.java +++ b/src/main/java/ninja/javafx/smartcsv/preferences/PreferencesFileReader.java @@ -2,7 +2,7 @@ The MIT License (MIT) ----------------------------------------------------------------------------- - Copyright (c) 2015 javafx.ninja + Copyright (c) 2015-2016 javafx.ninja Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/src/main/java/ninja/javafx/smartcsv/preferences/PreferencesFileWriter.java b/src/main/java/ninja/javafx/smartcsv/preferences/PreferencesFileWriter.java index 76fe6e9..682893b 100644 --- a/src/main/java/ninja/javafx/smartcsv/preferences/PreferencesFileWriter.java +++ b/src/main/java/ninja/javafx/smartcsv/preferences/PreferencesFileWriter.java @@ -2,7 +2,7 @@ The MIT License (MIT) ----------------------------------------------------------------------------- - Copyright (c) 2015 javafx.ninja + Copyright (c) 2015-2016 javafx.ninja Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/src/main/java/ninja/javafx/smartcsv/preferences/QuoteModeHelper.java b/src/main/java/ninja/javafx/smartcsv/preferences/QuoteModeHelper.java index 7c29017..1273591 100644 --- a/src/main/java/ninja/javafx/smartcsv/preferences/QuoteModeHelper.java +++ b/src/main/java/ninja/javafx/smartcsv/preferences/QuoteModeHelper.java @@ -2,7 +2,7 @@ The MIT License (MIT) ----------------------------------------------------------------------------- - Copyright (c) 2015 javafx.ninja + Copyright (c) 2015-2016 javafx.ninja Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/src/main/java/ninja/javafx/smartcsv/validation/RevalidationService.java b/src/main/java/ninja/javafx/smartcsv/validation/RevalidationService.java index 9a36af2..65b7f8b 100644 --- a/src/main/java/ninja/javafx/smartcsv/validation/RevalidationService.java +++ b/src/main/java/ninja/javafx/smartcsv/validation/RevalidationService.java @@ -2,7 +2,7 @@ The MIT License (MIT) ----------------------------------------------------------------------------- - Copyright (c) 2015 javafx.ninja + Copyright (c) 2015-2016 javafx.ninja Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/src/main/java/ninja/javafx/smartcsv/validation/ValidationConfiguration.java b/src/main/java/ninja/javafx/smartcsv/validation/ValidationConfiguration.java deleted file mode 100644 index 848c42c..0000000 --- a/src/main/java/ninja/javafx/smartcsv/validation/ValidationConfiguration.java +++ /dev/null @@ -1,176 +0,0 @@ -/* - The MIT License (MIT) - ----------------------------------------------------------------------------- - - Copyright (c) 2015 javafx.ninja - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in - all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - THE SOFTWARE. - -*/ - -package ninja.javafx.smartcsv.validation; - -import com.google.gson.annotations.SerializedName; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * validation configuration - */ -public class ValidationConfiguration { - - @SerializedName("headers") - private HeaderConfiguration headerConfiguration = new HeaderConfiguration(); - - @SerializedName("columns") - private Map> columnConfigurations = new HashMap<>(); - - public String[] headerNames() { - if (noHeader()) return null; - return headerConfiguration.getNames(); - } - - public void setHeaderNames(String[] headerNames) { - headerConfiguration.setNames(headerNames); - } - - public Boolean getUniqueRuleFor(String column) { - return (Boolean)getValue(column, "unique"); - } - - public Boolean getIntegerRuleFor(String column) { - return (Boolean)getValue(column, "integer"); - } - - public Boolean getDoubleRuleFor(String column) { - return (Boolean)getValue(column, "double"); - } - - public Boolean getNotEmptyRuleFor(String column) { - return (Boolean)getValue(column, "not empty"); - } - - public Integer getMinLengthRuleFor(String column) { - return doubleToInteger((Double)getValue(column, "minlength")); - } - - public Integer getMaxLengthRuleFor(String column) { - Double value = (Double)getValue(column, "maxlength"); - return value != null ? doubleToInteger(value) : null; - } - - public String getDateRuleFor(String column) { - return (String)getValue(column, "date"); - } - - public Boolean getAlphanumericRuleFor(String column) { - return (Boolean)getValue(column, "alphanumeric"); - } - - public String getRegexpRuleFor(String column) { - return (String)getValue(column, "regexp"); - } - - public List getValueOfRuleFor(String column) { - return (List)getValue(column, "value of"); - } - - public String getGroovyRuleFor(String column) { - return (String)getValue(column, "groovy"); - } - - public void setIntegerRuleFor(String column, Boolean value) { - setValue(column, value, "integer"); - } - - public void setDoubleRuleFor(String column, Boolean value) { - setValue(column, value, "double"); - } - - public void setNotEmptyRuleFor(String column, Boolean value) { - setValue(column, value, "not empty"); - } - - public void setUniqueRuleFor(String column, Boolean value) { - setValue(column, value, "unique"); - } - - - public void setMinLengthRuleFor(String column, Integer value) { - setValue(column, value == null ? null : value.doubleValue(), "minlength"); - } - - public void setMaxLengthRuleFor(String column, Integer value) { - setValue(column, value == null ? null : value.doubleValue(), "maxlength"); - } - - public void setDateRuleFor(String column, String value) { - setValue(column, value, "date"); - } - - public void setAlphanumericRuleFor(String column, Boolean value) { - setValue(column, value, "alphanumeric"); - } - - public void setRegexpRuleFor(String column, String value) { - setValue(column, value, "regexp"); - } - - public void setValueOfRuleFor(String column, List value) { - setValue(column, value, "value of"); - } - - public void setGroovyRuleFor(String column, String value) { - setValue(column, value, "groovy"); - } - - private void setValue(String column, Object value, String key) { - if (!columnConfigurations.containsKey(column)) { - columnConfigurations.put(column, new HashMap<>()); - } - - if (value == null && columnConfigurations.get(column).containsKey(key)) { - columnConfigurations.get(column).remove(key); - } else { - columnConfigurations.get(column).put(key, value); - } - } - - private Object getValue(String column, String key) { - if (columnConfigurations != null) { - Map rulesForColumn = columnConfigurations.get(column); - if (rulesForColumn != null) { - return columnConfigurations.get(column).get(key); - } - } - return null; - } - - private boolean noHeader() { - return headerConfiguration == null; - } - - private Integer doubleToInteger(Double value) { - if (value == null) return null; - return (int)Math.round(value); - } - -} diff --git a/src/main/java/ninja/javafx/smartcsv/validation/ValidationError.java b/src/main/java/ninja/javafx/smartcsv/validation/ValidationError.java index 7a8b487..072fae9 100644 --- a/src/main/java/ninja/javafx/smartcsv/validation/ValidationError.java +++ b/src/main/java/ninja/javafx/smartcsv/validation/ValidationError.java @@ -2,7 +2,7 @@ The MIT License (MIT) ----------------------------------------------------------------------------- - Copyright (c) 2015 javafx.ninja + Copyright (c) 2015-2016 javafx.ninja Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/src/main/java/ninja/javafx/smartcsv/validation/ValidationFileReader.java b/src/main/java/ninja/javafx/smartcsv/validation/ValidationFileReader.java index 45e8a60..f98380a 100644 --- a/src/main/java/ninja/javafx/smartcsv/validation/ValidationFileReader.java +++ b/src/main/java/ninja/javafx/smartcsv/validation/ValidationFileReader.java @@ -2,7 +2,7 @@ The MIT License (MIT) ----------------------------------------------------------------------------- - Copyright (c) 2015 javafx.ninja + Copyright (c) 2015-2016 javafx.ninja Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -28,10 +28,14 @@ package ninja.javafx.smartcsv.validation; import com.google.gson.GsonBuilder; import ninja.javafx.smartcsv.FileReader; +import ninja.javafx.smartcsv.validation.configuration.Field; +import ninja.javafx.smartcsv.validation.configuration.ValidationConfiguration; import java.io.File; import java.io.IOException; +import static ninja.javafx.smartcsv.validation.configuration.Type.*; + /** * This class loads the constraints as json config */ @@ -42,6 +46,30 @@ public class ValidationFileReader implements FileReader @Override public void read(File file) throws IOException { config = new GsonBuilder().create().fromJson(new java.io.FileReader(file), ValidationConfiguration.class); + setDefaults(); + } + + private void setDefaults() { + for (Field field : config.getFields()) { + if (field.getType() == null) { + field.setType(STRING); + } + if (field.getType() == DATE) { + if (field.getFormat() == null) { + field.setFormat("yyyy-MM-dd"); + } + } + if (field.getType() == DATETIME) { + if (field.getFormat() == null) { + field.setFormat("yyyy-MM-ddThh:mm:ssZ"); + } + } + if (field.getType() == TIME) { + if (field.getFormat() == null) { + field.setFormat("hh:mm:ss"); + } + } + } } public ValidationConfiguration getContent() { diff --git a/src/main/java/ninja/javafx/smartcsv/validation/ValidationFileWriter.java b/src/main/java/ninja/javafx/smartcsv/validation/ValidationFileWriter.java index 328fcb9..6c7a47c 100644 --- a/src/main/java/ninja/javafx/smartcsv/validation/ValidationFileWriter.java +++ b/src/main/java/ninja/javafx/smartcsv/validation/ValidationFileWriter.java @@ -2,7 +2,7 @@ The MIT License (MIT) ----------------------------------------------------------------------------- - Copyright (c) 2015 javafx.ninja + Copyright (c) 2015-2016 javafx.ninja Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -29,6 +29,7 @@ package ninja.javafx.smartcsv.validation; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import ninja.javafx.smartcsv.FileWriter; +import ninja.javafx.smartcsv.validation.configuration.ValidationConfiguration; import java.io.File; import java.io.IOException; diff --git a/src/main/java/ninja/javafx/smartcsv/validation/ValidationFormatHelper.java b/src/main/java/ninja/javafx/smartcsv/validation/ValidationFormatHelper.java new file mode 100644 index 0000000..90ade1b --- /dev/null +++ b/src/main/java/ninja/javafx/smartcsv/validation/ValidationFormatHelper.java @@ -0,0 +1,43 @@ +package ninja.javafx.smartcsv.validation; + +/** + * @author abi + */ +public class ValidationFormatHelper { + + + public static String dateFormat(String format, String defaultFormat) { + + if (format != null && !format.trim().isEmpty()) { + format = format.trim(); + if (format.startsWith("fmt:")) { + format = format.substring(4); + format = format.replace("%Y", "yyyy"); + format = format.replace("%y", "yy"); + format = format.replace("%m", "MM"); + format = format.replace("%d", "dd"); + format = format.replace("%a", "E"); + format = format.replace("%A", "EEEE"); + format = format.replace("%w", "F"); + format = format.replace("%b", "MMM"); + format = format.replace("%B", "MMMMM"); + format = format.replace("%H", "HH"); + format = format.replace("%I", "hh"); + format = format.replace("%p", "a"); + format = format.replace("%M", "mm"); + format = format.replace("%S", "ss"); + format = format.replace("%z", "Z"); + format = format.replace("%Z", "z"); + format = format.replace("%j", "DDD"); + format = format.replace("%U", "ww"); + return format; + } + } + return defaultFormat; + } + + public static Integer doubleToInteger(Double value) { + if (value == null) return null; + return (int)Math.round(value); + } +} diff --git a/src/main/java/ninja/javafx/smartcsv/validation/ValidationMessage.java b/src/main/java/ninja/javafx/smartcsv/validation/ValidationMessage.java index 732091a..984c652 100644 --- a/src/main/java/ninja/javafx/smartcsv/validation/ValidationMessage.java +++ b/src/main/java/ninja/javafx/smartcsv/validation/ValidationMessage.java @@ -2,7 +2,7 @@ The MIT License (MIT) ----------------------------------------------------------------------------- - Copyright (c) 2015 javafx.ninja + Copyright (c) 2015-2016 javafx.ninja Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/src/main/java/ninja/javafx/smartcsv/validation/Validator.java b/src/main/java/ninja/javafx/smartcsv/validation/Validator.java index cd620f4..9a9d728 100644 --- a/src/main/java/ninja/javafx/smartcsv/validation/Validator.java +++ b/src/main/java/ninja/javafx/smartcsv/validation/Validator.java @@ -2,7 +2,7 @@ The MIT License (MIT) ----------------------------------------------------------------------------- - Copyright (c) 2015 javafx.ninja + Copyright (c) 2015-2016 javafx.ninja Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -27,11 +27,19 @@ package ninja.javafx.smartcsv.validation; import ninja.javafx.smartcsv.fx.table.model.ColumnValueProvider; +import ninja.javafx.smartcsv.validation.checker.*; +import ninja.javafx.smartcsv.validation.configuration.Constraints; +import ninja.javafx.smartcsv.validation.configuration.Field; +import ninja.javafx.smartcsv.validation.configuration.ValidationConfiguration; import java.util.HashMap; import java.util.List; import java.util.Map; +import static ninja.javafx.smartcsv.validation.ValidationFormatHelper.dateFormat; +import static ninja.javafx.smartcsv.validation.configuration.StringFormat.*; +import static ninja.javafx.smartcsv.validation.configuration.Type.*; + /** * This class checks all the validations defined in the * Config against a given value @@ -134,70 +142,115 @@ public class Validator { private void initColumnValidations() { if (hasConfig()) { - String[] columns = validationConfig.headerNames(); - for (String column : columns) { + for (Field column : validationConfig.getFields()) { initializeColumnWithRules(column); } } } - private void initializeColumnWithRules(String column) { - Boolean alphaNumeric = validationConfig.getAlphanumericRuleFor(column); - if (alphaNumeric != null && alphaNumeric) { - add(column, new AlphaNumericValidation()); + private void initializeColumnWithRules(String columnName) { + if (hasConfig()) { + for (Field column : validationConfig.getFields()) { + if (column.getName().equals(columnName)) { + initializeColumnWithRules(column); + break; + } + } } - Boolean doubleRule = validationConfig.getDoubleRuleFor(column); - if (doubleRule != null && doubleRule) { - add(column, new DoubleValidation()); + } + + private void initializeColumnWithRules(Field column) { + + if (column.getType() != null) { + if (column.getType() == NUMBER) { + add(column.getName(), new DoubleValidation()); + } + + if (column.getType() == INTEGER) { + add(column.getName(), new IntegerValidation()); + } + + if (column.getType() == DATE) { + String format = dateFormat(column.getFormat(), "YYYY-MM-DD"); + add(column.getName(), new DateValidation(format)); + } + + if (column.getType() == DATETIME) { + String format = dateFormat(column.getFormat(), "YYYY-MM-DDThh:mm:ssZ"); + add(column.getName(), new DateValidation(format)); + } + + if (column.getType() == TIME) { + String format = dateFormat(column.getFormat(), "hh:mm:ss"); + add(column.getName(), new DateValidation(format)); + } + + if (column.getType() == STRING && column.getFormat().equalsIgnoreCase(EMAIL.getExternalValue())) { + add(column.getName(), new EmailValidation()); + } + + if (column.getType() == STRING && column.getFormat().equalsIgnoreCase(URI.getExternalValue())) { + add(column.getName(), new UriValidation()); + } + + if (column.getType() == STRING && column.getFormat().equalsIgnoreCase(UUID.getExternalValue())) { + add(column.getName(), new UuidValidation()); + } + + if (column.getType() == STRING && column.getFormat().equalsIgnoreCase(BINARY.getExternalValue())) { + add(column.getName(), new BinaryValidation()); + } + + if (column.getType() == STRING && column.getFormat() == null) { + columnValidationMap.get(column).remove(Validation.Type.STRING); + } } - Boolean integerRule = validationConfig.getIntegerRuleFor(column); - if (integerRule != null && integerRule) { - add(column, new IntegerValidation()); - } - - Boolean notEmptyRule = validationConfig.getNotEmptyRuleFor(column); - if (notEmptyRule != null && notEmptyRule) { - add(column, new NotEmptyValidation()); - } - - Boolean uniqueRule = validationConfig.getUniqueRuleFor(column); - if (uniqueRule != null && uniqueRule) { - add(column, new UniqueValidation(columnValueProvider, column)); - } - - String dateRule = validationConfig.getDateRuleFor(column); - if (dateRule != null && !dateRule.trim().isEmpty()) { - add(column, new DateValidation(dateRule)); - } - - Integer minLength = validationConfig.getMinLengthRuleFor(column); - if (minLength != null) { - add(column, new MinLengthValidation(minLength)); - } - - Integer maxLength = validationConfig.getMaxLengthRuleFor(column); - if (maxLength != null) { - add(column, new MaxLengthValidation(maxLength)); - } - - String regexp = validationConfig.getRegexpRuleFor(column); - if (regexp != null && !regexp.trim().isEmpty()) { - add(column, new RegExpValidation(regexp)); - } - - String groovy = validationConfig.getGroovyRuleFor(column); + String groovy = column.getGroovy(); if (groovy != null && !groovy.trim().isEmpty()) { - add(column, new GroovyValidation(groovy)); + add(column.getName(), new GroovyValidation(groovy)); } - List valueOfRule = validationConfig.getValueOfRuleFor(column); - if (valueOfRule != null && !valueOfRule.isEmpty()) { - add(column, new ValueOfValidation(valueOfRule)); + + Constraints constraints = column.getConstraints(); + if (constraints != null) { + Boolean notEmptyRule = constraints.getRequired(); + if (notEmptyRule != null && notEmptyRule) { + add(column.getName(), new NotEmptyValidation()); + } + + Boolean uniqueRule = constraints.getUnique(); + if (uniqueRule != null && uniqueRule) { + add(column.getName(), new UniqueValidation(columnValueProvider, column.getName())); + } + + Integer minLength = constraints.getMinLength(); + if (minLength != null) { + add(column.getName(), new MinLengthValidation(minLength)); + } + + Integer maxLength = constraints.getMaxLength(); + if (maxLength != null) { + add(column.getName(), new MaxLengthValidation(maxLength)); + } + + String regexp = constraints.getPattern(); + if (regexp != null && !regexp.trim().isEmpty()) { + add(column.getName(), new RegExpValidation(regexp)); + } + + + + List valueOfRule = constraints.getEnumeration(); + if (valueOfRule != null && !valueOfRule.isEmpty()) { + add(column.getName(), new ValueOfValidation(valueOfRule)); + } } } + + public ValidationError isHeaderValid(String[] headerNames) { ValidationError result = null; if (validationConfig != null) { @@ -227,4 +280,6 @@ public class Validator { } return result; } + + } diff --git a/src/main/java/ninja/javafx/smartcsv/validation/checker/BinaryValidation.java b/src/main/java/ninja/javafx/smartcsv/validation/checker/BinaryValidation.java new file mode 100644 index 0000000..73ca314 --- /dev/null +++ b/src/main/java/ninja/javafx/smartcsv/validation/checker/BinaryValidation.java @@ -0,0 +1,50 @@ +/* + The MIT License (MIT) + ----------------------------------------------------------------------------- + + Copyright (c) 2015-2016 javafx.ninja + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + +*/ +package ninja.javafx.smartcsv.validation.checker; + +import ninja.javafx.smartcsv.validation.ValidationError; + +import java.util.Base64; + +/** + * checks if the value is a base64 encoded string representing binary data + */ +public class BinaryValidation extends EmptyValueIsValid { + + @Override + public void check(int row, String value, ValidationError error) { + try { + Base64.getDecoder().decode(value); + } catch (IllegalArgumentException e) { + error.add("validation.message.binary"); + } + } + + @Override + public Type getType() { + return Type.STRING; + } +} diff --git a/src/main/java/ninja/javafx/smartcsv/validation/DateValidation.java b/src/main/java/ninja/javafx/smartcsv/validation/checker/DateValidation.java similarity index 91% rename from src/main/java/ninja/javafx/smartcsv/validation/DateValidation.java rename to src/main/java/ninja/javafx/smartcsv/validation/checker/DateValidation.java index eae88e9..9adef8d 100644 --- a/src/main/java/ninja/javafx/smartcsv/validation/DateValidation.java +++ b/src/main/java/ninja/javafx/smartcsv/validation/checker/DateValidation.java @@ -2,7 +2,7 @@ The MIT License (MIT) ----------------------------------------------------------------------------- - Copyright (c) 2015 javafx.ninja + Copyright (c) 2015-2016 javafx.ninja Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -23,7 +23,9 @@ THE SOFTWARE. */ -package ninja.javafx.smartcsv.validation; +package ninja.javafx.smartcsv.validation.checker; + +import ninja.javafx.smartcsv.validation.ValidationError; import static org.apache.commons.validator.GenericValidator.isDate; diff --git a/src/main/java/ninja/javafx/smartcsv/validation/DoubleValidation.java b/src/main/java/ninja/javafx/smartcsv/validation/checker/DoubleValidation.java similarity index 90% rename from src/main/java/ninja/javafx/smartcsv/validation/DoubleValidation.java rename to src/main/java/ninja/javafx/smartcsv/validation/checker/DoubleValidation.java index 39864b9..a98324b 100644 --- a/src/main/java/ninja/javafx/smartcsv/validation/DoubleValidation.java +++ b/src/main/java/ninja/javafx/smartcsv/validation/checker/DoubleValidation.java @@ -2,7 +2,7 @@ The MIT License (MIT) ----------------------------------------------------------------------------- - Copyright (c) 2015 javafx.ninja + Copyright (c) 2015-2016 javafx.ninja Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -23,7 +23,9 @@ THE SOFTWARE. */ -package ninja.javafx.smartcsv.validation; +package ninja.javafx.smartcsv.validation.checker; + +import ninja.javafx.smartcsv.validation.ValidationError; import static org.apache.commons.validator.GenericValidator.isDouble; diff --git a/src/main/java/ninja/javafx/smartcsv/validation/HeaderConfiguration.java b/src/main/java/ninja/javafx/smartcsv/validation/checker/EmailValidation.java similarity index 66% rename from src/main/java/ninja/javafx/smartcsv/validation/HeaderConfiguration.java rename to src/main/java/ninja/javafx/smartcsv/validation/checker/EmailValidation.java index 1bcabea..4c759db 100644 --- a/src/main/java/ninja/javafx/smartcsv/validation/HeaderConfiguration.java +++ b/src/main/java/ninja/javafx/smartcsv/validation/checker/EmailValidation.java @@ -2,8 +2,8 @@ The MIT License (MIT) ----------------------------------------------------------------------------- - Copyright (c) 2015 javafx.ninja - + Copyright (c) 2015-2016 javafx.ninja + Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights @@ -21,26 +21,27 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - + */ +package ninja.javafx.smartcsv.validation.checker; -package ninja.javafx.smartcsv.validation; - -import com.google.gson.annotations.SerializedName; +import ninja.javafx.smartcsv.validation.ValidationError; +import org.apache.commons.validator.routines.EmailValidator; /** - * header configuration for the validation + * checks if the value is a valid email address */ -public class HeaderConfiguration { +public class EmailValidation extends EmptyValueIsValid { - @SerializedName("list") - private String[] names ; - - public String[] getNames() { - return names; + @Override + public void check(int row, String value, ValidationError error) { + if (!EmailValidator.getInstance().isValid(value)) { + error.add("validation.message.email"); + } } - public void setNames(String[] names) { - this.names = names; + @Override + public Type getType() { + return Type.STRING; } } diff --git a/src/main/java/ninja/javafx/smartcsv/validation/EmptyValueIsValid.java b/src/main/java/ninja/javafx/smartcsv/validation/checker/EmptyValueIsValid.java similarity index 84% rename from src/main/java/ninja/javafx/smartcsv/validation/EmptyValueIsValid.java rename to src/main/java/ninja/javafx/smartcsv/validation/checker/EmptyValueIsValid.java index 3757a05..f7e0420 100644 --- a/src/main/java/ninja/javafx/smartcsv/validation/EmptyValueIsValid.java +++ b/src/main/java/ninja/javafx/smartcsv/validation/checker/EmptyValueIsValid.java @@ -1,4 +1,4 @@ -package ninja.javafx.smartcsv.validation; +package ninja.javafx.smartcsv.validation.checker; /** * validations based on this are not validated when the value is null or empty diff --git a/src/main/java/ninja/javafx/smartcsv/validation/GroovyValidation.java b/src/main/java/ninja/javafx/smartcsv/validation/checker/GroovyValidation.java similarity index 94% rename from src/main/java/ninja/javafx/smartcsv/validation/GroovyValidation.java rename to src/main/java/ninja/javafx/smartcsv/validation/checker/GroovyValidation.java index f169f0a..f8c3fa3 100644 --- a/src/main/java/ninja/javafx/smartcsv/validation/GroovyValidation.java +++ b/src/main/java/ninja/javafx/smartcsv/validation/checker/GroovyValidation.java @@ -2,7 +2,7 @@ The MIT License (MIT) ----------------------------------------------------------------------------- - Copyright (c) 2015 javafx.ninja + Copyright (c) 2015-2016 javafx.ninja Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -23,11 +23,12 @@ THE SOFTWARE. */ -package ninja.javafx.smartcsv.validation; +package ninja.javafx.smartcsv.validation.checker; import groovy.lang.Binding; import groovy.lang.GroovyShell; import groovy.lang.Script; +import ninja.javafx.smartcsv.validation.ValidationError; import org.codehaus.groovy.control.CompilationFailedException; /** diff --git a/src/main/java/ninja/javafx/smartcsv/validation/IntegerValidation.java b/src/main/java/ninja/javafx/smartcsv/validation/checker/IntegerValidation.java similarity index 90% rename from src/main/java/ninja/javafx/smartcsv/validation/IntegerValidation.java rename to src/main/java/ninja/javafx/smartcsv/validation/checker/IntegerValidation.java index 83ef7f9..8013dec 100644 --- a/src/main/java/ninja/javafx/smartcsv/validation/IntegerValidation.java +++ b/src/main/java/ninja/javafx/smartcsv/validation/checker/IntegerValidation.java @@ -2,7 +2,7 @@ The MIT License (MIT) ----------------------------------------------------------------------------- - Copyright (c) 2015 javafx.ninja + Copyright (c) 2015-2016 javafx.ninja Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -23,7 +23,9 @@ THE SOFTWARE. */ -package ninja.javafx.smartcsv.validation; +package ninja.javafx.smartcsv.validation.checker; + +import ninja.javafx.smartcsv.validation.ValidationError; import static org.apache.commons.validator.GenericValidator.isInt; diff --git a/src/main/java/ninja/javafx/smartcsv/validation/MaxLengthValidation.java b/src/main/java/ninja/javafx/smartcsv/validation/checker/MaxLengthValidation.java similarity index 91% rename from src/main/java/ninja/javafx/smartcsv/validation/MaxLengthValidation.java rename to src/main/java/ninja/javafx/smartcsv/validation/checker/MaxLengthValidation.java index 8e97eb3..3ad61fd 100644 --- a/src/main/java/ninja/javafx/smartcsv/validation/MaxLengthValidation.java +++ b/src/main/java/ninja/javafx/smartcsv/validation/checker/MaxLengthValidation.java @@ -2,7 +2,7 @@ The MIT License (MIT) ----------------------------------------------------------------------------- - Copyright (c) 2015 javafx.ninja + Copyright (c) 2015-2016 javafx.ninja Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -23,7 +23,9 @@ THE SOFTWARE. */ -package ninja.javafx.smartcsv.validation; +package ninja.javafx.smartcsv.validation.checker; + +import ninja.javafx.smartcsv.validation.ValidationError; import static org.apache.commons.validator.GenericValidator.maxLength; diff --git a/src/main/java/ninja/javafx/smartcsv/validation/MinLengthValidation.java b/src/main/java/ninja/javafx/smartcsv/validation/checker/MinLengthValidation.java similarity index 91% rename from src/main/java/ninja/javafx/smartcsv/validation/MinLengthValidation.java rename to src/main/java/ninja/javafx/smartcsv/validation/checker/MinLengthValidation.java index 2cbf4b0..af16ee6 100644 --- a/src/main/java/ninja/javafx/smartcsv/validation/MinLengthValidation.java +++ b/src/main/java/ninja/javafx/smartcsv/validation/checker/MinLengthValidation.java @@ -2,7 +2,7 @@ The MIT License (MIT) ----------------------------------------------------------------------------- - Copyright (c) 2015 javafx.ninja + Copyright (c) 2015-2016 javafx.ninja Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -23,7 +23,9 @@ THE SOFTWARE. */ -package ninja.javafx.smartcsv.validation; +package ninja.javafx.smartcsv.validation.checker; + +import ninja.javafx.smartcsv.validation.ValidationError; import static org.apache.commons.validator.GenericValidator.minLength; diff --git a/src/main/java/ninja/javafx/smartcsv/validation/NotEmptyValidation.java b/src/main/java/ninja/javafx/smartcsv/validation/checker/NotEmptyValidation.java similarity index 91% rename from src/main/java/ninja/javafx/smartcsv/validation/NotEmptyValidation.java rename to src/main/java/ninja/javafx/smartcsv/validation/checker/NotEmptyValidation.java index 809fbb2..aac6fc5 100644 --- a/src/main/java/ninja/javafx/smartcsv/validation/NotEmptyValidation.java +++ b/src/main/java/ninja/javafx/smartcsv/validation/checker/NotEmptyValidation.java @@ -2,7 +2,7 @@ The MIT License (MIT) ----------------------------------------------------------------------------- - Copyright (c) 2015 javafx.ninja + Copyright (c) 2015-2016 javafx.ninja Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -23,7 +23,9 @@ THE SOFTWARE. */ -package ninja.javafx.smartcsv.validation; +package ninja.javafx.smartcsv.validation.checker; + +import ninja.javafx.smartcsv.validation.ValidationError; import static org.apache.commons.validator.GenericValidator.isBlankOrNull; diff --git a/src/main/java/ninja/javafx/smartcsv/validation/RegExpValidation.java b/src/main/java/ninja/javafx/smartcsv/validation/checker/RegExpValidation.java similarity index 91% rename from src/main/java/ninja/javafx/smartcsv/validation/RegExpValidation.java rename to src/main/java/ninja/javafx/smartcsv/validation/checker/RegExpValidation.java index aa9bb27..9410f2a 100644 --- a/src/main/java/ninja/javafx/smartcsv/validation/RegExpValidation.java +++ b/src/main/java/ninja/javafx/smartcsv/validation/checker/RegExpValidation.java @@ -2,7 +2,7 @@ The MIT License (MIT) ----------------------------------------------------------------------------- - Copyright (c) 2015 javafx.ninja + Copyright (c) 2015-2016 javafx.ninja Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -23,7 +23,9 @@ THE SOFTWARE. */ -package ninja.javafx.smartcsv.validation; +package ninja.javafx.smartcsv.validation.checker; + +import ninja.javafx.smartcsv.validation.ValidationError; import static org.apache.commons.validator.GenericValidator.matchRegexp; diff --git a/src/main/java/ninja/javafx/smartcsv/validation/UniqueValidation.java b/src/main/java/ninja/javafx/smartcsv/validation/checker/UniqueValidation.java similarity index 94% rename from src/main/java/ninja/javafx/smartcsv/validation/UniqueValidation.java rename to src/main/java/ninja/javafx/smartcsv/validation/checker/UniqueValidation.java index 3a62d19..045ed9a 100644 --- a/src/main/java/ninja/javafx/smartcsv/validation/UniqueValidation.java +++ b/src/main/java/ninja/javafx/smartcsv/validation/checker/UniqueValidation.java @@ -2,7 +2,7 @@ The MIT License (MIT) ----------------------------------------------------------------------------- - Copyright (c) 2015 javafx.ninja + Copyright (c) 2015-2016 javafx.ninja Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -23,9 +23,10 @@ THE SOFTWARE. */ -package ninja.javafx.smartcsv.validation; +package ninja.javafx.smartcsv.validation.checker; import ninja.javafx.smartcsv.fx.table.model.ColumnValueProvider; +import ninja.javafx.smartcsv.validation.ValidationError; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/ninja/javafx/smartcsv/validation/checker/UriValidation.java b/src/main/java/ninja/javafx/smartcsv/validation/checker/UriValidation.java new file mode 100644 index 0000000..764e3b1 --- /dev/null +++ b/src/main/java/ninja/javafx/smartcsv/validation/checker/UriValidation.java @@ -0,0 +1,51 @@ +/* + The MIT License (MIT) + ----------------------------------------------------------------------------- + + Copyright (c) 2015-2016 javafx.ninja + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + +*/ +package ninja.javafx.smartcsv.validation.checker; + +import ninja.javafx.smartcsv.validation.ValidationError; + +import java.net.URI; +import java.net.URISyntaxException; + +/** + * checks if the value is a valid uri address + */ +public class UriValidation extends EmptyValueIsValid { + + @Override + public void check(int row, String value, ValidationError error) { + try { + new URI(value); + } catch (URISyntaxException e) { + error.add("validation.message.uri"); + } + } + + @Override + public Type getType() { + return Type.STRING; + } +} diff --git a/src/main/java/ninja/javafx/smartcsv/validation/AlphaNumericValidation.java b/src/main/java/ninja/javafx/smartcsv/validation/checker/UuidValidation.java similarity index 74% rename from src/main/java/ninja/javafx/smartcsv/validation/AlphaNumericValidation.java rename to src/main/java/ninja/javafx/smartcsv/validation/checker/UuidValidation.java index 84650dd..366866f 100644 --- a/src/main/java/ninja/javafx/smartcsv/validation/AlphaNumericValidation.java +++ b/src/main/java/ninja/javafx/smartcsv/validation/checker/UuidValidation.java @@ -2,7 +2,7 @@ The MIT License (MIT) ----------------------------------------------------------------------------- - Copyright (c) 2015 javafx.ninja + Copyright (c) 2015-2016 javafx.ninja Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -23,25 +23,28 @@ THE SOFTWARE. */ -package ninja.javafx.smartcsv.validation; +package ninja.javafx.smartcsv.validation.checker; -import static org.apache.commons.validator.GenericValidator.matchRegexp; +import ninja.javafx.smartcsv.validation.ValidationError; + +import java.util.UUID; /** - * Checks if the value is alpha numeric + * checks if the value is a valid uuid */ -public class AlphaNumericValidation extends EmptyValueIsValid { +public class UuidValidation extends EmptyValueIsValid { + @Override public void check(int row, String value, ValidationError error) { - if (!matchRegexp(value, "[0-9a-zA-Z]*")) { - error.add("validation.message.alphanumeric"); + try { + UUID.fromString(value); + } catch (IllegalArgumentException e) { + error.add("validation.message.uuid"); } } @Override public Type getType() { - return Type.ALPHANUMERIC; + return Type.STRING; } - - } diff --git a/src/main/java/ninja/javafx/smartcsv/validation/Validation.java b/src/main/java/ninja/javafx/smartcsv/validation/checker/Validation.java similarity index 86% rename from src/main/java/ninja/javafx/smartcsv/validation/Validation.java rename to src/main/java/ninja/javafx/smartcsv/validation/checker/Validation.java index 931b0ad..59af85b 100644 --- a/src/main/java/ninja/javafx/smartcsv/validation/Validation.java +++ b/src/main/java/ninja/javafx/smartcsv/validation/checker/Validation.java @@ -2,7 +2,7 @@ The MIT License (MIT) ----------------------------------------------------------------------------- - Copyright (c) 2015 javafx.ninja + Copyright (c) 2015-2016 javafx.ninja Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -23,14 +23,16 @@ THE SOFTWARE. */ -package ninja.javafx.smartcsv.validation; +package ninja.javafx.smartcsv.validation.checker; + +import ninja.javafx.smartcsv.validation.ValidationError; /** * Interface for all validations */ public interface Validation { - enum Type { NOT_EMPTY, UNIQUE, DOUBLE, INTEGER, MIN_LENGTH, MAX_LENGTH, DATE, ALPHANUMERIC, REGEXP, VALUE_OF, GROOVY } + enum Type { NOT_EMPTY, UNIQUE, DOUBLE, INTEGER, MIN_LENGTH, MAX_LENGTH, DATE, REGEXP, VALUE_OF, STRING, GROOVY } void check(int row, String value, ValidationError error); Type getType(); boolean canBeChecked(String value); diff --git a/src/main/java/ninja/javafx/smartcsv/validation/ValueOfValidation.java b/src/main/java/ninja/javafx/smartcsv/validation/checker/ValueOfValidation.java similarity index 91% rename from src/main/java/ninja/javafx/smartcsv/validation/ValueOfValidation.java rename to src/main/java/ninja/javafx/smartcsv/validation/checker/ValueOfValidation.java index cf5edb4..6304c7b 100644 --- a/src/main/java/ninja/javafx/smartcsv/validation/ValueOfValidation.java +++ b/src/main/java/ninja/javafx/smartcsv/validation/checker/ValueOfValidation.java @@ -2,7 +2,7 @@ The MIT License (MIT) ----------------------------------------------------------------------------- - Copyright (c) 2015 javafx.ninja + Copyright (c) 2015-2016 javafx.ninja Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -23,7 +23,9 @@ THE SOFTWARE. */ -package ninja.javafx.smartcsv.validation; +package ninja.javafx.smartcsv.validation.checker; + +import ninja.javafx.smartcsv.validation.ValidationError; import java.util.List; diff --git a/src/main/java/ninja/javafx/smartcsv/validation/configuration/Constraints.java b/src/main/java/ninja/javafx/smartcsv/validation/configuration/Constraints.java new file mode 100644 index 0000000..465aaf4 --- /dev/null +++ b/src/main/java/ninja/javafx/smartcsv/validation/configuration/Constraints.java @@ -0,0 +1,96 @@ +/* + The MIT License (MIT) + ----------------------------------------------------------------------------- + + Copyright (c) 2015-2016 javafx.ninja + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + +*/ + +package ninja.javafx.smartcsv.validation.configuration; + +import com.google.gson.annotations.SerializedName; + +import java.util.List; + +/** + * contraints defined in JSON Table Schema + * @see JSON Table Schema + */ +public class Constraints { + private Boolean required; + private Boolean unique; + private Integer minLength; + private Integer maxLength; + + private String pattern; + + @SerializedName("enum") + private List enumeration; + + public Boolean getRequired() { + return required; + } + + public void setRequired(Boolean required) { + this.required = required; + } + + public Boolean getUnique() { + return unique; + } + + public void setUnique(Boolean unique) { + this.unique = unique; + } + + public Integer getMinLength() { + return minLength; + } + + public void setMinLength(Integer minLength) { + this.minLength = minLength; + } + + public Integer getMaxLength() { + return maxLength; + } + + public void setMaxLength(Integer maxLength) { + this.maxLength = maxLength; + } + + public String getPattern() { + return pattern; + } + + public void setPattern(String pattern) { + this.pattern = pattern; + } + + public List getEnumeration() { + return enumeration; + } + + public void setEnumeration(List enumeration) { + this.enumeration = enumeration; + } + +} diff --git a/src/main/java/ninja/javafx/smartcsv/validation/configuration/Field.java b/src/main/java/ninja/javafx/smartcsv/validation/configuration/Field.java new file mode 100644 index 0000000..2da9d53 --- /dev/null +++ b/src/main/java/ninja/javafx/smartcsv/validation/configuration/Field.java @@ -0,0 +1,109 @@ +/* + The MIT License (MIT) + ----------------------------------------------------------------------------- + + Copyright (c) 2015-2016 javafx.ninja + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + +*/ + +package ninja.javafx.smartcsv.validation.configuration; + +/** + * this class represents a field in the configuration + * @see JSON Table Schema + */ +public class Field { + + private String name; + private String title; + private Type type; + private String description; + private String format; + private Object missingValue; + private Constraints constraints; + private String groovy; + + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public Type getType() { + return type; + } + + public void setType(Type type) { + this.type = type; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getFormat() { + return format; + } + + public void setFormat(String format) { + this.format = format; + } + + public Object getMissingValue() { + return missingValue; + } + + public void setMissingValue(Object missingValue) { + this.missingValue = missingValue; + } + + public Constraints getConstraints() { + return constraints; + } + + public void setConstraints(Constraints constraints) { + this.constraints = constraints; + } + + public String getGroovy() { + return groovy; + } + + public void setGroovy(String groovy) { + this.groovy = groovy; + } + +} diff --git a/src/main/java/ninja/javafx/smartcsv/validation/configuration/StringFormat.java b/src/main/java/ninja/javafx/smartcsv/validation/configuration/StringFormat.java new file mode 100644 index 0000000..1740118 --- /dev/null +++ b/src/main/java/ninja/javafx/smartcsv/validation/configuration/StringFormat.java @@ -0,0 +1,60 @@ +/* + The MIT License (MIT) + ----------------------------------------------------------------------------- + + Copyright (c) 2015-2016 javafx.ninja + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + +*/ + +package ninja.javafx.smartcsv.validation.configuration; + +/** + * Enumeration for format values for type string in JSON Table Schema + * @see JSON Table Schema + */ +public enum StringFormat { + DEFAULT(null), + EMAIL("email"), + URI("uri"), + BINARY("binary"), + UUID("uuid"); + + private String externalValue; + + StringFormat(String externalValue) { + this.externalValue = externalValue; + } + + public String getExternalValue() { + return externalValue; + } + + public static StringFormat fromExternalValue(String externalValue) { + if (externalValue != null) { + for (StringFormat value : StringFormat.values()) { + if (externalValue.equals(value.getExternalValue())) { + return value; + } + } + } + return DEFAULT; + } +} diff --git a/src/main/java/ninja/javafx/smartcsv/validation/configuration/Type.java b/src/main/java/ninja/javafx/smartcsv/validation/configuration/Type.java new file mode 100644 index 0000000..ab45441 --- /dev/null +++ b/src/main/java/ninja/javafx/smartcsv/validation/configuration/Type.java @@ -0,0 +1,48 @@ +/* + The MIT License (MIT) + ----------------------------------------------------------------------------- + + Copyright (c) 2015-2016 javafx.ninja + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + +*/ + +package ninja.javafx.smartcsv.validation.configuration; + +import com.google.gson.annotations.SerializedName; + +/** + * Types of JSON Table Schema + * @see JSON Table Schema + */ +public enum Type { + @SerializedName("string")STRING, + @SerializedName("integer")INTEGER, + @SerializedName("number")NUMBER, + @SerializedName("date")DATE, + @SerializedName("datetime")DATETIME, + @SerializedName("time")TIME +// TODO: currently not supported +// @SerializedName("object") OBJECT, +// @SerializedName("array") ARRAY, +// @SerializedName("duration") DURATION, +// @SerializedName("geopoint") GEOPOINT, +// @SerializedName("geojson") GEOJSON +} diff --git a/src/main/java/ninja/javafx/smartcsv/validation/configuration/ValidationConfiguration.java b/src/main/java/ninja/javafx/smartcsv/validation/configuration/ValidationConfiguration.java new file mode 100644 index 0000000..cf372d1 --- /dev/null +++ b/src/main/java/ninja/javafx/smartcsv/validation/configuration/ValidationConfiguration.java @@ -0,0 +1,81 @@ +/* + The MIT License (MIT) + ----------------------------------------------------------------------------- + + Copyright (c) 2015-2016 javafx.ninja + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + +*/ + +package ninja.javafx.smartcsv.validation.configuration; + +import com.google.gson.annotations.SerializedName; + +import java.util.ArrayList; +import java.util.List; + +/** + * Configuration based on JSON Table Schema + * @see JSON Table Schema + */ +public class ValidationConfiguration { + + @SerializedName("fields") + private Field[] fields; + + public Field[] getFields() { + return fields; + } + + public void setFields(Field[] fields) { + this.fields = fields; + } + + public Field getFieldConfiguration(String column) { + for (Field field : fields) { + if (field.getName().equals(column)) { + return field; + } + } + return null; + } + + public String[] headerNames() { + if (fields != null) { + List headerNames = new ArrayList<>(); + for (Field field : fields) { + headerNames.add(field.getName()); + } + return headerNames.toArray(new String[headerNames.size()]); + } + + return null; + } + + public void setHeaderNames(String[] header) { + fields = new Field[header.length]; + int i = 0; + for (String headerName: header) { + fields[i] = new Field(); + fields[i].setName(headerName); + i++; + } + } +} diff --git a/src/main/resources/ninja/javafx/smartcsv/fx/about/about.fxml b/src/main/resources/ninja/javafx/smartcsv/fx/about/about.fxml index 55b5cab..cddb96c 100644 --- a/src/main/resources/ninja/javafx/smartcsv/fx/about/about.fxml +++ b/src/main/resources/ninja/javafx/smartcsv/fx/about/about.fxml @@ -91,7 +91,7 @@ -