%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.turbine.util.validation.InputValidator |
|
|
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 | 0 | private static String NullInputError = "Null Input Not Allowed"; |
32 | 0 | 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 | 0 | this(AllowNullInput, NoMaxSize, EmptyArgv); |
44 | 0 | } |
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 | 0 | { |
56 | 0 | this.allowNullInput = allowNullInput; |
57 | 0 | this.maxSize = maxSize; |
58 | 0 | this.argv = argv; |
59 | 0 | } |
60 | ||
61 | /** |
|
62 | * @param boolean allowNullInput, set allowNullInput |
|
63 | */ |
|
64 | public void setAllowNullInput(boolean allowNullInput) |
|
65 | { |
|
66 | 0 | this.allowNullInput = allowNullInput; |
67 | 0 | } |
68 | ||
69 | /** |
|
70 | * @param int maxSize, set maxSize |
|
71 | */ |
|
72 | public void setMaxSize(int maxSize) |
|
73 | { |
|
74 | 0 | this.maxSize = maxSize; |
75 | 0 | } |
76 | ||
77 | /** |
|
78 | * @param String argv, set argv |
|
79 | */ |
|
80 | public void setArgv(String argv) |
|
81 | { |
|
82 | 0 | this.argv = argv; |
83 | 0 | } |
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 | 0 | checkInput(input); |
94 | 0 | return true; |
95 | } |
|
96 | 0 | catch (Exception e) |
97 | { |
|
98 | 0 | 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 | 0 | checkInput(input); |
111 | } |
|
112 | 0 | catch (Exception e) |
113 | { |
|
114 | 0 | return e.toString(); |
115 | 0 | } |
116 | ||
117 | // there is no error |
|
118 | 0 | 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 | 0 | int size = 0; |
129 | 0 | if (value != null) |
130 | { |
|
131 | 0 | value = value.trim(); |
132 | 0 | size = value.length(); |
133 | } |
|
134 | ||
135 | 0 | if (!allowNullInput && value == null) |
136 | { |
|
137 | 0 | throw new Exception(NullInputError); |
138 | } |
|
139 | ||
140 | 0 | if (maxSize != NoMaxSize && size > maxSize) |
141 | { |
|
142 | 0 | throw new Exception(MaxSizeExceededError); |
143 | } |
|
144 | ||
145 | // allow the subclass to check specifics |
|
146 | 0 | check(value); |
147 | 0 | } |
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 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |