org.apache.bval.routines.StandardValidation Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bval-xstream Show documentation
Show all versions of bval-xstream Show documentation
BVal XML Metadata with XStream
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.bval.routines;
import org.apache.bval.model.Features;
import org.apache.bval.model.MetaProperty;
import org.apache.bval.model.Validation;
import org.apache.bval.model.ValidationContext;
import org.apache.bval.model.ValidationListener;
import org.apache.bval.xml.XMLMetaValue;
import java.util.Collection;
import java.util.Date;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import static org.apache.bval.model.Features.Property.MANDATORY;
import static org.apache.bval.model.Features.Property.MAX_LENGTH;
import static org.apache.bval.model.Features.Property.MAX_VALUE;
import static org.apache.bval.model.Features.Property.MIN_LENGTH;
import static org.apache.bval.model.Features.Property.MIN_VALUE;
import static org.apache.bval.model.Features.Property.REG_EXP;
import static org.apache.bval.model.Features.Property.TIME_LAG;
/**
* Description: This class implements the standard validations for properties!
* You can subclass this class and replace the implementation
* in the beanInfo-xml by providing it a validation "standard"
*/
public class StandardValidation implements Validation {
/** key for this validation in the validation list of the beanInfos */
public String getValidationId() {
return "standard";
}
public void validate(ValidationContext context) {
validateMandatory(context);
validateMaxLength(context);
validateMinLength(context);
validateMaxValue(context);
validateMinValue(context);
validateRegExp(context);
validateTimeLag(context);
}
protected void validateTimeLag(ValidationContext context) {
String lag = (String) context.getMetaProperty().getFeature(TIME_LAG);
if (lag == null) return;
if (context.getPropertyValue() == null) return;
long date = ((Date) context.getPropertyValue()).getTime();
long now = System.currentTimeMillis();
if (XMLMetaValue.TIMELAG_Future.equals(lag)) {
if (date < now) {
context.getListener().addError(TIME_LAG, context);
}
} else if (XMLMetaValue.TIMELAG_Past.equals(lag)) {
if (date > now) {
context.getListener().addError(TIME_LAG, context);
}
} else {
throw new IllegalArgumentException("unknown timelag " + lag + " at " + context);
}
}
private static final String REG_EXP_PATTERN = "cachedRegExpPattern";
protected void validateRegExp(ValidationContext context) {
final MetaProperty meta = context.getMetaProperty();
final String regExp = (String) meta.getFeature(REG_EXP);
if (regExp == null) return;
if (context.getPropertyValue() == null) return;
final String value = String.valueOf(context.getPropertyValue());
try {
Pattern pattern = (Pattern) meta.getFeature(REG_EXP_PATTERN);
if (pattern == null) {
pattern = Pattern.compile(regExp);
meta.putFeature(REG_EXP_PATTERN, pattern);
}
if (!pattern.matcher(value).matches()) {
context.getListener().addError(REG_EXP, context);
}
} catch (PatternSyntaxException e) {
throw new IllegalArgumentException(
"regular expression malformed. regexp " + regExp + " at " + context, e);
}
}
protected void validateMinValue(ValidationContext context) {
@SuppressWarnings("unchecked")
Comparable
© 2015 - 2024 Weber Informatics LLC | Privacy Policy