1 16 17 package org.apache.taglibs.random; 18 19 import java.util.ArrayList ; 20 import java.util.HashMap ; 21 import java.util.StringTokenizer ; 22 23 import javax.servlet.jsp.JspException ; 24 import javax.servlet.jsp.PageContext ; 25 import javax.servlet.jsp.tagext.TagSupport ; 26 27 74 75 public class RandomStrgTag extends TagSupport { 76 77 80 private boolean allchars = false; 81 84 private Integer length = new Integer (8); 85 88 private HashMap hmap; 89 92 private String map = null; 93 96 private ArrayList lower = null; 97 100 private ArrayList upper = null; 101 104 private char[] single = null; 105 108 private int singlecount = 0; 109 112 private String algorithm = null; 113 116 private String provider = null; 117 120 private String charset = null; 121 122 133 public final int doStartTag() throws JspException { 134 135 RandomStrg random = new RandomStrg(); 138 139 if (charset != null) { 141 generateCharset(charset); 142 } else { 143 generateCharset("a-zA-Z0-9"); 144 } 145 146 if (map != null) { 147 try { 148 149 if ((hmap = (HashMap ) pageContext.findAttribute(map)) == null) 151 throw new JspException ("A hashmap does not exist in any " + 153 "scope under the name " + map); 154 } catch (ClassCastException cce) { 155 throw new JspException ("The named attribute exists but it is not" + 156 " a hashmap."); 157 } 158 159 random.setHmap(hmap); } 161 162 if (lower != null) 164 random.setRanges(lower, upper); 165 166 if (single != null) 168 random.setSingle(single, singlecount); 169 170 random.setLength(length); 172 random.setAllchars(allchars); 174 if (algorithm != null) 177 random.setAlgorithm(algorithm); 178 if (provider != null) 179 random.setProvider(provider); 180 181 random.generateRandomObject(); 183 184 pageContext.setAttribute(id, random, PageContext.PAGE_SCOPE); 186 187 return SKIP_BODY; 188 } 189 190 196 public final void setMap(String value) { 197 map = value; 198 } 199 200 205 public final void setLength(String value) { 206 try { 207 length = new Integer (value); 208 } catch (NumberFormatException ne) { 209 pageContext.getServletContext().log("length attribute could not be" + 210 " turned into an Integer default value was used"); 211 } 212 } 213 214 220 public final void setAlgorithm(String value) { 221 algorithm = value; 222 } 223 224 230 public final void setProvider(String value) { 231 provider = value; 232 } 233 234 242 public final void setCharset(String value) { 243 charset = value; 244 } 245 246 private void generateCharset(String value) { 247 boolean more = true; 249 250 allchars = false; 252 single = null; 253 singlecount = 0; 254 255 lower = new ArrayList (3); 258 upper = new ArrayList (3); 259 260 if (value.compareTo("all") == 0) { 262 allchars = true; more = false; 266 } else if ((value.charAt(1) == '-') && (value.charAt(0) != '\\')) { 267 while (more && (value.charAt(1) == '-')){ 269 270 if (value.charAt(0) == '\\') 272 break; 273 else { 274 lower.add(new Character (value.charAt(0))); 276 upper.add(new Character (value.charAt(2))); 277 } 278 279 if (value.length() <= 3) 281 more = false; 282 else 283 value = value.substring(3); 286 } 287 } 288 289 if (more) { 291 292 single = new char[30]; 294 StringTokenizer tokens = new StringTokenizer (value); 296 297 while (tokens.hasMoreTokens()) { 298 String token = tokens.nextToken(); 300 301 if (token.length() > 1) 302 single[singlecount++] = '-'; 304 305 single[singlecount++] = token.charAt(0); 307 } 308 } 309 } 310 } 311 | Popular Tags |