1 21 22 25 package net.sf.launch4j.binding; 26 27 import java.io.File ; 28 import java.util.Arrays ; 29 import java.util.Collection ; 30 import java.util.HashSet ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 34 import net.sf.launch4j.Util; 35 import net.sf.launch4j.config.ConfigPersister; 36 37 40 public class Validator { 41 public static final String ALPHANUMERIC_PATTERN = "[\\w]*?"; 42 public static final String ALPHA_PATTERN = "[\\w&&\\D]*?"; 43 public static final String NUMERIC_PATTERN = "[\\d]*?"; 44 public static final String 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; public static final int MAX_ARGS = 32767 - 2048; 50 51 private Validator() {} 52 53 public static boolean isEmpty(String s) { 54 return s == null || s.equals(""); 55 } 56 57 public static void checkNotNull(Object o, String property, String name) { 58 if (o == null) { 59 signalViolation(property, 60 Messages.getString("Validator.empty.field", name)); 61 } 62 } 63 64 public static void checkString(String s, int maxLength, String property, 65 String 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 strings, int maxLength, int totalMaxLength, 76 String property, String name) { 77 if (strings == null) { 78 return; 79 } 80 int totalLength = 0; 81 for (Iterator iter = strings.iterator(); iter.hasNext();) { 82 String s = (String ) 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 s, int maxLength, String pattern, 92 String property, String 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 strings, int maxLength, int totalMaxLength, 101 String pattern, String property, String name, String msg) { 102 if (strings == null) { 103 return; 104 } 105 int totalLength = 0; 106 for (Iterator iter = strings.iterator(); iter.hasNext();) { 107 String s = (String ) 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 s, int maxLength, String property, 122 String 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 s, int maxLength, String pattern, 132 String property, String 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 property, String 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 property, String 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 property, String 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 s, String [] strings, String property, 171 String name) { 172 if (isEmpty(s)) { 173 signalViolation(property, 174 Messages.getString("Validator.empty.field", name)); 175 } 176 List 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 property, String msg) { 184 if (!condition) { 185 signalViolation(property, msg); 186 } 187 } 188 189 public static void checkFalse(boolean condition, String property, String msg) { 190 if (condition) { 191 signalViolation(property, msg); 192 } 193 } 194 195 public static void checkElementsNotNullUnique(Collection c, String property, 196 String msg) { 197 if (c.contains(null) 198 || new HashSet (c).size() != c.size()) { 199 signalViolation(property, 200 Messages.getString("Validator.already.exists", msg)); 201 } 202 } 203 204 public static void checkElementsUnique(Collection c, String property, String msg) { 205 if (new HashSet (c).size() != c.size()) { 206 signalViolation(property, 207 Messages.getString("Validator.already.exists", msg)); 208 } 209 } 210 211 public static void checkFile(File f, String property, String fileDescription) { 212 File 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 f, String property, String fileDescription) { 222 if (f != null && f.getPath().length() > 0) { 223 checkFile(f, property, fileDescription); 224 } 225 } 226 227 public static void checkRelativeWinPath(String path, String property, String 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 property, String 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 property, String msg) { 245 throw new InvariantViolationException(property, msg); 246 } 247 } 248 | Popular Tags |