KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > input > validators > StringValidator


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.input.validators;
21
22 import java.util.Properties JavaDoc;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 import com.sslexplorer.boot.CodedException;
28 import com.sslexplorer.boot.PropertyDefinition;
29 import com.sslexplorer.boot.PropertyValidator;
30 import com.sslexplorer.boot.Util;
31 import com.sslexplorer.core.CoreException;
32
33 /**
34  * {@link PropertyValidator} implementation that validates string input and
35  * accepts a number of <i>Validator properties</i>.
36  * <ul>
37  * <li><b>minLength</b> - The minimum string length. This defaults to
38  * <code>zero</code></li>
39  * <li><b>maxLength</b> - The maximum string value. This defaults to
40  * <code>255</code>
41  * <li><b>regExp</b> - A regular expression to validate against. By default no
42  * pattern is matched</li>
43  * <li><b>pattern</b> - A pattern to validate against. By default no pattern
44  * is matched</li>
45  * <li><b>trim</b> - Boolean indicating whether to trim before validating
46  * (defaults to true)
47  * </ul>
48  *
49  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
50  */

51 public class StringValidator implements PropertyValidator {
52
53     final static Log log = LogFactory.getLog(IntegerValidator.class);
54
55     private int minLength = 0;
56     private int maxLength = 255;
57     private String JavaDoc regExp = "", pattern = "";
58     private boolean trim;
59     
60     //
61
protected int regExpErrCode = ErrorConstants.ERR_STRING_DOESNT_MATCH_REGEXP;
62
63     /**
64      * Constructor.
65      *
66      * @param minLength minimum length
67      * @param maxLength maximum length
68      * @param regExp regular expression or <code>null</code> not to check
69      * @param pattern simple pattern or <code>null</code> not to check
70      * @param trim trim string before validating
71      */

72     public StringValidator(int minLength, int maxLength, String JavaDoc regExp, String JavaDoc pattern, boolean trim) {
73         this.minLength = minLength;
74         this.maxLength = maxLength;
75         this.regExp = regExp;
76         this.pattern = pattern;
77         this.trim = trim;
78     }
79
80     /**
81      * Constructor. By default uses <code>zero</code> and <code>255</code>.
82      *
83      */

84     public StringValidator() {
85     }
86
87     /*
88      * (non-Javadoc)
89      *
90      * @see com.sslexplorer.boot.PropertyValidator#validate(com.sslexplorer.boot.PropertyDefinition,
91      * java.lang.String, java.util.Properties)
92      */

93     public void validate(PropertyDefinition definition, String JavaDoc value, Properties JavaDoc properties) throws CodedException {
94         //
95
if ("true".equalsIgnoreCase(properties == null ? "true" : properties.getProperty("trim", String.valueOf(trim)))) {
96             value = value.trim();
97         }
98
99         // Get the range
100
int min = minLength;
101         try {
102             if (properties != null && properties.containsKey("minLength"))
103                 min = Integer.parseInt(properties.getProperty("minLength"));
104         } catch (NumberFormatException JavaDoc nfe) {
105             log.error("Failed to get minimum value for validator.", nfe);
106             throw new CoreException(ErrorConstants.ERR_INTERNAL_ERROR,
107                             ErrorConstants.CATEGORY_NAME,
108                             ErrorConstants.BUNDLE_NAME,
109                             null,
110                             value);
111         }
112         int max = maxLength;
113         try {
114             if (properties != null && properties.containsKey("maxLength"))
115                 max = Integer.parseInt(properties.getProperty("maxLength"));
116         } catch (NumberFormatException JavaDoc nfe) {
117             log.error("Failed to get maximum value for validator.", nfe);
118             throw new CoreException(ErrorConstants.ERR_INTERNAL_ERROR,
119                             ErrorConstants.CATEGORY_NAME,
120                             ErrorConstants.BUNDLE_NAME,
121                             null,
122                             value);
123         }
124
125         // Validate
126
if (value.length() < min) {
127             throw new CoreException(ErrorConstants.ERR_STRING_TOO_SHORT,
128                             ErrorConstants.CATEGORY_NAME,
129                             ErrorConstants.BUNDLE_NAME,
130                             null,
131                             String.valueOf(min),
132                             String.valueOf(max),
133                             value,
134                             null);
135         }
136         if (value.length() > max) {
137             throw new CoreException(ErrorConstants.ERR_STRING_TOO_LONG,
138                             ErrorConstants.CATEGORY_NAME,
139                             ErrorConstants.BUNDLE_NAME,
140                             null,
141                             String.valueOf(min),
142                             String.valueOf(max),
143                             value,
144                             null);
145         }
146
147         // Regular expression
148
String JavaDoc regExp = properties == null ? this.regExp : Util.trimmedOrBlank(properties.getProperty("regExp", this.regExp));
149         if (regExp != null && !regExp.equals("") && !value.matches(regExp)) {
150             throw new CoreException(regExpErrCode,
151                             ErrorConstants.CATEGORY_NAME,
152                             ErrorConstants.BUNDLE_NAME,
153                             null,
154                             String.valueOf(regExp),
155                             value,
156                             null,
157                             null);
158
159         }
160
161         // Simple pattern
162
String JavaDoc pattern = Util.trimmedOrBlank(properties == null ? this.pattern : properties.getProperty("pattern", this.pattern));
163         if (!pattern.equals("")) {
164             pattern = Util.parseSimplePatternToRegExp(pattern);
165             if(!value.matches(pattern)) {
166                 throw new CoreException(ErrorConstants.ERR_STRING_DOESNT_MATCH_SIMPLE_PATTERN,
167                                 ErrorConstants.CATEGORY_NAME,
168                                 ErrorConstants.BUNDLE_NAME,
169                                 null,
170                                 String.valueOf(pattern),
171                                 value,
172                                 null,
173                                 null);
174             }
175         }
176     }
177
178 }
179
Popular Tags