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 2011/12/02 17:33:45 UTC
svn commit: r1209569 [35/50] - in /struts/struts2/branches/STRUTS_3_X:
apps/blank/src/main/java/example/ apps/blank/src/test/java/example/
apps/jboss-blank/src/main/java/example/
apps/jboss-blank/src/test/java/example/ apps/mailreader/src/main/java/mai...
Added: struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/ConversionErrorFieldValidator.java
URL: http://svn.apache.org/viewvc/struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/ConversionErrorFieldValidator.java?rev=1209569&view=auto
==============================================================================
--- struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/ConversionErrorFieldValidator.java (added)
+++ struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/ConversionErrorFieldValidator.java Fri Dec 2 16:33:03 2011
@@ -0,0 +1,83 @@
+/*
+ * Copyright 2002-2006,2009 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.struts2.xwork2.validator.validators;
+
+import org.apache.struts2.xwork2.ActionContext;
+import org.apache.struts2.xwork2.conversion.impl.XWorkConverter;
+import org.apache.struts2.xwork2.validator.ValidationException;
+
+import java.util.Map;
+
+
+/**
+ * <!-- START SNIPPET: javadoc -->
+ * Field Validator that checks if a conversion error occured for this field.
+ * <!-- END SNIPPET: javadoc -->
+ * <p/>
+ * <!-- START SNIPPET: parameters -->
+ * <ul>
+ * <li>fieldName - The field name this validator is validating. Required if using Plain-Validator Syntax otherwise not required</li>
+ * </ul>
+ * <!-- END SNIPPET: parameters -->
+ *
+ * <!-- START SNIPPET: example -->
+ * <pre>
+ * <!-- Plain Validator Syntax -->
+ * <validator type="conversion">
+ * <param name="fieldName">myField</param>
+ * <message>Conversion Error Occurred</message>
+ * </validator>
+ *
+ * <!-- Field Validator Syntax -->
+ * <field name="myField">
+ * <field-validator type="conversion">
+ * <message>Conversion Error Occurred</message>
+ * </field-validator>
+ * </field>
+ * </pre>
+ * <!-- END SNIPPET: example -->
+ *
+ * @author Jason Carreira
+ * @author tm_jee
+ *
+ * @version $Date $Id: ConversionErrorFieldValidator.java 1209415 2011-12-02 11:24:48Z lukaszlenart $
+ */
+public class ConversionErrorFieldValidator extends RepopulateConversionErrorFieldValidatorSupport {
+
+ /**
+ * The validation implementation must guarantee that setValidatorContext will
+ * be called with a non-null ValidatorContext before validate is called.
+ *
+ * @param object
+ * @throws ValidationException
+ */
+ @Override
+ public void doValidate(Object object) throws ValidationException {
+ String fieldName = getFieldName();
+ String fullFieldName = getValidatorContext().getFullFieldName(fieldName);
+ ActionContext context = ActionContext.getContext();
+ Map<String, Object> conversionErrors = context.getConversionErrors();
+
+ if (conversionErrors.containsKey(fullFieldName)) {
+ if ((defaultMessage == null) || ("".equals(defaultMessage.trim()))) {
+ defaultMessage = XWorkConverter.getConversionErrorMessage(fullFieldName, context.getValueStack());
+ }
+
+ addFieldError(fieldName, object);
+ }
+ }
+
+}
Added: struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/DateRangeFieldValidator.java
URL: http://svn.apache.org/viewvc/struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/DateRangeFieldValidator.java?rev=1209569&view=auto
==============================================================================
--- struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/DateRangeFieldValidator.java (added)
+++ struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/DateRangeFieldValidator.java Fri Dec 2 16:33:03 2011
@@ -0,0 +1,104 @@
+/*
+ * Copyright 2002-2006,2009 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.struts2.xwork2.validator.validators;
+
+import java.util.Date;
+
+
+/**
+ * <!-- START SNIPPET: javadoc -->
+ *
+ * Field Validator that checks if the date supplied is within a specific range.
+ *
+ * <b>NOTE:</b> If no date converter is specified, XWorkBasicConverter will kick
+ * in to do the date conversion, which by default using the <code>Date.SHORT</code> format using
+ * the a programmatically specified locale else falling back to the system
+ * default locale.
+ *
+ *
+ * <!-- END SNIPPET: javadoc -->
+ *
+ * <p/>
+ *
+ * <!-- START SNIPPET: parameters -->
+ * <ul>
+ * <li>fieldName - The field name this validator is validating. Required if using Plain-Validator Syntax otherwise not required</li>
+ * <li>min - the min date range. If not specified will not be checked.</li>
+ * <li>max - the max date range. If not specified will not be checked.</li>
+ * </ul>
+ * <!-- END SNIPPET: parameters -->
+ *
+ *
+ * <pre>
+ * <!-- START SNIPPET: examples -->
+ * <validators>
+ * <!-- Plain Validator syntax -->
+ * <validator type="date">
+ * <param name="fieldName">birthday</param>
+ * <param name="min">01/01/1990</param>
+ * <param name="max">01/01/2000</param>
+ * <message>Birthday must be within ${min} and ${max}</message>
+ * </validator>
+ *
+ * <!-- Field Validator Syntax -->
+ * <field name="birthday">
+ * <field-validator type="date">
+ * <param name="min">01/01/1990</param>
+ * <param name="max">01/01/2000</param>
+ * <message>Birthday must be within ${min} and ${max}</message>
+ * </field>
+ * </field>
+ *
+ * </validators>
+ * <!-- END SNIPPET: examples -->
+ * </pre>
+ *
+ *
+ * @author Jason Carreira
+ * @version $Date: 2011-12-02 12:24:48 +0100 (Fri, 02 Dec 2011) $ $Id: DateRangeFieldValidator.java 1209415 2011-12-02 11:24:48Z lukaszlenart $
+ */
+public class DateRangeFieldValidator extends AbstractRangeValidator {
+
+ private Date max;
+ private Date min;
+
+
+ public void setMax(Date max) {
+ this.max = max;
+ }
+
+ public Date getMax() {
+ return max;
+ }
+
+ public void setMin(Date min) {
+ this.min = min;
+ }
+
+ public Date getMin() {
+ return min;
+ }
+
+ @Override
+ protected Comparable getMaxComparatorValue() {
+ return max;
+ }
+
+ @Override
+ protected Comparable getMinComparatorValue() {
+ return min;
+ }
+}
Added: struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/DoubleRangeFieldValidator.java
URL: http://svn.apache.org/viewvc/struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/DoubleRangeFieldValidator.java?rev=1209569&view=auto
==============================================================================
--- struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/DoubleRangeFieldValidator.java (added)
+++ struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/DoubleRangeFieldValidator.java Fri Dec 2 16:33:03 2011
@@ -0,0 +1,160 @@
+/*
+ * Copyright 2002-2006,2009 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.struts2.xwork2.validator.validators;
+
+import org.apache.struts2.xwork2.validator.ValidationException;
+
+/**
+ * <!-- START SNIPPET: javadoc -->
+ * Field Validator that checks if the double specified is within a certain range.
+ * <!-- END SNIPPET: javadoc -->
+ *
+ *
+ * <!-- START SNIPPET: parameters -->
+ * <ul>
+ * <li>fieldName - The field name this validator is validating. Required if using
+Plain-Validator Syntax otherwise not required</li>
+ * <li>minInclusive - the minimum inclusive value in FloatValue format specified by Java language (if none is specified, it will
+not be checked) </li>
+ * <li>maxInclusive - the maximum inclusive value in FloatValue format specified by Java language (if none is specified, it will
+not be checked) </li>
+ * <li>minExclusive - the minimum exclusive value in FloatValue format specified by Java language (if none is specified, it will
+not be checked) </li>
+ * <li>maxExclusive - the maximum exclusive value in FloatValue format specified by Java language (if none is specified, it will
+not be checked) </li>
+ * </ul>
+ * <!-- END SNIPPET: parameters -->
+ *
+ *
+ * <pre>
+ * <!-- START SNIPPET: examples -->
+ * <validators>
+ * <!-- Plain Validator Syntax -->
+ * <validator type="double">
+ * <param name="fieldName">percentage</param>
+ * <param name="minInclusive">20.1</param>
+ * <param name="maxInclusive">50.1</param>
+ * <message>Age needs to be between ${minInclusive} and
+${maxInclusive} (inclusive)</message>
+ * </validator>
+ *
+ * <!-- Field Validator Syntax -->
+ * <field name="percentage">
+ * <field-validator type="double">
+ * <param name="minExclusive">0.123</param>
+ * <param name="maxExclusive">99.98</param>
+ * <message>Percentage needs to be between ${minExclusive}
+and ${maxExclusive} (exclusive)</message>
+ * </field-validator>
+ * </field>
+ * </validators>
+ * <!-- END SNIPPET: examples -->
+ * </pre>
+ *
+ * @author Rainer Hermanns
+ * @author Rene Gielen
+ *
+ * @version $Id: DoubleRangeFieldValidator.java 1209415 2011-12-02 11:24:48Z lukaszlenart $
+ */
+// START SNIPPET: field-level-validator
+public class DoubleRangeFieldValidator extends FieldValidatorSupport {
+
+ String maxInclusive = null;
+ String minInclusive = null;
+ String minExclusive = null;
+ String maxExclusive = null;
+
+ Double maxInclusiveValue = null;
+ Double minInclusiveValue = null;
+ Double minExclusiveValue = null;
+ Double maxExclusiveValue = null;
+
+ public void validate(Object object) throws ValidationException {
+ String fieldName = getFieldName();
+ Double value;
+ try {
+ Object obj = this.getFieldValue(fieldName, object);
+ if (obj == null) {
+ return;
+ }
+ value = Double.valueOf(obj.toString());
+ } catch (NumberFormatException e) {
+ return;
+ }
+
+ parseParameterValues();
+ if ((maxInclusiveValue != null && value.compareTo(maxInclusiveValue) > 0) ||
+ (minInclusiveValue != null && value.compareTo(minInclusiveValue) < 0) ||
+ (maxExclusiveValue != null && value.compareTo(maxExclusiveValue) >= 0) ||
+ (minExclusiveValue != null && value.compareTo(minExclusiveValue) <= 0)) {
+ addFieldError(fieldName, object);
+ }
+ }
+
+ private void parseParameterValues() {
+ this.minInclusiveValue = parseDouble(minInclusive);
+ this.maxInclusiveValue = parseDouble(maxInclusive);
+ this.minExclusiveValue = parseDouble(minExclusive);
+ this.maxExclusiveValue = parseDouble(maxExclusive);
+ }
+
+ private Double parseDouble (String value) {
+ if (value != null) {
+ try {
+ return Double.valueOf(value);
+ } catch (NumberFormatException e) {
+ if (log.isWarnEnabled()) {
+ log.warn("DoubleRangeFieldValidator - [parseDouble]: Unable to parse given double parameter " + value);
+ }
+ }
+ }
+ return null;
+ }
+
+ public void setMaxInclusive(String maxInclusive) {
+ this.maxInclusive = maxInclusive;
+ }
+
+ public String getMaxInclusive() {
+ return maxInclusive;
+ }
+
+ public void setMinInclusive(String minInclusive) {
+ this.minInclusive = minInclusive;
+ }
+
+ public String getMinInclusive() {
+ return minInclusive;
+ }
+
+ public String getMinExclusive() {
+ return minExclusive;
+ }
+
+ public void setMinExclusive(String minExclusive) {
+ this.minExclusive = minExclusive;
+ }
+
+ public String getMaxExclusive() {
+ return maxExclusive;
+ }
+
+ public void setMaxExclusive(String maxExclusive) {
+ this.maxExclusive = maxExclusive;
+ }
+}
+// END SNIPPET: field-level-validator
Added: struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/EmailValidator.java
URL: http://svn.apache.org/viewvc/struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/EmailValidator.java?rev=1209569&view=auto
==============================================================================
--- struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/EmailValidator.java (added)
+++ struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/EmailValidator.java Fri Dec 2 16:33:03 2011
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2002-2006,2009 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.struts2.xwork2.validator.validators;
+
+
+/**
+ * <!-- START SNIPPET: javadoc -->
+ * EmailValidator checks that a given String field, if not empty,
+ * is a valid email address.
+ * <p/>
+ * <p/>
+ * The regular expression used to validate that the string is an email address
+ * is:
+ * </p>
+ * <pre>
+ * \\b(^[_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)|(\\..{2,3})|(\\..{2,3}\\..{2,3}))$)\\b
+ * </pre>
+ * <!-- END SNIPPET: javadoc -->
+ *
+ *
+ * <!-- START SNIPPET: parameters -->
+ * <ul>
+ * <li>fieldName - The field name this validator is validating. Required if using Plain-Validator Syntax otherwise not required</li>
+ * </ul>
+ * <!-- END SNIPPET: parameters -->
+ *
+ *
+ * <pre>
+ * <!-- START SNIPPET: example -->
+ * <!-- Plain Validator Syntax -->
+ * <validators>
+ * <validator type="email">
+ * <param name="fieldName">myEmail</param>
+ * <message>Must provide a valid email</message>
+ * </validator>
+ * </validators>
+ *
+ * <!-- Field Validator Syntax -->
+ * <field name="myEmail">
+ * <field-validator type="email">
+ * <message>Must provide a valid email</message>
+ * </field-validator>
+ * </field>
+ * <!-- END SNIPPET: example -->
+ * </pre>
+ *
+ * @author jhouse
+ * @author tm_jee
+ * @version $Date: 2011-12-02 12:24:48 +0100 (Fri, 02 Dec 2011) $ $Id: EmailValidator.java 1209415 2011-12-02 11:24:48Z lukaszlenart $
+ */
+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-]+)*((\\.[A-Za-z0-9]{2,})|(\\.[A-Za-z0-9]{2,}\\.[A-Za-z0-9]{2,}))$)\\b";
+
+ public EmailValidator() {
+ setExpression(emailAddressPattern);
+ setCaseSensitive(false);
+ }
+
+}
+
+
Added: struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/ExpressionValidator.java
URL: http://svn.apache.org/viewvc/struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/ExpressionValidator.java?rev=1209569&view=auto
==============================================================================
--- struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/ExpressionValidator.java (added)
+++ struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/ExpressionValidator.java Fri Dec 2 16:33:03 2011
@@ -0,0 +1,86 @@
+/*
+ * Copyright 2002-2006,2009 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.struts2.xwork2.validator.validators;
+
+import org.apache.struts2.xwork2.validator.ValidationException;
+
+
+/**
+ * <!-- START SNIPPET: javadoc -->
+ * A Non-Field Level validator that validates based on regular expression supplied.
+ * <!-- END SNIPPET: javadoc -->
+ * <p/>
+ *
+ * <!-- START SNIPPET: parameters -->
+ * <ul>
+ * <li>expression - the Ognl expression to be evaluated against the stack (Must evaluate to a Boolean)</li>
+ * </ul>
+ * <!-- END SNIPPET: parameters -->
+ *
+ *
+ * <pre>
+ * <!-- START SNIPPET: example -->
+ * <validators>
+ * <validator type="expression">
+ * <param name="expression"> .... </param>
+ * <message>Failed to meet Ognl Expression .... </message>
+ * </validator>
+ * </validators>
+ * <!-- END SNIPPET: example -->
+ * </pre>
+ *
+ * @author Jason Carreira
+ */
+// START SNIPPET: global-level-validator
+public class ExpressionValidator extends ValidatorSupport {
+
+ private String expression;
+
+
+ public void setExpression(String expression) {
+ this.expression = expression;
+ }
+
+ public String getExpression() {
+ return expression;
+ }
+
+ public void validate(Object object) throws ValidationException {
+ Boolean answer = Boolean.FALSE;
+ Object obj = null;
+
+ try {
+ obj = getFieldValue(expression, object);
+ } catch (ValidationException e) {
+ throw e;
+ } catch (Exception e) {
+ // let this pass, but it will be logged right below
+ }
+
+ if ((obj != null) && (obj instanceof Boolean)) {
+ answer = (Boolean) obj;
+ } else {
+ log.warn("Got result of " + obj + " when trying to get Boolean.");
+ }
+
+ if (!answer.booleanValue()) {
+ if (log.isDebugEnabled()) log.debug("Validation failed on expression " + expression + " with validated object "+ object);
+ addActionError(object);
+ }
+ }
+}
+// END SNIPPET: global-level-validator
+
Added: struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/FieldExpressionValidator.java
URL: http://svn.apache.org/viewvc/struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/FieldExpressionValidator.java?rev=1209569&view=auto
==============================================================================
--- struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/FieldExpressionValidator.java (added)
+++ struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/FieldExpressionValidator.java Fri Dec 2 16:33:03 2011
@@ -0,0 +1,98 @@
+/*
+ * Copyright 2002-2006,2009 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.struts2.xwork2.validator.validators;
+
+import org.apache.struts2.xwork2.validator.ValidationException;
+
+
+/**
+ * <!-- START SNIPPET: javadoc -->
+ * Validates a field using an OGNL expression.
+ * <!-- END SNIPPET: javadoc -->
+ * <p/>
+ *
+ * <!-- START SNIPPET: parameters -->
+ * <ul>
+ * <li>fieldName - The field name this validator is validating. Required if using Plain-Validator Syntax otherwise not required</li>
+ * <li>expression - The Ognl expression (must evaluate to a boolean) which is to be evalidated the stack</li>
+ * </ul>
+ * <!-- END SNIPPET: parameters -->
+ *
+ * <pre>
+ * <!-- START SNIPPET: example -->
+ * <!-- Plain Validator Syntax -->
+ * <validators>
+ * <!-- Plain Validator Syntax -->
+ * <validator type="fieldexpression">
+ * <param name="fieldName">myField</param>
+ * <param name="expression"><![CDATA[#myCreditLimit > #myGirfriendCreditLimit]]></param>
+ * <message>My credit limit should be MORE than my girlfriend</message>
+ * <validator>
+ *
+ * <!-- Field Validator Syntax -->
+ * <field name="myField">
+ * <field-validator type="fieldexpression">
+ * <param name="expression"><![CDATA[#myCreditLimit > #myGirfriendCreditLimit]]></param>
+ * <message>My credit limit should be MORE than my girlfriend</message>
+ * </field-validator>
+ * </field>
+ *
+ * </vaidators>
+ * <!-- END SNIPPET: example -->
+ * </pre>
+ *
+ *
+ * @author $Author: lukaszlenart $
+ * @version $Revision: 1209415 $
+ */
+public class FieldExpressionValidator extends FieldValidatorSupport {
+
+ private String expression;
+
+
+ public void setExpression(String expression) {
+ this.expression = expression;
+ }
+
+ public String getExpression() {
+ return expression;
+ }
+
+ public void validate(Object object) throws ValidationException {
+ String fieldName = getFieldName();
+
+ Boolean answer = Boolean.FALSE;
+ Object obj = null;
+
+ try {
+ obj = getFieldValue(expression, object);
+ } catch (ValidationException e) {
+ throw e;
+ } catch (Exception e) {
+ // let this pass, but it will be logged right below
+ }
+
+ if ((obj != null) && (obj instanceof Boolean)) {
+ answer = (Boolean) obj;
+ } else {
+ log.warn("Got result of " + obj + " when trying to get Boolean.");
+ }
+
+ if (!answer.booleanValue()) {
+ addFieldError(fieldName, object);
+ }
+ }
+}
Added: struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/FieldValidatorSupport.java
URL: http://svn.apache.org/viewvc/struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/FieldValidatorSupport.java?rev=1209569&view=auto
==============================================================================
--- struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/FieldValidatorSupport.java (added)
+++ struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/FieldValidatorSupport.java Fri Dec 2 16:33:03 2011
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2002-2006,2009 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.struts2.xwork2.validator.validators;
+
+import org.apache.struts2.xwork2.validator.FieldValidator;
+
+
+/**
+ * Base class for field validators.
+ *
+ * @author Jason Carreira
+ */
+public abstract class FieldValidatorSupport extends ValidatorSupport implements FieldValidator {
+
+ private String fieldName;
+ private String type;
+
+ public void setFieldName(String fieldName) {
+ this.fieldName = fieldName;
+ }
+
+ public String getFieldName() {
+ return fieldName;
+ }
+
+ @Override
+ public void setValidatorType(String type) {
+ this.type = type;
+ }
+
+ @Override
+ public String getValidatorType() {
+ return type;
+ }
+}
Added: struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/IntRangeFieldValidator.java
URL: http://svn.apache.org/viewvc/struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/IntRangeFieldValidator.java?rev=1209569&view=auto
==============================================================================
--- struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/IntRangeFieldValidator.java (added)
+++ struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/IntRangeFieldValidator.java Fri Dec 2 16:33:03 2011
@@ -0,0 +1,93 @@
+/*
+ * Copyright 2002-2006,2009 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.struts2.xwork2.validator.validators;
+
+
+/**
+ * <!-- START SNIPPET: javadoc -->
+ * Field Validator that checks if the integer specified is within a certain range.
+ * <!-- END SNIPPET: javadoc -->
+ *
+ *
+ * <!-- START SNIPPET: parameters -->
+ * <ul>
+ * <li>fieldName - The field name this validator is validating. Required if using Plain-Validator Syntax otherwise not required</li>
+ * <li>min - the minimum value (if none is specified, it will not be checked) </li>
+ * <li>max - the maximum value (if none is specified, it will not be checked) </li>
+ * </ul>
+ * <!-- END SNIPPET: parameters -->
+ *
+ *
+ * <pre>
+ * <!-- START SNIPPET: examples -->
+ * <validators>
+ * <!-- Plain Validator Syntax -->
+ * <validator type="int">
+ * <param name="fieldName">age</param>
+ * <param name="min">20</param>
+ * <param name="max">50</param>
+ * <message>Age needs to be between ${min} and ${max}</message>
+ * </validator>
+ *
+ * <!-- Field Validator Syntax -->
+ * <field name="age">
+ * <field-validator type="int">
+ * <param name="min">20</param>
+ * <param name="max">50</param>
+ * <message>Age needs to be between ${min} and ${max}</message>
+ * </field-validator>
+ * </field>
+ * </validators>
+ * <!-- END SNIPPET: examples -->
+ * </pre>
+ *
+ *
+ *
+ * @author Jason Carreira
+ * @version $Date: 2011-12-02 12:24:48 +0100 (Fri, 02 Dec 2011) $ $Id: IntRangeFieldValidator.java 1209415 2011-12-02 11:24:48Z lukaszlenart $
+ */
+public class IntRangeFieldValidator extends AbstractRangeValidator {
+
+ Integer max = null;
+ Integer min = null;
+
+
+ public void setMax(Integer max) {
+ this.max = max;
+ }
+
+ public Integer getMax() {
+ return max;
+ }
+
+ @Override
+ public Comparable getMaxComparatorValue() {
+ return max;
+ }
+
+ public void setMin(Integer min) {
+ this.min = min;
+ }
+
+ public Integer getMin() {
+ return min;
+ }
+
+ @Override
+ public Comparable getMinComparatorValue() {
+ return min;
+ }
+}
Added: struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/LongRangeFieldValidator.java
URL: http://svn.apache.org/viewvc/struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/LongRangeFieldValidator.java?rev=1209569&view=auto
==============================================================================
--- struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/LongRangeFieldValidator.java (added)
+++ struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/LongRangeFieldValidator.java Fri Dec 2 16:33:03 2011
@@ -0,0 +1,92 @@
+/*
+ * Copyright 2002-2006,2009 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.struts2.xwork2.validator.validators;
+
+
+/**
+ * <!-- START SNIPPET: javadoc -->
+ * Field Validator that checks if the long specified is within a certain range.
+ * <!-- END SNIPPET: javadoc -->
+ *
+ *
+ * <!-- START SNIPPET: parameters -->
+ * <ul>
+ * <li>fieldName - The field name this validator is validating. Required if using Plain-Validator Syntax otherwise not required</li>
+ * <li>min - the minimum value (if none is specified, it will not be checked) </li>
+ * <li>max - the maximum value (if none is specified, it will not be checked) </li>
+ * </ul>
+ * <!-- END SNIPPET: parameters -->
+ *
+ *
+ * <pre>
+ * <!-- START SNIPPET: examples -->
+ * <validators>
+ * <!-- Plain Validator Syntax -->
+ * <validator type="long">
+ * <param name="fieldName">age</param>
+ * <param name="min">20</param>
+ * <param name="max">50</param>
+ * <message>Age needs to be between ${min} and ${max}</message>
+ * </validator>
+ *
+ * <!-- Field Validator Syntax -->
+ * <field name="age">
+ * <field-validator type="long">
+ * <param name="min">20</param>
+ * <param name="max">50</param>
+ * <message>Age needs to be between ${min} and ${max}</message>
+ * </field-validator>
+ * </field>
+ * </validators>
+ * <!-- END SNIPPET: examples -->
+ * </pre>
+ *
+ *
+ *
+ * @version $Date: 2011-12-02 12:24:48 +0100 (Fri, 02 Dec 2011) $
+ */
+public class LongRangeFieldValidator extends AbstractRangeValidator {
+
+ Long max = null;
+ Long min = null;
+
+
+ public void setMax(Long max) {
+ this.max = max;
+ }
+
+ public Long getMax() {
+ return max;
+ }
+
+ @Override
+ public Comparable getMaxComparatorValue() {
+ return max;
+ }
+
+ public void setMin(Long min) {
+ this.min = min;
+ }
+
+ public Long getMin() {
+ return min;
+ }
+
+ @Override
+ public Comparable getMinComparatorValue() {
+ return min;
+ }
+}
Added: struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/RegexFieldValidator.java
URL: http://svn.apache.org/viewvc/struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/RegexFieldValidator.java?rev=1209569&view=auto
==============================================================================
--- struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/RegexFieldValidator.java (added)
+++ struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/RegexFieldValidator.java Fri Dec 2 16:33:03 2011
@@ -0,0 +1,154 @@
+/*
+ * Copyright 2002-2006,2009 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.struts2.xwork2.validator.validators;
+
+import org.apache.struts2.xwork2.validator.ValidationException;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * <!-- START SNIPPET: javadoc -->
+ * Validates a string field using a regular expression.
+ * <!-- END SNIPPET: javadoc -->
+ * <p/>
+ *
+ *
+ * <!-- START SNIPPET: parameters -->
+ * <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>
+ * <li>trim - Boolean (Optional). Sets whether the expression should be trimed before matching. Default is <code>true</code>.</li>
+ * </ul>
+ * <!-- END SNIPPET: parameters -->
+ *
+ *
+ * <pre>
+ * <!-- START SNIPPET: example -->
+ * <validators>
+ * <!-- Plain Validator Syntax -->
+ * <validator type="regex">
+ * <param name="fieldName">myStrangePostcode</param>
+ * <param name="expression"><![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>
+ * </field-validator>
+ * </field>
+ * </validators>
+ * <!-- END SNIPPET: example -->
+ * </pre>
+ *
+ * @author Quake Wang
+ * @version $Date: 2011-12-02 12:24:48 +0100 (Fri, 02 Dec 2011) $ $Revision: 1209415 $
+ */
+public class RegexFieldValidator extends FieldValidatorSupport {
+
+ private String expression;
+ private boolean caseSensitive = true;
+ private boolean trim = 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 || expression == null) {
+ return;
+ }
+
+ // XW-375 - must be a string
+ if (!(value instanceof String)) {
+ return;
+ }
+
+ // 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);
+ }
+
+ String compare = (String) value;
+ if ( trim ) {
+ compare = compare.trim();
+ }
+ Matcher matcher = pattern.matcher( compare );
+
+ 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;
+ }
+
+ /**
+ * @return Returns whether the expression should be trimed before matching.
+ * Default is <code>true</code>.
+ */
+ public boolean isTrimed() {
+ return trim;
+ }
+
+ /**
+ * Sets whether the expression should be trimed before matching.
+ * Default is <code>true</code>.
+ */
+ public void setTrim(boolean trim) {
+ this.trim = trim;
+ }
+
+}
Added: struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/RepopulateConversionErrorFieldValidatorSupport.java
URL: http://svn.apache.org/viewvc/struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/RepopulateConversionErrorFieldValidatorSupport.java?rev=1209569&view=auto
==============================================================================
--- struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/RepopulateConversionErrorFieldValidatorSupport.java (added)
+++ struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/RepopulateConversionErrorFieldValidatorSupport.java Fri Dec 2 16:33:03 2011
@@ -0,0 +1,209 @@
+/*
+ * Copyright 2002-2006,2009 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.struts2.xwork2.validator.validators;
+
+import org.apache.struts2.xwork2.ActionContext;
+import org.apache.struts2.xwork2.ActionInvocation;
+import org.apache.struts2.xwork2.interceptor.PreResultListener;
+import org.apache.struts2.xwork2.util.ValueStack;
+import org.apache.struts2.xwork2.util.logging.Logger;
+import org.apache.struts2.xwork2.util.logging.LoggerFactory;
+import org.apache.struts2.xwork2.validator.ValidationException;
+import org.apache.commons.lang.StringEscapeUtils;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+/**
+ *
+ *
+ * An abstract base class that adds in the capability to populate the stack with
+ * a fake parameter map when a conversion error has occurred and the 'repopulateField'
+ * property is set to "true".
+ *
+ * <p/>
+ *
+ *
+ * <!-- START SNIPPET: javadoc -->
+ *
+ * The capability of auto-repopulating the stack with a fake parameter map when
+ * a conversion error has occurred can be done with 'repopulateField' property
+ * set to "true".
+ *
+ * <p/>
+ *
+ * This is typically usefull when one wants to repopulate the field with the original value
+ * when a conversion error occurred. Eg. with a textfield that only allows an Integer
+ * (the action class have an Integer field declared), upon conversion error, the incorrectly
+ * entered integer (maybe a text 'one') will not appear when dispatched back. With 'repopulateField'
+ * porperty set to true, it will, meaning the textfield will have 'one' as its value
+ * upon conversion error.
+ *
+ * <!-- END SNIPPET: javadoc -->
+ *
+ * <p/>
+ *
+ * <pre>
+ * <!-- START SNIPPET: exampleJspPage -->
+ *
+ * <!-- myJspPage.jsp -->
+ * <ww:form action="someAction" method="POST">
+ * ....
+ * <ww:textfield
+ * label="My Integer Field"
+ * name="myIntegerField" />
+ * ....
+ * <ww:submit />
+ * </ww:form>
+ *
+ * <!-- END SNIPPET: exampleJspPage -->
+ * </pre>
+ *
+ * <pre>
+ * <!-- START SNIPPET: exampleXwork -->
+ *
+ * <!-- xwork.xml -->
+ * <xwork>
+ * <include file="xwork-default.xml" />
+ * ....
+ * <package name="myPackage" extends="xwork-default">
+ * ....
+ * <action name="someAction" class="example.MyActionSupport.java">
+ * <result name="input">myJspPage.jsp</result>
+ * <result>success.jsp</result>
+ * </action>
+ * ....
+ * </package>
+ * ....
+ * </xwork>
+ *
+ * <!-- END SNIPPET:exampleXwork -->
+ * </pre>
+ *
+ *
+ * <pre>
+ * <!-- START SNIPPET: exampleJava -->
+ *
+ * <!-- MyActionSupport.java -->
+ * public class MyActionSupport extends ActionSupport {
+ * private Integer myIntegerField;
+ *
+ * public Integer getMyIntegerField() { return this.myIntegerField; }
+ * public void setMyIntegerField(Integer myIntegerField) {
+ * this.myIntegerField = myIntegerField;
+ * }
+ * }
+ *
+ * <!-- END SNIPPET: exampleJava -->
+ * </pre>
+ *
+ *
+ * <pre>
+ * <!-- START SNIPPET: exampleValidation -->
+ *
+ * <!-- MyActionSupport-someAction-validation.xml -->
+ * <validators>
+ * ...
+ * <field name="myIntegerField">
+ * <field-validator type="conversion">
+ * <param name="repopulateField">true</param>
+ * <message>Conversion Error (Integer Wanted)</message>
+ * </field-validator>
+ * </field>
+ * ...
+ * </validators>
+ *
+ * <!-- END SNIPPET: exampleValidation -->
+ * </pre>
+ *
+ * @author tm_jee
+ * @version $Date: 2011-12-02 12:24:48 +0100 (Fri, 02 Dec 2011) $ $Id: RepopulateConversionErrorFieldValidatorSupport.java 1209415 2011-12-02 11:24:48Z lukaszlenart $
+ */
+public abstract class RepopulateConversionErrorFieldValidatorSupport extends FieldValidatorSupport {
+
+ private static final Logger LOG = LoggerFactory.getLogger(RepopulateConversionErrorFieldValidatorSupport.class);
+
+ private String repopulateFieldAsString = "false";
+ private boolean repopulateFieldAsBoolean = false;
+
+ public String getRepopulateField() {
+ return repopulateFieldAsString;
+ }
+
+ public void setRepopulateField(String repopulateField) {
+ this.repopulateFieldAsString = repopulateField == null ? repopulateField : repopulateField.trim();
+ this.repopulateFieldAsBoolean = "true".equalsIgnoreCase(this.repopulateFieldAsString) ? (true) : (false);
+ }
+
+ public void validate(Object object) throws ValidationException {
+ doValidate(object);
+ if (repopulateFieldAsBoolean) {
+ repopulateField(object);
+ }
+ }
+
+ public void repopulateField(Object object) throws ValidationException {
+
+ ActionInvocation invocation = ActionContext.getContext().getActionInvocation();
+ Map<String, Object> conversionErrors = ActionContext.getContext().getConversionErrors();
+
+ String fieldName = getFieldName();
+ String fullFieldName = getValidatorContext().getFullFieldName(fieldName);
+ if (conversionErrors.containsKey(fullFieldName)) {
+ Object value = conversionErrors.get(fullFieldName);
+
+ final Map<Object, Object> fakeParams = new LinkedHashMap<Object, Object>();
+ boolean doExprOverride = false;
+
+ if (value instanceof String[]) {
+ // take the first element, if possible
+ String[] tmpValue = (String[]) value;
+ if ((tmpValue.length > 0)) {
+ doExprOverride = true;
+ fakeParams.put(fullFieldName, escape(tmpValue[0]));
+ } else {
+ if (LOG.isWarnEnabled()) {
+ LOG.warn("value is an empty array of String or with first element in it as null [" + value + "], will not repopulate conversion error ");
+ }
+ }
+ } else if (value instanceof String) {
+ String tmpValue = (String) value;
+ doExprOverride = true;
+ fakeParams.put(fullFieldName, escape(tmpValue));
+ } else {
+ // opps... it should be
+ if (LOG.isWarnEnabled()) {
+ LOG.warn("conversion error value is not a String or array of String but instead is [" + value + "], will not repopulate conversion error");
+ }
+ }
+
+ if (doExprOverride) {
+ invocation.addPreResultListener(new PreResultListener() {
+ public void beforeResult(ActionInvocation invocation, String resultCode) {
+ ValueStack stack = ActionContext.getContext().getValueStack();
+ stack.setExprOverrides(fakeParams);
+ }
+ });
+ }
+ }
+ }
+
+ protected String escape(String value) {
+ return "\"" + StringEscapeUtils.escapeJava(value) + "\"";
+ }
+
+ protected abstract void doValidate(Object object) throws ValidationException;
+}
Added: struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/RequiredFieldValidator.java
URL: http://svn.apache.org/viewvc/struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/RequiredFieldValidator.java?rev=1209569&view=auto
==============================================================================
--- struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/RequiredFieldValidator.java (added)
+++ struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/RequiredFieldValidator.java Fri Dec 2 16:33:03 2011
@@ -0,0 +1,72 @@
+/*
+ * Copyright 2002-2006,2009 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.struts2.xwork2.validator.validators;
+
+import org.apache.struts2.xwork2.validator.ValidationException;
+
+
+/**
+ * <!-- START SNIPPET: javadoc -->
+ * RequiredFieldValidator checks if the specified field is not null.
+ * <!-- END SNIPPET: javadoc -->
+ * <p/>
+ *
+ *
+ * <!-- START SNIPPET: parameters -->
+ * <ul>
+ * <li>fieldName - field name if plain-validator syntax is used, not needed if field-validator syntax is used</li>
+ * </ul>
+ * <!-- END SNIPPET: parameters -->
+ *
+ *
+ * <pre>
+ * <!-- START SNIPPET: example -->
+ * <validators>
+ *
+ * <!-- Plain Validator Syntax -->
+ * <validator type="required">
+ * <param name="fieldName">username</param>
+ * <message>username must not be null</message>
+ * </validator>
+ *
+ *
+ * <!-- Field Validator Syntax -->
+ * <field name="username">
+ * <field-validator type="required">
+ * <message>username must not be null</message>
+ * </field-validator>
+ * </field>
+ *
+ * </validators>
+ * <!-- END SNIPPET: example -->
+ * </pre>
+ *
+ *
+ *
+ * @author rainerh
+ * @version $Revision: 1209415 $
+ */
+public class RequiredFieldValidator extends FieldValidatorSupport {
+
+ public void validate(Object object) throws ValidationException {
+ String fieldName = getFieldName();
+ Object value = this.getFieldValue(fieldName, object);
+
+ if (value == null) {
+ addFieldError(fieldName, object);
+ }
+ }
+}
Added: struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/RequiredStringValidator.java
URL: http://svn.apache.org/viewvc/struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/RequiredStringValidator.java?rev=1209569&view=auto
==============================================================================
--- struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/RequiredStringValidator.java (added)
+++ struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/RequiredStringValidator.java Fri Dec 2 16:33:03 2011
@@ -0,0 +1,92 @@
+/*
+ * Copyright 2002-2006,2009 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.struts2.xwork2.validator.validators;
+
+import org.apache.struts2.xwork2.validator.ValidationException;
+
+
+/**
+ * <!-- START SNIPPET: javadoc -->
+ * RequiredStringValidator checks that a String field is non-null and has a length > 0.
+ * (i.e. it isn't ""). The "trim" parameter determines whether it will {@link String#trim() trim}
+ * the String before performing the length check. If unspecified, the String will be trimmed.
+ * <!-- END SNIPPET: javadoc -->
+ * <p/>
+ *
+ * <!-- START SNIPPET: parameters -->
+ * <ul>
+ * <li>fieldName - The field name this validator is validating. Required if using Plain-Validator Syntax otherwise not required</li>
+ * <li>trim - trim the field name value before validating (default is true)</li>
+ * </ul>
+ * <!-- END SNIPPET: parameters -->
+ *
+ *
+ * <pre>
+ * <!-- START SNIPPET: examples -->
+ * <validators>
+ * <!-- Plain-Validator Syntax -->
+ * <validator type="requiredstring">
+ * <param name="fieldName">username</param>
+ * <param name="trim">true</param>
+ * <message>username is required</message>
+ * </validator>
+ *
+ * <!-- Field-Validator Syntax -->
+ * <field name="username">
+ * <field-validator type="requiredstring">
+ * <param name="trim">true</param>
+ * <message>username is required</message>
+ * </field-validator>
+ * </field>
+ * </validators>
+ * <!-- END SNIPPET: examples -->
+ * </pre>
+ *
+ * @author rainerh
+ * @version $Date: 2011-12-02 12:24:48 +0100 (Fri, 02 Dec 2011) $ $Id: RequiredStringValidator.java 1209415 2011-12-02 11:24:48Z lukaszlenart $
+ */
+public class RequiredStringValidator extends FieldValidatorSupport {
+
+ private boolean doTrim = true;
+
+
+ public void setTrim(boolean trim) {
+ doTrim = trim;
+ }
+
+ public boolean getTrim() {
+ return doTrim;
+ }
+
+ public void validate(Object object) throws ValidationException {
+ String fieldName = getFieldName();
+ Object value = this.getFieldValue(fieldName, object);
+
+ if (!(value instanceof String)) {
+ addFieldError(fieldName, object);
+ } else {
+ String s = (String) value;
+
+ if (doTrim) {
+ s = s.trim();
+ }
+
+ if (s.length() == 0) {
+ addFieldError(fieldName, object);
+ }
+ }
+ }
+}
Added: struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/ShortRangeFieldValidator.java
URL: http://svn.apache.org/viewvc/struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/ShortRangeFieldValidator.java?rev=1209569&view=auto
==============================================================================
--- struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/ShortRangeFieldValidator.java (added)
+++ struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/ShortRangeFieldValidator.java Fri Dec 2 16:33:03 2011
@@ -0,0 +1,92 @@
+/*
+ * Copyright 2002-2006,2009 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.struts2.xwork2.validator.validators;
+
+
+/**
+ * <!-- START SNIPPET: javadoc -->
+ * Field Validator that checks if the short specified is within a certain range.
+ * <!-- END SNIPPET: javadoc -->
+ *
+ *
+ * <!-- START SNIPPET: parameters -->
+ * <ul>
+ * <li>fieldName - The field name this validator is validating. Required if using Plain-Validator Syntax otherwise not required</li>
+ * <li>min - the minimum value (if none is specified, it will not be checked) </li>
+ * <li>max - the maximum value (if none is specified, it will not be checked) </li>
+ * </ul>
+ * <!-- END SNIPPET: parameters -->
+ *
+ *
+ * <pre>
+ * <!-- START SNIPPET: examples -->
+ * <validators>
+ * <!-- Plain Validator Syntax -->
+ * <validator type="short">
+ * <param name="fieldName">age</param>
+ * <param name="min">20</param>
+ * <param name="max">50</param>
+ * <message>Age needs to be between ${min} and ${max}</message>
+ * </validator>
+ *
+ * <!-- Field Validator Syntax -->
+ * <field name="age">
+ * <field-validator type="short">
+ * <param name="min">20</param>
+ * <param name="max">50</param>
+ * <message>Age needs to be between ${min} and ${max}</message>
+ * </field-validator>
+ * </field>
+ * </validators>
+ * <!-- END SNIPPET: examples -->
+ * </pre>
+ *
+ *
+ *
+ * @version $Date: 2011-12-02 12:24:48 +0100 (Fri, 02 Dec 2011) $
+ */
+public class ShortRangeFieldValidator extends AbstractRangeValidator {
+
+ Short max = null;
+ Short min = null;
+
+
+ public void setMax(Short max) {
+ this.max = max;
+ }
+
+ public Short getMax() {
+ return max;
+ }
+
+ @Override
+ public Comparable getMaxComparatorValue() {
+ return max;
+ }
+
+ public void setMin(Short min) {
+ this.min = min;
+ }
+
+ public Short getMin() {
+ return min;
+ }
+
+ @Override
+ public Comparable getMinComparatorValue() {
+ return min;
+ }
+}
Added: struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/StringLengthFieldValidator.java
URL: http://svn.apache.org/viewvc/struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/StringLengthFieldValidator.java?rev=1209569&view=auto
==============================================================================
--- struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/StringLengthFieldValidator.java (added)
+++ struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/StringLengthFieldValidator.java Fri Dec 2 16:33:03 2011
@@ -0,0 +1,125 @@
+/*
+ * Copyright 2002-2006,2009 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.struts2.xwork2.validator.validators;
+
+import org.apache.struts2.xwork2.validator.ValidationException;
+
+/**
+ * <!-- START SNIPPET: javadoc -->
+ * StringLengthFieldValidator checks that a String field is of a certain length. If the "minLength"
+ * parameter is specified, it will make sure that the String has at least that many characters. If
+ * the "maxLength" parameter is specified, it will make sure that the String has at most that many
+ * characters. The "trim" parameter determines whether it will {@link String#trim() trim} the
+ * String before performing the length check. If unspecified, the String will be trimmed.
+ * <!-- END SNIPPET: javadoc -->
+ * <p/>
+ *
+ *
+ * <!-- START SNIPPET: parameters -->
+ * <ul>
+ * <li>fieldName - The field name this validator is validating. Required if using Plain-Validator Syntax otherwise not required</li>
+ * <li>maxLength - The max length of the field value. Default ignore.</li>
+ * <li>minLength - The min length of the field value. Default ignore.</li>
+ * <li>trim - Trim the field value before evaluating its min/max length. Default true</li>
+ * </ul>
+ * <!-- END SNIPPET: parameters -->
+ *
+ *
+ * <pre>
+ * <!--START SNIPPET: example -->
+ * <validators>
+ * <!-- Plain Validator Syntax -->
+ * <validator type="stringlength">
+ * <param name="fieldName">myPurchaseCode</param>
+ * <param name="minLength">10</param>
+ * <param name="maxLength">10</param>
+ * <param name="trim">true</param>
+ * <message>Your purchase code needs to be 10 characters long</message>
+ * </validator>
+ *
+ * <!-- Field Validator Syntax -->
+ * <field name="myPurchaseCode">
+ * <field-validator type="stringlength">
+ * <param name="minLength">10</param>
+ * <param name="maxLength">10</param>
+ * <param name="trim">true</param>
+ * <message>Your purchase code needs to be 10 characters long</message>
+ * </field-validator>
+ * </field>
+ * </validators>
+ * <!-- END SNIPPET: example -->
+ * </pre>
+ *
+ *
+ * @author Jason Carreira
+ * @author Mark Woon
+ * @author tmjee
+ * @version $Date: 2011-12-02 12:24:48 +0100 (Fri, 02 Dec 2011) $ $Id: StringLengthFieldValidator.java 1209415 2011-12-02 11:24:48Z lukaszlenart $
+ */
+public class StringLengthFieldValidator extends FieldValidatorSupport {
+
+ private boolean doTrim = true;
+ private int maxLength = -1;
+ private int minLength = -1;
+
+
+ public void setMaxLength(int maxLength) {
+ this.maxLength = maxLength;
+ }
+
+ public int getMaxLength() {
+ return maxLength;
+ }
+
+ public void setMinLength(int minLength) {
+ this.minLength = minLength;
+ }
+
+ public int getMinLength() {
+ return minLength;
+ }
+
+ public void setTrim(boolean trim) {
+ doTrim = trim;
+ }
+
+ public boolean getTrim() {
+ return doTrim;
+ }
+
+ public void validate(Object object) throws ValidationException {
+ String fieldName = getFieldName();
+ String val = (String) getFieldValue(fieldName, object);
+
+ if (val == null || val.length() <= 0) {
+ // use a required validator for these
+ return;
+ }
+ if (doTrim) {
+ val = val.trim();
+ if (val.length() <= 0) {
+ // use a required validator
+ return;
+ }
+ }
+
+ if ((minLength > -1) && (val.length() < minLength)) {
+ addFieldError(fieldName, object);
+ } else if ((maxLength > -1) && (val.length() > maxLength)) {
+ addFieldError(fieldName, object);
+ }
+ }
+}
Added: struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/URLValidator.java
URL: http://svn.apache.org/viewvc/struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/URLValidator.java?rev=1209569&view=auto
==============================================================================
--- struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/URLValidator.java (added)
+++ struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/URLValidator.java Fri Dec 2 16:33:03 2011
@@ -0,0 +1,80 @@
+/*
+ * Copyright 2002-2006,2009 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.struts2.xwork2.validator.validators;
+
+import org.apache.struts2.xwork2.validator.ValidationException;
+import org.apache.struts2.xwork2.util.URLUtil;
+
+
+/**
+ * <!-- START SNIPPET: javadoc -->
+ *
+ * URLValidator checks that a given field is a String and a valid URL
+ *
+ * <!-- END SNIPPET: javadoc -->
+ *
+ * <p/>
+ *
+ * <!-- START SNIPPET: parameters -->
+ *
+ * <ul>
+ * <li>fieldName - The field name this validator is validating. Required if using Plain-Validator Syntax otherwise not required</li>
+ * </ul>
+ *
+ * <!-- END SNIPPET: parameters -->
+ *
+ * <p/>
+ *
+ * <pre>
+ * <!-- START SNIPPET: examples -->
+ *
+ * <validators>
+ * <!-- Plain Validator Syntax -->
+ * <validator type="url">
+ * <param name="fieldName">myHomePage</param>
+ * <message>Invalid homepage url</message>
+ * </validator>
+ *
+ * <!-- Field Validator Syntax -->
+ * <field name="myHomepage">
+ * <message>Invalid homepage url</message>
+ * </field>
+ * </validators>
+ *
+ * <!-- END SNIPPET: examples -->
+ * </pre>
+ *
+ *
+ * @author $Author: lukaszlenart $
+ * @version $Date: 2011-12-02 12:24:48 +0100 (Fri, 02 Dec 2011) $ $Revision: 1209415 $
+ */
+public class URLValidator extends FieldValidatorSupport {
+
+ 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 || value.toString().length() == 0) {
+ return;
+ }
+
+ if (!(value.getClass().equals(String.class)) || !URLUtil.verifyUrl((String) value)) {
+ addFieldError(fieldName, object);
+ }
+ }
+}
Added: struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/ValidatorSupport.java
URL: http://svn.apache.org/viewvc/struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/ValidatorSupport.java?rev=1209569&view=auto
==============================================================================
--- struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/ValidatorSupport.java (added)
+++ struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/ValidatorSupport.java Fri Dec 2 16:33:03 2011
@@ -0,0 +1,218 @@
+/*
+ * Copyright 2002-2006,2009 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.struts2.xwork2.validator.validators;
+
+import org.apache.struts2.xwork2.util.TextParseUtil;
+import org.apache.struts2.xwork2.util.ValueStack;
+import org.apache.struts2.xwork2.util.logging.Logger;
+import org.apache.struts2.xwork2.util.logging.LoggerFactory;
+import org.apache.struts2.xwork2.validator.DelegatingValidatorContext;
+import org.apache.struts2.xwork2.validator.ShortCircuitableValidator;
+import org.apache.struts2.xwork2.validator.ValidationException;
+import org.apache.struts2.xwork2.validator.Validator;
+import org.apache.struts2.xwork2.validator.ValidatorContext;
+import org.apache.commons.lang.StringUtils;
+
+import java.util.ArrayList;
+import java.util.List;
+
+
+/**
+ * Abstract implementation of the Validator interface suitable for subclassing.
+ *
+ * @author Jason Carreira
+ * @author tm_jee
+ * @author Martin Gilday
+ */
+public abstract class ValidatorSupport implements Validator, ShortCircuitableValidator {
+
+ protected final Logger log = LoggerFactory.getLogger(this.getClass());
+ protected String defaultMessage = "";
+ protected String messageKey;
+ private ValidatorContext validatorContext;
+ private boolean shortCircuit;
+ private boolean parse;
+ private String type;
+ private String[] messageParameters;
+ private ValueStack stack;
+
+
+ public void setValueStack(ValueStack stack) {
+ this.stack = stack;
+ }
+
+ public void setDefaultMessage(String message) {
+ if (StringUtils.isNotEmpty(message)) {
+ this.defaultMessage = message;
+ }
+ }
+
+ public String getDefaultMessage() {
+ return defaultMessage;
+ }
+
+ public void setParse(boolean parse) {
+ this.parse = parse;
+ }
+
+ public boolean getParse() {
+ return parse;
+ }
+
+ public String getMessage(Object object) {
+ String message;
+ boolean pop = false;
+
+ if (!stack.getRoot().contains(object)) {
+ stack.push(object);
+ pop = true;
+ }
+
+ stack.push(this);
+
+ if (messageKey != null) {
+ if ((defaultMessage == null) || ("".equals(defaultMessage.trim()))) {
+ defaultMessage = messageKey;
+ }
+ if (validatorContext == null) {
+ validatorContext = new DelegatingValidatorContext(object);
+ }
+ List<Object> parsedMessageParameters = null;
+ if (messageParameters != null) {
+ parsedMessageParameters = new ArrayList<Object>();
+ for (String messageParameter : messageParameters) {
+ if (messageParameter != null) {
+ try {
+ Object val = stack.findValue(messageParameter);
+ parsedMessageParameters.add(val);
+ } catch (Exception e) {
+ // if there's an exception in parsing, we'll just treat the expression itself as the
+ // parameter
+ log.warn("exception while parsing message parameter [" + messageParameter + "]", e);
+ parsedMessageParameters.add(messageParameter);
+ }
+ }
+ }
+ }
+
+ message = validatorContext.getText(messageKey, defaultMessage, parsedMessageParameters);
+
+ } else {
+ message = defaultMessage;
+ }
+
+ if (StringUtils.isNotBlank(message))
+ message = TextParseUtil.translateVariables(message, stack);
+
+ stack.pop();
+
+ if (pop) {
+ stack.pop();
+ }
+
+ return message;
+ }
+
+ public void setMessageKey(String key) {
+ messageKey = key;
+ }
+
+ public String getMessageKey() {
+ return messageKey;
+ }
+
+ public String[] getMessageParameters() {
+ return this.messageParameters;
+ }
+
+ public void setMessageParameters(String[] messageParameters) {
+ this.messageParameters = messageParameters;
+ }
+
+ public void setShortCircuit(boolean shortcircuit) {
+ shortCircuit = shortcircuit;
+ }
+
+ public boolean isShortCircuit() {
+ return shortCircuit;
+ }
+
+ public void setValidatorContext(ValidatorContext validatorContext) {
+ this.validatorContext = validatorContext;
+ }
+
+ public ValidatorContext getValidatorContext() {
+ return validatorContext;
+ }
+
+ public void setValidatorType(String type) {
+ this.type = type;
+ }
+
+ public String getValidatorType() {
+ return type;
+ }
+
+ /**
+ * Parse <code>expression</code> passed in against value stack. Only parse
+ * when 'parse' param is set to true, else just returns the expression unparsed.
+ *
+ * @param expression
+ * @return Object
+ */
+ protected Object conditionalParse(String expression) {
+ if (parse) {
+ return TextParseUtil.translateVariables('$', expression, stack);
+ }
+ return expression;
+ }
+
+ /**
+ * Return the field value named <code>name</code> from <code>object</code>,
+ * <code>object</code> should have the appropriate getter/setter.
+ *
+ * @param name
+ * @param object
+ * @return Object as field value
+ * @throws ValidationException
+ */
+ protected Object getFieldValue(String name, Object object) throws ValidationException {
+
+ boolean pop = false;
+
+ if (!stack.getRoot().contains(object)) {
+ stack.push(object);
+ pop = true;
+ }
+
+ Object retVal = stack.findValue(name);
+
+ if (pop) {
+ stack.pop();
+ }
+
+ return retVal;
+ }
+
+ protected void addActionError(Object object) {
+ validatorContext.addActionError(getMessage(object));
+ }
+
+ protected void addFieldError(String propertyName, Object object) {
+ validatorContext.addFieldError(propertyName, getMessage(object));
+ }
+
+}
Added: struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/VisitorFieldValidator.java
URL: http://svn.apache.org/viewvc/struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/VisitorFieldValidator.java?rev=1209569&view=auto
==============================================================================
--- struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/VisitorFieldValidator.java (added)
+++ struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/VisitorFieldValidator.java Fri Dec 2 16:33:03 2011
@@ -0,0 +1,216 @@
+/*
+ * Copyright 2002-2006,2009 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.struts2.xwork2.validator.validators;
+
+import org.apache.struts2.xwork2.ActionContext;
+import org.apache.struts2.xwork2.inject.Inject;
+import org.apache.struts2.xwork2.util.ValueStack;
+import org.apache.struts2.xwork2.validator.ActionValidatorManager;
+import org.apache.struts2.xwork2.validator.DelegatingValidatorContext;
+import org.apache.struts2.xwork2.validator.ValidationException;
+import org.apache.struts2.xwork2.validator.ValidatorContext;
+
+import java.util.Collection;
+
+
+/**
+ * <!-- START SNIPPET: javadoc -->
+ * The VisitorFieldValidator allows you to forward validation to object
+ * properties of your action using the object's own validation files. This
+ * allows you to use the ModelDriven development pattern and manage your
+ * validations for your models in one place, where they belong, next to your
+ * model classes. The VisitorFieldValidator can handle either simple Object
+ * properties, Collections of Objects, or Arrays.
+ * <!-- END SNIPPET: javadoc -->
+ * <p/>
+ *
+ * <!-- START SNIPPET: parameters -->
+ * <ul>
+ * <li>fieldName - field name if plain-validator syntax is used, not needed if field-validator syntax is used</li>
+ * <li>context - the context of which validation should take place. Optional</li>
+ * <li>appendPrefix - the prefix to be added to field. Optional </li>
+ * </ul>
+ * <!-- END SNIPPET: parameters -->
+ *
+ * <pre>
+ * <!-- START SNIPPET: example -->
+ * <validators>
+ * <!-- Plain Validator Syntax -->
+ * <validator type="visitor">
+ * <param name="fieldName">user</param>
+ * <param name="context">myContext</param>
+ * <param name="appendPrefix">true</param>
+ * </validator>
+ *
+ * <!-- Field Validator Syntax -->
+ * <field name="user">
+ * <field-validator type="visitor">
+ * <param name="context">myContext</param>
+ * <param name="appendPrefix">true</param>
+ * </field-validator>
+ * </field>
+ * </validators>
+ * <!-- END SNIPPET: example -->
+ * </pre>
+ *
+ * <!-- START SNIPPET: explanation -->
+ * <p>In the example above, if the acion's getUser() method return User object, XWork
+ * will look for User-myContext-validation.xml for the validators. Since appednPrefix is true,
+ * every field name will be prefixed with 'user' such that if the actual field name for 'name' is
+ * 'user.name' </p>
+ * <!-- END SNIPPET: explanation -->
+ *
+ * @author Jason Carreira
+ * @author Rainer Hermanns
+ * @version $Date: 2011-12-02 12:24:48 +0100 (Fri, 02 Dec 2011) $ $Id: VisitorFieldValidator.java 1209415 2011-12-02 11:24:48Z lukaszlenart $
+ */
+public class VisitorFieldValidator extends FieldValidatorSupport {
+
+ private String context;
+ private boolean appendPrefix = true;
+ private ActionValidatorManager actionValidatorManager;
+
+
+ @Inject
+ public void setActionValidatorManager(ActionValidatorManager mgr) {
+ this.actionValidatorManager = mgr;
+ }
+
+ /**
+ * Sets whether the field name of this field validator should be prepended to the field name of
+ * the visited field to determine the full field name when an error occurs. The default is
+ * true.
+ */
+ public void setAppendPrefix(boolean appendPrefix) {
+ this.appendPrefix = appendPrefix;
+ }
+
+ /**
+ * Flags whether the field name of this field validator should be prepended to the field name of
+ * the visited field to determine the full field name when an error occurs. The default is
+ * true.
+ */
+ public boolean isAppendPrefix() {
+ return appendPrefix;
+ }
+
+ public void setContext(String context) {
+ this.context = context;
+ }
+
+ public String getContext() {
+ return context;
+ }
+
+ public void validate(Object object) throws ValidationException {
+ String fieldName = getFieldName();
+ Object value = this.getFieldValue(fieldName, object);
+ if (value == null) {
+ log.warn("The visited object is null, VisitorValidator will not be able to handle validation properly. Please make sure the visited object is not null for VisitorValidator to function properly");
+ return;
+ }
+ ValueStack stack = ActionContext.getContext().getValueStack();
+
+ stack.push(object);
+
+ String visitorContext = (context == null) ? ActionContext.getContext().getName() : context;
+
+ if (value instanceof Collection) {
+ Collection coll = (Collection) value;
+ Object[] array = coll.toArray();
+
+ validateArrayElements(array, fieldName, visitorContext);
+ } else if (value instanceof Object[]) {
+ Object[] array = (Object[]) value;
+
+ validateArrayElements(array, fieldName, visitorContext);
+ } else {
+ validateObject(fieldName, value, visitorContext);
+ }
+
+ stack.pop();
+ }
+
+ private void validateArrayElements(Object[] array, String fieldName, String visitorContext) throws ValidationException {
+ if (array == null) {
+ return;
+ }
+
+ for (int i = 0; i < array.length; i++) {
+ Object o = array[i];
+ if (o != null) {
+ validateObject(fieldName + "[" + i + "]", o, visitorContext);
+ }
+ }
+ }
+
+ private void validateObject(String fieldName, Object o, String visitorContext) throws ValidationException {
+ ValueStack stack = ActionContext.getContext().getValueStack();
+ stack.push(o);
+
+ ValidatorContext validatorContext;
+
+ if (appendPrefix) {
+ validatorContext = new AppendingValidatorContext(getValidatorContext(), o, fieldName, getMessage(o));
+ } else {
+ ValidatorContext parent = getValidatorContext();
+ validatorContext = new DelegatingValidatorContext(parent, DelegatingValidatorContext.makeTextProvider(o, parent), parent);
+ }
+
+ actionValidatorManager.validate(o, visitorContext, validatorContext);
+ stack.pop();
+ }
+
+
+ public static class AppendingValidatorContext extends DelegatingValidatorContext {
+ private String field;
+ private String message;
+ private ValidatorContext parent;
+
+ public AppendingValidatorContext(ValidatorContext parent, Object object, String field, String message) {
+ super(parent, makeTextProvider(object, parent), parent);
+
+ this.field = field;
+ this.message = message;
+ this.parent = parent;
+ }
+
+ /**
+ * Translates a simple field name into a full field name in Ognl syntax
+ *
+ * @param fieldName field name in OGNL syntax
+ * @return field name in OGNL syntax
+ */
+ @Override
+ public String getFullFieldName(String fieldName) {
+ return field + "." + fieldName;
+ }
+
+ public String getFullFieldNameFromParent(String fieldName) {
+ return parent.getFullFieldName(field + "." + fieldName);
+ }
+
+ @Override
+ public void addActionError(String anErrorMessage) {
+ super.addFieldError(getFullFieldName(field), message + anErrorMessage);
+ }
+
+ @Override
+ public void addFieldError(String fieldName, String errorMessage) {
+ super.addFieldError(getFullFieldName(fieldName), message + errorMessage);
+ }
+ }
+}
Added: struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/package.html
URL: http://svn.apache.org/viewvc/struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/package.html?rev=1209569&view=auto
==============================================================================
--- struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/package.html (added)
+++ struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/java/org/apache/struts2/xwork2/validator/validators/package.html Fri Dec 2 16:33:03 2011
@@ -0,0 +1 @@
+<body>XWork default validator classes.</body>
Added: struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/resources/org/apache/struts2/xwork2/validator/validators/default.xml
URL: http://svn.apache.org/viewvc/struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/resources/org/apache/struts2/xwork2/validator/validators/default.xml?rev=1209569&view=auto
==============================================================================
--- struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/resources/org/apache/struts2/xwork2/validator/validators/default.xml (added)
+++ struts/struts2/branches/STRUTS_3_X/xwork-core/src/main/resources/org/apache/struts2/xwork2/validator/validators/default.xml Fri Dec 2 16:33:03 2011
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE validators PUBLIC
+ "-//Apache Struts//XWork Validator Config 1.0//EN"
+ "http://struts.apache.org/dtds/xwork-validator-config-1.0.dtd">
+
+<!-- START SNIPPET: validators-default -->
+<validators>
+ <validator name="required" class="org.apache.struts2.xwork2.validator.validators.RequiredFieldValidator"/>
+ <validator name="requiredstring" class="org.apache.struts2.xwork2.validator.validators.RequiredStringValidator"/>
+ <validator name="int" class="org.apache.struts2.xwork2.validator.validators.IntRangeFieldValidator"/>
+ <validator name="long" class="org.apache.struts2.xwork2.validator.validators.LongRangeFieldValidator"/>
+ <validator name="short" class="org.apache.struts2.xwork2.validator.validators.ShortRangeFieldValidator"/>
+ <validator name="double" class="org.apache.struts2.xwork2.validator.validators.DoubleRangeFieldValidator"/>
+ <validator name="date" class="org.apache.struts2.xwork2.validator.validators.DateRangeFieldValidator"/>
+ <validator name="expression" class="org.apache.struts2.xwork2.validator.validators.ExpressionValidator"/>
+ <validator name="fieldexpression" class="org.apache.struts2.xwork2.validator.validators.FieldExpressionValidator"/>
+ <validator name="email" class="org.apache.struts2.xwork2.validator.validators.EmailValidator"/>
+ <validator name="url" class="org.apache.struts2.xwork2.validator.validators.URLValidator"/>
+ <validator name="visitor" class="org.apache.struts2.xwork2.validator.validators.VisitorFieldValidator"/>
+ <validator name="conversion" class="org.apache.struts2.xwork2.validator.validators.ConversionErrorFieldValidator"/>
+ <validator name="stringlength" class="org.apache.struts2.xwork2.validator.validators.StringLengthFieldValidator"/>
+ <validator name="regex" class="org.apache.struts2.xwork2.validator.validators.RegexFieldValidator"/>
+ <validator name="conditionalvisitor" class="org.apache.struts2.xwork2.validator.validators.ConditionalVisitorFieldValidator"/>
+</validators>
+<!-- END SNIPPET: validators-default -->