KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jresearch > gossip > validator > Validator


1 /*
2  * $$Id: Validator.java,v 1.4 2005/06/09 10:03:35 bel70 Exp $$
3  *
4  * ***** BEGIN LICENSE BLOCK *****
5  * The contents of this file are subject to the Mozilla Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License
8  * at http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific language governing rights and
13  * limitations under the License.
14  *
15  * The Original Code is JGossip forum code.
16  *
17  * The Initial Developer of the Original Code is the JResearch, Org.
18  * Portions created by the Initial Developer are Copyright (C) 2004
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  * Dmitry Belov <bel@jresearch.org>
23  *
24  * ***** END LICENSE BLOCK ***** */

25 /*
26  * Created on Nov 8, 2003
27  *
28  */

29 package org.jresearch.gossip.validator;
30
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpSession JavaDoc;
33
34 import org.apache.commons.validator.Field;
35 import org.apache.commons.validator.GenericValidator;
36 import org.apache.commons.validator.ValidatorAction;
37 import org.apache.commons.validator.util.ValidatorUtils;
38 import org.apache.oro.text.perl.Perl5Util;
39 import org.apache.struts.action.ActionMessages;
40 import org.apache.struts.validator.Resources;
41 import org.jresearch.gossip.IConst;
42
43 /**
44  * DOCUMENT ME!
45  *
46  * @author Bel
47  */

48 public class Validator {
49     /**
50      * DOCUMENT ME!
51      *
52      * @param bean
53      * DOCUMENT ME!
54      * @param va
55      * DOCUMENT ME!
56      * @param field
57      * DOCUMENT ME!
58      * @param errors
59      * DOCUMENT ME!
60      * @param request
61      * DOCUMENT ME!
62      *
63      * @return DOCUMENT ME!
64      */

65     public boolean validateEmail(Object JavaDoc bean, ValidatorAction va, Field field,
66             ActionMessages errors, HttpServletRequest JavaDoc request) {
67         String JavaDoc value = ValidatorUtils.getValueAsString(bean, field
68                 .getProperty());
69         Perl5Util util = new Perl5Util();
70
71         if (!GenericValidator.isBlankOrNull(value)) {
72             if ((!util
73                     .match(
74                             "/( )|(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)|(^_+@)|(^\\-+@)/",
75                             value))
76                     && util
77                             .match(
78                                     "/^[\\w\\'\\.\\-]+@((\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)|[a-zA-Z0-9\\-]{2,})$/",
79                                     value)) {
80                 return true;
81             } else {
82
83                 errors.add(field.getKey(), Resources.getActionMessage(request,
84                         va, field));
85
86                 return false;
87             }
88         }
89
90         return true;
91     }
92
93     /**
94      * DOCUMENT ME!
95      *
96      * @param bean
97      * DOCUMENT ME!
98      * @param va
99      * DOCUMENT ME!
100      * @param field
101      * DOCUMENT ME!
102      * @param errors
103      * DOCUMENT ME!
104      * @param request
105      * DOCUMENT ME!
106      *
107      * @return DOCUMENT ME!
108      */

109     public boolean validateTwoFields(Object JavaDoc bean, ValidatorAction va,
110             Field field, ActionMessages errors, HttpServletRequest JavaDoc request) {
111         String JavaDoc value = ValidatorUtils.getValueAsString(bean, field
112                 .getProperty());
113         String JavaDoc sProperty2 = field.getVarValue("secondProperty");
114         String JavaDoc value2 = ValidatorUtils.getValueAsString(bean, sProperty2);
115
116         if (!GenericValidator.isBlankOrNull(value)) {
117             try {
118                 if (!value.equals(value2)) {
119                     errors.add(field.getKey(), Resources.getActionMessage(
120                             request, va, field));
121
122                     return false;
123                 }
124             } catch (Exception JavaDoc e) {
125                 errors.add(field.getKey(), Resources.getActionMessage(request,
126                         va, field));
127
128                 return false;
129             }
130         }
131
132         return true;
133     }
134
135     public boolean validateConfirmCode(Object JavaDoc bean, ValidatorAction va,
136             Field field, ActionMessages errors, HttpServletRequest JavaDoc request) {
137         String JavaDoc value = ValidatorUtils.getValueAsString(bean, field
138                 .getProperty());
139         HttpSession JavaDoc session = request.getSession();
140         String JavaDoc value2 = (String JavaDoc) session
141                 .getAttribute(IConst.SESSION.CONFIRM_CODE);
142
143         if (!GenericValidator.isBlankOrNull(value)) {
144             try {
145                 if (!value.equals(value2)) {
146                     errors.add(field.getKey(), Resources.getActionMessage(
147                             request, va, field));
148
149                     return false;
150                 }
151             } catch (Exception JavaDoc e) {
152                 errors.add(field.getKey(), Resources.getActionMessage(request,
153                         va, field));
154
155                 return false;
156             }
157         }
158
159         return true;
160     }
161 }
162
Popular Tags