KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.regex.Matcher JavaDoc;
24 import java.util.regex.Pattern JavaDoc;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 import com.sslexplorer.boot.CodedException;
30 import com.sslexplorer.boot.PropertyDefinition;
31 import com.sslexplorer.boot.PropertyValidator;
32 import com.sslexplorer.boot.ReplacementEngine;
33 import com.sslexplorer.boot.Replacer;
34 import com.sslexplorer.core.CoreException;
35 import com.sslexplorer.core.stringreplacement.VariableReplacement;
36
37
38 /**
39  * {@link PropertyValidator} implementation that excepts two <i>Validator properties</i>.
40  * <ul>
41  * <li><b>minValue</b> - The minimum integer value. This defaults to <code>zero</code></li>
42  * <li><b>maxValue</b> - The maximum integer value. This defaults to {@link Integer#MAX_VALUE}.</li>
43  * </ul>
44  *
45  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
46  */

47 public class IntegerValidator implements PropertyValidator {
48     
49     final static Log log = LogFactory.getLog(IntegerValidator.class);
50     
51     private int defaultMin = 0;
52     private int defaultMax = Integer.MAX_VALUE;
53     
54     /**
55      * Constructor.
56      *
57      * @param defaultMin default minimum value
58      * @param defaultMax default maximum value
59      */

60     public IntegerValidator(int defaultMin, int defaultMax) {
61         this.defaultMin = defaultMin;
62         this.defaultMax = defaultMax;
63     }
64     
65    /**
66     * Constructor. The by default uses {@link Integer#MIN_VALUE} and
67     * {@link Integer#MAX_VALUE}.
68     *
69     */

70     public IntegerValidator() {
71     }
72
73     /* (non-Javadoc)
74      * @see com.sslexplorer.boot.PropertyValidator#validate(com.sslexplorer.boot.PropertyDefinition, java.lang.String, java.util.Properties)
75      */

76     public void validate(PropertyDefinition definition, String JavaDoc value, Properties JavaDoc properties) throws CodedException {
77         
78         // Get the range
79
int min = defaultMin;
80         try {
81             if(properties != null && properties.containsKey("minValue"))
82                 min = Integer.parseInt(properties.getProperty("minValue"));
83         }
84         catch(NumberFormatException JavaDoc nfe) {
85             log.error("Failed to get minimum value for validator.", nfe);
86             throw new CoreException(ErrorConstants.ERR_INTERNAL_ERROR, ErrorConstants.CATEGORY_NAME, ErrorConstants.BUNDLE_NAME, null, value);
87         }
88         int max = defaultMax;
89         try {
90             if(properties != null && properties.containsKey("maxValue"))
91                 max = Integer.parseInt(properties.getProperty("maxValue"));
92         }
93         catch(NumberFormatException JavaDoc nfe) {
94             log.error("Failed to get maximum value for validator.", nfe);
95             throw new CoreException(ErrorConstants.ERR_INTERNAL_ERROR, ErrorConstants.CATEGORY_NAME, ErrorConstants.BUNDLE_NAME, null, value);
96         }
97
98         /* We may support replacement variables so
99          * to validate we must replace with the minimum value
100          */

101         if(properties != null && "true".equalsIgnoreCase(properties.getProperty("replacementVariables"))) {
102             Replacer r = new Replacer() {
103                 public String JavaDoc getReplacement(Pattern JavaDoc pattern, Matcher JavaDoc matcher, String JavaDoc replacementPattern) {
104                     return replacementPattern;
105                 }
106             };
107             ReplacementEngine re = new ReplacementEngine();
108             re.addPattern(VariableReplacement.VARIABLE_PATTERN, r, String.valueOf(min));
109             value = re.replace(value);
110         }
111         
112         // Validate
113
try {
114             int i = Integer.parseInt(value);
115             if(i < min || i > max) {
116                 throw new CoreException(ErrorConstants.ERR_INTEGER_OUT_OF_RANGE, ErrorConstants.CATEGORY_NAME, ErrorConstants.BUNDLE_NAME, null, String.valueOf(min), String.valueOf(max), value, null);
117             }
118         }
119         catch(NumberFormatException JavaDoc nfe) {
120             throw new CoreException(ErrorConstants.ERR_NOT_AN_INTEGER, ErrorConstants.CATEGORY_NAME, ErrorConstants.BUNDLE_NAME, null, String.valueOf(min), String.valueOf(max), value, null);
121         }
122     }
123
124 }
125
Popular Tags