KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > util > core > ValidationUtil


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
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 package com.blandware.atleap.webapp.util.core;
17
18 import com.blandware.atleap.common.util.DateUtil;
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.apache.commons.validator.Field;
22 import org.apache.commons.validator.GenericValidator;
23 import org.apache.commons.validator.ValidatorAction;
24 import org.apache.commons.validator.util.ValidatorUtils;
25 import org.apache.struts.Globals;
26 import org.apache.struts.action.ActionMessages;
27 import org.apache.struts.validator.Resources;
28
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import java.io.PrintWriter JavaDoc;
31 import java.io.StringWriter JavaDoc;
32 import java.text.SimpleDateFormat JavaDoc;
33 import java.util.Date JavaDoc;
34 import java.util.Locale JavaDoc;
35
36
37 /**
38  * ValidationUtil Helper class for performing custom validations that
39  * aren't already included in the core Struts Validator.
40  * <p><a HREF="ValidationUtil.java.htm"><i>View Source</i></a>
41  * </p>
42  *
43  * @author Matt Raible <a HREF="mailto:matt@raibledesigns.com">&lt;matt@raibledesigns.com&gt;</a>
44  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
45  * @version $Revision: 1.3 $ $Date: 2005/08/03 15:39:04 $
46  */

47 public class ValidationUtil {
48
49     protected transient final Log log = LogFactory.getLog(ValidationUtil.class);
50
51     //~ Methods ================================================================
52

53     /**
54      * Validates that two fields match (have the same value)
55      *
56      * @param bean The bean to which a field belongs
57      * @param va Validator action
58      * @param field Form field to validate
59      * @param errors Action messages that will accept validation error
60      * @param request The request
61      * @return boolean Whether fields match
62      */

63     public static boolean validateTwoFields(Object JavaDoc bean, ValidatorAction va,
64                                             Field field, ActionMessages errors,
65                                             HttpServletRequest JavaDoc request) {
66         String JavaDoc value =
67                 ValidatorUtils.getValueAsString(bean, field.getProperty());
68         String JavaDoc sProperty2 = field.getVarValue("secondProperty");
69         String JavaDoc value2 = ValidatorUtils.getValueAsString(bean, sProperty2);
70
71         if ( !GenericValidator.isBlankOrNull(value) ) {
72             try {
73                 if ( !value.equals(value2) ) {
74                     errors.add(field.getKey(),
75                             Resources.getActionMessage(request, va, field));
76
77                     return false;
78                 }
79             } catch ( Exception JavaDoc e ) {
80                 errors.add(field.getKey(),
81                         Resources.getActionMessage(request, va, field));
82
83                 return false;
84             }
85         }
86
87         return true;
88     }
89
90     /**
91      * Validates date to match default date pattern for request locale without time symbols
92      *
93      * @param bean The bean to which a field belongs
94      * @param va Validator action
95      * @param field Form field to validate
96      * @param errors Action messages that will accept validation error
97      * @param request The request
98      * @return boolean Whether date matches pattern
99      */

100     public static Date JavaDoc validateDate(Object JavaDoc bean, ValidatorAction va,
101                                     Field field, ActionMessages errors,
102                                     HttpServletRequest JavaDoc request) {
103
104         Log log = LogFactory.getLog(ValidationUtil.class);
105
106         if ( log.isDebugEnabled() ) {
107             log.debug("validateDate called: field=" + field.getKey());
108         }
109         Date JavaDoc result = null;
110         String JavaDoc value = ValidatorUtils.getValueAsString(bean, field.getProperty());
111
112         if ( log.isDebugEnabled() ) {
113             log.debug("Value: " + value);
114         }
115
116         Locale JavaDoc locale = (Locale JavaDoc) request.getSession().getAttribute(Globals.LOCALE_KEY);
117         String JavaDoc datePattern = DateUtil.getDatePattern(locale);
118
119         if ( log.isDebugEnabled() ) {
120             log.debug("Date pattern: " + datePattern);
121         }
122
123         if ( !GenericValidator.isBlankOrNull(value) ) {
124             try {
125                 SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc(datePattern, locale);
126                 formatter.setLenient(false);
127                 result = formatter.parse(value);
128             } catch ( Exception JavaDoc e ) {
129                 if ( log.isErrorEnabled() ) {
130                     StringWriter JavaDoc sw = new StringWriter JavaDoc();
131                     e.printStackTrace(new PrintWriter JavaDoc(sw));
132                     log.error(sw.toString());
133                 }
134             }
135             if ( result == null ) {
136                 errors.add(field.getKey(), Resources.getActionMessage(request, va, field));
137             }
138         }
139         return result;
140     }
141 }
142
Popular Tags