KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > launch4j > binding > Validator


1 /*
2     Launch4j (http://launch4j.sourceforge.net/)
3     Cross-platform Java application wrapper for creating Windows native executables.
4
5     Copyright (C) 2004, 2006 Grzegorz Kowal
6
7     This program is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License
18     along with this program; if not, write to the Free Software
19     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */

21
22 /*
23  * Created on 2004-01-30
24  */

25 package net.sf.launch4j.binding;
26
27 import java.io.File JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.HashSet JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33
34 import net.sf.launch4j.Util;
35 import net.sf.launch4j.config.ConfigPersister;
36
37 /**
38  * @author Copyright (C) 2004 Grzegorz Kowal
39  */

40 public class Validator {
41     public static final String JavaDoc ALPHANUMERIC_PATTERN = "[\\w]*?";
42     public static final String JavaDoc ALPHA_PATTERN = "[\\w&&\\D]*?";
43     public static final String JavaDoc NUMERIC_PATTERN = "[\\d]*?";
44     public static final String JavaDoc PATH_PATTERN = "[\\w|[ .,:\\-/\\\\]]*?";
45
46     public static final int MAX_STR = 128;
47     public static final int MAX_PATH = 260;
48     public static final int MAX_BIG_STR = 8192; // or 16384;
49
public static final int MAX_ARGS = 32767 - 2048;
50
51     private Validator() {}
52
53     public static boolean isEmpty(String JavaDoc s) {
54         return s == null || s.equals("");
55     }
56
57     public static void checkNotNull(Object JavaDoc o, String JavaDoc property, String JavaDoc name) {
58         if (o == null) {
59             signalViolation(property,
60                     Messages.getString("Validator.empty.field", name));
61         }
62     }
63
64     public static void checkString(String JavaDoc s, int maxLength, String JavaDoc property,
65             String JavaDoc name) {
66         if (s == null || s.length() == 0) {
67             signalViolation(property,
68                     Messages.getString("Validator.empty.field", name));
69         }
70         if (s.length() > maxLength) {
71             signalLengthViolation(property, name, maxLength);
72         }
73     }
74
75     public static void checkOptStrings(List JavaDoc strings, int maxLength, int totalMaxLength,
76             String JavaDoc property, String JavaDoc name) {
77         if (strings == null) {
78             return;
79         }
80         int totalLength = 0;
81         for (Iterator JavaDoc iter = strings.iterator(); iter.hasNext();) {
82             String JavaDoc s = (String JavaDoc) iter.next();
83             checkString(s, maxLength, property, name);
84             totalLength += s.length();
85             if (totalLength > totalMaxLength) {
86                 signalLengthViolation(property, name, totalMaxLength);
87             }
88         }
89     }
90
91     public static void checkString(String JavaDoc s, int maxLength, String JavaDoc pattern,
92             String JavaDoc property, String JavaDoc name) {
93         checkString(s, maxLength, property, name);
94         if (!s.matches(pattern)) {
95             signalViolation(property,
96                     Messages.getString("Validator.invalid.data", name));
97         }
98     }
99
100     public static void checkOptStrings(List JavaDoc strings, int maxLength, int totalMaxLength,
101             String JavaDoc pattern, String JavaDoc property, String JavaDoc name, String JavaDoc msg) {
102         if (strings == null) {
103             return;
104         }
105         int totalLength = 0;
106         for (Iterator JavaDoc iter = strings.iterator(); iter.hasNext();) {
107             String JavaDoc s = (String JavaDoc) iter.next();
108             checkString(s, maxLength, property, name);
109             if (!s.matches(pattern)) {
110                 signalViolation(property, msg != null
111                         ? msg
112                         : Messages.getString("Validator.invalid.data", name));
113             }
114             totalLength += s.length();
115             if (totalLength > totalMaxLength) {
116                 signalLengthViolation(property, name, totalMaxLength);
117             }
118         }
119     }
120
121     public static void checkOptString(String JavaDoc s, int maxLength, String JavaDoc property,
122             String JavaDoc name) {
123         if (s == null || s.length() == 0) {
124             return;
125         }
126         if (s.length() > maxLength) {
127             signalLengthViolation(property, name, maxLength);
128         }
129     }
130
131     public static void checkOptString(String JavaDoc s, int maxLength, String JavaDoc pattern,
132             String JavaDoc property, String JavaDoc name) {
133         if (s == null || s.length() == 0) {
134             return;
135         }
136         if (s.length() > maxLength) {
137             signalLengthViolation(property, name, maxLength);
138         }
139         if (!s.matches(pattern)) {
140             signalViolation(property,
141                     Messages.getString("Validator.invalid.data", name));
142         }
143     }
144
145     public static void checkRange(int value, int min, int max,
146             String JavaDoc property, String JavaDoc name) {
147         if (value < min || value > max) {
148             signalViolation(property,
149                     Messages.getString("Validator.must.be.in.range", name,
150                             String.valueOf(min), String.valueOf(max)));
151         }
152     }
153
154     public static void checkRange(char value, char min, char max,
155             String JavaDoc property, String JavaDoc name) {
156         if (value < min || value > max) {
157             signalViolation(property, Messages.getString("Validator.must.be.in.range",
158                     name, String.valueOf(min), String.valueOf(max)));
159         }
160     }
161
162     public static void checkMin(int value, int min, String JavaDoc property, String JavaDoc name) {
163         if (value < min) {
164             signalViolation(property,
165                     Messages.getString("Validator.must.be.at.least", name,
166                             String.valueOf(min)));
167         }
168     }
169
170     public static void checkIn(String JavaDoc s, String JavaDoc[] strings, String JavaDoc property,
171             String JavaDoc name) {
172         if (isEmpty(s)) {
173             signalViolation(property,
174                     Messages.getString("Validator.empty.field", name));
175         }
176         List JavaDoc list = Arrays.asList(strings);
177         if (!list.contains(s)) {
178             signalViolation(property,
179                     Messages.getString("Validator.invalid.option", name, list.toString()));
180         }
181     }
182
183     public static void checkTrue(boolean condition, String JavaDoc property, String JavaDoc msg) {
184         if (!condition) {
185             signalViolation(property, msg);
186         }
187     }
188     
189     public static void checkFalse(boolean condition, String JavaDoc property, String JavaDoc msg) {
190         if (condition) {
191             signalViolation(property, msg);
192         }
193     }
194     
195     public static void checkElementsNotNullUnique(Collection JavaDoc c, String JavaDoc property,
196             String JavaDoc msg) {
197         if (c.contains(null)
198                 || new HashSet JavaDoc(c).size() != c.size()) {
199             signalViolation(property,
200                     Messages.getString("Validator.already.exists", msg));
201         }
202     }
203
204     public static void checkElementsUnique(Collection JavaDoc c, String JavaDoc property, String JavaDoc msg) {
205         if (new HashSet JavaDoc(c).size() != c.size()) {
206             signalViolation(property,
207                     Messages.getString("Validator.already.exists", msg));
208         }
209     }
210
211     public static void checkFile(File JavaDoc f, String JavaDoc property, String JavaDoc fileDescription) {
212         File JavaDoc cfgPath = ConfigPersister.getInstance().getConfigPath();
213         if (f == null
214                 || f.getPath().equals("")
215                 || (!f.exists() && !Util.getAbsoluteFile(cfgPath, f).exists())) {
216             signalViolation(property,
217                     Messages.getString("Validator.doesnt.exist", fileDescription));
218         }
219     }
220
221     public static void checkOptFile(File JavaDoc f, String JavaDoc property, String JavaDoc fileDescription) {
222         if (f != null && f.getPath().length() > 0) {
223             checkFile(f, property, fileDescription);
224         }
225     }
226
227     public static void checkRelativeWinPath(String JavaDoc path, String JavaDoc property, String JavaDoc msg) {
228         if (path == null
229                 || path.equals("")
230                 || path.startsWith("/")
231                 || path.startsWith("\\")
232                 || path.indexOf(':') != -1) {
233             signalViolation(property, msg);
234         }
235     }
236
237     public static void signalLengthViolation(String JavaDoc property, String JavaDoc name,
238             int maxLength) {
239         signalViolation(property,
240                 Messages.getString("Validator.exceeds.max.length", name,
241                         String.valueOf(maxLength)));
242     }
243
244     public static void signalViolation(String JavaDoc property, String JavaDoc msg) {
245         throw new InvariantViolationException(property, msg);
246     }
247 }
248
Popular Tags