View Javadoc

1   package org.apache.turbine.util.validation;
2   
3   /*
4    * Copyright 2001-2005 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 264148 2005-08-29 14:21:04Z henning $
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 EmptyArgv = "";  // no optional arguments
29  
30      // default error messages
31      private static String NullInputError = "Null Input Not Allowed";
32      private static String MaxSizeExceededError = "Maximum Size Exceeded";
33  
34      private boolean allowNullInput;
35      private int maxSize;
36      private String 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 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 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 input)
90      {
91          try
92          {
93              checkInput(input);
94              return true;
95          }
96          catch (Exception 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 getErrorMessage(String input)
107     {
108         try
109         {
110             checkInput(input);
111         }
112         catch (Exception 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 value)
126             throws Exception
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(NullInputError);
138         }
139 
140         if (maxSize != NoMaxSize && size > maxSize)
141         {
142             throw new Exception(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 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 input)
159             throws Exception;
160 
161 }