diff options
author | Denis Anisimov <denis@vaadin.com> | 2016-08-10 11:45:40 +0300 |
---|---|---|
committer | Denis Anisimov <denis@vaadin.com> | 2016-08-10 13:34:29 +0300 |
commit | 314eb377ddc3112fbcd0b55f214ab6f0b7c606a9 (patch) | |
tree | a42cc2c6084994d3ca704d860092db57880c878e /server/src/main/java/com/vaadin/legacy | |
parent | 2fdb601126bc8d7e03c71b3f63a6550fdf07a418 (diff) | |
download | vaadin-framework-314eb377ddc3112fbcd0b55f214ab6f0b7c606a9.tar.gz vaadin-framework-314eb377ddc3112fbcd0b55f214ab6f0b7c606a9.zip |
Move old validators to legacy package (pre-#87).
Change-Id: If6212ff2b7da0e87b3c68a3372b5f0b5a2a55b1a
Diffstat (limited to 'server/src/main/java/com/vaadin/legacy')
20 files changed, 1855 insertions, 0 deletions
diff --git a/server/src/main/java/com/vaadin/legacy/data/validator/LegacyAbstractStringValidator.java b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyAbstractStringValidator.java new file mode 100644 index 0000000000..3610a01dec --- /dev/null +++ b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyAbstractStringValidator.java @@ -0,0 +1,53 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.legacy.data.validator; + +/** + * Validator base class for validating strings. + * <p> + * To include the value that failed validation in the exception message you can + * use "{0}" in the error message. This will be replaced with the failed value + * (converted to string using {@link #toString()}) or "null" if the value is + * null. + * </p> + * + * @author Vaadin Ltd. + * @since 5.4 + */ +@SuppressWarnings("serial") +public abstract class LegacyAbstractStringValidator extends LegacyAbstractValidator<String> { + + /** + * Constructs a validator for strings. + * + * <p> + * Null and empty string values are always accepted. To reject empty values, + * set the field being validated as required. + * </p> + * + * @param errorMessage + * the message to be included in an {@link InvalidValueException} + * (with "{0}" replaced by the value that failed validation). + * */ + public LegacyAbstractStringValidator(String errorMessage) { + super(errorMessage); + } + + @Override + public Class<String> getType() { + return String.class; + } +} diff --git a/server/src/main/java/com/vaadin/legacy/data/validator/LegacyAbstractValidator.java b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyAbstractValidator.java new file mode 100644 index 0000000000..a496e96663 --- /dev/null +++ b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyAbstractValidator.java @@ -0,0 +1,149 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.legacy.data.validator; + +import com.vaadin.legacy.data.Validator; + +/** + * Abstract {@link com.vaadin.legacy.data.Validator Validator} implementation that + * provides a basic Validator implementation except the + * {@link #isValidValue(Object)} method. + * <p> + * To include the value that failed validation in the exception message you can + * use "{0}" in the error message. This will be replaced with the failed value + * (converted to string using {@link #toString()}) or "null" if the value is + * null. + * </p> + * <p> + * The default implementation of AbstractValidator does not support HTML in + * error messages. To enable HTML support, override + * {@link InvalidValueException#getHtmlMessage()} and throw such exceptions from + * {@link #validate(Object)}. + * </p> + * <p> + * Since Vaadin 7, subclasses can either implement {@link #validate(Object)} + * directly or implement {@link #isValidValue(Object)} when migrating legacy + * applications. To check validity, {@link #validate(Object)} should be used. + * </p> + * + * @param <T> + * The type + * @author Vaadin Ltd. + * @since 5.4 + */ +public abstract class LegacyAbstractValidator<T> implements Validator { + + /** + * Error message that is included in an {@link InvalidValueException} if + * such is thrown. + */ + private String errorMessage; + + /** + * Constructs a validator with the given error message. + * + * @param errorMessage + * the message to be included in an {@link InvalidValueException} + * (with "{0}" replaced by the value that failed validation). + */ + public LegacyAbstractValidator(String errorMessage) { + this.errorMessage = errorMessage; + } + + /** + * Since Vaadin 7, subclasses of AbstractValidator should override + * {@link #isValidValue(Object)} or {@link #validate(Object)} instead of + * {@link #isValid(Object)}. {@link #validate(Object)} should normally be + * used to check values. + * + * @param value + * @return true if the value is valid + */ + public boolean isValid(Object value) { + try { + validate(value); + return true; + } catch (InvalidValueException e) { + return false; + } + } + + /** + * Internally check the validity of a value. This method can be used to + * perform validation in subclasses if customization of the error message is + * not needed. Otherwise, subclasses should override + * {@link #validate(Object)} and the return value of this method is ignored. + * + * This method should not be called from outside the validator class itself. + * + * @param value + * @return + */ + protected abstract boolean isValidValue(T value); + + @Override + public void validate(Object value) throws InvalidValueException { + // isValidType ensures that value can safely be cast to TYPE + if (!isValidType(value) || !isValidValue((T) value)) { + String message = getErrorMessage().replace("{0}", + String.valueOf(value)); + throw new InvalidValueException(message); + } + } + + /** + * Checks the type of the value to validate to ensure it conforms with + * getType. Enables sub classes to handle the specific type instead of + * Object. + * + * @param value + * The value to check + * @return true if the value can safely be cast to the type specified by + * {@link #getType()} + */ + protected boolean isValidType(Object value) { + if (value == null) { + return true; + } + + return getType().isAssignableFrom(value.getClass()); + } + + /** + * Returns the message to be included in the exception in case the value + * does not validate. + * + * @return the error message provided in the constructor or using + * {@link #setErrorMessage(String)}. + */ + public String getErrorMessage() { + return errorMessage; + } + + /** + * Sets the message to be included in the exception in case the value does + * not validate. The exception message is typically shown to the end user. + * + * @param errorMessage + * the error message. "{0}" is automatically replaced by the + * value that did not validate. + */ + public void setErrorMessage(String errorMessage) { + this.errorMessage = errorMessage; + } + + public abstract Class<T> getType(); +} diff --git a/server/src/main/java/com/vaadin/legacy/data/validator/LegacyBeanValidator.java b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyBeanValidator.java new file mode 100644 index 0000000000..823001a419 --- /dev/null +++ b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyBeanValidator.java @@ -0,0 +1,183 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package com.vaadin.legacy.data.validator; + +import java.io.Serializable; +import java.util.Locale; +import java.util.Set; + +import javax.validation.ConstraintViolation; +import javax.validation.MessageInterpolator.Context; +import javax.validation.Validation; +import javax.validation.ValidatorFactory; +import javax.validation.metadata.ConstraintDescriptor; + +import com.vaadin.legacy.data.Validator; + +/** + * Vaadin {@link Validator} using the JSR-303 (javax.validation) + * annotation-based bean validation. + * + * The annotations of the fields of the beans are used to determine the + * validation to perform. + * + * Note that a JSR-303 implementation (e.g. Hibernate Validator or Apache Bean + * Validation - formerly agimatec validation) must be present on the project + * classpath when using bean validation. + * + * @since 7.0 + * + * @author Petri Hakala + * @author Henri Sara + */ +public class LegacyBeanValidator implements Validator { + + private static final long serialVersionUID = 1L; + private static ValidatorFactory factory; + + private transient javax.validation.Validator javaxBeanValidator; + private String propertyName; + private Class<?> beanClass; + private Locale locale; + + /** + * Simple implementation of a message interpolator context that returns + * fixed values. + */ + protected static class SimpleContext implements Context, Serializable { + + private final Object value; + private final ConstraintDescriptor<?> descriptor; + + /** + * Create a simple immutable message interpolator context. + * + * @param value + * value being validated + * @param descriptor + * ConstraintDescriptor corresponding to the constraint being + * validated + */ + public SimpleContext(Object value, ConstraintDescriptor<?> descriptor) { + this.value = value; + this.descriptor = descriptor; + } + + @Override + public ConstraintDescriptor<?> getConstraintDescriptor() { + return descriptor; + } + + @Override + public Object getValidatedValue() { + return value; + } + + } + + /** + * Creates a Vaadin {@link Validator} utilizing JSR-303 bean validation. + * + * @param beanClass + * bean class based on which the validation should be performed + * @param propertyName + * property to validate + */ + public LegacyBeanValidator(Class<?> beanClass, String propertyName) { + this.beanClass = beanClass; + this.propertyName = propertyName; + locale = Locale.getDefault(); + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.data.Validator#validate(java.lang.Object) + */ + @Override + public void validate(final Object value) throws InvalidValueException { + Set<?> violations = getJavaxBeanValidator().validateValue(beanClass, + propertyName, value); + if (violations.size() > 0) { + InvalidValueException[] causes = new InvalidValueException[violations + .size()]; + int i = 0; + for (Object v : violations) { + final ConstraintViolation<?> violation = (ConstraintViolation<?>) v; + String msg = getJavaxBeanValidatorFactory() + .getMessageInterpolator().interpolate( + violation.getMessageTemplate(), + new SimpleContext(value, violation + .getConstraintDescriptor()), locale); + causes[i] = new InvalidValueException(msg); + ++i; + } + + throw new InvalidValueException(null, causes); + } + } + + /** + * Sets the locale used for validation error messages. + * + * Revalidation is not automatically triggered by setting the locale. + * + * @param locale + */ + public void setLocale(Locale locale) { + this.locale = locale; + } + + /** + * Gets the locale used for validation error messages. + * + * @return locale used for validation + */ + public Locale getLocale() { + return locale; + } + + /** + * Returns the underlying JSR-303 bean validator factory used. A factory is + * created using {@link Validation} if necessary. + * + * @return {@link ValidatorFactory} to use + */ + protected static ValidatorFactory getJavaxBeanValidatorFactory() { + if (factory == null) { + factory = Validation.buildDefaultValidatorFactory(); + } + + return factory; + } + + /** + * Returns a shared Validator instance to use. An instance is created using + * the validator factory if necessary and thereafter reused by the + * {@link LegacyBeanValidator} instance. + * + * @return the JSR-303 {@link javax.validation.Validator} to use + */ + protected javax.validation.Validator getJavaxBeanValidator() { + if (javaxBeanValidator == null) { + javaxBeanValidator = getJavaxBeanValidatorFactory().getValidator(); + } + + return javaxBeanValidator; + } + +} diff --git a/server/src/main/java/com/vaadin/legacy/data/validator/LegacyBigDecimalRangeValidator.java b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyBigDecimalRangeValidator.java new file mode 100644 index 0000000000..46d8c99437 --- /dev/null +++ b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyBigDecimalRangeValidator.java @@ -0,0 +1,50 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.legacy.data.validator; + +import java.math.BigDecimal; + +/** + * Validator for validating that an {@link BigDecimal} is inside a given range. + * + * @author Vaadin Ltd. + * @since 7.4 + */ +@SuppressWarnings("serial") +public class LegacyBigDecimalRangeValidator extends LegacyRangeValidator<BigDecimal> { + + /** + * Creates a validator for checking that an BigDecimal is within a given + * range. + * + * By default the range is inclusive i.e. both minValue and maxValue are + * valid values. Use {@link #setMinValueIncluded(boolean)} or + * {@link #setMaxValueIncluded(boolean)} to change it. + * + * + * @param errorMessage + * the message to display in case the value does not validate. + * @param minValue + * The minimum value to accept or null for no limit + * @param maxValue + * The maximum value to accept or null for no limit + */ + public LegacyBigDecimalRangeValidator(String errorMessage, BigDecimal minValue, + BigDecimal maxValue) { + super(errorMessage, BigDecimal.class, minValue, maxValue); + } + +} diff --git a/server/src/main/java/com/vaadin/legacy/data/validator/LegacyBigIntegerRangeValidator.java b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyBigIntegerRangeValidator.java new file mode 100644 index 0000000000..4e11a65c78 --- /dev/null +++ b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyBigIntegerRangeValidator.java @@ -0,0 +1,50 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.legacy.data.validator; + +import java.math.BigInteger; + +/** + * Validator for validating that an {@link BigInteger} is inside a given range. + * + * @author Vaadin Ltd. + * @since 7.4 + */ +@SuppressWarnings("serial") +public class LegacyBigIntegerRangeValidator extends LegacyRangeValidator<BigInteger> { + + /** + * Creates a validator for checking that an BigInteger is within a given + * range. + * + * By default the range is inclusive i.e. both minValue and maxValue are + * valid values. Use {@link #setMinValueIncluded(boolean)} or + * {@link #setMaxValueIncluded(boolean)} to change it. + * + * + * @param errorMessage + * the message to display in case the value does not validate. + * @param minValue + * The minimum value to accept or null for no limit + * @param maxValue + * The maximum value to accept or null for no limit + */ + public LegacyBigIntegerRangeValidator(String errorMessage, BigInteger minValue, + BigInteger maxValue) { + super(errorMessage, BigInteger.class, minValue, maxValue); + } + +} diff --git a/server/src/main/java/com/vaadin/legacy/data/validator/LegacyByteRangeValidator.java b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyByteRangeValidator.java new file mode 100644 index 0000000000..1f795d1e42 --- /dev/null +++ b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyByteRangeValidator.java @@ -0,0 +1,46 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.legacy.data.validator; + +/** + * Validator for validating that an {@link Byte} is inside a given range. + * + * @author Vaadin Ltd. + * @since 7.4 + */ +@SuppressWarnings("serial") +public class LegacyByteRangeValidator extends LegacyRangeValidator<Byte> { + + /** + * Creates a validator for checking that an Byte is within a given range. + * + * By default the range is inclusive i.e. both minValue and maxValue are + * valid values. Use {@link #setMinValueIncluded(boolean)} or + * {@link #setMaxValueIncluded(boolean)} to change it. + * + * + * @param errorMessage + * the message to display in case the value does not validate. + * @param minValue + * The minimum value to accept or null for no limit + * @param maxValue + * The maximum value to accept or null for no limit + */ + public LegacyByteRangeValidator(String errorMessage, Byte minValue, Byte maxValue) { + super(errorMessage, Byte.class, minValue, maxValue); + } + +} diff --git a/server/src/main/java/com/vaadin/legacy/data/validator/LegacyCompositeValidator.java b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyCompositeValidator.java new file mode 100644 index 0000000000..04d6045396 --- /dev/null +++ b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyCompositeValidator.java @@ -0,0 +1,270 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package com.vaadin.legacy.data.validator; + +import java.util.Collection; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; + +import com.vaadin.legacy.data.Validator; + +/** + * The <code>CompositeValidator</code> allows you to chain (compose) many + * validators to validate one field. The contained validators may be required to + * all validate the value to validate or it may be enough that one contained + * validator validates the value. This behaviour is controlled by the modes + * <code>AND</code> and <code>OR</code>. + * + * @author Vaadin Ltd. + * @since 3.0 + */ +@SuppressWarnings("serial") +public class LegacyCompositeValidator implements Validator { + + public enum CombinationMode { + /** + * The validators are combined with <code>AND</code> clause: validity of + * the composite implies validity of the all validators it is composed + * of must be valid. + */ + AND, + /** + * The validators are combined with <code>OR</code> clause: validity of + * the composite implies that some of validators it is composed of must + * be valid. + */ + OR; + } + + /** + * @deprecated As of 7.0, use {@link CombinationMode#AND} instead + */ + @Deprecated + public static final CombinationMode MODE_AND = CombinationMode.AND; + /** + * @deprecated As of 7.0, use {@link CombinationMode#OR} instead + */ + @Deprecated + public static final CombinationMode MODE_OR = CombinationMode.OR; + + private String errorMessage; + + /** + * Operation mode. + */ + private CombinationMode mode = CombinationMode.AND; + + /** + * List of contained validators. + */ + private final List<Validator> validators = new LinkedList<Validator>(); + + /** + * Construct a composite validator in <code>AND</code> mode without error + * message. + */ + public LegacyCompositeValidator() { + this(CombinationMode.AND, ""); + } + + /** + * Constructs a composite validator in given mode. + * + * @param mode + * @param errorMessage + */ + public LegacyCompositeValidator(CombinationMode mode, String errorMessage) { + setErrorMessage(errorMessage); + setMode(mode); + } + + /** + * Validates the given value. + * <p> + * The value is valid, if: + * <ul> + * <li><code>MODE_AND</code>: All of the sub-validators are valid + * <li><code>MODE_OR</code>: Any of the sub-validators are valid + * </ul> + * + * If the value is invalid, validation error is thrown. If the error message + * is set (non-null), it is used. If the error message has not been set, the + * first error occurred is thrown. + * </p> + * + * @param value + * the value to check. + * @throws Validator.InvalidValueException + * if the value is not valid. + */ + @Override + public void validate(Object value) throws Validator.InvalidValueException { + switch (mode) { + case AND: + for (Validator validator : validators) { + validator.validate(value); + } + return; + + case OR: + Validator.InvalidValueException first = null; + for (Validator v : validators) { + try { + v.validate(value); + return; + } catch (final Validator.InvalidValueException e) { + if (first == null) { + first = e; + } + } + } + if (first == null) { + return; + } + final String em = getErrorMessage(); + if (em != null) { + throw new Validator.InvalidValueException(em); + } else { + throw first; + } + } + } + + /** + * Gets the mode of the validator. + * + * @return Operation mode of the validator: {@link CombinationMode#AND} or + * {@link CombinationMode#OR}. + */ + public final CombinationMode getMode() { + return mode; + } + + /** + * Sets the mode of the validator. The valid modes are: + * <ul> + * <li>{@link CombinationMode#AND} (default) + * <li>{@link CombinationMode#OR} + * </ul> + * + * @param mode + * the mode to set. + */ + public void setMode(CombinationMode mode) { + if (mode == null) { + throw new IllegalArgumentException( + "The validator can't be set to null"); + } + this.mode = mode; + } + + /** + * Gets the error message for the composite validator. If the error message + * is null, original error messages of the sub-validators are used instead. + */ + public String getErrorMessage() { + if (errorMessage != null) { + return errorMessage; + } + + // TODO Return composite error message + + return null; + } + + /** + * Adds validator to the interface. + * + * @param validator + * the Validator object which performs validation checks on this + * set of data field values. + */ + public void addValidator(Validator validator) { + if (validator == null) { + return; + } + validators.add(validator); + } + + /** + * Removes a validator from the composite. + * + * @param validator + * the Validator object which performs validation checks on this + * set of data field values. + */ + public void removeValidator(Validator validator) { + validators.remove(validator); + } + + /** + * Gets sub-validators by class. + * + * <p> + * If the component contains directly or recursively (it contains another + * composite containing the validator) validators compatible with given type + * they are returned. This only applies to <code>AND</code> mode composite + * validators. + * </p> + * + * <p> + * If the validator is in <code>OR</code> mode or does not contain any + * validators of given type null is returned. + * </p> + * + * @param validatorType + * The type of validators to return + * + * @return Collection<Validator> of validators compatible with given type + * that must apply or null if none found. + */ + public Collection<Validator> getSubValidators(Class validatorType) { + if (mode != CombinationMode.AND) { + return null; + } + + final HashSet<Validator> found = new HashSet<Validator>(); + for (Validator v : validators) { + if (validatorType.isAssignableFrom(v.getClass())) { + found.add(v); + } + if (v instanceof LegacyCompositeValidator + && ((LegacyCompositeValidator) v).getMode() == MODE_AND) { + final Collection<Validator> c = ((LegacyCompositeValidator) v) + .getSubValidators(validatorType); + if (c != null) { + found.addAll(c); + } + } + } + + return found.isEmpty() ? null : found; + } + + /** + * Sets the message to be included in the exception in case the value does + * not validate. The exception message is typically shown to the end user. + * + * @param errorMessage + * the error message. + */ + public void setErrorMessage(String errorMessage) { + this.errorMessage = errorMessage; + } + +} diff --git a/server/src/main/java/com/vaadin/legacy/data/validator/LegacyDateRangeValidator.java b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyDateRangeValidator.java new file mode 100644 index 0000000000..ad44353435 --- /dev/null +++ b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyDateRangeValidator.java @@ -0,0 +1,61 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.legacy.data.validator; + +import java.util.Date; + +import com.vaadin.shared.ui.datefield.Resolution; + +/** + * Validator for validating that a Date is inside a given range. + * + * <p> + * Note that the comparison is done directly on the Date object so take care + * that the hours/minutes/seconds/milliseconds of the min/max values are + * properly set. + * </p> + * + * @author Vaadin Ltd. + * @since 7.0 + */ +public class LegacyDateRangeValidator extends LegacyRangeValidator<Date> { + + /** + * Creates a validator for checking that an Date is within a given range. + * <p> + * By default the range is inclusive i.e. both minValue and maxValue are + * valid values. Use {@link #setMinValueIncluded(boolean)} or + * {@link #setMaxValueIncluded(boolean)} to change it. + * </p> + * <p> + * Note that the comparison is done directly on the Date object so take care + * that the hours/minutes/seconds/milliseconds of the min/max values are + * properly set. + * </p> + * + * @param errorMessage + * the message to display in case the value does not validate. + * @param minValue + * The minimum value to accept or null for no limit + * @param maxValue + * The maximum value to accept or null for no limit + */ + public LegacyDateRangeValidator(String errorMessage, Date minValue, + Date maxValue, Resolution resolution) { + super(errorMessage, Date.class, minValue, maxValue); + } + +} diff --git a/server/src/main/java/com/vaadin/legacy/data/validator/LegacyDoubleRangeValidator.java b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyDoubleRangeValidator.java new file mode 100644 index 0000000000..0110cbffff --- /dev/null +++ b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyDoubleRangeValidator.java @@ -0,0 +1,47 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.legacy.data.validator; + +/** + * Validator for validating that a {@link Double} is inside a given range. + * + * @author Vaadin Ltd. + * @since 7.0 + */ +@SuppressWarnings("serial") +public class LegacyDoubleRangeValidator extends LegacyRangeValidator<Double> { + + /** + * Creates a validator for checking that an Double is within a given range. + * + * By default the range is inclusive i.e. both minValue and maxValue are + * valid values. Use {@link #setMinValueIncluded(boolean)} or + * {@link #setMaxValueIncluded(boolean)} to change it. + * + * + * @param errorMessage + * the message to display in case the value does not validate. + * @param minValue + * The minimum value to accept or null for no limit + * @param maxValue + * The maximum value to accept or null for no limit + */ + public LegacyDoubleRangeValidator(String errorMessage, Double minValue, + Double maxValue) { + super(errorMessage, Double.class, minValue, maxValue); + } + +} diff --git a/server/src/main/java/com/vaadin/legacy/data/validator/LegacyDoubleValidator.java b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyDoubleValidator.java new file mode 100644 index 0000000000..0c804e7247 --- /dev/null +++ b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyDoubleValidator.java @@ -0,0 +1,73 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.legacy.data.validator; + +import com.vaadin.data.Property; +import com.vaadin.data.util.converter.StringToDoubleConverter; + +/** + * String validator for a double precision floating point number. See + * {@link com.vaadin.legacy.data.validator.LegacyAbstractStringValidator} for more + * information. + * + * @author Vaadin Ltd. + * @since 5.4 + * @deprecated As of 7.0. Use a {@link StringToDoubleConverter} converter on the + * field instead or bind the field to a {@link Property} of type + * {@link Double}. + */ +@Deprecated +@SuppressWarnings("serial") +public class LegacyDoubleValidator extends LegacyAbstractStringValidator { + + /** + * Creates a validator for checking that a string can be parsed as an + * double. + * + * @param errorMessage + * the message to display in case the value does not validate. + * @deprecated As of 7.0. Use a Double converter on the field instead and/or + * use a {@link LegacyDoubleRangeValidator} for validating that the + * value is inside a given range. + */ + @Deprecated + public LegacyDoubleValidator(String errorMessage) { + super(errorMessage); + } + + @Override + protected boolean isValidValue(String value) { + try { + Double.parseDouble(value); + return true; + } catch (Exception e) { + return false; + } + } + + @Override + public void validate(Object value) throws InvalidValueException { + if (value != null && value instanceof Double) { + // Allow Doubles to pass through the validator for easier + // migration. Otherwise a TextField connected to an double property + // with a DoubleValidator will fail. + return; + } + + super.validate(value); + } + +} diff --git a/server/src/main/java/com/vaadin/legacy/data/validator/LegacyEmailValidator.java b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyEmailValidator.java new file mode 100644 index 0000000000..d6a7cdd485 --- /dev/null +++ b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyEmailValidator.java @@ -0,0 +1,50 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.legacy.data.validator; + +/** + * String validator for e-mail addresses. The e-mail address syntax is not + * complete according to RFC 822 but handles the vast majority of valid e-mail + * addresses correctly. + * + * See {@link com.vaadin.legacy.data.validator.LegacyAbstractStringValidator} for more + * information. + * + * <p> + * An empty string or a null is always accepted - use the required flag on + * fields or a separate validator (or override {@link #isValidValue(String)}) to + * fail on empty values. + * </p> + * + * @author Vaadin Ltd. + * @since 5.4 + */ +@SuppressWarnings("serial") +public class LegacyEmailValidator extends LegacyRegexpValidator { + + /** + * Creates a validator for checking that a string is a syntactically valid + * e-mail address. + * + * @param errorMessage + * the message to display in case the value does not validate. + */ + public LegacyEmailValidator(String errorMessage) { + super( + "^([a-zA-Z0-9_\\.\\-+])+@(([a-zA-Z0-9-])+\\.)+([a-zA-Z0-9]{2,4})+$", + true, errorMessage); + } +} diff --git a/server/src/main/java/com/vaadin/legacy/data/validator/LegacyFloatRangeValidator.java b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyFloatRangeValidator.java new file mode 100644 index 0000000000..6bd5a53778 --- /dev/null +++ b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyFloatRangeValidator.java @@ -0,0 +1,47 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.legacy.data.validator; + +/** + * Validator for validating that a {@link Float} is inside a given range. + * + * @author Vaadin Ltd. + * @since 7.4 + */ +@SuppressWarnings("serial") +public class LegacyFloatRangeValidator extends LegacyRangeValidator<Float> { + + /** + * Creates a validator for checking that an Float is within a given range. + * + * By default the range is inclusive i.e. both minValue and maxValue are + * valid values. Use {@link #setMinValueIncluded(boolean)} or + * {@link #setMaxValueIncluded(boolean)} to change it. + * + * + * @param errorMessage + * the message to display in case the value does not validate. + * @param minValue + * The minimum value to accept or null for no limit + * @param maxValue + * The maximum value to accept or null for no limit + */ + public LegacyFloatRangeValidator(String errorMessage, Float minValue, + Float maxValue) { + super(errorMessage, Float.class, minValue, maxValue); + } + +} diff --git a/server/src/main/java/com/vaadin/legacy/data/validator/LegacyIntegerRangeValidator.java b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyIntegerRangeValidator.java new file mode 100644 index 0000000000..272c1bd40a --- /dev/null +++ b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyIntegerRangeValidator.java @@ -0,0 +1,47 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.legacy.data.validator; + +/** + * Validator for validating that an {@link Integer} is inside a given range. + * + * @author Vaadin Ltd. + * @since 5.4 + */ +@SuppressWarnings("serial") +public class LegacyIntegerRangeValidator extends LegacyRangeValidator<Integer> { + + /** + * Creates a validator for checking that an Integer is within a given range. + * + * By default the range is inclusive i.e. both minValue and maxValue are + * valid values. Use {@link #setMinValueIncluded(boolean)} or + * {@link #setMaxValueIncluded(boolean)} to change it. + * + * + * @param errorMessage + * the message to display in case the value does not validate. + * @param minValue + * The minimum value to accept or null for no limit + * @param maxValue + * The maximum value to accept or null for no limit + */ + public LegacyIntegerRangeValidator(String errorMessage, Integer minValue, + Integer maxValue) { + super(errorMessage, Integer.class, minValue, maxValue); + } + +} diff --git a/server/src/main/java/com/vaadin/legacy/data/validator/LegacyIntegerValidator.java b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyIntegerValidator.java new file mode 100644 index 0000000000..e6c2bae815 --- /dev/null +++ b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyIntegerValidator.java @@ -0,0 +1,73 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.legacy.data.validator; + +import com.vaadin.data.Property; +import com.vaadin.data.util.converter.StringToIntegerConverter; + +/** + * String validator for integers. See + * {@link com.vaadin.legacy.data.validator.LegacyAbstractStringValidator} for more + * information. + * + * @author Vaadin Ltd. + * @since 5.4 + * @deprecated As of 7.0. Use a {@link StringToIntegerConverter} converter on + * the field instead or bind the field to a {@link Property} of type + * {@link Integer}. + */ +@SuppressWarnings("serial") +@Deprecated +public class LegacyIntegerValidator extends LegacyAbstractStringValidator { + + /** + * Creates a validator for checking that a string can be parsed as an + * integer. + * + * @param errorMessage + * the message to display in case the value does not validate. + * @deprecated As of 7.0. Use an Integer converter on the field instead + * and/or use an {@link LegacyIntegerRangeValidator} for validating + * that the value is inside a given range. + */ + @Deprecated + public LegacyIntegerValidator(String errorMessage) { + super(errorMessage); + + } + + @Override + protected boolean isValidValue(String value) { + try { + Integer.parseInt(value); + return true; + } catch (Exception e) { + return false; + } + } + + @Override + public void validate(Object value) throws InvalidValueException { + if (value != null && value instanceof Integer) { + // Allow Integers to pass through the validator for easier + // migration. Otherwise a TextField connected to an integer property + // with an IntegerValidator will fail. + return; + } + + super.validate(value); + } +} diff --git a/server/src/main/java/com/vaadin/legacy/data/validator/LegacyLongRangeValidator.java b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyLongRangeValidator.java new file mode 100644 index 0000000000..4fb38de459 --- /dev/null +++ b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyLongRangeValidator.java @@ -0,0 +1,46 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.legacy.data.validator; + +/** + * Validator for validating that an {@link Long} is inside a given range. + * + * @author Vaadin Ltd. + * @since 7.4 + */ +@SuppressWarnings("serial") +public class LegacyLongRangeValidator extends LegacyRangeValidator<Long> { + + /** + * Creates a validator for checking that an Long is within a given range. + * + * By default the range is inclusive i.e. both minValue and maxValue are + * valid values. Use {@link #setMinValueIncluded(boolean)} or + * {@link #setMaxValueIncluded(boolean)} to change it. + * + * + * @param errorMessage + * the message to display in case the value does not validate. + * @param minValue + * The minimum value to accept or null for no limit + * @param maxValue + * The maximum value to accept or null for no limit + */ + public LegacyLongRangeValidator(String errorMessage, Long minValue, Long maxValue) { + super(errorMessage, Long.class, minValue, maxValue); + } + +} diff --git a/server/src/main/java/com/vaadin/legacy/data/validator/LegacyNullValidator.java b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyNullValidator.java new file mode 100644 index 0000000000..0df18cb461 --- /dev/null +++ b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyNullValidator.java @@ -0,0 +1,102 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package com.vaadin.legacy.data.validator; + +import com.vaadin.legacy.data.Validator; + +/** + * This validator is used for validating properties that do or do not allow null + * values. By default, nulls are not allowed. + * + * @author Vaadin Ltd. + * @since 3.0 + */ +@SuppressWarnings("serial") +public class LegacyNullValidator implements Validator { + + private boolean onlyNullAllowed; + + private String errorMessage; + + /** + * Creates a new NullValidator. + * + * @param errorMessage + * the error message to display on invalidation. + * @param onlyNullAllowed + * Are only nulls allowed? + */ + public LegacyNullValidator(String errorMessage, boolean onlyNullAllowed) { + setErrorMessage(errorMessage); + setNullAllowed(onlyNullAllowed); + } + + /** + * Validates the data given in value. + * + * @param value + * the value to validate. + * @throws Validator.InvalidValueException + * if the value was invalid. + */ + @Override + public void validate(Object value) throws Validator.InvalidValueException { + if ((onlyNullAllowed && value != null) + || (!onlyNullAllowed && value == null)) { + throw new Validator.InvalidValueException(errorMessage); + } + } + + /** + * Returns <code>true</code> if nulls are allowed otherwise + * <code>false</code>. + */ + public final boolean isNullAllowed() { + return onlyNullAllowed; + } + + /** + * Sets if nulls (and only nulls) are to be allowed. + * + * @param onlyNullAllowed + * If true, only nulls are allowed. If false only non-nulls are + * allowed. Do we allow nulls? + */ + public void setNullAllowed(boolean onlyNullAllowed) { + this.onlyNullAllowed = onlyNullAllowed; + } + + /** + * Gets the error message that is displayed in case the value is invalid. + * + * @return the Error Message. + */ + public String getErrorMessage() { + return errorMessage; + } + + /** + * Sets the error message to be displayed on invalid value. + * + * @param errorMessage + * the Error Message to set. + */ + public void setErrorMessage(String errorMessage) { + this.errorMessage = errorMessage; + } + +} diff --git a/server/src/main/java/com/vaadin/legacy/data/validator/LegacyRangeValidator.java b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyRangeValidator.java new file mode 100644 index 0000000000..d9106e9bc6 --- /dev/null +++ b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyRangeValidator.java @@ -0,0 +1,197 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.legacy.data.validator; + +/** + * An base implementation for validating any objects that implement + * {@link Comparable}. + * + * Verifies that the value is of the given type and within the (optionally) + * given limits. Typically you want to use a sub class of this like + * {@link LegacyIntegerRangeValidator}, {@link LegacyDoubleRangeValidator} or + * {@link LegacyDateRangeValidator} in applications. + * <p> + * Note that {@link LegacyRangeValidator} always accept null values. Make a field + * required to ensure that no empty values are accepted or override + * {@link #isValidValue(Comparable)}. + * </p> + * + * @param <T> + * The type of Number to validate. Must implement Comparable so that + * minimum and maximum checks work. + * @author Vaadin Ltd. + * @since 7.0 + */ +public class LegacyRangeValidator<T extends Comparable> extends LegacyAbstractValidator<T> { + + private T minValue = null; + private boolean minValueIncluded = true; + private T maxValue = null; + private boolean maxValueIncluded = true; + private Class<T> type; + + /** + * Creates a new range validator of the given type. + * + * @param errorMessage + * The error message to use if validation fails + * @param type + * The type of object the validator can validate. + * @param minValue + * The minimum value that should be accepted or null for no limit + * @param maxValue + * The maximum value that should be accepted or null for no limit + */ + public LegacyRangeValidator(String errorMessage, Class<T> type, T minValue, + T maxValue) { + super(errorMessage); + this.type = type; + this.minValue = minValue; + this.maxValue = maxValue; + } + + /** + * Checks if the minimum value is part of the accepted range + * + * @return true if the minimum value is part of the range, false otherwise + */ + public boolean isMinValueIncluded() { + return minValueIncluded; + } + + /** + * Sets if the minimum value is part of the accepted range + * + * @param minValueIncluded + * true if the minimum value should be part of the range, false + * otherwise + */ + public void setMinValueIncluded(boolean minValueIncluded) { + this.minValueIncluded = minValueIncluded; + } + + /** + * Checks if the maximum value is part of the accepted range + * + * @return true if the maximum value is part of the range, false otherwise + */ + public boolean isMaxValueIncluded() { + return maxValueIncluded; + } + + /** + * Sets if the maximum value is part of the accepted range + * + * @param maxValueIncluded + * true if the maximum value should be part of the range, false + * otherwise + */ + public void setMaxValueIncluded(boolean maxValueIncluded) { + this.maxValueIncluded = maxValueIncluded; + } + + /** + * Gets the minimum value of the range + * + * @return the minimum value + */ + public T getMinValue() { + return minValue; + } + + /** + * Sets the minimum value of the range. Use + * {@link #setMinValueIncluded(boolean)} to control whether this value is + * part of the range or not. + * + * @param minValue + * the minimum value + */ + public void setMinValue(T minValue) { + this.minValue = minValue; + } + + /** + * Gets the maximum value of the range + * + * @return the maximum value + */ + public T getMaxValue() { + return maxValue; + } + + /** + * Sets the maximum value of the range. Use + * {@link #setMaxValueIncluded(boolean)} to control whether this value is + * part of the range or not. + * + * @param maxValue + * the maximum value + */ + public void setMaxValue(T maxValue) { + this.maxValue = maxValue; + } + + /* + * (non-Javadoc) + * + * @see + * com.vaadin.data.validator.AbstractValidator#isValidValue(java.lang.Object + * ) + */ + @Override + protected boolean isValidValue(T value) { + if (value == null + || (String.class.equals(getType()) && "".equals(value))) { + return true; + } + + if (getMinValue() != null) { + // Ensure that the min limit is ok + int result = value.compareTo(getMinValue()); + if (result < 0) { + // value less than min value + return false; + } else if (result == 0 && !isMinValueIncluded()) { + // values equal and min value not included + return false; + } + } + if (getMaxValue() != null) { + // Ensure that the Max limit is ok + int result = value.compareTo(getMaxValue()); + if (result > 0) { + // value greater than max value + return false; + } else if (result == 0 && !isMaxValueIncluded()) { + // values equal and max value not included + return false; + } + } + return true; + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.data.validator.AbstractValidator#getType() + */ + @Override + public Class<T> getType() { + return type; + } + +} diff --git a/server/src/main/java/com/vaadin/legacy/data/validator/LegacyRegexpValidator.java b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyRegexpValidator.java new file mode 100644 index 0000000000..0bc93f44b2 --- /dev/null +++ b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyRegexpValidator.java @@ -0,0 +1,115 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.legacy.data.validator; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * String validator comparing the string against a Java regular expression. Both + * complete matches and substring matches are supported. + * + * <p> + * For the Java regular expression syntax, see + * {@link java.util.regex.Pattern#sum} + * </p> + * <p> + * See {@link com.vaadin.legacy.data.validator.LegacyAbstractStringValidator} for more + * information. + * </p> + * <p> + * An empty string or a null is always accepted - use the required flag on + * fields or a separate validator (or override {@link #isValidValue(String)}) to + * fail on empty values. + * </p> + * + * @author Vaadin Ltd. + * @since 5.4 + */ +@SuppressWarnings("serial") +public class LegacyRegexpValidator extends LegacyAbstractStringValidator { + + private Pattern pattern; + private boolean complete; + private transient Matcher matcher = null; + + /** + * Creates a validator for checking that the regular expression matches the + * complete string to validate. + * + * @param regexp + * a Java regular expression + * @param errorMessage + * the message to display in case the value does not validate. + */ + public LegacyRegexpValidator(String regexp, String errorMessage) { + this(regexp, true, errorMessage); + } + + /** + * Creates a validator for checking that the regular expression matches the + * string to validate. + * + * @param regexp + * a Java regular expression + * @param complete + * true to use check for a complete match, false to look for a + * matching substring + * @param errorMessage + * the message to display in case the value does not validate. + */ + public LegacyRegexpValidator(String regexp, boolean complete, String errorMessage) { + super(errorMessage); + pattern = Pattern.compile(regexp); + this.complete = complete; + } + + /* + * (non-Javadoc) + * + * @see + * com.vaadin.data.validator.AbstractValidator#isValidValue(java.lang.Object + * ) + */ + @Override + protected boolean isValidValue(String value) { + if (value == null || value.isEmpty()) { + return true; + } + if (complete) { + return getMatcher(value).matches(); + } else { + return getMatcher(value).find(); + } + } + + /** + * Get a new or reused matcher for the pattern + * + * @param value + * the string to find matches in + * @return Matcher for the string + */ + private Matcher getMatcher(String value) { + if (matcher == null) { + matcher = pattern.matcher(value); + } else { + matcher.reset(value); + } + return matcher; + } + +} diff --git a/server/src/main/java/com/vaadin/legacy/data/validator/LegacyShortRangeValidator.java b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyShortRangeValidator.java new file mode 100644 index 0000000000..26bca9d78c --- /dev/null +++ b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyShortRangeValidator.java @@ -0,0 +1,47 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.legacy.data.validator; + +/** + * Validator for validating that an {@link Short} is inside a given range. + * + * @author Vaadin Ltd. + * @since 7.4 + */ +@SuppressWarnings("serial") +public class LegacyShortRangeValidator extends LegacyRangeValidator<Short> { + + /** + * Creates a validator for checking that an Short is within a given range. + * + * By default the range is inclusive i.e. both minValue and maxValue are + * valid values. Use {@link #setMinValueIncluded(boolean)} or + * {@link #setMaxValueIncluded(boolean)} to change it. + * + * + * @param errorMessage + * the message to display in case the value does not validate. + * @param minValue + * The minimum value to accept or null for no limit + * @param maxValue + * The maximum value to accept or null for no limit + */ + public LegacyShortRangeValidator(String errorMessage, Short minValue, + Short maxValue) { + super(errorMessage, Short.class, minValue, maxValue); + } + +} diff --git a/server/src/main/java/com/vaadin/legacy/data/validator/LegacyStringLengthValidator.java b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyStringLengthValidator.java new file mode 100644 index 0000000000..6d7a1d63e8 --- /dev/null +++ b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyStringLengthValidator.java @@ -0,0 +1,149 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package com.vaadin.legacy.data.validator; + +/** + * This <code>StringLengthValidator</code> is used to validate the length of + * strings. + * + * @author Vaadin Ltd. + * @since 3.0 + */ +@SuppressWarnings("serial") +public class LegacyStringLengthValidator extends LegacyAbstractStringValidator { + + private Integer minLength = null; + + private Integer maxLength = null; + + private boolean allowNull = true; + + /** + * Creates a new StringLengthValidator with a given error message. + * + * @param errorMessage + * the message to display in case the value does not validate. + */ + public LegacyStringLengthValidator(String errorMessage) { + super(errorMessage); + } + + /** + * Creates a new StringLengthValidator with a given error message and + * minimum and maximum length limits. + * + * @param errorMessage + * the message to display in case the value does not validate. + * @param minLength + * the minimum permissible length of the string or null for no + * limit. A negative value for no limit is also supported for + * backwards compatibility. + * @param maxLength + * the maximum permissible length of the string or null for no + * limit. A negative value for no limit is also supported for + * backwards compatibility. + * @param allowNull + * Are null strings permissible? This can be handled better by + * setting a field as required or not. + */ + public LegacyStringLengthValidator(String errorMessage, Integer minLength, + Integer maxLength, boolean allowNull) { + this(errorMessage); + setMinLength(minLength); + setMaxLength(maxLength); + setNullAllowed(allowNull); + } + + /** + * Checks if the given value is valid. + * + * @param value + * the value to validate. + * @return <code>true</code> for valid value, otherwise <code>false</code>. + */ + @Override + protected boolean isValidValue(String value) { + if (value == null) { + return allowNull; + } + final int len = value.length(); + if ((minLength != null && minLength > -1 && len < minLength) + || (maxLength != null && maxLength > -1 && len > maxLength)) { + return false; + } + return true; + } + + /** + * Returns <code>true</code> if null strings are allowed. + * + * @return <code>true</code> if allows null string, otherwise + * <code>false</code>. + */ + @Deprecated + public final boolean isNullAllowed() { + return allowNull; + } + + /** + * Gets the maximum permissible length of the string. + * + * @return the maximum length of the string or null if there is no limit + */ + public Integer getMaxLength() { + return maxLength; + } + + /** + * Gets the minimum permissible length of the string. + * + * @return the minimum length of the string or null if there is no limit + */ + public Integer getMinLength() { + return minLength; + } + + /** + * Sets whether null-strings are to be allowed. This can be better handled + * by setting a field as required or not. + */ + @Deprecated + public void setNullAllowed(boolean allowNull) { + this.allowNull = allowNull; + } + + /** + * Sets the maximum permissible length of the string. + * + * @param maxLength + * the maximum length to accept or null for no limit + */ + public void setMaxLength(Integer maxLength) { + this.maxLength = maxLength; + } + + /** + * Sets the minimum permissible length. + * + * @param minLength + * the minimum length to accept or null for no limit + */ + public void setMinLength(Integer minLength) { + this.minLength = minLength; + } + +} |