Author: oheger Date: Wed Sep 5 15:29:33 2018 New Revision: 1840137 URL: http://svn.apache.org/viewvc?rev=1840137&view=rev Log: [CONFIGURATION-713] Added support for Regex patterns.
Thanks to Lars W for the patch. Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/convert/PropertyConverter.java commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/convert/TestPropertyConverter.java Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/convert/PropertyConverter.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/convert/PropertyConverter.java?rev=1840137&r1=1840136&r2=1840137&view=diff ============================================================================== --- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/convert/PropertyConverter.java (original) +++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/convert/PropertyConverter.java Wed Sep 5 15:29:33 2018 @@ -36,6 +36,8 @@ import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Locale; +import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; import org.apache.commons.configuration2.ex.ConversionException; import org.apache.commons.lang3.BooleanUtils; @@ -171,6 +173,10 @@ final class PropertyConverter { return toURL(value); } + else if (Pattern.class.equals(cls)) + { + return toPattern(value); + } else if (Locale.class.equals(cls)) { return toLocale(value); @@ -602,6 +608,36 @@ final class PropertyConverter } } + /** + * Convert the specified object into a Pattern. + * + * @param value the value to convert + * @return the converted value + * @throws ConversionException thrown if the value cannot be converted to a Pattern + */ + public static Pattern toPattern(Object value) throws ConversionException + { + if (value instanceof Pattern) + { + return (Pattern) value; + } + else if (value instanceof String) + { + try + { + return Pattern.compile((String) value); + } + catch (PatternSyntaxException e) + { + throw new ConversionException("The value " + value + " can't be converted to a Pattern", e); + } + } + else + { + throw new ConversionException("The value " + value + " can't be converted to a Pattern"); + } + } + /** * Convert the specified object into a Locale. * Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/convert/TestPropertyConverter.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/convert/TestPropertyConverter.java?rev=1840137&r1=1840136&r2=1840137&view=diff ============================================================================== --- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/convert/TestPropertyConverter.java (original) +++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/convert/TestPropertyConverter.java Wed Sep 5 15:29:33 2018 @@ -25,6 +25,7 @@ import java.lang.annotation.ElementType; import java.math.BigDecimal; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.regex.Pattern; import org.apache.commons.configuration2.ex.ConversionException; import org.junit.Test; @@ -188,6 +189,28 @@ public class TestPropertyConverter PropertyConverter.toNumber("42", Object.class); } + /** + * Tests conversion to patterns when the passed in objects are already + * patterns. + */ + @Test + public void testToPatternDirect() + { + Pattern p = Pattern.compile(".+"); + assertSame("Wrong pattern", p, PropertyConverter.toPattern(p)); + } + + /** + * Tests conversion to patterns when the passed in objects have a compatible + * string representation. + */ + @Test + public void testToPatternFromString() + { + Pattern p = Pattern.compile(".+"); + assertEquals("Wrong conversion result", p.pattern(), PropertyConverter.toPattern(".+").pattern()); + } + @Test public void testToEnumFromEnum() {