From: Roland Häder Date: Mon, 31 Jul 2017 20:29:03 +0000 (+0200) Subject: updated jar(s) X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=4b7c1b03e7ca11a41f21420a6dd2dd19fc761a8f;p=jcoreee.git updated jar(s) Signed-off-by: Roland Häder --- diff --git a/src/org/mxchange/jcoreee/validator/BaseObjectValidator.java b/src/org/mxchange/jcoreee/validator/BaseObjectValidator.java index 6fc1583..4f19b93 100644 --- a/src/org/mxchange/jcoreee/validator/BaseObjectValidator.java +++ b/src/org/mxchange/jcoreee/validator/BaseObjectValidator.java @@ -45,11 +45,11 @@ public abstract class BaseObjectValidator implements Validator, Serializable { * component's id has been found, it stops iteration on requiredFields * (which saves execution time). *

- * @param context FacesContext instance - * @param component UIComponent instance - * @param value Value to check + * @param context FacesContext instance + * @param component UIComponent instance + * @param value Value to check * @param requiredFields Array of required field names (ending with) - * @param allowNull Wether null or empty values are allowed + * @param allowNull Wether null or empty values are allowed *

* @throws ValidatorException If something more horrible went wrong */ @@ -58,14 +58,11 @@ public abstract class BaseObjectValidator implements Validator, Serializable { //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("preValidate: context={0},component={1},value={2},requiredFields={3} - CALLED!", context, component, value, Arrays.toString(requiredFields))); //NOI18N // Init message and key - FacesMessage facesMessage = null; + String requiredMessage = null; // Get client id final String clientId = component.getClientId(); - // Default is no field is valid - boolean isValidField = false; - // Check component's id against required fields and find a match for (final String field : requiredFields) { // Get logger @@ -73,13 +70,10 @@ public abstract class BaseObjectValidator implements Validator, Serializable { // Is it the same? if (clientId.endsWith(field)) { - // Is valid field - isValidField = true; - // Is it null? if ((!allowNull) && (null == value)) { // Value it null - facesMessage = new FacesMessage(MessageFormat.format("Field {0} is null.", field)); //NOI18N + requiredMessage = MessageFormat.format("Field {0} is null.", field); //NOI18N } // Abort here @@ -88,20 +82,10 @@ public abstract class BaseObjectValidator implements Validator, Serializable { } // Debug message - //this.getLogger().logDebug(MessageFormat.format("preValidate: isValidField={0}", isValidField)); //NOI18N - - // Valid field? - if (!isValidField) { - // Invalid field - facesMessage = new FacesMessage(MessageFormat.format("Value {0} for clientId={1} is not valid/unexpected.", value, clientId)); //NOI18N - } - - // Debug message - //this.getLogger().logDebug(MessageFormat.format("preValidate: facesMessage={0}", facesMessage)); //NOI18N - + //* NOISY-DEBUG: */ this.getLogger().logDebug(MessageFormat.format("preValidate: requiredMessage={0}", requiredMessage)); //NOI18N // Is it not null? - if (null != facesMessage) { - throw new ValidatorException(facesMessage); + if (null != requiredMessage) { + throw new ValidatorException(new FacesMessage(MessageFormat.format("Value {0} for clientId={1} is not valid/unexpected.", value, clientId))); } // Trace message diff --git a/src/org/mxchange/jcoreee/validator/bool/BaseBooleanValidator.java b/src/org/mxchange/jcoreee/validator/bool/BaseBooleanValidator.java index 0194bdb..7cbad80 100644 --- a/src/org/mxchange/jcoreee/validator/bool/BaseBooleanValidator.java +++ b/src/org/mxchange/jcoreee/validator/bool/BaseBooleanValidator.java @@ -49,8 +49,7 @@ public abstract class BaseBooleanValidator extends BaseObjectValidator implement // Get client id and init message + key String clientId = component.getClientId(); - FacesMessage facesMessage = null; - String requiredMessage; + String requiredMessage = null; // So far all fine, no check if the field is fine for (final String field : requiredFields) { @@ -65,7 +64,6 @@ public abstract class BaseBooleanValidator extends BaseObjectValidator implement requiredMessage = MessageFormat.format("Field {0} is not Boolean: {1}", field, value); //NOI18N // Value is not right type - facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, requiredMessage, requiredMessage); //NOI18N break; } @@ -98,16 +96,15 @@ public abstract class BaseBooleanValidator extends BaseObjectValidator implement } // Abort processing here - facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, requiredMessage, requiredMessage); break; } } } // Is facesMessage set? - if (null != facesMessage) { + if (null != requiredMessage) { // Abort here - throw new ValidatorException(facesMessage); + throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, requiredMessage, requiredMessage)); } } diff --git a/src/org/mxchange/jcoreee/validator/date/BaseDateValidator.java b/src/org/mxchange/jcoreee/validator/date/BaseDateValidator.java index 726ba1f..37be7f2 100644 --- a/src/org/mxchange/jcoreee/validator/date/BaseDateValidator.java +++ b/src/org/mxchange/jcoreee/validator/date/BaseDateValidator.java @@ -46,8 +46,7 @@ public abstract class BaseDateValidator extends BaseObjectValidator { // Get client id and init message + key String clientId = component.getClientId(); - FacesMessage facesMessage = null; - String requiredMessage; + String requiredMessage = null; // So far all fine, no check if the field is fine for (final String field : requiredFields) { @@ -62,7 +61,6 @@ public abstract class BaseDateValidator extends BaseObjectValidator { requiredMessage = MessageFormat.format("Field {0} is not Date: {1}", field, value); //NOI18N // Value is not right type - facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, requiredMessage, requiredMessage); //NOI18N break; } @@ -74,9 +72,9 @@ public abstract class BaseDateValidator extends BaseObjectValidator { } // Is facesMessage set? - if (null != facesMessage) { + if (null != requiredMessage) { // Abort here - throw new ValidatorException(facesMessage); + throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, requiredMessage, requiredMessage)); } } diff --git a/src/org/mxchange/jcoreee/validator/number/BaseLongValidator.java b/src/org/mxchange/jcoreee/validator/number/BaseLongValidator.java deleted file mode 100644 index d44ce31..0000000 --- a/src/org/mxchange/jcoreee/validator/number/BaseLongValidator.java +++ /dev/null @@ -1,113 +0,0 @@ -/* - * Copyright (C) 2016, 2017 Roland Häder - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.mxchange.jcoreee.validator.number; - -import java.text.MessageFormat; -import javax.faces.application.FacesMessage; -import javax.faces.component.UIComponent; -import javax.faces.context.FacesContext; -import javax.faces.validator.Validator; -import javax.faces.validator.ValidatorException; -import org.mxchange.jcoreee.validator.BaseObjectValidator; - -/** - * A general number value validator. - *

- * @author Roland Häder - */ -public abstract class BaseLongValidator extends BaseObjectValidator implements Validator { - - /** - * Serial number - */ - private static final long serialVersionUID = 25_481_878_590_589_321L; - - @Override - public void preValidate (final FacesContext context, final UIComponent component, final Object value, final String[] requiredFields, Boolean allowNull) throws ValidatorException { - // Trace message - //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("preValidate: context={0},component={1},value={2},requiredFields={3},allowNull={4} - CALLED!", context, component, value, Arrays.toString(requiredFields, allowNull))); //NOI18N - - // Pre-validate - super.preValidate(context, component, value, requiredFields, allowNull); - - // Get client id and init message + key - String clientId = component.getClientId(); - FacesMessage facesMessage = null; - String requiredMessage = null; - - // So far all fine, no check if the field is fine - for (final String field : requiredFields) { - // Debug message - //this.getLogger().logDebug(MessageFormat.format("preValidate: field={0},clientId={1}", field, clientId)); //NOI18N - - // Is it the same? - if (clientId.endsWith(field)) { - // Init variables - Integer i = null; - Long l = null; - - // Compare value's type - if (value instanceof String) { - // Is a string, try long - try { - l = Long.valueOf((String) value); - } catch (final NumberFormatException ex) { - // Cannot parse string to long - requiredMessage = MessageFormat.format("Field {0} cannot be parsed to long: {1}, exception:{2}", field, value, ex); - - // Abort processing here - break; - } - } else if (value instanceof Integer) { - // Is integer - i = (Integer) value; - } else if (value instanceof Long) { - // Is long - l = (Long) value; - } - - // Is the number below zero? - if ((!allowNull) && ((i instanceof Integer) && (i < 0))) { - // Generate message - requiredMessage = MessageFormat.format("Value {0} for field {1} is below zero.", i, field); //NOI18N - - // Abort processing here - break; - } else if ((!allowNull) && ((l instanceof Long) && (l < 0))) { - // Generate message - requiredMessage = MessageFormat.format("Value {0} for field {1} is below zero.", l, field); //NOI18N - - // Abort processing here - break; - } - } - } - - // Is requiredMessage set? - if (null != requiredMessage) { - // Embed into FacesMessage - facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, requiredMessage, requiredMessage); - } - - // Is facesMessage set? - if (null != facesMessage) { - // Abort here - throw new ValidatorException(facesMessage); - } - } - -} diff --git a/src/org/mxchange/jcoreee/validator/number/BaseNumberValidator.java b/src/org/mxchange/jcoreee/validator/number/BaseNumberValidator.java new file mode 100644 index 0000000..f5ddf22 --- /dev/null +++ b/src/org/mxchange/jcoreee/validator/number/BaseNumberValidator.java @@ -0,0 +1,96 @@ +/* + * Copyright (C) 2016, 2017 Roland Häder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.jcoreee.validator.number; + +import java.text.MessageFormat; +import javax.faces.application.FacesMessage; +import javax.faces.component.UIComponent; +import javax.faces.context.FacesContext; +import javax.faces.validator.Validator; +import javax.faces.validator.ValidatorException; +import org.mxchange.jcoreee.validator.BaseObjectValidator; + +/** + * A general number value validator. + *

+ * @author Roland Häder + */ +public abstract class BaseNumberValidator extends BaseObjectValidator implements Validator { + + /** + * Serial number + */ + private static final long serialVersionUID = 25_481_878_590_589_321L; + + @Override + public void preValidate (final FacesContext context, final UIComponent component, final Object value, final String[] requiredFields, Boolean allowNull) throws ValidatorException { + // Trace message + //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("preValidate: context={0},component={1},value={2},requiredFields={3},allowNull={4} - CALLED!", context, component, value, Arrays.toString(requiredFields, allowNull))); //NOI18N + + // Pre-validate + super.preValidate(context, component, value, requiredFields, allowNull); + + // Get client id and init message + key + String clientId = component.getClientId(); + String requiredMessage = null; + + // So far all fine, no check if the field is fine + for (final String field : requiredFields) { + // Debug message + //this.getLogger().logDebug(MessageFormat.format("preValidate: field={0},clientId={1}", field, clientId)); //NOI18N + + // Is it the same? + if (clientId.endsWith(field)) { + // Init variables + Number number = null; + + // Compare value's type, supported: String, Long, Integer + if (value instanceof String) { + // Is a string, then try to parse it as Long, largest range + try { + number = Long.valueOf((String) value); + } catch (final NumberFormatException ex) { + // Cannot parse string to long + requiredMessage = MessageFormat.format("Field {0} cannot be parsed to long: {1}, exception:{2}", field, value, ex); + + // Abort processing here + break; + } + } else if ((value instanceof Integer) || (value instanceof Long) || (value instanceof Short)) { + // Is any used number + number = (Number) value; + } + + // Is the number below zero? Casting again largest range will not result in unexpected comparison + if ((!allowNull) && ((number instanceof Number) && ((Long) number < 0))) { + // Generate message + requiredMessage = MessageFormat.format("Value {0} for field {1} is below zero.", number, field); //NOI18N + + // Abort processing here + break; + } + } + } + + // Is facesMessage set? + if (null != requiredMessage) { + // Abort here + throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, requiredMessage, requiredMessage)); + } + } + +} diff --git a/src/org/mxchange/jcoreee/validator/string/BaseStringValidator.java b/src/org/mxchange/jcoreee/validator/string/BaseStringValidator.java index c13116b..84fc781 100644 --- a/src/org/mxchange/jcoreee/validator/string/BaseStringValidator.java +++ b/src/org/mxchange/jcoreee/validator/string/BaseStringValidator.java @@ -46,7 +46,7 @@ public abstract class BaseStringValidator extends BaseObjectValidator { // Get client id and init message + key String clientId = component.getClientId(); - FacesMessage facesMessage = null; + String requiredMessage = null; // So far all fine, no check if the field is fine for (final String field : requiredFields) { @@ -58,7 +58,7 @@ public abstract class BaseStringValidator extends BaseObjectValidator { // Compare value's type if ((!allowNull) && (!(value instanceof String))) { // Value is empty - facesMessage = new FacesMessage(MessageFormat.format("Field {0} is not String: {1}", field, value)); //NOI18N + requiredMessage = MessageFormat.format("Field {0} is not String: {1}", field, value); //NOI18N } // Cast to string @@ -66,18 +66,19 @@ public abstract class BaseStringValidator extends BaseObjectValidator { // Is it empty? if (str.isEmpty()) { - // Generate message - facesMessage = new FacesMessage(MessageFormat.format("Field {0} is empty.", field)); //NOI18N + // Empty strings are currently not accepted, may indicate flaw in invoker + requiredMessage = MessageFormat.format("Field {0} is empty.", field); //NOI18N } } } // Debug message - //this.getLogger().logDebug(MessageFormat.format("preValidate: facesMessage={0}", facesMessage)); //NOI18N + //this.getLogger().logDebug(MessageFormat.format("preValidate: requiredMessage={0}", requiredMessage)); //NOI18N // Is it not null? - if (null != facesMessage) { - throw new ValidatorException(facesMessage); + if (null != requiredMessage) { + // Then there was something wrong with it + throw new ValidatorException(new FacesMessage(requiredMessage)); } // Trace message