KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > util > validation > InputValidator


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

18
19 /**
20  * @author <a HREF="mailto:mikeh@ncsa.uiuc.edu">Mike Haberman</a>
21  * @version $Id: InputValidator.java,v 1.4.2.2 2004/05/20 03:28:02 seade Exp $
22  * @deprecated Use Intake or commons-validator
23  */

24 public abstract class InputValidator
25 {
26     public static final boolean AllowNullInput = true;// allow null
27
public static final int NoMaxSize = -1; // no size restrictions
28
public static final String JavaDoc EmptyArgv = ""; // no optional arguments
29

30     // default error messages
31
private static String JavaDoc NullInputError = "Null Input Not Allowed";
32     private static String JavaDoc MaxSizeExceededError = "Maximum Size Exceeded";
33
34     private boolean allowNullInput;
35     private int maxSize;
36     private String JavaDoc argv;
37
38     /**
39      * default Constructor,
40      */

41     public InputValidator()
42     {
43         this(AllowNullInput, NoMaxSize, EmptyArgv);
44     }
45
46     /**
47      * Constructor,
48      * @param boolean allowNullInput
49      * @param int maxSize
50      * @param String argv
51      */

52     public InputValidator(boolean allowNullInput,
53                           int maxSize,
54                           String JavaDoc argv)
55     {
56         this.allowNullInput = allowNullInput;
57         this.maxSize = maxSize;
58         this.argv = argv;
59     }
60
61     /**
62      * @param boolean allowNullInput, set allowNullInput
63      */

64     public void setAllowNullInput(boolean allowNullInput)
65     {
66         this.allowNullInput = allowNullInput;
67     }
68
69     /**
70      * @param int maxSize, set maxSize
71      */

72     public void setMaxSize(int maxSize)
73     {
74         this.maxSize = maxSize;
75     }
76
77     /**
78      * @param String argv, set argv
79      */

80     public void setArgv(String JavaDoc argv)
81     {
82         this.argv = argv;
83     }
84
85     /**
86      * @param String input, input to be checked
87      * @return boolean, whether or not the input is valid
88      */

89     public boolean isValid(String JavaDoc input)
90     {
91         try
92         {
93             checkInput(input);
94             return true;
95         }
96         catch (Exception JavaDoc e)
97         {
98             return false;
99         }
100     }
101
102     /**
103      * @param String input, input to be checked
104      * @return String, error message or null
105      */

106     public String JavaDoc getErrorMessage(String JavaDoc input)
107     {
108         try
109         {
110             checkInput(input);
111         }
112         catch (Exception JavaDoc e)
113         {
114             return e.toString();
115         }
116
117         // there is no error
118
return null;
119     }
120
121     /**
122      * @param String value
123      * @exception Exception, a generic exception.
124      */

125     public void checkInput(String JavaDoc value)
126             throws Exception JavaDoc
127     {
128         int size = 0;
129         if (value != null)
130         {
131             value = value.trim();
132             size = value.length();
133         }
134
135         if (!allowNullInput && value == null)
136         {
137             throw new Exception JavaDoc(NullInputError);
138         }
139
140         if (maxSize != NoMaxSize && size > maxSize)
141         {
142             throw new Exception JavaDoc(MaxSizeExceededError);
143         }
144
145         // allow the subclass to check specifics
146
check(value);
147     }
148
149     /**
150      * @return String, the expected format of the input
151      */

152     public abstract String JavaDoc getExpectedFormat();
153
154     /**
155      * @param String input, input to be checked
156      * all subclasses must define this method
157      */

158     protected abstract void check(String JavaDoc input)
159             throws Exception JavaDoc;
160
161 }
162
Popular Tags