1 16 package org.apache.jetspeed.util; 17 18 import org.apache.oro.text.perl.*; 19 21 import java.net.*; 22 import org.apache.commons.lang.StringUtils; 23 24 31 public class ValidationHelper 32 { 33 42 public static boolean isAlphaNumeric(String evalString, boolean required) 43 { 44 if (StringUtils.isEmpty(evalString)) 45 { 46 if (true == required) 47 { 48 return false; 49 } 50 return true; 51 } 52 Perl5Util util = new Perl5Util(); 53 return util.match("/^[\\w\\s]+$/", evalString); 54 } 55 56 public static boolean isAlphaNumeric(String evalString, boolean required, int maxLength) 57 { 58 if (isTooLong(evalString, maxLength)) 59 { 60 return false; 61 } 62 return isAlphaNumeric(evalString, required); 63 } 64 65 public static boolean isLooseAlphaNumeric(String evalString, boolean required) 66 { 67 if (StringUtils.isEmpty(evalString)) 68 { 69 if (true == required) 70 { 71 return false; 72 } 73 return true; 74 } 75 Perl5Util util = new Perl5Util(); 76 return util.match("/^[\\w\\s\\.\\,\\/\\-\\(\\)\\+]+$/", evalString); 77 } 78 79 public static boolean isLooseAlphaNumeric(String evalString, boolean required, int maxLength) 80 { 81 if (isTooLong(evalString, maxLength)) 82 { 83 return false; 84 } 85 return isLooseAlphaNumeric(evalString, required); 86 } 87 88 94 public static boolean isDecimal(String evalString, boolean required) 95 { 96 if (StringUtils.isEmpty(evalString)) 97 { 98 if (true == required) 99 { 100 return false; 101 } 102 return true; 103 } 104 Perl5Util util = new Perl5Util(); 105 return util.match("/^(\\d+\\.)?\\d+$/", evalString); 106 } 107 108 public static boolean isDecimal(String evalString, boolean required, int maxLength) 109 { 110 if (isTooLong(evalString, maxLength)) 111 { 112 return false; 113 } 114 return isDecimal(evalString, required); 115 } 116 117 123 public static boolean isInteger (String evalString, boolean required) 124 { 125 if (StringUtils.isEmpty(evalString)) 126 { 127 if (true == required) 128 { 129 return false; 130 } 131 return true; 132 } 133 Perl5Util util = new Perl5Util(); 134 return util.match("/^\\d+$/", evalString); 135 } 136 137 public static boolean isInteger(String evalString, boolean required, int maxLength) 138 { 139 if (isTooLong(evalString, maxLength)) 140 { 141 return false; 142 } 143 return isInteger(evalString, required); 144 } 145 146 152 public static boolean isEmailAddress(String evalString, boolean required) 153 { 154 if (StringUtils.isEmpty(evalString)) 155 { 156 if (true == required) 157 { 158 return false; 159 } 160 return true; 161 } 162 Perl5Util util = new Perl5Util(); 163 return util.match("/^(?:\\w[\\w-]*\\.)*\\w[\\w-]*@(?:\\w[\\w-]*\\.)+\\w[\\w-]*$/", evalString); 164 } 165 166 public static boolean isEmailAddress(String evalString, boolean required, int maxLength) 167 { 168 if (isTooLong(evalString, maxLength)) 169 { 170 return false; 171 } 172 return isEmailAddress(evalString, required); 173 } 174 175 181 public static boolean isURL(String evalString, boolean required) 182 { 183 try 184 { 185 if (StringUtils.isEmpty(evalString)) 186 { 187 if (true == required) 188 { 189 return false; 190 } 191 return true; 192 } 193 194 URL url = new URL(evalString); 195 196 201 return true; 204 } 205 catch (Exception e) 206 { 207 System.err.println(evalString+" is not a valid URL: "+ e); 208 return false; 209 } 210 } 211 212 public static boolean isURL(String evalString, boolean required, int maxLength) 213 { 214 if (isTooLong(evalString, maxLength)) 215 { 216 return false; 217 } 218 return isURL(evalString, required); 219 } 220 221 public static boolean isValidIdentifier(String folderName) 222 { 223 boolean valid = true; 224 225 char[] chars = folderName.toCharArray(); 226 for (int ix = 0; ix < chars.length; ix++) 227 { 228 if (!Character.isJavaIdentifierPart(chars[ix])) 229 { 230 valid = false; 231 break; 232 } 233 } 234 return valid; 235 } 236 237 public static boolean isTooLong(String evalString, int maxLength) 238 { 239 if (null != evalString) 240 { 241 return (evalString.length() > maxLength); 242 } 243 return false; 244 } 245 246 } 247 248 | Popular Tags |