You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ni...@apache.org on 2007/11/14 20:49:24 UTC
svn commit: r595023 - in /commons/proper/validator/trunk/src:
main/java/org/apache/commons/validator/routines/RegexValidator.java
test/java/org/apache/commons/validator/routines/RegexValidatorTest.java
Author: niallp
Date: Wed Nov 14 11:49:23 2007
New Revision: 595023
URL: http://svn.apache.org/viewvc?rev=595023&view=rev
Log:
VALIDATOR-214 - RegexValidator is a bit of a mess - simplify, removing all the static cruft
Modified:
commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/RegexValidator.java
commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/RegexValidatorTest.java
Modified: commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/RegexValidator.java
URL: http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/RegexValidator.java?rev=595023&r1=595022&r2=595023&view=diff
==============================================================================
--- commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/RegexValidator.java (original)
+++ commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/RegexValidator.java Wed Nov 14 11:49:23 2007
@@ -23,29 +23,6 @@
/**
* <b>Regular Expression</b> validation (using JDK 1.4+ regex support).
* <p>
- * This validator provides convenient regular expression validation
- * in one of two ways:
- *
- * <h4>1. One Off validation using the static methods</h4>
- * <ul>
- * <li>Validate <code>true</code> or <code>false</code>:</li>
- * <ul>
- * <li><code>boolean valid = RegexValidator.isValid(value, regex);</code></li>
- * <li><code>boolean valid = RegexValidator.isValid(value, regex, caseSensitive);</code></li>
- * </ul>
- * <li>Validate returning an aggregated String of the matched groups:</li>
- * <ul>
- * <li><code>String result = RegexValidator.validate(value, regex);</code></li>
- * <li><code>String result = RegexValidator.validate(value, regex, caseSensitive);</code></li>
- * </ul>
- * <li>Validate returning the matched groups:</li>
- * <ul>
- * <li><code>String[] result = RegexValidator.match(value, regex);</code></li>
- * <li><code>String[] result = RegexValidator.match(value, regex, caseSensitive);</code></li>
- * </ul>
- * </ul>
- *
- * <h4>2. Re-using cached instances validating against one or more regular expression</h4>
* Construct the validator either for a single regular expression or a set (array) of
* regular expressions. By default validation is <i>case sensitive</i> but constructors
* are provided to allow <i>case in-sensitive</i> validation. For example to create
@@ -77,10 +54,8 @@
* @version $Revision$ $Date$
* @since Validator 1.4
*/
-public final class RegexValidator implements Serializable {
+public class RegexValidator implements Serializable {
- private static final String MISSING_REGEX = "Regular Expression is missing";
- private final Pattern pattern;
private final Pattern[] patterns;
/**
@@ -91,7 +66,7 @@
* validate against
*/
public RegexValidator(String regex) {
- this(regex, 0);
+ this(regex, true);
}
/**
@@ -104,39 +79,23 @@
* sensitive</i>, otherwise matching is <i>case in-sensitive</i>
*/
public RegexValidator(String regex, boolean caseSensitive) {
- this(regex, (caseSensitive ? 0 : Pattern.CASE_INSENSITIVE));
+ this(new String[] {regex}, caseSensitive);
}
/**
- * Construct a validator for a single regular expression with
- * the specified flags.
- *
- * @param regex The regular expression this validator will
- * validate against
- * @param flags Bit mask of matching flags - see {@link Pattern} for values
- */
- private RegexValidator(String regex, int flags) {
- if (regex == null || regex.length() == 0) {
- throw new IllegalArgumentException(MISSING_REGEX);
- }
- this.pattern = Pattern.compile(regex, flags);
- this.patterns = null;
- }
-
- /**
- * Construct a <i>case sensitive</i> validator for a set
- * of regular expressions.
+ * Construct a <i>case sensitive</i> validator that matches any one
+ * of the set of regular expressions.
*
* @param regexs The set of regular expressions this validator will
* validate against
*/
public RegexValidator(String[] regexs) {
- this(regexs, 0);
+ this(regexs, true);
}
/**
- * Construct a validator for a set of regular expressions
- * with the specified case sensitivity.
+ * Construct a validator that matches any one of the set of regular
+ * expressions with the specified case sensitivity.
*
* @param regexs The set of regular expressions this validator will
* validate against
@@ -144,29 +103,17 @@
* sensitive</i>, otherwise matching is <i>case in-sensitive</i>
*/
public RegexValidator(String[] regexs, boolean caseSensitive) {
- this(regexs, (caseSensitive ? 0: Pattern.CASE_INSENSITIVE));
- }
-
- /**
- * Construct a validator for a set of regular expressions
- * the specified flags.
- *
- * @param regexs The set of regular expressions this validator will
- * validate against
- * @param flags Bit mask of matching flags - see {@link Pattern} for values
- */
- private RegexValidator(String[] regexs, int flags) {
if (regexs == null || regexs.length == 0) {
throw new IllegalArgumentException("Regular expressions are missing");
}
patterns = new Pattern[regexs.length];
+ int flags = (caseSensitive ? 0: Pattern.CASE_INSENSITIVE);
for (int i = 0; i < regexs.length; i++) {
if (regexs[i] == null || regexs[i].length() == 0) {
throw new IllegalArgumentException("Regular expression[" + i + "] is missing");
}
patterns[i] = Pattern.compile(regexs[i], flags);
}
- this.pattern = null;
}
/**
@@ -179,66 +126,13 @@
public boolean isValid(String value) {
if (value == null) {
return false;
- } else if (pattern != null) {
- return pattern.matcher(value).matches();
- } else {
- for (int i = 0; i < patterns.length; i++) {
- if (patterns[i].matcher(value).matches()) {
- return true;
- }
- }
- return false;
- }
- }
-
- /**
- * Validate a value against a regular expression
- * (<i>case sensitive</i>).
- *
- * @param value Value to validate
- * @param regex The regular expression to validate against.
- * @return <code>true</code> if the value is valid
- * otherwise <code>false</code>.
- */
- public static boolean isValid(String value, String regex) {
- return RegexValidator.isValid(value, regex, 0);
- }
-
- /**
- * Validate a value against a regular expression
- * with the specified case sensitivity.
- *
- * @param value Value to validate
- * @param regex The regular expression to validate against.
- * @param caseSensitive when <code>true</code> matching is <i>case
- * sensitive</i>, otherwise matching is <i>case in-sensitive</i>
- * @return <code>true</code> if the value is valid
- * otherwise <code>false</code>.
- */
- public static boolean isValid(String value, String regex, boolean caseSensitive) {
- int flags = caseSensitive ? 0 : Pattern.CASE_INSENSITIVE;
- return RegexValidator.isValid(value, regex, flags);
- }
-
- /**
- * Validate a value against a regular expression with the
- * specified flags.
- *
- * @param value Value to validate
- * @param regex The regular expression to validate against.
- * @param flags Bit mask of matching flags - see {@link Pattern} for values
- * @return <code>true</code> if the value is valid
- * otherwise <code>false</code>.
- */
- private static boolean isValid(String value, String regex, int flags) {
- if (regex == null || regex.length() == 0) {
- throw new IllegalArgumentException(MISSING_REGEX);
}
- if (value == null) {
- return false;
- } else {
- return Pattern.compile(regex, flags).matcher(value).matches();
+ for (int i = 0; i < patterns.length; i++) {
+ if (patterns[i].matcher(value).matches()) {
+ return true;
+ }
}
+ return false;
}
/**
@@ -252,92 +146,21 @@
public String[] match(String value) {
if (value == null) {
return null;
- } else if (pattern != null) {
- return RegexValidator.match(value, pattern);
- } else {
- String[] result = null;
- for (int i = 0; i < patterns.length; i++) {
- result = RegexValidator.match(value, patterns[i]);
- if (result != null) {
- return result;
+ }
+ for (int i = 0; i < patterns.length; i++) {
+ Matcher matcher = patterns[i].matcher(value);
+ if (matcher.matches()) {
+ int count = matcher.groupCount();
+ String[] groups = new String[count];
+ for (int j = 0; j < count; j++) {
+ groups[j] = matcher.group(j+1);
}
+ return groups;
}
- return null;
- }
- }
-
- /**
- * Validate a value against the specified regular expression
- * returning the matched groups (<i>case sensitive</i>).
- *
- * @param value Value to validate
- * @param regex The regular expression to validate against.
- * @return String array of the <i>groups</i> matched if
- * valid or <code>null</code> if invalid
- */
- public static String[] match(String value, String regex) {
- return RegexValidator.match(value, regex, 0);
- }
-
- /**
- * Validate a value against a regular expression with the
- * specified <i>case sensitivity</i> returning the matched groups.
- *
- * @param value Value to validate
- * @param regex The regular expression to validate against.
- * @param caseSensitive when <code>true</code> matching is <i>case
- * sensitive</i>, otherwise matching is <i>case in-sensitive</i>
- * @return String array of the <i>groups</i> matched if
- * valid or <code>null</code> if invalid
- */
- public static String[] match(String value, String regex, boolean caseSensitive) {
- int flags = caseSensitive ? 0 : Pattern.CASE_INSENSITIVE;
- return RegexValidator.match(value, regex, flags);
- }
-
- /**
- * Validate a value against a regular expression with the
- * specified flags returning the matched groups.
- *
- * @param value Value to validate
- * @param regex The regular expression to validate against.
- * @param caseSensitive when <code>true</code> matching is <i>case
- * sensitive</i>, otherwise matching is <i>case in-sensitive</i>
- * @return String array of the <i>groups</i> matched if
- * valid or <code>null</code> if invalid
- */
- private static String[] match(String value, String regex, int flags) {
- if (regex == null || regex.length() == 0) {
- throw new IllegalArgumentException(MISSING_REGEX);
}
- if (value == null) {
- return null;
- }
- return RegexValidator.match(value, Pattern.compile(regex, flags));
+ return null;
}
- /**
- * Validate a value against the specified pattern
- * returning the matched groups.
- *
- * @param value Value to validate
- * @param pattern The pattern to match against.
- * @return String array of the <i>groups</i> matched if
- * valid or <code>null</code> if invalid
- */
- private static String[] match(String value, Pattern pattern) {
- Matcher matcher = pattern.matcher(value);
- if (matcher.matches()) {
- int count = matcher.groupCount();
- String[] groups = new String[count];
- for (int i = 0; i < count; i++) {
- groups[i] = matcher.group(i+1);
- }
- return groups;
- } else {
- return null;
- }
- }
/**
* Validate a value against the set of regular expressions
@@ -350,100 +173,25 @@
public String validate(String value) {
if (value == null) {
return null;
- } else if (pattern != null) {
- return RegexValidator.validate(value, pattern);
- } else {
- String result = null;
- for (int i = 0; i < patterns.length; i++) {
- result = RegexValidator.validate(value, patterns[i]);
- if (result != null) {
- return result;
- }
- }
- return null;
- }
- }
-
- /**
- * Validate a value against the specified regular expression
- * returning a String value of the aggregated groups
- * (<i>case sensitive</i>).
- *
- * @param value Value to validate
- * @param regex The regular expression to validate against.
- * @return Aggregated String value comprised of the
- * <i>groups</i> matched if valid or <code>null</code> if invalid
- */
- public static String validate(String value, String regex) {
- return RegexValidator.validate(value, regex, 0);
- }
-
- /**
- * Validate a value against a regular expression with the
- * specified <i>case sensitivity</i> returning a String
- * value of the aggregated groups.
- *
- * @param value Value to validate
- * @param regex The regular expression to validate against.
- * @param caseSensitive when <code>true</code> matching is <i>case
- * sensitive</i>, otherwise matching is <i>case in-sensitive</i>
- * @return Aggregated String value comprised of the
- * <i>groups</i> matched if valid or <code>null</code> if invalid
- */
- public static String validate(String value, String regex, boolean caseSensitive) {
- int flags = caseSensitive ? 0 : Pattern.CASE_INSENSITIVE;
- return RegexValidator.validate(value, regex, flags);
- }
-
- /**
- * Validate a value against a regular expression with the
- * specified flags returning a String value of the aggregated
- * groups.
- *
- * @param value Value to validate
- * @param regex The regular expression to validate against.
- * @param flags Bit mask of matching flags - see {@link Pattern} for values
- * @return Aggregated String value comprised of the
- * <i>groups</i> matched if valid or <code>null</code> if invalid
- */
- private static String validate(String value, String regex, int flags) {
- if (regex == null || regex.length() == 0) {
- throw new IllegalArgumentException(MISSING_REGEX);
- }
- if (value == null) {
- return null;
}
- return RegexValidator.validate(value, Pattern.compile(regex, flags));
- }
-
- /**
- * Validate a value against the specified pattern
- * returning a String value of the aggregated groups.
- *
- * @param value Value to validate
- * @param pattern The pattern to validate against.
- * @return Aggregated String value comprised of the
- * <i>groups</i> matched if valid or <code>null</code> if invalid
- */
- private static String validate(String value, Pattern pattern) {
- Matcher matcher = pattern.matcher(value);
- if (matcher.matches()) {
- int count = matcher.groupCount();
- if (count == 1) {
- return matcher.group(1);
- } else {
+ for (int i = 0; i < patterns.length; i++) {
+ Matcher matcher = patterns[i].matcher(value);
+ if (matcher.matches()) {
+ int count = matcher.groupCount();
+ if (count == 1) {
+ return matcher.group(1);
+ }
StringBuffer buffer = new StringBuffer();
- for (int i = 0; i < count; i++) {
- String component = matcher.group(i+1);
+ for (int j = 0; j < count; j++) {
+ String component = matcher.group(j+1);
if (component != null) {
buffer.append(component);
}
}
return buffer.toString();
}
- } else {
- return null;
}
+ return null;
}
/**
@@ -451,11 +199,16 @@
* @return A String representation of this validator
*/
public String toString() {
- if (pattern != null) {
- return "RegexValidator{" + pattern.pattern() + "}";
- } else {
- return "RegexValidator[" + patterns.length + "]";
+ StringBuffer buffer = new StringBuffer();
+ buffer.append("RegexValidator{");
+ for (int i = 0; i < patterns.length; i++) {
+ if (i > 0) {
+ buffer.append(",");
+ }
+ buffer.append(patterns[i].pattern());
}
+ buffer.append("}");
+ return buffer.toString();
}
}
Modified: commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/RegexValidatorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/RegexValidatorTest.java?rev=595023&r1=595022&r2=595023&view=diff
==============================================================================
--- commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/RegexValidatorTest.java (original)
+++ commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/RegexValidatorTest.java Wed Nov 14 11:49:23 2007
@@ -28,7 +28,6 @@
*/
public class RegexValidatorTest extends TestCase {
- private static final String MISSING_REGEX = "Regular Expression is missing";
private static final String REGEX = "^([abc]*)(?:\\-)([DEF]*)(?:\\-)([123]*)$";
private static final String COMPONENT_1 = "([abc]{3})";
@@ -64,33 +63,9 @@
}
/**
- * Test static methods.
- */
- public void testStatic() {
-
- // isValid()
- assertEquals("Sensitive isValid() valid", true, RegexValidator.isValid("ac-DE-1", REGEX));
- assertEquals("Sensitive isValid() invalid", false, RegexValidator.isValid("AB-de-1", REGEX));
- assertEquals("Insensitive isValid() valid", true, RegexValidator.isValid("AB-de-1", REGEX, false));
- assertEquals("Insensitive isValid() invalid", false, RegexValidator.isValid("ABd-de-1", REGEX, false));
-
- // validate()
- assertEquals("Sensitive validate() valid", "acDE1", RegexValidator.validate("ac-DE-1", REGEX));
- assertEquals("Sensitive validate() invalid", null, RegexValidator.validate("AB-de-1", REGEX));
- assertEquals("Insensitive validate() valid", "ABde1", RegexValidator.validate("AB-de-1", REGEX, false));
- assertEquals("Insensitive validate() invalid", null, RegexValidator.validate("ABd-de-1", REGEX, false));
-
- // match()
- checkArray("Sensitive match() valid", new String[] {"ac", "DE", "1"}, RegexValidator.match("ac-DE-1", REGEX));
- checkArray("Sensitive match() invalid", null, RegexValidator.match("AB-de-1", REGEX));
- checkArray("Insensitive match() valid", new String[] {"AB", "de", "1"}, RegexValidator.match("AB-de-1", REGEX, false));
- checkArray("Insensitive match() invalid", null, RegexValidator.match("ABd-de-1", REGEX, false));
- }
-
- /**
* Test instance methods with single regular expression.
*/
- public void testInstanceSingle() {
+ public void testSingle() {
RegexValidator sensitive = new RegexValidator(REGEX);
RegexValidator insensitive = new RegexValidator(REGEX, false);
@@ -111,12 +86,14 @@
checkArray("Sensitive match() invalid", null, sensitive.match("AB-de-1"));
checkArray("Insensitive match() valid", new String[] {"AB", "de", "1"}, insensitive.match("AB-de-1"));
checkArray("Insensitive match() invalid", null, insensitive.match("ABd-de-1"));
+ assertEquals("validate one", "ABC", (new RegexValidator("^([A-Z]*)$")).validate("ABC"));
+ checkArray("match one", new String[] {"ABC"}, (new RegexValidator("^([A-Z]*)$")).match("ABC"));
}
/**
- * Test instance methods with multiple regular expressions.
+ * Test with multiple regular expressions (case sensitive).
*/
- public void testInstanceMultipleSensitive() {
+ public void testMultipleSensitive() {
// ------------ Set up Sensitive Validators
RegexValidator multiple = new RegexValidator(MULTIPLE_REGEX);
@@ -155,9 +132,9 @@
}
/**
- * Test instance methods with multiple regular expressions.
+ * Test with multiple regular expressions (case in-sensitive).
*/
- public void testInstanceMultipleInsensitive() {
+ public void testMultipleInsensitive() {
// ------------ Set up In-sensitive Validators
RegexValidator multiple = new RegexValidator(MULTIPLE_REGEX, false);
@@ -196,96 +173,27 @@
}
/**
- * Test instance methods with multiple regular expressions.
- */
- public void testMatchOneGroup() {
-
- assertEquals("validate()", "ABC", RegexValidator.validate("ABC", "^([A-Z]*)$"));
- checkArray("match()", new String[] {"ABC"}, RegexValidator.match("ABC", "^([A-Z]*)$"));
- }
-
- /**
* Test Null value
*/
public void testNullValue() {
- // Test instance methods
RegexValidator validator = new RegexValidator(REGEX);
assertEquals("Instance isValid()", false, validator.isValid(null));
assertEquals("Instance validate()", null, validator.validate(null));
assertEquals("Instance match()", null, validator.match(null));
-
- // Test static methods
- assertEquals("Static isValid()", false, RegexValidator.isValid(null, REGEX));
- assertEquals("Static validate()", null, RegexValidator.validate(null, REGEX));
- assertEquals("Static match()", null, RegexValidator.match(null, REGEX));
- }
-
- /**
- * Test exceptions
- */
- public void testStaicMissingRegex() {
-
- // isValid() - Null regular expression
- try {
- RegexValidator.isValid("abc", null);
- fail("isValid Null - expected IllegalArgumentException");
- } catch (IllegalArgumentException e) {
- assertEquals("isValid Null", MISSING_REGEX, e.getMessage());
- }
-
- // validate() - Null regular expression
- try {
- RegexValidator.validate("abc", null);
- fail("validate Null - expected IllegalArgumentException");
- } catch (IllegalArgumentException e) {
- assertEquals("validate Null", MISSING_REGEX, e.getMessage());
- }
-
- // match() - Null regular expression
- try {
- RegexValidator.match("abc", null);
- fail("match Null - expected IllegalArgumentException");
- } catch (IllegalArgumentException e) {
- assertEquals("match Null", MISSING_REGEX, e.getMessage());
- }
-
- // isValid() - Zero Length regular expression
- try {
- RegexValidator.isValid("abc", "");
- fail("isValid Zero Length - expected IllegalArgumentException");
- } catch (IllegalArgumentException e) {
- assertEquals("isValid Zero Length", MISSING_REGEX, e.getMessage());
- }
-
- // validate() - Zero Length regular expression
- try {
- RegexValidator.validate("abc", "");
- fail("validate Zero Length - expected IllegalArgumentException");
- } catch (IllegalArgumentException e) {
- assertEquals("validate Zero Length", MISSING_REGEX, e.getMessage());
- }
-
- // match() - Zero Length regular expression
- try {
- RegexValidator.match("abc", "");
- fail("match Zero Length - expected IllegalArgumentException");
- } catch (IllegalArgumentException e) {
- assertEquals("match Zero Length", MISSING_REGEX, e.getMessage());
- }
}
/**
* Test exceptions
*/
- public void testInstanceMissingRegex() {
+ public void testMissingRegex() {
// Single Regular Expression - null
try {
new RegexValidator((String)null);
fail("Single Null - expected IllegalArgumentException");
} catch (IllegalArgumentException e) {
- assertEquals("Single Null", MISSING_REGEX, e.getMessage());
+ assertEquals("Single Null", "Regular expression[0] is missing", e.getMessage());
}
// Single Regular Expression - Zero Length
@@ -293,7 +201,7 @@
new RegexValidator("");
fail("Single Zero Length - expected IllegalArgumentException");
} catch (IllegalArgumentException e) {
- assertEquals("Single Zero Length", MISSING_REGEX, e.getMessage());
+ assertEquals("Single Zero Length", "Regular expression[0] is missing", e.getMessage());
}
// Multiple Regular Expression - Null array
@@ -337,7 +245,7 @@
public void testExceptions() {
String invalidRegex = "^([abCD12]*$";
try {
- RegexValidator.isValid("ab", invalidRegex);
+ new RegexValidator(invalidRegex);
} catch (PatternSyntaxException e) {
// expected
}
@@ -351,7 +259,7 @@
assertEquals("Single", "RegexValidator{" + REGEX + "}", single.toString());
RegexValidator multiple = new RegexValidator(new String[] {REGEX, REGEX});
- assertEquals("Multiple", "RegexValidator[2]", multiple.toString());
+ assertEquals("Multiple", "RegexValidator{" + REGEX + "," + REGEX + "}", multiple.toString());
}
/**