68 lines
2.0 KiB
Java
68 lines
2.0 KiB
Java
|
package org.gcube.resourcemanagement.model.reference.properties;
|
||
|
|
||
|
import java.util.Objects;
|
||
|
import java.util.regex.Matcher;
|
||
|
import java.util.regex.Pattern;
|
||
|
|
||
|
import org.gcube.informationsystem.types.annotations.ISProperty;
|
||
|
import org.gcube.resourcemanagement.model.impl.properties.RegexPropertyImpl;
|
||
|
import org.gcube.resourcemanagement.model.reference.properties.utilities.PropertyValidator;
|
||
|
import org.gcube.resourcemanagement.model.reference.properties.utilities.ValidatedTypedProperty;
|
||
|
import org.gcube.resourcemanagement.model.reference.properties.utilities.Validation;
|
||
|
|
||
|
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||
|
|
||
|
/**
|
||
|
* A property validated with a regular expression.
|
||
|
*
|
||
|
* @author Manuele Simi (ISTI CNR)
|
||
|
*
|
||
|
*/
|
||
|
@JsonDeserialize(as=RegexPropertyImpl.class)
|
||
|
public interface RegexProperty extends GCubeProperty, ValidatedTypedProperty<String, String> {
|
||
|
|
||
|
public static final String NAME = "RegexProperty"; // RegexProperty.class.getSimpleName();
|
||
|
|
||
|
@Override
|
||
|
default Validation validate() {
|
||
|
return new RegexValidator().validate(this);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Validator for {@link RegexProperty} properties.
|
||
|
* @author Manuele Simi (ISTI CNR)
|
||
|
*
|
||
|
*/
|
||
|
class RegexValidator implements PropertyValidator<RegexProperty> {
|
||
|
|
||
|
@Override
|
||
|
public Validation validate(RegexProperty property) {
|
||
|
if (Objects.isNull(property.getValue()))
|
||
|
return Validation.fail("The value of the regex property is not set.");
|
||
|
if (Objects.isNull(property.getType()))
|
||
|
return Validation.fail("The type of the regex property is not set.");
|
||
|
|
||
|
Pattern pattern = Pattern.compile(property.getType());
|
||
|
Matcher matcher = pattern.matcher(property.getValue());
|
||
|
return matcher.find() ?
|
||
|
Validation.success("Accepted!") : Validation.fail(property.getValue() + " was not a valid match!");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
@ISProperty
|
||
|
@Override
|
||
|
public String getValue();
|
||
|
|
||
|
@Override
|
||
|
public void setValue(String value);
|
||
|
|
||
|
@ISProperty
|
||
|
@Override
|
||
|
public String getType();
|
||
|
|
||
|
@Override
|
||
|
public void setType(String type);
|
||
|
|
||
|
}
|