1 16 package org.apache.taglibs.string; 17 18 import org.apache.commons.lang.RandomStringUtils; 19 import org.apache.commons.lang.StringUtils; 20 import org.apache.commons.lang.math.NumberUtils; 21 22 import javax.servlet.jsp.JspException ; 23 24 25 31 59 public class RandomStringTag extends StringTagSupport { 60 static public String NUMERIC = "numeric"; 61 static public String ALPHANUMERIC = "alphanumeric"; 62 static public String ALPHABET = "alphabet"; 63 static public String UNICODE = "unicode"; 64 private String count; 65 private String start; 66 private String end; 67 private String type; 68 69 public RandomStringTag() { 70 super(); 71 } 72 73 78 public String getCount() { 79 return this.count; 80 } 81 82 87 public void setCount(String count) { 88 this.count = count; 89 } 90 91 96 public String getStart() { 97 return this.start; 98 } 99 100 105 public void setStart(String start) { 106 this.start = start; 107 } 108 109 114 public String getEnd() { 115 return this.end; 116 } 117 118 123 public void setEnd(String end) { 124 this.end = end; 125 } 126 127 132 public String getType() { 133 return this.type; 134 } 135 136 141 public void setType(String type) { 142 this.type = type; 143 } 144 145 public String changeString(String text) throws JspException { 146 int st = 0; int ed = 0; int ct = 0; boolean letters = false; 150 boolean numbers = false; 151 152 letters = ALPHABET.equals(type); 153 numbers = NUMERIC.equals(type); 154 155 if (ALPHANUMERIC.equals(type)) { 156 letters = true; 157 numbers = true; 158 } 159 160 char[] setChrs = null; 161 162 if (!StringUtils.isBlank(text)) { 163 setChrs = text.toCharArray(); 164 } 165 166 if (!StringUtils.isEmpty(start)) { 167 try { 168 if (NumberUtils.isNumber(start)) { 169 st = NumberUtils.createNumber(start).intValue(); 170 } else { 171 st = (int) start.charAt(0); 172 } 173 } catch (NumberFormatException nfe) { 174 } catch (NullPointerException npe) { } 176 } 177 178 if (!StringUtils.isEmpty(end)) { 179 try { 180 if (NumberUtils.isNumber(end)) { 181 ed = NumberUtils.createNumber(end).intValue(); 182 } else { 183 ed = (int) end.charAt(0); 184 } 185 } catch (NumberFormatException nfe) { 186 nfe.printStackTrace(); 187 } catch (NullPointerException npe) { npe.printStackTrace(); 189 } 190 } 191 192 if ( (setChrs != null) ) { 195 if ( st > setChrs.length-1 ) { 196 st =0; 197 } 198 if ( ed == 0 || ed > setChrs.length-1 ) { 199 ed = setChrs.length-1; 200 } 201 202 } 203 204 try { 205 ct = NumberUtils.createNumber(count).intValue(); 206 } catch (NumberFormatException nfe) { 207 nfe.printStackTrace(); 208 } catch (NullPointerException npe) { npe.printStackTrace(); 210 } 211 212 String randomString = null; 213 try { 214 randomString = RandomStringUtils.random(ct, st, ed, letters, numbers, setChrs); 215 } catch ( Exception exc ) { 216 if ( letters && numbers ) { 218 randomString = RandomStringUtils.randomAlphanumeric( ct ); 219 } else if ( letters ) { 220 randomString = RandomStringUtils.randomNumeric( ct ); 221 } else if ( numbers ) { 222 randomString = RandomStringUtils.randomNumeric( ct ); 223 } else { 224 randomString = RandomStringUtils.random( ct ); 225 } 226 StringBuffer msg = new StringBuffer (); 227 msg.append( "Exception on RandomStringTag: " + exc ); 228 msg.append("\nCT: " + ct); 229 msg.append("\nST: " + st); 230 msg.append("\nED: " + ed); 231 msg.append("\nletters: " + letters); 232 msg.append("\nnumbers: " + numbers); 233 msg.append("\nset: " + (setChrs == null ? "null" : ""+setChrs.length + " characters") ); 234 msg.append("\nreturning " + randomString ); 235 System.err.println( msg.toString()); 236 } 237 238 242 return randomString; 243 } 244 245 public void initAttributes() { 246 this.count = null; 247 this.start = null; 248 this.end = null; 249 this.type = UNICODE; 250 } 251 252 } 253 | Popular Tags |