KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > pageflow > validation > ValidatorRules


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.pageflow.validation;
19
20 import org.apache.commons.validator.ValidatorAction;
21 import org.apache.commons.validator.Field;
22 import org.apache.commons.validator.ValidatorUtil;
23 import org.apache.commons.validator.GenericValidator;
24 import org.apache.struts.action.ActionMessages;
25 import org.apache.struts.validator.Resources;
26 import org.apache.struts.validator.FieldChecks;
27 import org.apache.beehive.netui.pageflow.internal.InternalExpressionUtils;
28 import org.apache.beehive.netui.util.logging.Logger;
29
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import javax.servlet.ServletContext JavaDoc;
32
33 public class ValidatorRules
34         extends FieldChecks
35 {
36     private static final Logger _log = Logger.getInstance( ValidatorRules.class );
37     
38     /**
39      * Check if a given expression evaluates to <code>true</code>.
40      *
41      * @param bean the bean that validation is being performed on.
42      * @param va the <code>ValidatorAction</code> that is currently being performed.
43      * @param field the <code>Field</code> object associated with the current field being validated.
44      * @param errors the <code>ActionMessages</code> object to add errors to if any validation errors occur.
45      * @param request the current request object.
46      * @return <code>true</code> if the given expression evaluates to <code>true</code>
47      */

48     public static boolean validateValidWhen( Object JavaDoc bean, ValidatorAction va, Field field, ActionMessages errors,
49                                              HttpServletRequest JavaDoc request, ServletContext JavaDoc servletContext )
50     {
51
52         String JavaDoc value;
53         
54         if ( isString( bean ) )
55         {
56             value = ( String JavaDoc ) bean;
57         }
58         else
59         {
60             value = ValidatorUtil.getValueAsString( bean, field.getProperty() );
61         }
62
63         if ( ! GenericValidator.isBlankOrNull( value ) )
64         {
65             String JavaDoc condition = field.getVarValue( "netui_validwhen" );
66             
67             try
68             {
69                 if ( ! InternalExpressionUtils.evaluateCondition( condition, bean, request, servletContext ) )
70                 {
71                     errors.add( field.getKey(), Resources.getActionError( request, va, field ) );
72                     return false;
73                 }
74             }
75             catch ( Exception JavaDoc e )
76             {
77                 _log.error( "Error evaluating expression " + condition + " for ValidWhen rule on field "
78                             + field.getProperty() + " on bean of type " +
79                             ( bean != null ? bean.getClass().getName() : null ) );
80                 
81                 errors.add( field.getKey(), Resources.getActionError( request, va, field ) );
82                 return false;
83             }
84         }
85
86         return true;
87     }
88     
89     /**
90      * Check if a field's value is within a range ("min" and "max" Long variables on the passed-in Field).
91      *
92      * @param bean the bean that validation is being performed on.
93      * @param va the <code>ValidatorAction</code> that is currently being performed.
94      * @param field the <code>Field</code> object associated with the current field being validated.
95      * @param errors the <code>ActionMessages</code> object to add errors to if any validation errors occur.
96      * @param request the current request object.
97      * @return <code>true</code> if in range, false otherwise.
98      */

99     public static boolean validateLongRange( Object JavaDoc bean, ValidatorAction va, Field field, ActionMessages errors,
100                                              HttpServletRequest JavaDoc request )
101     {
102
103         String JavaDoc value;
104         
105         if ( isString( bean ) )
106         {
107             value = ( String JavaDoc ) bean;
108         }
109         else
110         {
111             value = ValidatorUtil.getValueAsString( bean, field.getProperty() );
112         }
113
114         if ( ! GenericValidator.isBlankOrNull( value ) )
115         {
116             try
117             {
118                 long longValue = Long.parseLong( value );
119                 long min = Long.parseLong( field.getVarValue( "min" ) );
120                 long max = Long.parseLong( field.getVarValue( "max" ) );
121
122                 if ( longValue < min || longValue > max )
123                 {
124                     errors.add( field.getKey(), Resources.getActionError( request, va, field ) );
125                     return false;
126                 }
127             }
128             catch ( Exception JavaDoc e )
129             {
130                 errors.add( field.getKey(), Resources.getActionError( request, va, field ) );
131                 return false;
132             }
133         }
134
135         return true;
136     }
137 }
138
Popular Tags