001: /*
002: Launch4j (http://launch4j.sourceforge.net/)
003: Cross-platform Java application wrapper for creating Windows native executables.
004:
005: Copyright (c) 2004, 2007 Grzegorz Kowal
006:
007: All rights reserved.
008:
009: Redistribution and use in source and binary forms, with or without modification,
010: are permitted provided that the following conditions are met:
011:
012: * Redistributions of source code must retain the above copyright notice,
013: this list of conditions and the following disclaimer.
014: * Redistributions in binary form must reproduce the above copyright notice,
015: this list of conditions and the following disclaimer in the documentation
016: and/or other materials provided with the distribution.
017: * Neither the name of the Launch4j nor the names of its contributors
018: may be used to endorse or promote products derived from this software without
019: specific prior written permission.
020:
021: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
022: "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
023: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
024: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
025: CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
026: EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
027: PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
028: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
029: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
030: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
031: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
032: */
033:
034: /*
035: * Created on 2004-01-30
036: */
037: package net.sf.launch4j.binding;
038:
039: import java.io.File;
040: import java.util.Arrays;
041: import java.util.Collection;
042: import java.util.HashSet;
043: import java.util.Iterator;
044: import java.util.List;
045:
046: import net.sf.launch4j.Util;
047: import net.sf.launch4j.config.ConfigPersister;
048:
049: /**
050: * @author Copyright (C) 2004 Grzegorz Kowal
051: */
052: public class Validator {
053: public static final String ALPHANUMERIC_PATTERN = "[\\w]*?";
054: public static final String ALPHA_PATTERN = "[\\w&&\\D]*?";
055: public static final String NUMERIC_PATTERN = "[\\d]*?";
056: public static final String PATH_PATTERN = "[\\w|[ .,:\\-/\\\\]]*?";
057:
058: public static final int MAX_STR = 128;
059: public static final int MAX_PATH = 260;
060: public static final int MAX_BIG_STR = 8192; // or 16384;
061: public static final int MAX_ARGS = 32767 - 2048;
062:
063: private Validator() {
064: }
065:
066: public static boolean isEmpty(String s) {
067: return s == null || s.equals("");
068: }
069:
070: public static void checkNotNull(Object o, String property,
071: String name) {
072: if (o == null) {
073: signalViolation(property, Messages.getString(
074: "Validator.empty.field", name));
075: }
076: }
077:
078: public static void checkString(String s, int maxLength,
079: String property, String name) {
080: if (s == null || s.length() == 0) {
081: signalViolation(property, Messages.getString(
082: "Validator.empty.field", name));
083: }
084: if (s.length() > maxLength) {
085: signalLengthViolation(property, name, maxLength);
086: }
087: }
088:
089: public static void checkOptStrings(List strings, int maxLength,
090: int totalMaxLength, String property, String name) {
091: if (strings == null) {
092: return;
093: }
094: int totalLength = 0;
095: for (Iterator iter = strings.iterator(); iter.hasNext();) {
096: String s = (String) iter.next();
097: checkString(s, maxLength, property, name);
098: totalLength += s.length();
099: if (totalLength > totalMaxLength) {
100: signalLengthViolation(property, name, totalMaxLength);
101: }
102: }
103: }
104:
105: public static void checkString(String s, int maxLength,
106: String pattern, String property, String name) {
107: checkString(s, maxLength, property, name);
108: if (!s.matches(pattern)) {
109: signalViolation(property, Messages.getString(
110: "Validator.invalid.data", name));
111: }
112: }
113:
114: public static void checkOptStrings(List strings, int maxLength,
115: int totalMaxLength, String pattern, String property,
116: String name, String msg) {
117: if (strings == null) {
118: return;
119: }
120: int totalLength = 0;
121: for (Iterator iter = strings.iterator(); iter.hasNext();) {
122: String s = (String) iter.next();
123: checkString(s, maxLength, property, name);
124: if (!s.matches(pattern)) {
125: signalViolation(property, msg != null ? msg : Messages
126: .getString("Validator.invalid.data", name));
127: }
128: totalLength += s.length();
129: if (totalLength > totalMaxLength) {
130: signalLengthViolation(property, name, totalMaxLength);
131: }
132: }
133: }
134:
135: public static void checkOptString(String s, int maxLength,
136: String property, String name) {
137: if (s == null || s.length() == 0) {
138: return;
139: }
140: if (s.length() > maxLength) {
141: signalLengthViolation(property, name, maxLength);
142: }
143: }
144:
145: public static void checkOptString(String s, int maxLength,
146: String pattern, String property, String name) {
147: if (s == null || s.length() == 0) {
148: return;
149: }
150: if (s.length() > maxLength) {
151: signalLengthViolation(property, name, maxLength);
152: }
153: if (!s.matches(pattern)) {
154: signalViolation(property, Messages.getString(
155: "Validator.invalid.data", name));
156: }
157: }
158:
159: public static void checkRange(int value, int min, int max,
160: String property, String name) {
161: if (value < min || value > max) {
162: signalViolation(property, Messages.getString(
163: "Validator.must.be.in.range", name, String
164: .valueOf(min), String.valueOf(max)));
165: }
166: }
167:
168: public static void checkRange(char value, char min, char max,
169: String property, String name) {
170: if (value < min || value > max) {
171: signalViolation(property, Messages.getString(
172: "Validator.must.be.in.range", name, String
173: .valueOf(min), String.valueOf(max)));
174: }
175: }
176:
177: public static void checkMin(int value, int min, String property,
178: String name) {
179: if (value < min) {
180: signalViolation(property, Messages.getString(
181: "Validator.must.be.at.least", name, String
182: .valueOf(min)));
183: }
184: }
185:
186: public static void checkIn(String s, String[] strings,
187: String property, String name) {
188: if (isEmpty(s)) {
189: signalViolation(property, Messages.getString(
190: "Validator.empty.field", name));
191: }
192: List list = Arrays.asList(strings);
193: if (!list.contains(s)) {
194: signalViolation(property, Messages.getString(
195: "Validator.invalid.option", name, list.toString()));
196: }
197: }
198:
199: public static void checkTrue(boolean condition, String property,
200: String msg) {
201: if (!condition) {
202: signalViolation(property, msg);
203: }
204: }
205:
206: public static void checkFalse(boolean condition, String property,
207: String msg) {
208: if (condition) {
209: signalViolation(property, msg);
210: }
211: }
212:
213: public static void checkElementsNotNullUnique(Collection c,
214: String property, String msg) {
215: if (c.contains(null) || new HashSet(c).size() != c.size()) {
216: signalViolation(property, Messages.getString(
217: "Validator.already.exists", msg));
218: }
219: }
220:
221: public static void checkElementsUnique(Collection c,
222: String property, String msg) {
223: if (new HashSet(c).size() != c.size()) {
224: signalViolation(property, Messages.getString(
225: "Validator.already.exists", msg));
226: }
227: }
228:
229: public static void checkFile(File f, String property,
230: String fileDescription) {
231: File cfgPath = ConfigPersister.getInstance().getConfigPath();
232: if (f == null
233: || f.getPath().equals("")
234: || (!f.exists() && !Util.getAbsoluteFile(cfgPath, f)
235: .exists())) {
236: signalViolation(property, Messages.getString(
237: "Validator.doesnt.exist", fileDescription));
238: }
239: }
240:
241: public static void checkOptFile(File f, String property,
242: String fileDescription) {
243: if (f != null && f.getPath().length() > 0) {
244: checkFile(f, property, fileDescription);
245: }
246: }
247:
248: public static void checkRelativeWinPath(String path,
249: String property, String msg) {
250: if (path == null || path.equals("") || path.startsWith("/")
251: || path.startsWith("\\") || path.indexOf(':') != -1) {
252: signalViolation(property, msg);
253: }
254: }
255:
256: public static void signalLengthViolation(String property,
257: String name, int maxLength) {
258: signalViolation(property, Messages.getString(
259: "Validator.exceeds.max.length", name, String
260: .valueOf(maxLength)));
261: }
262:
263: public static void signalViolation(String property, String msg) {
264: throw new InvariantViolationException(property, msg);
265: }
266: }
|