1 18 package net.sf.uitags.util; 19 20 import java.util.ArrayList ; 21 import java.util.Iterator ; 22 23 import org.apache.commons.collections.OrderedMap; 24 import org.apache.commons.collections.map.ListOrderedMap; 25 26 33 public final class UiString { 34 37 public static final int OPTION_NONE = 0; 38 41 public static final int OPTION_AUTO_SURROUND = 1; 42 45 public static final int OPTION_HTML_ENCODING = 2; 46 49 public static final int OPTION_JS_ESCAPE = 4; 50 53 public static final int OPTION_ALL = 54 OPTION_AUTO_SURROUND | OPTION_HTML_ENCODING | OPTION_JS_ESCAPE; 55 56 59 private OrderedMap pairs; 60 63 private ArrayList params; 64 67 private String template; 68 71 private int options; 72 75 private String surroundString = "\""; 76 77 82 public UiString(String template) { 83 this(template, OPTION_NONE); 84 } 85 86 93 public UiString(String template, int options) { 94 this.template = template; 95 this.params = new ArrayList (); 96 this.pairs = new ListOrderedMap(); 97 setDefaultOptions(options); 98 } 99 100 105 public void setDefaultOptions(int opts) { 106 if (opts < OPTION_NONE) { 107 throw new IllegalArgumentException ("Invalid options value."); 108 } 109 if (opts > OPTION_ALL) { 110 throw new IllegalArgumentException ("Invalid options value."); 111 } 112 this.options = opts; 113 } 114 115 118 public void clearOptions() { 119 setDefaultOptions(OPTION_NONE); 120 } 121 122 125 public void autoSurround() { 126 setDefaultOptions(this.options | OPTION_AUTO_SURROUND); 127 } 128 129 132 public void htmlEncode() { 133 setDefaultOptions(this.options | OPTION_HTML_ENCODING); 134 } 135 136 139 public void jsEscape() { 140 setDefaultOptions(this.options | OPTION_JS_ESCAPE); 141 } 142 143 149 public void setSurroundString(String str) { 150 this.surroundString = str; 151 } 152 153 160 public void bind(Object param) { 161 bind(param, this.options); 162 } 163 164 171 public void bind(Object param, int opts) { 172 if (isMapped()) { 173 throw new IllegalStateException ("Bind cannot be used along with map."); 174 } 175 this.params.add(process(param, opts)); 176 } 177 178 184 public void map(String key, Object value) { 185 map(key, value, this.options); 186 } 187 188 196 public void map(String key, Object value, int opts) { 197 if (isBound()) { 198 throw new IllegalStateException ("Map cannot be used along with bind."); 199 } 200 this.pairs.put(key, process(value, opts)); 201 } 202 203 209 private boolean isMapped() { 210 return this.pairs.size() != 0; 211 } 212 213 219 private boolean isBound() { 220 return this.params.size() != 0; 221 } 222 223 230 private String process(Object obj, int opts) { 231 String temp = String.valueOf(obj); 232 if (isSet(opts, OPTION_HTML_ENCODING)) { 233 temp = encodeHtml(temp); 234 } 235 236 if (isSet(opts, OPTION_JS_ESCAPE)) { 237 temp = escapeJs(temp); 238 } 239 240 if (isSet(opts, OPTION_AUTO_SURROUND)) { 241 temp = this.surroundString + temp + this.surroundString; 242 } 243 244 return temp; 245 } 246 247 250 public void clearBindParameters() { 251 this.params.clear(); 252 } 253 254 262 private boolean isSet(int opts, int specific) { 263 return (opts & specific) != 0; 264 } 265 266 272 private String encodeHtml(String str) { 273 StringBuffer buffer = new StringBuffer (); 274 char curr; 275 for (int i = 0; i < str.length(); i++) { 276 curr = str.charAt(i); 277 switch (curr) { 278 case '<': 279 buffer.append("<"); 280 break; 281 case '>': 282 buffer.append(">"); 283 break; 284 case '&': 285 buffer.append("&"); 286 break; 287 case ' ': 288 buffer.append(" "); 289 break; 290 case '"': 291 buffer.append("""); 292 break; 293 case '\'': 294 buffer.append("'"); 295 break; 296 default: 297 buffer.append(curr); 298 } 299 } 300 return String.valueOf(buffer); 301 } 302 303 310 private String escapeJs(String str) { 311 return escapeJava(str); 312 } 313 314 320 private String escapeJava(String str) { 321 StringBuffer buffer = new StringBuffer (); 322 char curr; 323 for (int i = 0; i < str.length(); i++) { 324 curr = str.charAt(i); 325 switch (curr) { 326 case '\'': 327 buffer.append("\\\'"); 328 break; 329 case '"': 330 buffer.append("\\\""); 331 break; 332 case '\\': 333 buffer.append("\\\\"); 334 break; 335 default: 336 buffer.append(curr); 337 } 338 } 339 return String.valueOf(buffer); 340 } 341 342 380 public String construct() { 381 if (isBound()) { 382 try { 383 return simpleConstruct(this.template, 384 (String []) this.params.toArray(new String [this.params.size()])); 385 } 386 catch (IllegalArgumentException e) { 387 throw new IllegalStateException (e.getMessage()); 388 } 389 } 390 391 String result = this.template; 392 Iterator iter = this.pairs.orderedMapIterator(); 395 while (iter.hasNext()) { 396 String key = (String ) iter.next(); 398 399 result = result.replaceAll("\\{" + key + "\\}", 403 escapeJava(String.valueOf(this.pairs.get(key)))); 404 } 405 result = result.replaceAll("\\\\\\{", "\\{"); 406 result = result.replaceAll("\\\\\\}", "\\}"); 407 408 return result; 409 } 410 411 426 public static String simpleConstruct(String template, String [] params) { 427 int currIndex = 0; 428 int openIndex = 0; 429 int closeIndex = 0; 430 int listIndex = 0; 431 StringBuffer result = new StringBuffer (); 432 boolean loop = true; 433 434 while (true) { 436 openIndex = template.indexOf("{", currIndex); 437 if (openIndex < 0) { 438 loop = false; 439 } 440 else { 441 closeIndex = template.indexOf("}", openIndex); 442 if (closeIndex < 0) { 443 loop = false; 444 } 445 } 446 if (!loop) { 447 result.append(template.substring(currIndex, template.length())); 448 break; 449 } 450 result.append(template.substring(currIndex, openIndex)); 451 try { 452 listIndex = Integer.parseInt( 453 template.substring(openIndex + 1, closeIndex)); 454 if (listIndex < 0) { throw new NumberFormatException ( 456 "Invalid parameter position index '" + listIndex + "'."); 457 } 458 if (listIndex >= params.length) { String holder = "{" + listIndex + "}"; 461 result.append(holder); 462 currIndex = openIndex + holder.length(); 463 } 464 else { 465 result.append(String.valueOf(params[listIndex])); 466 currIndex = closeIndex + 1; 469 } 470 } 471 catch (NumberFormatException x) { result.append("{"); 473 currIndex = openIndex + 1; 478 } 479 } 480 return result.toString(); 481 } 482 } | Popular Tags |