mirror of
https://github.com/apache/struts.git
synced 2026-08-11 09:36:57 +00:00
WW-3894 uses dedicated params to specify OGNL expressions
git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@1432585 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
+46
-27
@@ -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;
|
||||
* <!-- Plain Validator Syntax -->
|
||||
* <validator type="regex">
|
||||
* <param name="fieldName">myStrangePostcode</param>
|
||||
* <param name="expression"><![CDATA[([aAbBcCdD][123][eEfFgG][456])]]<>/param>
|
||||
* <param name="regex"><![CDATA[([aAbBcCdD][123][eEfFgG][456])]]<>/param>
|
||||
* </validator>
|
||||
*
|
||||
* <!-- Field Validator Syntax -->
|
||||
* <field name="myStrangePostcode">
|
||||
* <field-validator type="regex">
|
||||
* <param name="expression"><![CDATA[([aAbBcCdD][123][eEfFgG][456])]]></param>
|
||||
* <param name="regex"><![CDATA[([aAbBcCdD][123][eEfFgG][456])]]></param>
|
||||
* </field-validator>
|
||||
* </field>
|
||||
*
|
||||
* <!-- Field Validator Syntax with expressions -->
|
||||
* <field name="myStrangePostcode">
|
||||
* <field-validator type="regex">
|
||||
* <param name="regexExpression">${regexValue}</param> <!-- will be evaluated as: String getRegexValue() -->
|
||||
* <param name="caseSensitiveExpression">${caseSensitiveValue}</param> <!-- will be evaluated as: boolean getCaseSensitiveValue() -->
|
||||
* <param name="trimExpression">${trimValue}</param> <!-- will be evaluated as: boolean getTrimValue() -->
|
||||
* </field-validator>
|
||||
* </field>
|
||||
* </validators>
|
||||
@@ -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 FieldValidatorSupport {
|
||||
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 FieldValidatorSupport {
|
||||
// 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 FieldValidatorSupport {
|
||||
/**
|
||||
* @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 FieldValidatorSupport {
|
||||
* 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 FieldValidatorSupport {
|
||||
* 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+10
-10
@@ -40,7 +40,7 @@ public class RegexFieldValidatorTest extends XWorkTestCase {
|
||||
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 extends XWorkTestCase {
|
||||
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 extends XWorkTestCase {
|
||||
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 extends XWorkTestCase {
|
||||
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 extends XWorkTestCase {
|
||||
|
||||
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 extends XWorkTestCase {
|
||||
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 extends XWorkTestCase {
|
||||
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());
|
||||
|
||||
Reference in New Issue
Block a user