1 16 17 package org.apache.taglibs.random; 18 19 import java.security.NoSuchAlgorithmException ; 20 import java.security.NoSuchProviderException ; 21 import java.security.SecureRandom ; 22 import java.util.ArrayList ; 23 import java.util.HashMap ; 24 import java.util.Random ; 25 26 import javax.servlet.jsp.JspException ; 27 28 36 37 public class RandomStrg { 38 39 42 private String randomstr; 43 46 private boolean allchars = false; 47 50 private Integer length = new Integer (8); 51 54 private HashMap hmap; 55 58 private ArrayList lower = null; 59 62 private ArrayList upper = null; 63 66 private char[] single = null; 67 70 private int singlecount = 0; 71 75 private boolean singles = false; 76 79 private String algorithm = null; 80 83 private String provider = null; 84 88 private boolean secure = false; 89 92 private Random random = null; 93 96 private SecureRandom secrandom = null; 97 98 103 private final float getFloat() { 104 if (random == null) 105 return secrandom.nextFloat(); 106 else 107 return random.nextFloat(); 108 } 109 110 115 public final void generateRandomObject() throws JspException { 116 117 if (secure) { 119 try { 120 if (provider != null) 122 random = SecureRandom.getInstance(algorithm, provider); 124 else 125 random = SecureRandom.getInstance(algorithm); 126 } catch (NoSuchAlgorithmException ne) { 127 throw new JspException (ne.getMessage()); 128 } catch (NoSuchProviderException pe) { 129 throw new JspException (pe.getMessage()); 130 } 131 } else 132 random = new Random (); 133 } 134 135 139 private final void generaterandom() { 140 141 if (allchars) 143 for (int i = 0; i < length.intValue(); i++) 144 randomstr = randomstr + new Character ((char)((int) 34 + 145 ((int)(getFloat() * 93)))).toString(); 146 else if (singles) { 147 149 if (upper.size() == 3) { 150 152 for (int i = 0; i < length.intValue(); i++) { 154 157 if (((int) (getFloat() * 100)) % 2 == 0) { 159 160 if (((int) (getFloat() * 100)) % 2 == 0) 162 randomstr = randomstr + randomSingle().toString(); 164 else 165 randomstr = randomstr + randomChar((Character )lower.get(2), 167 (Character )upper.get(2)).toString(); 168 } else { 169 171 if (((int) (getFloat() * 100)) % 2 == 0) 172 randomstr = randomstr + randomChar((Character )lower.get(1), 174 (Character )upper.get(1)).toString(); 175 else 176 randomstr = randomstr + randomChar((Character )lower.get(0), 178 (Character )upper.get(0)).toString(); 179 } 180 } 181 } else if (upper.size() == 2) { 182 185 for (int i = 0; i < length.intValue(); i++) { 187 190 if (((int)(getFloat() * 100)) % 2 == 0) { 191 192 randomstr = randomstr + randomSingle().toString(); 194 } else if (((int) (getFloat() * 100)) % 2 == 0) { 195 196 randomstr = randomstr + randomChar((Character )lower.get(1), 198 (Character )upper.get(1)).toString(); 199 } else { 200 201 randomstr = randomstr + randomChar((Character )lower.get(0), 203 (Character )upper.get(0)).toString(); 204 } 205 } 206 } else if (upper.size() == 1) { 207 208 for (int i = 0; i < length.intValue(); i++) { 210 if (((int) getFloat() * 100) % 2 == 0) 211 randomstr = randomstr + randomSingle().toString(); 213 else 214 randomstr = randomstr + randomChar((Character )lower.get(0), 216 (Character )upper.get(0)).toString(); 217 } 218 } else { 219 for (int i = 0; i < length.intValue(); i++) 221 randomstr = randomstr + randomSingle().toString(); 222 } 223 } else { 224 225 if (upper.size() == 3) { 227 228 for (int i = 0; i < length.intValue(); i++) { 230 231 if (((int) (getFloat() * 100)) % 2 == 0) { 232 233 randomstr = randomstr + randomChar((Character )lower.get(2), 235 (Character )upper.get(2)).toString(); 236 } else if (((int) (getFloat() * 100)) % 2 == 0) { 237 238 randomstr = randomstr + randomChar((Character )lower.get(1), 240 (Character )upper.get(1)).toString(); 241 } else { 242 243 randomstr = randomstr + randomChar((Character )lower.get(0), 245 (Character )upper.get(0)).toString(); 246 } 247 } 248 } else if (upper.size() == 2) { 249 250 for (int i = 0; i < length.intValue(); i++) { 252 if (((int) (getFloat() * 100)) % 2 == 0) 253 randomstr = randomstr + randomChar((Character )lower.get(1), 255 (Character )upper.get(1)).toString(); 256 else 257 randomstr = randomstr + randomChar((Character )lower.get(0), 259 (Character )upper.get(0)).toString(); 260 } 261 } else 262 263 for (int i = 0; i < length.intValue(); i++) 265 randomstr = randomstr + randomChar((Character )lower.get(0), 267 (Character )upper.get(0)).toString(); 268 } 269 } 270 271 277 private final Character randomSingle() { 278 279 return (new Character (single[(int)((getFloat() * singlecount) - 1)])); 280 } 281 282 291 private final Character randomChar(Character lower, Character upper) { 292 int tempval; 293 char low = lower.charValue(); 294 char up = upper.charValue(); 295 296 tempval = (int)((int)low + (getFloat() * ((int)(up - low)))); 298 299 return (new Character ((char) tempval)); 301 } 302 303 310 public final String getRandom() { 311 312 randomstr = new String (); 313 314 generaterandom(); 316 if (hmap != null) { 317 318 while (hmap.containsKey(randomstr)) { 319 generaterandom(); 321 } 322 323 hmap.put(randomstr, null); } 325 326 return randomstr; 327 } 328 329 336 public final void setRanges(ArrayList low, ArrayList up) { 337 lower = low; 338 upper = up; 339 } 340 341 342 348 public final void setHmap(HashMap map) { 349 hmap = map; 350 } 351 352 358 public final void setLength(Integer value) { 359 length = value; 360 } 361 362 368 public final void setAlgorithm(String value) { 369 algorithm = value; 370 secure = true; } 372 373 379 public final void setProvider(String value) { 380 provider = value; 381 } 382 383 389 public final void setAllchars(boolean value) { 390 allchars = value; 391 } 392 393 401 public final void setSingle(char[] chars, int value) { 402 single = chars; singlecount = value; singles = true; } 406 } 407 | Popular Tags |