Return-Path: X-Original-To: apmail-struts-commits-archive@minotaur.apache.org Delivered-To: apmail-struts-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 6E09E18F15 for ; Wed, 17 Jun 2015 21:09:12 +0000 (UTC) Received: (qmail 76344 invoked by uid 500); 17 Jun 2015 21:09:03 -0000 Delivered-To: apmail-struts-commits-archive@struts.apache.org Received: (qmail 76281 invoked by uid 500); 17 Jun 2015 21:09:03 -0000 Mailing-List: contact commits-help@struts.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@struts.apache.org Delivered-To: mailing list commits@struts.apache.org Received: (qmail 74960 invoked by uid 99); 17 Jun 2015 21:09:02 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 17 Jun 2015 21:09:02 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 5E59FE00AA; Wed, 17 Jun 2015 21:09:02 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: lukaszlenart@apache.org To: commits@struts.apache.org Date: Wed, 17 Jun 2015 21:09:28 -0000 Message-Id: <00bf448ce82e462d93a9e67133c081d1@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [28/57] [partial] struts git commit: Merges xwork packages into struts http://git-wip-us.apache.org/repos/asf/struts/blob/31af5842/core/src/main/java/com/opensymphony/xwork2/validator/annotations/EmailValidator.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/com/opensymphony/xwork2/validator/annotations/EmailValidator.java b/core/src/main/java/com/opensymphony/xwork2/validator/annotations/EmailValidator.java new file mode 100644 index 0000000..7b40811 --- /dev/null +++ b/core/src/main/java/com/opensymphony/xwork2/validator/annotations/EmailValidator.java @@ -0,0 +1,133 @@ +/* + * 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 com.opensymphony.xwork2.validator.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * + * This validator checks that a field is a valid e-mail address if it contains a non-empty String. + * + * + *

Annotation usage: + * + * + *

The annotation must be applied at method level. + * + * + *

Annotation parameters: + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
Parameter Required Default Notes
messageyes field error message
keyno i18n key from language specific properties file.
messageParamsno Additional params to be used to customize message - will be evaluated against the Value Stack
fieldNameno  
shortCircuitnofalseIf this validator should be used as shortCircuit.
typeyesValidatorType.FIELDEnum value from ValidatorType. Either FIELD or SIMPLE can be used here.
+ * + * + *

Example code: + * + *

+ * 
+ * @EmailValidator(message = "Default message", key = "i18n.key", shortCircuit = true)
+ * 
+ * 
+ * + * @author Rainer Hermanns + * @version $Id$ + */ +@Target({ElementType.METHOD}) +@Retention(RetentionPolicy.RUNTIME) +public @interface EmailValidator { + + /** + * The default error message for this validator. + * NOTE: It is required to set a message, if you are not using the message key for 18n lookup! + */ + String message() default ""; + + /** + * The message key to lookup for i18n. + */ + String key() default ""; + + /** + * Additional params to be used to customize message - will be evaluated against the Value Stack + */ + String[] messageParams() default {}; + + /** + * The optional fieldName for SIMPLE validator types. + */ + String fieldName() default ""; + + /** + * If this is activated, the validator will be used as short-circuit. + * + * Adds the short-circuit="true" attribute value if true. + * + */ + boolean shortCircuit() default false; + + /** + * The validation type for this field/method. + */ + ValidatorType type() default ValidatorType.FIELD; + +} http://git-wip-us.apache.org/repos/asf/struts/blob/31af5842/core/src/main/java/com/opensymphony/xwork2/validator/annotations/ExpressionValidator.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/com/opensymphony/xwork2/validator/annotations/ExpressionValidator.java b/core/src/main/java/com/opensymphony/xwork2/validator/annotations/ExpressionValidator.java new file mode 100644 index 0000000..8049388 --- /dev/null +++ b/core/src/main/java/com/opensymphony/xwork2/validator/annotations/ExpressionValidator.java @@ -0,0 +1,123 @@ +/* + * 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 com.opensymphony.xwork2.validator.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * + * This non-field level validator validates a supplied regular expression. + * + * + *

Annotation usage: + * + * + *

The annotation must be applied at method level. + * + * + *

Annotation parameters: + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
Parameter Required Default Notes
messageyes field error message
keyno i18n key from language specific properties file.
messageParamsno Additional params to be used to customize message - will be evaluated against the Value Stack
shortCircuitnofalseIf this validator should be used as shortCircuit.
expression yes   An OGNL expression that returns a boolean value.
+ * + * + *

Example code: + * + *

+ * 
+ * @ExpressionValidator(message = "Default message", key = "i18n.key", shortCircuit = true, expression = "an OGNL expression" )
+ * 
+ * 
+ * + * @author Rainer Hermanns + * @version $Id$ + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ ElementType.METHOD}) +public @interface ExpressionValidator { + + /** + * The expressions to validate. + * An OGNL expression that returns a boolean value. + */ + String expression(); + + /** + * The default error message for this validator. + * NOTE: It is required to set a message, if you are not using the message key for 18n lookup! + */ + String message() default ""; + + /** + * The message key to lookup for i18n. + */ + String key() default ""; + + /** + * Additional params to be used to customize message - will be evaluated against the Value Stack + */ + String[] messageParams() default {}; + + /** + * If this is activated, the validator will be used as short-circuit. + * + * Adds the short-circuit="true" attribute value if true. + * + */ + boolean shortCircuit() default false; + +} http://git-wip-us.apache.org/repos/asf/struts/blob/31af5842/core/src/main/java/com/opensymphony/xwork2/validator/annotations/FieldExpressionValidator.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/com/opensymphony/xwork2/validator/annotations/FieldExpressionValidator.java b/core/src/main/java/com/opensymphony/xwork2/validator/annotations/FieldExpressionValidator.java new file mode 100644 index 0000000..5941b04 --- /dev/null +++ b/core/src/main/java/com/opensymphony/xwork2/validator/annotations/FieldExpressionValidator.java @@ -0,0 +1,134 @@ +/* + * 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 com.opensymphony.xwork2.validator.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * + * This validator uses an OGNL expression to perform its validator. + * The error message will be added to the field if the expression returns + * false when it is evaluated against the value stack. + * + * + *

Annotation usage: + * + * + *

The annotation must be applied at method level. + * + * + *

Annotation parameters: + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
Parameter Required Default Notes
messageyes field error message
keyno i18n key from language specific properties file.
messageParamsno Additional params to be used to customize message - will be evaluated against the Value Stack
fieldNameno  
shortCircuitnofalseIf this validator should be used as shortCircuit.
expression yes   An OGNL expression that returns a boolean value.
+ * + * + *

Example code: + * + *

+ * 
+ * @FieldExpressionValidator(message = "Default message", key = "i18n.key", shortCircuit = true, expression = "an OGNL expression")
+ * 
+ * 
+ * + * @author Rainer Hermanns + * @version $Id$ + */ +@Target({ElementType.METHOD}) +@Retention(RetentionPolicy.RUNTIME) +public @interface FieldExpressionValidator { + + /** + * An OGNL expression that returns a boolean value. + */ + String expression(); + + /** + * The default error message for this validator. + * NOTE: It is required to set a message, if you are not using the message key for 18n lookup! + */ + String message() default ""; + + /** + * The message key to lookup for i18n. + */ + String key() default ""; + + /** + * Additional params to be used to customize message - will be evaluated against the Value Stack + */ + String[] messageParams() default {}; + + /** + * The optional fieldName for SIMPLE validator types. + */ + String fieldName() default ""; + + /** + * If this is activated, the validator will be used as short-circuit. + * + * Adds the short-circuit="true" attribute value if true. + * + */ + boolean shortCircuit() default false; +} http://git-wip-us.apache.org/repos/asf/struts/blob/31af5842/core/src/main/java/com/opensymphony/xwork2/validator/annotations/IntRangeFieldValidator.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/com/opensymphony/xwork2/validator/annotations/IntRangeFieldValidator.java b/core/src/main/java/com/opensymphony/xwork2/validator/annotations/IntRangeFieldValidator.java new file mode 100644 index 0000000..cba539e --- /dev/null +++ b/core/src/main/java/com/opensymphony/xwork2/validator/annotations/IntRangeFieldValidator.java @@ -0,0 +1,190 @@ +/* + * 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 com.opensymphony.xwork2.validator.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * + * This validator checks that a numeric field has a value within a specified range. + * If neither min nor max is set, nothing will be done. + * + * + *

Annotation usage: + * + * + *

The annotation must be applied at method level. + * + * + *

Annotation parameters: + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
Parameter Required Default Notes
messageyes field error message
keyno i18n key from language specific properties file.
messageParamsno Additional params to be used to customize message - will be evaluated against the Value Stack
fieldNameno  
shortCircuitnofalseIf this validator should be used as shortCircuit.
typeyesValidatorType.FIELDEnum value from ValidatorType. Either FIELD or SIMPLE can be used here.
min no   Integer property. The minimum the number must be.
minExpression no  OGNL expression used to obtain the minimum the number must be.
max no   Integer property. The maximum number can be. Can be an expression.!
maxExpression no  OGNL expression used to obtain the maximum number can be.
parsenofalseEnable parsing of min/max value.
+ * + *

If neither min nor max is set, nothing will be done.

+ * + *

The values for min and max must be inserted as String values so that "0" can be handled as a possible value.

+ * + * + *

Example code: + * + *

+ * 
+ * @IntRangeFieldValidator(message = "Default message", key = "i18n.key", shortCircuit = true, min = "0", max = "42")
+ *
+ * @IntRangeFieldValidator(message = "Default message", key = "i18n.key", shortCircuit = true, minExpression = "${minValue}", maxExpression = "${maxValue}")
+ * 
+ * 
+ * + * + * @author Rainer Hermanns + * @version $Id$ + */ +@Target({ElementType.METHOD}) +@Retention(RetentionPolicy.RUNTIME) +public @interface IntRangeFieldValidator { + + /** + * Integer property. The minimum the number must be. + */ + String min() default ""; + + /** + * The minimum number can be defined as an expression + */ + String minExpression() default ""; + + /** + * Integer property. The maximum number can be. + */ + String max() default ""; + + /** + * The maximum number can be defined as an expression + */ + String maxExpression() default ""; + + /** + * The default error message for this validator. + * NOTE: It is required to set a message, if you are not using the message key for 18n lookup! + */ + String message() default ""; + + /** + * The message key to lookup for i18n. + */ + String key() default ""; + + /** + * Additional params to be used to customize message - will be evaluated against the Value Stack + */ + String[] messageParams() default {}; + + /** + * The optional fieldName for SIMPLE validator types. + */ + String fieldName() default ""; + + /** + * If this is activated, the validator will be used as short-circuit. + * + * Adds the short-circuit="true" attribute value if true. + * + */ + boolean shortCircuit() default false; + + /** + * The validation type for this field/method. + */ + ValidatorType type() default ValidatorType.FIELD; +} http://git-wip-us.apache.org/repos/asf/struts/blob/31af5842/core/src/main/java/com/opensymphony/xwork2/validator/annotations/RegexFieldValidator.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/com/opensymphony/xwork2/validator/annotations/RegexFieldValidator.java b/core/src/main/java/com/opensymphony/xwork2/validator/annotations/RegexFieldValidator.java new file mode 100644 index 0000000..3997916 --- /dev/null +++ b/core/src/main/java/com/opensymphony/xwork2/validator/annotations/RegexFieldValidator.java @@ -0,0 +1,182 @@ +/* + * 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 com.opensymphony.xwork2.validator.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * + * Validates a string field using a regular expression. + * + * + *

Annotation usage: + * + * + *

The annotation must be applied at method level. + * + * + *

Annotation parameters: + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
Parameter Required Default Notes
messageyes field error message
keyno i18n key from language specific properties file.
messageParamsno Additional params to be used to customize message - will be evaluated against the Value Stack
fieldNameno  
shortCircuitnofalseIf this validator should be used as shortCircuit.
typeyesValidatorType.FIELDEnum value from ValidatorType. Either FIELD or SIMPLE can be used here.
regexyes The regex to validate the field value against.
+ * + * + *

Example code: + * + *

+ * 
+ * @RegexFieldValidator( key = "regex.field", regex = "yourregexp")
+ * 
+ * 
+ * + * @author Rainer Hermanns + * @version $Id$ + */ +@Target({ElementType.METHOD}) +@Retention(RetentionPolicy.RUNTIME) +public @interface RegexFieldValidator { + + /** + * The default error message for this validator. + * NOTE: It is required to set a message, if you are not using the message key for 18n lookup! + */ + String message() default ""; + + /** + * Additional params to be used to customize message - will be evaluated against the Value Stack + */ + String[] messageParams() default {}; + + /** + * The message key to lookup for i18n. + */ + String key() default ""; + + /** + * The optional fieldName for SIMPLE validator types. + */ + String fieldName() default ""; + + /** + * Regex used to evaluate field against it + * + * @return String regular expression + */ + String regex() default ""; + + /** + * Defines regex as an expression which first will be evaluated against the Value Stack to get proper regex. + * Thus allow to dynamically change regex base on user actions. + * + * @return String an expression which starts with '$' or '%' + */ + String regexExpression() default ""; + + /** + * To trim or not the value, default true - trim + * + * @return boolean trim or not the value before validation + */ + boolean trim() default true; + + /** + * Allows specify trim as an expression which will be evaluated during validation + * + * @return String an expression which starts with '$' or '%' + */ + String trimExpression() default ""; + + /** + * Match the value in case sensitive manner, default true + * + * @return boolean use case sensitive match or not + */ + boolean caseSensitive() default true; + + /** + * Allows specify caseSensitive as an expression which will be evaluated during validation + * + * @return boolean use case sensitive match or not + */ + String caseSensitiveExpression() default ""; + + /** + * If this is activated, the validator will be used as short-circuit. + * + * Adds the short-circuit="true" attribute value if true. + * + */ + boolean shortCircuit() default false; + + /** + * The validation type for this field/method. + */ + ValidatorType type() default ValidatorType.FIELD; + +} http://git-wip-us.apache.org/repos/asf/struts/blob/31af5842/core/src/main/java/com/opensymphony/xwork2/validator/annotations/RequiredFieldValidator.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/com/opensymphony/xwork2/validator/annotations/RequiredFieldValidator.java b/core/src/main/java/com/opensymphony/xwork2/validator/annotations/RequiredFieldValidator.java new file mode 100644 index 0000000..3039d3c --- /dev/null +++ b/core/src/main/java/com/opensymphony/xwork2/validator/annotations/RequiredFieldValidator.java @@ -0,0 +1,134 @@ +/* + * 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 com.opensymphony.xwork2.validator.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * + * This validator checks that a field is non-null. + * + * + *

Annotation usage: + * + * + *

The annotation must be applied at method level. + * + * + *

Annotation parameters: + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
Parameter Required Default Notes
messageyes field error message
keyno i18n key from language specific properties file.
messageParamsno Additional params to be used to customize message - will be evaluated against the Value Stack
fieldNameno  
shortCircuitnofalseIf this validator should be used as shortCircuit.
typeyesValidatorType.FIELDEnum value from ValidatorType. Either FIELD or SIMPLE can be used here.
+ * + * + *

Example code: + * + *

+ * 
+ * @RequiredFieldValidator(message = "Default message", key = "i18n.key", shortCircuit = true)
+ * 
+ * 
+ * + * + * @author Rainer Hermanns + * @version $Id$ + */ +@Target({ElementType.METHOD}) +@Retention(RetentionPolicy.RUNTIME) +public @interface RequiredFieldValidator { + + /** + * The default error message for this validator. + * NOTE: It is required to set a message, if you are not using the message key for 18n lookup! + */ + String message() default ""; + + /** + * The message key to lookup for i18n. + */ + String key() default ""; + + /** + * Additional params to be used to customize message - will be evaluated against the Value Stack + */ + String[] messageParams() default {}; + + /** + * The optional fieldName for SIMPLE validator types. + */ + String fieldName() default ""; + + /** + * If this is activated, the validator will be used as short-circuit. + * + * Adds the short-circuit="true" attribute value if true. + * + */ + boolean shortCircuit() default false; + + /** + * The validation type for this field/method. + */ + ValidatorType type() default ValidatorType.FIELD; + +} http://git-wip-us.apache.org/repos/asf/struts/blob/31af5842/core/src/main/java/com/opensymphony/xwork2/validator/annotations/RequiredStringValidator.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/com/opensymphony/xwork2/validator/annotations/RequiredStringValidator.java b/core/src/main/java/com/opensymphony/xwork2/validator/annotations/RequiredStringValidator.java new file mode 100644 index 0000000..7fe64ad --- /dev/null +++ b/core/src/main/java/com/opensymphony/xwork2/validator/annotations/RequiredStringValidator.java @@ -0,0 +1,145 @@ +/* + * 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 com.opensymphony.xwork2.validator.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * + * This validator checks that a String field is not empty (i.e. non-null with a length > 0). + * + * + *

Annotation usage: + * + * + *

The annotation must be applied at method level. + * + * + *

Annotation parameters: + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
Parameter Required Default Notes
messageyes field error message
keyno i18n key from language specific properties file.
messageParamsno Additional params to be used to customize message - will be evaluated against the Value Stack
fieldNameno  
shortCircuitnofalseIf this validator should be used as shortCircuit.
typeyesValidatorType.FIELDEnum value from ValidatorType. Either FIELD or SIMPLE can be used here.
trim no true Boolean property. Determines whether the String is trimmed before performing the length check.
+ * + * + *

Example code: + * + *

+ * 
+ * @RequiredStringValidator(message = "Default message", key = "i18n.key", shortCircuit = true, trim = true)
+ * 
+ * 
+ * + * + * @author Rainer Hermanns + * @version $Id$ + */ +@Target({ElementType.METHOD}) +@Retention(RetentionPolicy.RUNTIME) +public @interface RequiredStringValidator { + + /** + * Boolean property. Determines whether the String is trimmed before performing the length check. + */ + boolean trim() default true; + + /** + * The default error message for this validator. + * NOTE: It is required to set a message, if you are not using the message key for 18n lookup! + */ + String message() default ""; + + /** + * The message key to lookup for i18n. + */ + String key() default ""; + + /** + * Additional params to be used to customize message - will be evaluated against the Value Stack + */ + String[] messageParams() default {}; + + /** + * The optional fieldName for SIMPLE validator types. + */ + String fieldName() default ""; + + /** + * If this is activated, the validator will be used as short-circuit. + * + * Adds the short-circuit="true" attribute value if true. + * + */ + boolean shortCircuit() default false; + + /** + * The validation type for this field/method. + */ + ValidatorType type() default ValidatorType.FIELD; + +} http://git-wip-us.apache.org/repos/asf/struts/blob/31af5842/core/src/main/java/com/opensymphony/xwork2/validator/annotations/ShortRangeFieldValidator.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/com/opensymphony/xwork2/validator/annotations/ShortRangeFieldValidator.java b/core/src/main/java/com/opensymphony/xwork2/validator/annotations/ShortRangeFieldValidator.java new file mode 100644 index 0000000..dfe57ae --- /dev/null +++ b/core/src/main/java/com/opensymphony/xwork2/validator/annotations/ShortRangeFieldValidator.java @@ -0,0 +1,187 @@ +/* + * 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 com.opensymphony.xwork2.validator.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * + * This validator checks that a numeric field has a value within a specified range. + * If neither min nor max is set, nothing will be done. + * + * + *

Annotation usage: + * + * + *

The annotation must be applied at method level. + * + * + *

Annotation parameters: + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
Parameter Required Default Notes
messageyes field error message
keyno i18n key from language specific properties file.
messageParamsno Additional params to be used to customize message - will be evaluated against the Value Stack
fieldNameno  
shortCircuitnofalseIf this validator should be used as shortCircuit.
typeyesValidatorType.FIELDEnum value from ValidatorType. Either FIELD or SIMPLE can be used here.
min no   Short property. The minimum the number must be.
minExpression no  OGNL expression used to obtain the minimum the number must be.
max no   Short property. The maximum number can be.
maxExpression no  OGNL expression used to obtain the maximum number can be.
parsenofalseEnable parsing of min/max value.
+ * + *

If neither min nor max is set, nothing will be done.

+ * + *

The values for min and max must be inserted as String values so that "0" can be handled as a possible value.

+ * + * + *

Example code: + * + *

+ * 
+ * @IntRangeFieldValidator(message = "Default message", key = "i18n.key", shortCircuit = true, min = "0", max = "42")
+ *
+ * @IntRangeFieldValidator(message = "Default message", key = "i18n.key", shortCircuit = true, min = "${minValue}", max = "${maxValue}" parse="true")
+ * 
+ * 
+ * + */ +@Target({ElementType.METHOD}) +@Retention(RetentionPolicy.RUNTIME) +public @interface ShortRangeFieldValidator { + + /** + * Short property. The minimum the number must be. + */ + String min() default ""; + + /** + * The minimum the number must be defined as an expression. + */ + String minExpression() default ""; + + /** + * Short property. The maximum number can be. + */ + String max() default ""; + + /** + * The maximum number can be defined as an expression + */ + String maxExpression() default ""; + + /** + * The default error message for this validator. + * NOTE: It is required to set a message, if you are not using the message key for 18n lookup! + */ + String message() default ""; + + /** + * The message key to lookup for i18n. + */ + String key() default ""; + + /** + * Additional params to be used to customize message - will be evaluated against the Value Stack + */ + String[] messageParams() default {}; + + /** + * The optional fieldName for SIMPLE validator types. + */ + String fieldName() default ""; + + /** + * If this is activated, the validator will be used as short-circuit. + * + * Adds the short-circuit="true" attribute value if true. + * + */ + boolean shortCircuit() default false; + + /** + * The validation type for this field/method. + */ + ValidatorType type() default ValidatorType.FIELD; +} http://git-wip-us.apache.org/repos/asf/struts/blob/31af5842/core/src/main/java/com/opensymphony/xwork2/validator/annotations/StringLengthFieldValidator.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/com/opensymphony/xwork2/validator/annotations/StringLengthFieldValidator.java b/core/src/main/java/com/opensymphony/xwork2/validator/annotations/StringLengthFieldValidator.java new file mode 100644 index 0000000..6db6175 --- /dev/null +++ b/core/src/main/java/com/opensymphony/xwork2/validator/annotations/StringLengthFieldValidator.java @@ -0,0 +1,197 @@ +/* + * 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 com.opensymphony.xwork2.validator.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * + * This validator checks that a String field is of the right length. It assumes that the field is a String. + * If neither minLength nor maxLength is set, nothing will be done. + * + * + *

Annotation usage: + * + * + *

The annotation must be applied at method level. + * + * + *

Annotation parameters: + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
Parameter Required Default Notes
messageyes field error message
keyno i18n key from language specific properties file.
messageParamsno Additional params to be used to customize message - will be evaluated against the Value Stack
fieldNameno  
shortCircuitnofalseIf this validator should be used as shortCircuit.
typeyesValidatorType.FIELDEnum value from ValidatorType. Either FIELD or SIMPLE can be used here.
trim no true Boolean property. Determines whether the String is trimmed before performing the length check.
minLength no   Integer property. The minimum length the String must be.
minLengthExpression no  OGNL expression used to obtain the minimum length the String must be.
maxLength no   Integer property. The maximum length the String can be.
maxLengthExpression no  OGNL expression used to obtain the maximum length the String can be.
+ * + *

If neither minLength nor maxLength is set, nothing will be done.

+ * + * + * + *

Example code: + * + *

+ * 
+ * @StringLengthFieldValidator(message = "Default message", key = "i18n.key", shortCircuit = true, trim = true, minLength = "5",  maxLength = "12")
+ * 
+ * 
+ * + * @author Rainer Hermanns + * @version $Id$ + */ +@Target({ElementType.METHOD}) +@Retention(RetentionPolicy.RUNTIME) +public @interface StringLengthFieldValidator { + + /** + * Boolean property. Determines whether the String is trimmed before performing the length check. + */ + boolean trim() default true; + + /** + * Determines whether the String is trimmed before performing the length check but defined as an expression + */ + String trimExpression() default ""; + + /** + * Integer property. The minimum length the String must be. + */ + String minLength() default ""; + + /** + * The minimum length the String must be defined as an expression + */ + String minLengthExpression() default ""; + + /** + * Integer property. The maximum length the String can be. + */ + String maxLength() default ""; + + /** + * The maximum length the String can be defined as an expression + */ + String maxLengthExpression() default ""; + + /** + * The default error message for this validator. + * NOTE: It is required to set a message, if you are not using the message key for 18n lookup! + */ + String message() default ""; + + /** + * The message key to lookup for i18n. + */ + String key() default ""; + + /** + * Additional params to be used to customize message - will be evaluated against the Value Stack + */ + String[] messageParams() default {}; + + /** + * The optional fieldName for SIMPLE validator types. + */ + String fieldName() default ""; + + /** + * If this is activated, the validator will be used as short-circuit. + * + * Adds the short-circuit="true" attribute value if true. + * + */ + boolean shortCircuit() default false; + + /** + * The validation type for this field/method. + */ + ValidatorType[] type() default {ValidatorType.FIELD}; + +} http://git-wip-us.apache.org/repos/asf/struts/blob/31af5842/core/src/main/java/com/opensymphony/xwork2/validator/annotations/UrlValidator.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/com/opensymphony/xwork2/validator/annotations/UrlValidator.java b/core/src/main/java/com/opensymphony/xwork2/validator/annotations/UrlValidator.java new file mode 100644 index 0000000..9ad9223 --- /dev/null +++ b/core/src/main/java/com/opensymphony/xwork2/validator/annotations/UrlValidator.java @@ -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 com.opensymphony.xwork2.validator.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * This validator checks that a field is a valid URL. + * + *
+ * @UrlValidator(message = "Default message", key = "i18n.key", shortCircuit = true)
+ * 
+ */ +@Target({ElementType.METHOD}) +@Retention(RetentionPolicy.RUNTIME) +public @interface UrlValidator { + + /** + * The default error message for this validator. + * NOTE: It is required to set a message, if you are not using the message key for 18n lookup! + */ + String message() default ""; + + /** + * The message key to lookup for i18n. + */ + String key() default ""; + + /** + * Additional params to be used to customize message - will be evaluated against the Value Stack + */ + String[] messageParams() default {}; + + /** + * The optional fieldName for SIMPLE validator types. + */ + String fieldName() default ""; + + /** + * If this is activated, the validator will be used as short-circuit. + * + * Adds the short-circuit="true" attribute value if true. + */ + boolean shortCircuit() default false; + + /** + * The validation type for this field/method. + */ + ValidatorType type() default ValidatorType.FIELD; + + /** + * Defines regex to use to validate url + */ + String urlRegex() default ""; + + /** + * Defines regex as an expression which will be evaluated to string and used to validate url + */ + String urlRegexExpression() default ""; + +} http://git-wip-us.apache.org/repos/asf/struts/blob/31af5842/core/src/main/java/com/opensymphony/xwork2/validator/annotations/Validation.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/com/opensymphony/xwork2/validator/annotations/Validation.java b/core/src/main/java/com/opensymphony/xwork2/validator/annotations/Validation.java new file mode 100644 index 0000000..2769b12 --- /dev/null +++ b/core/src/main/java/com/opensymphony/xwork2/validator/annotations/Validation.java @@ -0,0 +1,137 @@ +/* + * 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 com.opensymphony.xwork2.validator.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * + * This annotation has been deprecated since 2.1 as its previous purpose, to define classes that support annotation validations, + * is no longer necessary. + * + * + *

Annotation usage: + * + * + *

The Validation annotation must be applied at Type level. + * + * + *

Annotation parameters: + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
Parameter Required Default Notes
validationsyes 
+ * + * + *

Example code: + * + * An Annotated Interface + *

+ * 
+ * @Validation()
+ * public interface AnnotationDataAware {
+ *
+ *     void setBarObj(Bar b);
+ *
+ *     Bar getBarObj();
+ *
+ *     @RequiredFieldValidator(message = "You must enter a value for data.")
+ *     @RequiredStringValidator(message = "You must enter a value for data.")
+ *     void setData(String data);
+ *
+ *     String getData();
+ * }
+ * 
+ * 
+ * + *

Example code: + * + * An Annotated Class + *

+ * 
+ * @Validation()
+ * public class SimpleAnnotationAction extends ActionSupport {
+ *
+ *     @RequiredFieldValidator(type = ValidatorType.FIELD, message = "You must enter a value for bar.")
+ *     @IntRangeFieldValidator(type = ValidatorType.FIELD, min = "6", max = "10", message = "bar must be between ${min} and ${max}, current value is ${bar}.")
+ *     public void setBar(int bar) {
+ *         this.bar = bar;
+ *     }
+ *
+ *     public int getBar() {
+ *         return bar;
+ *     }
+ *
+ *     @Validations(
+ *             requiredFields =
+ *                     {@RequiredFieldValidator(type = ValidatorType.SIMPLE, fieldName = "customfield", message = "You must enter a value for field.")},
+ *             requiredStrings =
+ *                     {@RequiredStringValidator(type = ValidatorType.SIMPLE, fieldName = "stringisrequired", message = "You must enter a value for string.")},
+ *             emails =
+ *                     { @EmailValidator(type = ValidatorType.SIMPLE, fieldName = "emailaddress", message = "You must enter a value for email.")},
+ *             urls =
+ *                     { @UrlValidator(type = ValidatorType.SIMPLE, fieldName = "hreflocation", message = "You must enter a value for email.")},
+ *             stringLengthFields =
+ *                     {@StringLengthFieldValidator(type = ValidatorType.SIMPLE, trim = true, minLength="10" , maxLength = "12", fieldName = "needstringlength", message = "You must enter a stringlength.")},
+ *             intRangeFields =
+ *                     { @IntRangeFieldValidator(type = ValidatorType.SIMPLE, fieldName = "intfield", min = "6", max = "10", message = "bar must be between ${min} and ${max}, current value is ${bar}.")},
+ *             dateRangeFields =
+ *                     {@DateRangeFieldValidator(type = ValidatorType.SIMPLE, fieldName = "datefield", min = "-1", max = "99", message = "bar must be between ${min} and ${max}, current value is ${bar}.")},
+ *             expressions = {
+ *                 @ExpressionValidator(expression = "foo > 1", message = "Foo must be greater than Bar 1. Foo = ${foo}, Bar = ${bar}."),
+ *                 @ExpressionValidator(expression = "foo > 2", message = "Foo must be greater than Bar 2. Foo = ${foo}, Bar = ${bar}."),
+ *                 @ExpressionValidator(expression = "foo > 3", message = "Foo must be greater than Bar 3. Foo = ${foo}, Bar = ${bar}."),
+ *                 @ExpressionValidator(expression = "foo > 4", message = "Foo must be greater than Bar 4. Foo = ${foo}, Bar = ${bar}."),
+ *                 @ExpressionValidator(expression = "foo > 5", message = "Foo must be greater than Bar 5. Foo = ${foo}, Bar = ${bar}.")
+ *     }
+ *     )
+ *     public String execute() throws Exception {
+ *         return SUCCESS;
+ *     }
+ * }
+ *
+ * 
+ * 
+ * + * @author Rainer Hermanns + * @deprecated Since Struts 2.1 because it isn't necessary anymore + * @version $Id$ + */ +@Target({ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +public @interface Validation { + + /** + * Used for class or interface validation rules. + */ + Validations[] validations() default {}; +} http://git-wip-us.apache.org/repos/asf/struts/blob/31af5842/core/src/main/java/com/opensymphony/xwork2/validator/annotations/ValidationParameter.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/com/opensymphony/xwork2/validator/annotations/ValidationParameter.java b/core/src/main/java/com/opensymphony/xwork2/validator/annotations/ValidationParameter.java new file mode 100644 index 0000000..1482aed --- /dev/null +++ b/core/src/main/java/com/opensymphony/xwork2/validator/annotations/ValidationParameter.java @@ -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 com.opensymphony.xwork2.validator.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * + * The ValidationParameter annotation is used as a parameter for CustomValidators. + * + * + *

Annotation usage: + * + * + *

The annotation must embedded into CustomValidator annotations as a parameter. + * + * + *

Annotation parameters: + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
Parameter Required Default Notes
nameyes parameter name.
valueyes parameter value.
+ * + * + *

Example code: + * + *

+ * 
+ * @CustomValidator(
+ *   type ="customValidatorName",
+ *   fieldName = "myField",
+ *   parameters = { @ValidationParameter( name = "paramName", value = "paramValue" ) }
+ * )
+ * 
+ * 
+ * + * @author jepjep + * @author Rainer Hermanns + */ +@Target( { ElementType.METHOD, ElementType.TYPE }) +@Retention(RetentionPolicy.RUNTIME) +public @interface ValidationParameter { + + String name(); + + String value(); + +} http://git-wip-us.apache.org/repos/asf/struts/blob/31af5842/core/src/main/java/com/opensymphony/xwork2/validator/annotations/Validations.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/com/opensymphony/xwork2/validator/annotations/Validations.java b/core/src/main/java/com/opensymphony/xwork2/validator/annotations/Validations.java new file mode 100644 index 0000000..3303842 --- /dev/null +++ b/core/src/main/java/com/opensymphony/xwork2/validator/annotations/Validations.java @@ -0,0 +1,188 @@ +/* + * 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 com.opensymphony.xwork2.validator.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * + *

If you want to use several annotations of the same type, these annotations must be nested within the @Validations() annotation.

+ * + * + *

Annotation usage: + * + * + *

Used at METHOD level. + * + * + *

Annotation parameters: + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
Parameter Required Notes
requiredFields no Add list of RequiredFieldValidators
customValidators no Add list of CustomValidators
conversionErrorFields no Add list of ConversionErrorFieldValidators
dateRangeFields no Add list of DateRangeFieldValidators
emails no Add list of EmailValidators
fieldExpressions no Add list of FieldExpressionValidators
intRangeFields no Add list of IntRangeFieldValidators
requiredStrings no Add list of RequiredStringValidators
stringLengthFields no Add list of StringLengthFieldValidators
urls no Add list of UrlValidators
visitorFields no Add list of VisitorFieldValidators
regexFields no Add list of RegexFieldValidator
expressions no Add list of ExpressionValidator
+ * + * + *

Example code: + * + *

+ * 
+ * @Validations(
+ *           requiredFields =
+ *                   {@RequiredFieldValidator(type = ValidatorType.SIMPLE, fieldName = "customfield", message = "You must enter a value for field.")},
+ *           requiredStrings =
+ *                   {@RequiredStringValidator(type = ValidatorType.SIMPLE, fieldName = "stringisrequired", message = "You must enter a value for string.")},
+ *           emails =
+ *                   { @EmailValidator(type = ValidatorType.SIMPLE, fieldName = "emailaddress", message = "You must enter a value for email.")},
+ *           urls =
+ *                   { @UrlValidator(type = ValidatorType.SIMPLE, fieldName = "hreflocation", message = "You must enter a value for email.")},
+ *           stringLengthFields =
+ *                   {@StringLengthFieldValidator(type = ValidatorType.SIMPLE, trim = true, minLength="10" , maxLength = "12", fieldName = "needstringlength", message = "You must enter a stringlength.")},
+ *           intRangeFields =
+ *                   { @IntRangeFieldValidator(type = ValidatorType.SIMPLE, fieldName = "intfield", min = "6", max = "10", message = "bar must be between ${min} and ${max}, current value is ${bar}.")},
+ *           shortRangeFields =
+ *                   { @ShortRangeFieldValidator(type = ValidatorType.SIMPLE, fieldName = "shortfield", min = "1", max = "128", message = "bar must be between ${min} and ${max}, current value is ${bar}.")},
+ *           dateRangeFields =
+ *                   {@DateRangeFieldValidator(type = ValidatorType.SIMPLE, fieldName = "datefield", min = "-1", max = "99", message = "bar must be between ${min} and ${max}, current value is ${bar}.")},
+ *           expressions = {
+ *               @ExpressionValidator(expression = "foo > 1", message = "Foo must be greater than Bar 1. Foo = ${foo}, Bar = ${bar}."),
+ *               @ExpressionValidator(expression = "foo > 2", message = "Foo must be greater than Bar 2. Foo = ${foo}, Bar = ${bar}."),
+ *               @ExpressionValidator(expression = "foo > 3", message = "Foo must be greater than Bar 3. Foo = ${foo}, Bar = ${bar}."),
+ *               @ExpressionValidator(expression = "foo > 4", message = "Foo must be greater than Bar 4. Foo = ${foo}, Bar = ${bar}."),
+ *               @ExpressionValidator(expression = "foo > 5", message = "Foo must be greater than Bar 5. Foo = ${foo}, Bar = ${bar}.")
+ *   }
+ *   )
+ *   public String execute() throws Exception {
+ *       return SUCCESS;
+ *   }
+ * 
+ * 
+ * + * @author jepjep + * @author Rainer Hermanns + * @version $Id$ + */ +@Target( { ElementType.METHOD, ElementType.TYPE }) +@Retention(RetentionPolicy.RUNTIME) +public @interface Validations { + + /** + * Custom Validation rules. + */ + public CustomValidator[] customValidators() default {}; + + public ConversionErrorFieldValidator[] conversionErrorFields() default {}; + + public DateRangeFieldValidator[] dateRangeFields() default {}; + + public EmailValidator[] emails() default {}; + + public FieldExpressionValidator[] fieldExpressions() default {}; + + public IntRangeFieldValidator[] intRangeFields() default {}; + + public RequiredFieldValidator[] requiredFields() default {}; + + public RequiredStringValidator[] requiredStrings() default {}; + + public StringLengthFieldValidator[] stringLengthFields() default {}; + + public UrlValidator[] urls() default {}; + + public ConditionalVisitorFieldValidator[] conditionalVisitorFields() default {}; + + public VisitorFieldValidator[] visitorFields() default {}; + + public RegexFieldValidator[] regexFields() default {}; + + public ExpressionValidator[] expressions() default {}; +} http://git-wip-us.apache.org/repos/asf/struts/blob/31af5842/core/src/main/java/com/opensymphony/xwork2/validator/annotations/ValidatorType.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/com/opensymphony/xwork2/validator/annotations/ValidatorType.java b/core/src/main/java/com/opensymphony/xwork2/validator/annotations/ValidatorType.java new file mode 100644 index 0000000..aed95d7 --- /dev/null +++ b/core/src/main/java/com/opensymphony/xwork2/validator/annotations/ValidatorType.java @@ -0,0 +1,34 @@ +/* + * 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 com.opensymphony.xwork2.validator.annotations; + +/** + * ValidatorType + * + * @author Rainer Hermanns + * @version $Id$ + */ +public enum ValidatorType { + + FIELD, SIMPLE; + + @Override + public String toString() { + return super.toString().toUpperCase(); + } + +} http://git-wip-us.apache.org/repos/asf/struts/blob/31af5842/core/src/main/java/com/opensymphony/xwork2/validator/annotations/VisitorFieldValidator.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/com/opensymphony/xwork2/validator/annotations/VisitorFieldValidator.java b/core/src/main/java/com/opensymphony/xwork2/validator/annotations/VisitorFieldValidator.java new file mode 100644 index 0000000..7d42339 --- /dev/null +++ b/core/src/main/java/com/opensymphony/xwork2/validator/annotations/VisitorFieldValidator.java @@ -0,0 +1,161 @@ +/* + * 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 com.opensymphony.xwork2.validator.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * + * The validator allows you to forward validator to object properties of your action + * using the objects own validator 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. + * The error message for the VisitorFieldValidator will be appended in front of validator messages added + * by the validations for the Object message. + * + * + *

Annotation usage: + * + * + *

The annotation must be applied at method level. + * + * + *

Annotation parameters: + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
Parameter Required Default Notes
messageyes field error message
keyno i18n key from language specific properties file.
messageParamsno Additional params to be used to customize message - will be evaluated against the Value Stack
fieldNameno  
shortCircuitnofalseIf this validator should be used as shortCircuit.
context no action alias Determines the context to use for validating the Object property. If not defined, the context of the Action validation is propogated to the Object property validation. In the case of Action validation, this context is the Action alias.
appendPrefix no true Determines 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. For example, suppose that the bean being validated has a "name" property. If appendPrefix is true, then the field error will be stored under the field "bean.name". If appendPrefix is false, then the field error will be stored under the field "name".
If you are using the VisitorFieldValidator to validate the model from a ModelDriven Action, you should set appendPrefix to false unless you are using "model.name" to reference the properties on your model.
+ * + * + *

Example code: + * + *

+ * 
+ * @VisitorFieldValidator(message = "Default message", key = "i18n.key", shortCircuit = true, context = "action alias", appendPrefix = true)
+ * 
+ * 
+ * + * @author Rainer Hermanns + * @version $Id$ + */ +@Target({ElementType.METHOD}) +@Retention(RetentionPolicy.RUNTIME) +public @interface VisitorFieldValidator { + + /** + * Determines the context to use for validating the Object property. + * If not defined, the context of the Action validator is propogated to the Object property validator. + * In the case of Action validator, this context is the Action alias. + */ + String context() default ""; + + /** + * Determines 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. For example, suppose that + * the bean being validated has a "name" property. + * + * If appendPrefix is true, then the field error will be stored under the field "bean.name". + * If appendPrefix is false, then the field error will be stored under the field "name". + * + * If you are using the VisitorFieldValidator to validate the model from a ModelDriven Action, + * you should set appendPrefix to false unless you are using "model.name" to reference the properties + * on your model. + */ + boolean appendPrefix() default true; + + /** + * The default error message for this validator. + * NOTE: It is required to set a message, if you are not using the message key for 18n lookup! + */ + String message() default ""; + + /** + * The message key to lookup for i18n. + */ + String key() default ""; + + /** + * The optional fieldName for SIMPLE validator types. + */ + String fieldName() default ""; + + /** + * Additional params to be used to customize message - will be evaluated against the Value Stack + */ + String[] messageParams() default {}; + + /** + * If this is activated, the validator will be used as short-circuit. + * + * Adds the short-circuit="true" attribute value if true. + * + */ + boolean shortCircuit() default false; +} http://git-wip-us.apache.org/repos/asf/struts/blob/31af5842/core/src/main/java/com/opensymphony/xwork2/validator/annotations/package.html ---------------------------------------------------------------------- diff --git a/core/src/main/java/com/opensymphony/xwork2/validator/annotations/package.html b/core/src/main/java/com/opensymphony/xwork2/validator/annotations/package.html new file mode 100644 index 0000000..ff910e7 --- /dev/null +++ b/core/src/main/java/com/opensymphony/xwork2/validator/annotations/package.html @@ -0,0 +1 @@ +Validator annotations. http://git-wip-us.apache.org/repos/asf/struts/blob/31af5842/core/src/main/java/com/opensymphony/xwork2/validator/package.html ---------------------------------------------------------------------- diff --git a/core/src/main/java/com/opensymphony/xwork2/validator/package.html b/core/src/main/java/com/opensymphony/xwork2/validator/package.html new file mode 100644 index 0000000..07e7894 --- /dev/null +++ b/core/src/main/java/com/opensymphony/xwork2/validator/package.html @@ -0,0 +1 @@ +XWork validation subsystem. \ No newline at end of file http://git-wip-us.apache.org/repos/asf/struts/blob/31af5842/core/src/main/java/com/opensymphony/xwork2/validator/validators/ConditionalVisitorFieldValidator.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/com/opensymphony/xwork2/validator/validators/ConditionalVisitorFieldValidator.java b/core/src/main/java/com/opensymphony/xwork2/validator/validators/ConditionalVisitorFieldValidator.java new file mode 100644 index 0000000..4c83bb6 --- /dev/null +++ b/core/src/main/java/com/opensymphony/xwork2/validator/validators/ConditionalVisitorFieldValidator.java @@ -0,0 +1,84 @@ +package com.opensymphony.xwork2.validator.validators; + +import com.opensymphony.xwork2.validator.ValidationException; + +/** + * + * The ConditionalVisitorFieldValidator will forward validation to the VisitorFieldValidator + * only if the expression will evaluate to true. + * + * + * + *
    + *
  • expression - an OGNL expression which should evaluate to true to pass validation to the VisitorFieldValidator
  • + *
+ * + * + *
+ * 
+ * <field name="colleaguePosition">
+ *     <field-validator type="conditionalvisitor">
+ *         <param name="expression">reason == 'colleague' and colleaguePositionID == 'OTHER'</param>
+ *         <message>You must select reason Colleague and position Other</message>
+ *     </field-validator>
+ * </field>
+ * 
+ * 
+ * + * @author Matt Raible + */ +public class ConditionalVisitorFieldValidator extends VisitorFieldValidator { + + private String expression; + + public void setExpression(String expression) { + this.expression = expression; + } + + public String getExpression() { + return expression; + } + + /** + * If expression evaluates to true, invoke visitor validation. + * + * @param object the object being validated + * @throws ValidationException + */ + @Override + public void validate(Object object) throws ValidationException { + if (validateExpression(object)) { + super.validate(object); + } + } + + /** + * Validate the expression contained in the "expression" paramter. + * + * @param object the object you're validating + * @return true if expression evaluates to true (implying a validation + * failure) + * @throws ValidationException if anything goes wrong + */ + public boolean validateExpression(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 {} when trying to get Boolean.", obj); + } + + return answer; + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/struts/blob/31af5842/core/src/main/java/com/opensymphony/xwork2/validator/validators/ConversionErrorFieldValidator.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/com/opensymphony/xwork2/validator/validators/ConversionErrorFieldValidator.java b/core/src/main/java/com/opensymphony/xwork2/validator/validators/ConversionErrorFieldValidator.java new file mode 100644 index 0000000..4fe0ea3 --- /dev/null +++ b/core/src/main/java/com/opensymphony/xwork2/validator/validators/ConversionErrorFieldValidator.java @@ -0,0 +1,84 @@ +/* + * 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 com.opensymphony.xwork2.validator.validators; + +import com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.conversion.impl.XWorkConverter; +import com.opensymphony.xwork2.validator.ValidationException; +import org.apache.commons.lang3.StringUtils; + +import java.util.Map; + + +/** + * + * Field Validator that checks if a conversion error occurred for this field. + * + *

+ * + *

    + *
  • fieldName - The field name this validator is validating. Required if using Plain-Validator Syntax otherwise not required
  • + *
+ * + * + * + *
+ *     <!-- 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>
+ * 
+ * + * + * @author Jason Carreira + * @author tm_jee + * + * @version $Date $Id$ + */ +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 conversionErrors = context.getConversionErrors(); + + if (conversionErrors.containsKey(fullFieldName)) { + if (StringUtils.isBlank(defaultMessage)) { + defaultMessage = XWorkConverter.getConversionErrorMessage(fullFieldName, context.getValueStack()); + } + + addFieldError(fieldName, object); + } + } + +}