1 7 package com.inversoft.verge.mvc.view; 8 9 10 import java.io.UnsupportedEncodingException ; 11 import java.util.Iterator ; 12 import java.util.List ; 13 import java.util.Map ; 14 15 import com.inversoft.util.ObjectTools; 16 import com.inversoft.util.StringTools; 17 18 19 32 public class HtmlViewToolkit { 33 34 35 private volatile static int unique = 0; 36 37 38 39 private HtmlViewToolkit() { 40 } 42 43 44 48 49 56 public static String createUniqueName() { 57 return HtmlConstants.INPUT_NAME_PREFIX + unique++; 58 } 59 60 61 75 public static String makeValueString(Object value, Object currentValue) { 76 String str = null; 77 if (value == null) { 78 str = ObjectTools.cleanToString(currentValue); 79 } else { 80 str = ObjectTools.cleanToString(value); 81 } 82 83 return str; 84 } 85 86 94 public static void appendHiddenTag(StringBuffer buf, String name, String value) { 95 appendHiddenTag(buf, null, name, value); 96 } 97 98 107 public static void appendHiddenTag(StringBuffer buf, String id, String name, 108 String value) { 109 if (value == null) { 110 value = ""; 111 } 112 113 buf.append("<input type=\"hidden\""); 114 115 if (id != null) { 116 buf.append(" id=\"").append(id).append("\""); 117 } 118 119 buf.append(" name=\"").append(name); 120 buf.append("\" value=\"").append(value).append("\"/>"); 121 } 122 123 134 public static void appendAttributes(StringBuffer buf, Map attributes, 135 List singleAttrs) { 136 137 if (attributes != null && !attributes.isEmpty()) { 138 Iterator iter = attributes.keySet().iterator(); 139 Object key; 140 Object value; 141 142 while (iter.hasNext()) { 143 key = iter.next(); 144 value = attributes.get(key); 145 146 if (key != null && value != null) { 147 buf.append(" ").append(key).append("=\""); 148 buf.append(value).append("\""); 149 } 150 } 151 } 152 153 if (singleAttrs != null && !singleAttrs.isEmpty()) { 154 Iterator iter = singleAttrs.iterator(); 155 Object value; 156 157 while (iter.hasNext()) { 158 value = iter.next(); 159 if (value != null) { 160 buf.append(" ").append(value); 161 } 162 } 163 } 164 } 165 166 175 public static void appendAttribute(StringBuffer buf, String name, String value) { 176 if (value != null) { 177 buf.append(" ").append(name).append("=\""); 178 buf.append(value).append("\""); 179 } 180 } 181 182 189 public static void appendURLParameter(StringBuffer buf, String name, String value) { 190 if (StringTools.isTrimmedEmpty(name)) { 191 throw new NullPointerException ("The name of a URL parameter can not " + 192 "be null or empty"); 193 } 194 195 try { 196 name = java.net.URLEncoder.encode(name, "UTF-8"); 197 198 if (!StringTools.isTrimmedEmpty(value)) { 199 value = java.net.URLEncoder.encode(value, "UTF-8"); 200 } else { 201 value = ""; 202 } 203 } catch (UnsupportedEncodingException uee) { 204 assert (false) : uee.toString(); 205 } 206 207 208 buf.append(name).append("=").append(value); 209 } 210 211 212 216 217 235 public static void appendInputTagStart(StringBuffer buf, String tagType, 236 String id, String name, String value) { 237 238 buf.append("<input"); 239 appendAttribute(buf, HtmlConstants.TYPE, tagType); 240 appendAttribute(buf, HtmlConstants.ID, id); 241 appendAttribute(buf, HtmlConstants.NAME, name); 242 appendAttribute(buf, HtmlConstants.VALUE, value); 243 } 244 245 258 public static void createTextTag(StringBuffer buf, String id, String name, 259 String value, Map attributes, List singleAttrs) { 260 261 appendInputTagStart(buf, HtmlConstants.TEXT_TYPE, id, name, value); 262 appendAttributes(buf, attributes, singleAttrs); 263 buf.append("/>"); 264 } 265 266 279 public static void createPasswordTag(StringBuffer buf, String id, String name, 280 String value, Map attributes, List singleAttrs) { 281 282 appendInputTagStart(buf, HtmlConstants.PASSWORD_TYPE, id, name, value); 283 appendAttributes(buf, attributes, singleAttrs); 284 buf.append("/>"); 285 } 286 287 305 public static void createRadioTag(StringBuffer buf, String id, String name, 306 String value, boolean checked, Map attributes, List singleAttrs) { 307 308 appendInputTagStart(buf, HtmlConstants.RADIO_TYPE, id, name, value); 309 if (checked) { 310 buf.append(" ").append(HtmlConstants.CHECKED); 311 } 312 appendAttributes(buf, attributes, singleAttrs); 313 buf.append("/>"); 314 } 315 316 334 public static void createCheckboxTag(StringBuffer buf, String id, String name, 335 String value, boolean checked, Map attributes, List singleAttrs) { 336 337 appendInputTagStart(buf, HtmlConstants.CHECKBOX_TYPE, id, name, value); 338 if (checked) { 339 buf.append(" ").append(HtmlConstants.CHECKED); 340 } 341 appendAttributes(buf, attributes, singleAttrs); 342 buf.append("/>"); 343 } 344 345 357 public static void createSubmitTag(StringBuffer buf, String id, String name, 358 String value, Map attributes, List singleAttrs) { 359 360 appendInputTagStart(buf, HtmlConstants.SUBMIT_TYPE, id, name, value); 361 appendAttributes(buf, attributes, singleAttrs); 362 buf.append("/>"); 363 } 364 365 378 public static void createImageTag(StringBuffer buf, String id, String name, 379 String src, String value, Map attributes, List singleAttrs) { 380 381 appendInputTagStart(buf, HtmlConstants.IMAGE_TYPE, id, name, value); 382 appendAttribute(buf, HtmlConstants.SRC, src); 383 appendAttributes(buf, attributes, singleAttrs); 384 buf.append("/>"); 385 } 386 387 388 392 393 405 public static void createFormStartTag(StringBuffer buf, String action, String id, 406 String name, String method, Map attributes, List singleAttrs) { 407 408 buf.append("<form"); 409 appendAttribute(buf, HtmlConstants.ACTION, action); 410 appendAttribute(buf, HtmlConstants.ID, id); 411 appendAttribute(buf, HtmlConstants.NAME, name); 412 appendAttribute(buf, HtmlConstants.METHOD, method); 413 appendAttributes(buf, attributes, singleAttrs); 414 buf.append(">"); 415 } 416 417 432 public static void createTextAreaTag(StringBuffer buf, String id, String name, 433 Object objectText, Map attributes, List singleAttrs) { 434 435 buf.append("<textarea"); 436 appendAttribute(buf, HtmlConstants.ID, id); 437 appendAttribute(buf, HtmlConstants.NAME, name); 438 appendAttributes(buf, attributes, singleAttrs); 439 buf.append(">"); 440 441 if (objectText != null) { 442 buf.append(objectText.toString()); 443 } 444 445 buf.append(HtmlConstants.TEXTAREA_END_TAG); 446 } 447 448 459 public static void createTextArea(StringBuffer buf, String id, String name, 460 String text, Map attributes, List singleAttrs) { 461 462 buf.append("<textarea"); 463 appendAttribute(buf, HtmlConstants.ID, id); 464 appendAttribute(buf, HtmlConstants.NAME, name); 465 appendAttributes(buf, attributes,singleAttrs); 466 buf.append(">").append(text).append("</textarea>"); 467 } 468 469 480 public static void createSelectStartTag(StringBuffer buf, String id, String name, 481 Map attributes, List singleAttrs) { 482 483 buf.append("<select"); 484 appendAttribute(buf, HtmlConstants.ID, id); 485 appendAttribute(buf, HtmlConstants.NAME, name); 486 appendAttributes(buf, attributes, singleAttrs); 487 buf.append(">"); 488 } 489 490 505 public static void createOptionTag(StringBuffer buf, String id, String value, 506 String text, boolean selected, Map attributes, List singleAttrs) { 507 508 buf.append("<option"); 509 appendAttribute(buf, HtmlConstants.ID, id); 510 appendAttribute(buf, HtmlConstants.VALUE, value); 511 if (selected) { 512 buf.append(" ").append(HtmlConstants.SELECTED); 513 } 514 appendAttributes(buf, attributes, singleAttrs); 515 buf.append(">"); 516 517 if (text != null) { 518 buf.append(text); 519 } 520 521 buf.append("</option>"); 522 } 523 524 538 public static void createOptionStartTag(StringBuffer buf, String value, 539 boolean selected, Map attributes, List singleAttrs) { 540 541 buf.append("<option"); 542 appendAttribute(buf, "value", value); 543 if (selected) { 544 buf.append(" ").append(HtmlConstants.SELECTED); 545 } 546 appendAttributes(buf, attributes, singleAttrs); 547 buf.append(">"); 548 } 549 550 551 555 556 566 public static void createAnchorTag(StringBuffer buf, String id, String name, 567 String href, Map attributes, List singleAttrs) { 568 buf.append("<a"); 569 appendAttribute(buf, HtmlConstants.ID, id); 570 appendAttribute(buf, HtmlConstants.NAME, name); 571 appendAttribute(buf, "href", href); 572 appendAttributes(buf, attributes, singleAttrs); 573 buf.append(">"); 574 } 575 576 586 public static void createImgTag(StringBuffer buf, String id, String name, 587 String src, Map attributes, List singleAttrs) { 588 buf.append("<img"); 589 appendAttribute(buf, HtmlConstants.ID, id); 590 appendAttribute(buf, HtmlConstants.NAME, name); 591 appendAttribute(buf, HtmlConstants.SRC, src); 592 appendAttributes(buf, attributes, singleAttrs); 593 buf.append("/>"); 594 } 595 } | Popular Tags |