diff --git a/src/main/java/ninja/javafx/smartcsv/validation/Validator.java b/src/main/java/ninja/javafx/smartcsv/validation/Validator.java index b182442..20c745a 100644 --- a/src/main/java/ninja/javafx/smartcsv/validation/Validator.java +++ b/src/main/java/ninja/javafx/smartcsv/validation/Validator.java @@ -30,6 +30,7 @@ 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.StringFormat; import ninja.javafx.smartcsv.validation.configuration.ValidationConfiguration; import java.util.HashMap; @@ -37,6 +38,7 @@ 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.*; /** @@ -184,6 +186,22 @@ public class Validator { 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()); + } } String groovy = column.getGroovy(); 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..4b00791 --- /dev/null +++ b/src/main/java/ninja/javafx/smartcsv/validation/checker/BinaryValidation.java @@ -0,0 +1,51 @@ +/* + 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.checker; + +import ninja.javafx.smartcsv.validation.ValidationError; + +import java.util.Base64; +import java.util.UUID; + +/** + * 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.BINARY; + } +} diff --git a/src/main/java/ninja/javafx/smartcsv/validation/checker/EmailValidation.java b/src/main/java/ninja/javafx/smartcsv/validation/checker/EmailValidation.java new file mode 100644 index 0000000..991af1c --- /dev/null +++ b/src/main/java/ninja/javafx/smartcsv/validation/checker/EmailValidation.java @@ -0,0 +1,47 @@ +/* + 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.checker; + +import ninja.javafx.smartcsv.validation.ValidationError; +import org.apache.commons.validator.routines.EmailValidator; + +/** + * checks if the value is a valid email address + */ +public class EmailValidation extends EmptyValueIsValid { + + @Override + public void check(int row, String value, ValidationError error) { + if (!EmailValidator.getInstance().isValid(value)) { + error.add("validation.message.email"); + } + } + + @Override + public Type getType() { + return Type.EMAIL; + } +} 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..fc558a7 --- /dev/null +++ b/src/main/java/ninja/javafx/smartcsv/validation/checker/UriValidation.java @@ -0,0 +1,51 @@ +/* + 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.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.URI; + } +} diff --git a/src/main/java/ninja/javafx/smartcsv/validation/checker/UuidValidation.java b/src/main/java/ninja/javafx/smartcsv/validation/checker/UuidValidation.java new file mode 100644 index 0000000..ef67cd8 --- /dev/null +++ b/src/main/java/ninja/javafx/smartcsv/validation/checker/UuidValidation.java @@ -0,0 +1,52 @@ +/* + 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.checker; + +import ninja.javafx.smartcsv.validation.ValidationError; + +import java.net.URI; +import java.net.URISyntaxException; +import java.util.UUID; + +/** + * checks if the value is a valid uuid + */ +public class UuidValidation extends EmptyValueIsValid { + + @Override + public void check(int row, String value, ValidationError error) { + try { + UUID.fromString(value); + } catch (IllegalArgumentException e) { + error.add("validation.message.uuid"); + } + } + + @Override + public Type getType() { + return Type.UUID; + } +} diff --git a/src/main/java/ninja/javafx/smartcsv/validation/checker/Validation.java b/src/main/java/ninja/javafx/smartcsv/validation/checker/Validation.java index a37fddf..2cb0cf3 100644 --- a/src/main/java/ninja/javafx/smartcsv/validation/checker/Validation.java +++ b/src/main/java/ninja/javafx/smartcsv/validation/checker/Validation.java @@ -32,7 +32,7 @@ import ninja.javafx.smartcsv.validation.ValidationError; */ public interface Validation { - enum Type { NOT_EMPTY, UNIQUE, DOUBLE, INTEGER, MIN_LENGTH, MAX_LENGTH, DATE, REGEXP, VALUE_OF, GROOVY } + enum Type { NOT_EMPTY, UNIQUE, DOUBLE, INTEGER, MIN_LENGTH, MAX_LENGTH, DATE, REGEXP, VALUE_OF, EMAIL, UUID, URI, BINARY, GROOVY } void check(int row, String value, ValidationError error); Type getType(); boolean canBeChecked(String value); diff --git a/src/main/resources/ninja/javafx/smartcsv/fx/smartcsv.properties b/src/main/resources/ninja/javafx/smartcsv/fx/smartcsv.properties index a369c5a..a3fe5e9 100644 --- a/src/main/resources/ninja/javafx/smartcsv/fx/smartcsv.properties +++ b/src/main/resources/ninja/javafx/smartcsv/fx/smartcsv.properties @@ -46,6 +46,7 @@ preferences.quoteMode = Quote mode: validation.message.not.empty = should not be empty validation.message.integer = should be an integer validation.message.double = should be a double +validation.message.email = should be an email address validation.message.alphanumeric = should be alphanumeric validation.message.groovy.exception = groovy script '{0}' throws exception: {1} validation.message.groovy.return.null = groovy script '{0}' returns null @@ -55,6 +56,9 @@ validation.message.date.format = is not a date of format {0} validation.message.regexp = does not match {0} validation.message.uniqueness.multiple = value {0} is not unique (found in rows {1}) validation.message.uniqueness.single = value {0} is not unique (found in row {1}) +validation.message.uri = should be an uri +validation.message.uuid = should be an uuid +validation.message.binary = should be a base64 encoded string representing binary data validation.message.header.length = number of headers is not correct! there are {0} but there should be {1} validation.message.header.match = header number {0} does not match "{1}" should be "{2}" diff --git a/src/main/resources/ninja/javafx/smartcsv/fx/smartcsv_de.properties b/src/main/resources/ninja/javafx/smartcsv/fx/smartcsv_de.properties index aae9727..89addd2 100644 --- a/src/main/resources/ninja/javafx/smartcsv/fx/smartcsv_de.properties +++ b/src/main/resources/ninja/javafx/smartcsv/fx/smartcsv_de.properties @@ -55,6 +55,7 @@ preferences.quoteMode = Einfassungsmodus: validation.message.not.empty = Darf nicht leer sein. validation.message.integer = Muss eine Zahl sein. validation.message.double = Muss eine Gleitkommazahl sein +validation.message.email = Muss eine Email-Adresse sein validation.message.alphanumeric = Darf nur Zahlen und Buchstaben enthalten. validation.message.groovy.exception = groovy script '{0}' wirft folgenden Fehler: {1} validation.message.groovy.return.null = groovy script '{0}' meldet "null" @@ -64,6 +65,9 @@ validation.message.date.format = Das Datumsformat entspricht nicht {0} validation.message.regexp = entspricht nicht dem regul\u00e4ren Ausdruck {0} validation.message.uniqueness.multiple = Wert {0} ist nicht einmalig (gefunden in den Zeilen {1}) validation.message.uniqueness.single = Wert {0} ist nicht einmalig (gefunden in Zeile {1}) +validation.message.uri = Muss eine URI sein +validation.message.uuid = Muss eine UUID sein +validation.message.binary = Muss ein base64 encoded Text sein, der bin\u00e4re Daten enth\u00e4lt validation.message.header.length = Anzahl der \u00dcberschriften ist nicht korrekt! Es sind {0} aber es sollten {1} sein validation.message.header.match = \u00dcberschrift in Spalte {0} stimmt nicht. "{1}" sollte "{3}" sein @@ -101,5 +105,5 @@ column = Spalte format.type.DEFAULT = Standard format.type.EMAIL = Email format.type.URI = URI -format.type.BINARY = Binär +format.type.BINARY = Bin\u00e4r format.type.UUID = UUID