Commits

Anonymous committed ff46d9c

fixed XW-375 (Conflicting validators)

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk/src@1000e221344d-f017-0410-9bd5-d282ab1896d7

Comments (0)

Files changed (3)

java/com/opensymphony/xwork/validator/validators/EmailValidator.java

  * @author tm_jee
  * @version $Date$ $Id$
  */
-public class EmailValidator extends StringRegexValidator {
+public class EmailValidator extends RegexFieldValidator {
 
 	// see XW-371 
     public static final String emailAddressPattern =
 "\\b(^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@([A-Za-z0-9-])+(\\.[A-Za-z0-9-]+)*((\\.com)|(\\.net)|(\\.org)|(\\.info)|(\\.edu)|(\\.mil)|(\\.gov)|(\\.biz)|(\\.ws)|(\\.us)|(\\.tv)|(\\.cc)|(\\.aero)|(\\.arpa)|(\\.coop)|(\\.int)|(\\.jobs)|(\\.museum)|(\\.name)|(\\.pro)|(\\.travel)|(\\.nato)|(\\.[A-Za-z0-9]{2,3})|(\\.[A-Za-z0-9]{2,3}\\.[A-Za-z0-9]{2,3}))$)\\b";
 
     public EmailValidator() {
-        setRegex(emailAddressPattern);
+        setExpression(emailAddressPattern);
         setCaseSensitive(false);
     }
 

java/com/opensymphony/xwork/validator/validators/RegexFieldValidator.java

 
 import com.opensymphony.xwork.validator.ValidationException;
 
+import java.util.regex.Pattern;
+import java.util.regex.Matcher;
+
 /**
  * <!-- START SNIPPET: javadoc -->
  * Validates a string field using a regular expression.
  * <ul>
  * 	  <li>fieldName - The field name this validator is validating. Required if using Plain-Validator Syntax otherwise not required</li>
  *    <li>expression - The RegExp expression  REQUIRED</li>
+ *    <li>caseSensitive - Boolean (Optional). Sets whether the expression should be matched against in a case-sensitive way. Default is <code>true</code>.</li>
  * </ul>
  * <!-- END SNIPPET: parameters -->
  * 
  * @version $Date$ $Revision$
  */
 public class RegexFieldValidator extends FieldValidatorSupport {
-    
+
     private String expression;
+    private boolean caseSensitive = true;
 
     public void validate(Object object) throws ValidationException {
         String fieldName = getFieldName();
         Object value = this.getFieldValue(fieldName, object);
         // if there is no value - don't do comparison
         // if a value is required, a required validator should be added to the field
-        if (value == null)
+        if (value == null || expression == null) {
+            return;
+        }
+
+        // XW-375 - must be a string
+        if (!(value instanceof String)) {
             return;
-        if (!(value instanceof String) || !((String) value).matches(expression)) {
+        }
+
+        // string must not be empty
+        String str = ((String) value).trim();
+        if (str.length() == 0) {
+            return;
+        }
+
+        // match against expression
+        Pattern pattern;
+        if (isCaseSensitive()) {
+            pattern = Pattern.compile(expression);
+        } else {
+            pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
+        }
+
+        Matcher matcher = pattern.matcher(str);
+
+        if (!matcher.matches()) {
             addFieldError(fieldName, object);
         }
     }
 
+    /**
+     * @return Returns the regular expression to be matched.
+     */
     public String getExpression() {
         return expression;
     }
 
+    /**
+     * Sets the regular expression to be matched.
+     */
     public void setExpression(String expression) {
         this.expression = expression;
     }
+
+    /**
+     * @return Returns whether the expression should be matched against in
+     *         a case-sensitive way.  Default is <code>true</code>.
+     */
+    public boolean isCaseSensitive() {
+        return caseSensitive;
+    }
+
+    /**
+     * Sets whether the expression should be matched against in
+     * a case-sensitive way.  Default is <code>true</code>.
+     */
+    public void setCaseSensitive(boolean caseSensitive) {
+        this.caseSensitive = caseSensitive;
+    }
+
 }

java/com/opensymphony/xwork/validator/validators/StringRegexValidator.java

  * @author jhouse
  * @see #setRegex(String)
  * @see #setCaseSensitive(boolean)
+ * @deprecated use RegexFieldValidator
  */
 public class StringRegexValidator extends FieldValidatorSupport {