Add RegexProperty

This commit is contained in:
Clément Fournier
2018-03-06 22:50:13 +01:00
parent 0967d50275
commit d489698157
4 changed files with 101 additions and 4 deletions

View File

@@ -24,10 +24,11 @@ import net.sourceforge.pmd.Rule;
/**
* Creates a single value property.
*
* @param theName Name of the property
* @param theDescription Description
* @param theUIOrder UI order
* @param theDefault Default value
* @param theName Name of the property
* @param theDescription Description
* @param theUIOrder UI order
* @param theDefault Default value
* @param isDefinedExternally Whether the property is defined in the XML (by a XPath rule) or not
*
* @throws IllegalArgumentException If name or description are empty, or UI order is negative.
*/

View File

@@ -31,6 +31,8 @@ public enum PropertyTypeId {
CHARACTER("Character", CharacterProperty.extractor(), ValueParserConstants.CHARACTER_PARSER),
CHARACTER_LIST("List[Character]", CharacterMultiProperty.extractor(), ValueParserConstants.CHARACTER_PARSER),
REGEX("Regex", RegexProperty.extractor(), ValueParserConstants.REGEX_PARSER),
INTEGER("Integer", IntegerProperty.extractor(), ValueParserConstants.INTEGER_PARSER),
INTEGER_LIST("List[Integer]", IntegerMultiProperty.extractor(), ValueParserConstants.INTEGER_PARSER),
LONG("Long", LongProperty.extractor(), ValueParserConstants.LONG_PARSER),

View File

@@ -0,0 +1,85 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.properties;
import java.util.regex.Pattern;
import net.sourceforge.pmd.properties.builders.PropertyDescriptorBuilderConversionWrapper;
import net.sourceforge.pmd.properties.builders.PropertyDescriptorBuilderConversionWrapper.SingleValue;
import net.sourceforge.pmd.properties.builders.SingleValuePropertyBuilder;
/**
* Property which has a regex pattern as a value.
*
* @author Clément Fournier
* @since 6.1.0
*/
public final class RegexProperty extends AbstractSingleValueProperty<Pattern> {
RegexProperty(String theName, String theDescription, Pattern theDefault, float theUIOrder, boolean isDefinedExternally) {
super(theName, theDescription, theDefault, theUIOrder, isDefinedExternally);
}
@Override
protected Pattern createFrom(String toParse) {
return Pattern.compile(toParse);
}
@Override
public Class<Pattern> type() {
return Pattern.class;
}
static SingleValue<Pattern, RegexPBuilder> extractor() {
return new PropertyDescriptorBuilderConversionWrapper.SingleValue<Pattern, RegexPBuilder>(Pattern.class, ValueParserConstants.REGEX_PARSER) {
@Override
protected RegexPBuilder newBuilder(String name) {
return new RegexPBuilder(name);
}
};
}
/**
* Creates a new builder for a regex property.
*
* @param name The name of the property
*
* @return A new builder
*/
public static RegexPBuilder named(String name) {
return new RegexPBuilder(name);
}
/** Builder for a {@link RegexProperty}. */
public static final class RegexPBuilder extends SingleValuePropertyBuilder<Pattern, RegexProperty.RegexPBuilder> {
private RegexPBuilder(String name) {
super(name);
}
/**
* Specify a default pattern for the property.
* The string value must be a valid regex pattern.
*
* @param val Regex pattern
*
* @return The same builder
*/
public RegexPBuilder defaultValue(String val) {
return super.defaultValue(Pattern.compile(val));
}
@Override
public RegexProperty build() {
return new RegexProperty(this.name, this.description, this.defaultValue, this.uiOrder, isDefinedInXML);
}
}
}

View File

@@ -14,6 +14,7 @@ import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
@@ -195,6 +196,14 @@ public final class ValueParserConstants {
}
};
/** Compiles a regex. */
static final ValueParser<Pattern> REGEX_PARSER = new ValueParser<Pattern>() {
@Override
public Pattern valueOf(String value) throws IllegalArgumentException {
return Pattern.compile(value);
}
};
/** Extract classes. */
static final ValueParser<Class> CLASS_PARSER = new ValueParser<Class>() {
@Override