You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by lu...@apache.org on 2013/01/13 12:00:37 UTC

svn commit: r1432585 - in /struts/struts2/trunk/xwork-core/src: main/java/com/opensymphony/xwork2/validator/validators/RegexFieldValidator.java test/java/com/opensymphony/xwork2/validator/RegexFieldValidatorTest.java

Author: lukaszlenart
Date: Sun Jan 13 11:00:37 2013
New Revision: 1432585

URL: http://svn.apache.org/viewvc?rev=1432585&view=rev
Log:
WW-3894 uses dedicated params to specify OGNL expressions

Modified:
    struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/RegexFieldValidator.java
    struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/RegexFieldValidatorTest.java

Modified: struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/RegexFieldValidator.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/RegexFieldValidator.java?rev=1432585&r1=1432584&r2=1432585&view=diff
==============================================================================
--- struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/RegexFieldValidator.java (original)
+++ struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/RegexFieldValidator.java Sun Jan 13 11:00:37 2013
@@ -34,6 +34,7 @@ import java.util.regex.Pattern;
  *    <li>caseSensitive - Boolean (Optional). Sets whether the expression should be matched against in a case-sensitive way. Default is <code>true</code>.</li>
  *    <li>trim - Boolean (Optional). Sets whether the expression should be trimed before matching. Default is <code>true</code>.</li>
  * </ul>
+ * You can mix normal params with expression aware params but thus was not tested
  * <!-- END SNIPPET: parameters -->
  * 
  * 
@@ -43,13 +44,22 @@ import java.util.regex.Pattern;
  *     &lt;!-- Plain Validator Syntax --&gt;
  *     &lt;validator type="regex"&gt;
  *         &lt;param name="fieldName"&gt;myStrangePostcode&lt;/param&gt;
- *         &lt;param name="expression"&gt;&lt;![CDATA[([aAbBcCdD][123][eEfFgG][456])]]&lt;&gt;/param&gt;
+ *         &lt;param name="regex"&gt;&lt;![CDATA[([aAbBcCdD][123][eEfFgG][456])]]&lt;&gt;/param&gt;
  *     &lt;/validator&gt;
  *
  *     &lt;!-- Field Validator Syntax --&gt;
  *     &lt;field name="myStrangePostcode"&gt;
  *         &lt;field-validator type="regex"&gt;
- *             &lt;param name="expression"&gt;&lt;![CDATA[([aAbBcCdD][123][eEfFgG][456])]]&gt;&lt;/param&gt;
+ *             &lt;param name="regex"&gt;&lt;![CDATA[([aAbBcCdD][123][eEfFgG][456])]]&gt;&lt;/param&gt;
+ *         &lt;/field-validator&gt;
+ *     &lt;/field&gt;
+ *
+ *     &lt;!-- Field Validator Syntax with expressions --&gt;
+ *     &lt;field name="myStrangePostcode"&gt;
+ *         &lt;field-validator type="regex"&gt;
+ *             &lt;param name="regexExpression"&gt;${regexValue}&lt;/param&gt; &lt;!-- will be evaluated as: String getRegexValue() --&gt;
+ *             &lt;param name="caseSensitiveExpression"&gt;${caseSensitiveValue}&lt;/param&gt; &lt;!-- will be evaluated as: boolean getCaseSensitiveValue() --&gt;
+ *             &lt;param name="trimExpression"&gt;${trimValue}&lt;/param&gt; &lt;!-- will be evaluated as: boolean getTrimValue() --&gt;
  *         &lt;/field-validator&gt;
  *     &lt;/field&gt;
  * &lt;/validators&gt;
@@ -61,7 +71,7 @@ import java.util.regex.Pattern;
  */
 public class RegexFieldValidator extends FieldValidatorSupport {
 
-    private String expression;
+    private String regex;
     private boolean caseSensitive = true;
     private boolean trim = true;
 
@@ -70,7 +80,7 @@ public class RegexFieldValidator extends
         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 || expression == null) {
+        if (value == null || regex == null) {
             return;
         }
 
@@ -88,9 +98,9 @@ public class RegexFieldValidator extends
         // match against expression
         Pattern pattern;
         if (isCaseSensitive()) {
-            pattern = Pattern.compile(expression);
+            pattern = Pattern.compile(regex);
         } else {
-            pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
+            pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
         }
 
         String compare = (String) value;
@@ -107,19 +117,22 @@ public class RegexFieldValidator extends
     /**
      * @return Returns the regular expression to be matched.
      */
-    public String getExpression() {
-        return expression;
+    public String getRegex() {
+        return regex;
     }
 
     /**
-     * Sets the regular expression to be matched.
+     * Sets the regular expression to be matched
      */
-    public void setExpression(String expression) {
-        if (parse) {
-            this.expression = (String) parse(expression, String.class);
-        } else {
-            this.expression = expression;
-        }
+    public void setRegex(String regex) {
+        this.regex = regex;
+    }
+
+    /**
+     * Sets the regular expression as an OGNL expression to be matched
+     */
+    public void setRegexExpression(String regexExpression) {
+        this.regex = (String) parse(regexExpression, String.class);
     }
 
     /**
@@ -134,12 +147,15 @@ public class RegexFieldValidator extends
      * Sets whether the expression should be matched against in
      * a case-sensitive way.  Default is <code>true</code>.
      */
-    public void setCaseSensitive(String caseSensitive) {
-        if (parse) {
-            this.caseSensitive = (Boolean) parse(caseSensitive, Boolean.class);
-        } else {
-            this.caseSensitive = Boolean.parseBoolean(caseSensitive);
-        }
+    public void setCaseSensitive(Boolean caseSensitive) {
+        this.caseSensitive = caseSensitive;
+    }
+
+    /**
+     * Allows specify caseSensitive param as an OGNL expression
+     */
+    public void setCaseSensitiveExpression(String caseSensitiveExpression) {
+        this.caseSensitive = (Boolean) parse(caseSensitiveExpression, Boolean.class);
     }
 
     /**
@@ -154,12 +170,15 @@ public class RegexFieldValidator extends
      * Sets whether the expression should be trimed before matching.
      * Default is <code>true</code>.
      */
-    public void setTrim(String trim) {
-        if (parse) {
-            this.trim = (Boolean) parse(trim, Boolean.class);
-        } else {
-            this.trim = Boolean.parseBoolean(trim);
-        }
+    public void setTrim(Boolean trim) {
+        this.trim = trim;
+    }
+
+    /**
+     * Allows specify trim param as an OGNL expression
+     */
+    public void setTrimExpression(String trimExpression) {
+        this.trim = (Boolean) parse(trimExpression, Boolean.class);
     }
 
 }

Modified: struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/RegexFieldValidatorTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/RegexFieldValidatorTest.java?rev=1432585&r1=1432584&r2=1432585&view=diff
==============================================================================
--- struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/RegexFieldValidatorTest.java (original)
+++ struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/RegexFieldValidatorTest.java Sun Jan 13 11:00:37 2013
@@ -40,7 +40,7 @@ public class RegexFieldValidatorTest ext
         ActionContext.getContext().setValueStack(stack);
 
         RegexFieldValidator validator = new RegexFieldValidator();
-        validator.setExpression("^Sec.*");
+        validator.setRegex("^Sec.*");
         validator.setValidatorContext(new GenericValidatorContext(new Object()));
         validator.setFieldName("username");
         validator.setValueStack(ActionContext.getContext().getValueStack());
@@ -60,8 +60,8 @@ public class RegexFieldValidatorTest ext
         ActionContext.getContext().setValueStack(stack);
 
         RegexFieldValidator validator = new RegexFieldValidator();
-        validator.setTrim("false");
-        validator.setExpression("^Sec.*\\s");
+        validator.setTrim(false);
+        validator.setRegex("^Sec.*\\s");
         validator.setValidatorContext(new GenericValidatorContext(new Object()));
         validator.setFieldName("username");
         validator.setValueStack(ActionContext.getContext().getValueStack());
@@ -81,7 +81,7 @@ public class RegexFieldValidatorTest ext
         ActionContext.getContext().setValueStack(stack);
 
         RegexFieldValidator validator = new RegexFieldValidator();
-        validator.setExpression("^Sec.*");
+        validator.setRegex("^Sec.*");
         validator.setValidatorContext(new GenericValidatorContext(new Object()));
         validator.setFieldName("username");
         validator.setValueStack(ActionContext.getContext().getValueStack());
@@ -106,7 +106,7 @@ public class RegexFieldValidatorTest ext
         ActionContext.getContext().setValueStack(stack);
 
         RegexFieldValidator validator = new RegexFieldValidator();
-        validator.setExpression("^Sec.*");
+        validator.setRegex("^Sec.*");
         validator.setValidatorContext(new GenericValidatorContext(new Object()));
         validator.setFieldName(null);
         validator.setValueStack(ActionContext.getContext().getValueStack());
@@ -120,14 +120,14 @@ public class RegexFieldValidatorTest ext
 
     public void testGetExpression() throws Exception {
         RegexFieldValidator validator = new RegexFieldValidator();
-        validator.setExpression("^Hello.*");
-        assertEquals("^Hello.*", validator.getExpression());
+        validator.setRegex("^Hello.*");
+        assertEquals("^Hello.*", validator.getRegex());
     }
 
     public void testIsTrimmed() throws Exception {
         RegexFieldValidator validator = new RegexFieldValidator();
         assertEquals(true, validator.isTrimed());
-        validator.setTrim("false");
+        validator.setTrim(false);
         assertEquals(false, validator.isTrimed());
     }
 
@@ -139,7 +139,7 @@ public class RegexFieldValidatorTest ext
         ActionContext.getContext().setValueStack(stack);
 
         RegexFieldValidator validator = new RegexFieldValidator();
-        validator.setExpression("^Sec.*");
+        validator.setRegex("^Sec.*");
         validator.setValidatorContext(new GenericValidatorContext(new Object()));
         validator.setFieldName("username");
         validator.setValueStack(ActionContext.getContext().getValueStack());
@@ -159,7 +159,7 @@ public class RegexFieldValidatorTest ext
         ActionContext.getContext().setValueStack(stack);
 
         RegexFieldValidator validator = new RegexFieldValidator();
-        validator.setExpression("[0-9][0-9]");
+        validator.setRegex("[0-9][0-9]");
         validator.setValidatorContext(new GenericValidatorContext(new Object()));
         validator.setFieldName("age");
         validator.setValueStack(ActionContext.getContext().getValueStack());