1 18 19 package org.apache.struts.taglib.html; 20 21 import java.util.Iterator ; 22 import java.util.Map ; 23 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.http.HttpServletResponse ; 26 import javax.servlet.jsp.JspException ; 27 28 import org.apache.struts.Globals; 29 import org.apache.struts.config.ModuleConfig; 30 import org.apache.struts.taglib.TagUtils; 31 import org.apache.struts.util.MessageResources; 32 import org.apache.struts.util.ModuleUtils; 33 34 45 46 public class ImgTag extends BaseHandlerTag { 47 48 50 public ImgTag() { 51 super(); 52 doDisabled = false; 53 } 54 55 57 60 protected String align = null; 61 62 public String getAlign() { 63 return (this.align); 64 } 65 66 public void setAlign(String align) { 67 this.align = align; 68 } 69 70 73 protected String border = null; 74 75 public String getBorder() { 76 return (this.border); 77 } 78 79 public void setBorder(String border) { 80 this.border = border; 81 } 82 83 91 protected String contextRelative = null; 92 93 95 public String getContextRelative() { 96 return (this.contextRelative); 97 } 98 99 101 public void setContextRelative(String contextRelative) { 102 this.contextRelative = contextRelative; 103 } 104 105 106 111 public boolean isContextRelativeSet() { 112 return Boolean.valueOf(this.contextRelative).booleanValue(); 113 } 114 115 118 protected String height = null; 119 120 public String getHeight() { 121 return (this.height); 122 } 123 124 public void setHeight(String height) { 125 this.height = height; 126 } 127 128 131 protected String hspace = null; 132 133 public String getHspace() { 134 return (this.hspace); 135 } 136 137 public void setHspace(String hspace) { 138 this.hspace = hspace; 139 } 140 141 144 protected String imageName = null; 145 146 public String getImageName() { 147 return (this.imageName); 148 } 149 150 public void setImageName(String imageName) { 151 this.imageName = imageName; 152 } 153 154 157 protected String ismap = null; 158 159 public String getIsmap() { 160 return (this.ismap); 161 } 162 163 public void setIsmap(String ismap) { 164 this.ismap = ismap; 165 } 166 167 172 protected String lowsrc = null; 173 174 178 public String getLowsrc() { 179 return (this.lowsrc); 180 } 181 182 public void setLowsrc(String lowsrc) { 183 this.lowsrc = lowsrc; 184 } 185 186 189 protected static MessageResources messages = 190 MessageResources.getMessageResources(Constants.Package + ".LocalStrings"); 191 192 195 protected String name = null; 196 197 public String getName() { 198 return (this.name); 199 } 200 201 public void setName(String name) { 202 this.name = name; 203 } 204 205 209 protected String page = null; 210 211 public String getPage() { 212 return (this.page); 213 } 214 215 public void setPage(String page) { 216 this.page = page; 217 } 218 219 223 protected String pageKey = null; 224 225 public String getPageKey() { 226 return (this.pageKey); 227 } 228 229 public void setPageKey(String pageKey) { 230 this.pageKey = pageKey; 231 } 232 233 237 protected String action = null; 238 239 public String getAction() { 240 return (this.action); 241 } 242 243 public void setAction(String action) { 244 this.action = action; 245 } 246 247 251 protected String module = null; 252 253 public String getModule() { 254 return (this.module); 255 } 256 257 public void setModule(String module) { 258 this.module = module; 259 } 260 261 266 protected String paramId = null; 267 268 public String getParamId() { 269 return (this.paramId); 270 } 271 272 public void setParamId(String paramId) { 273 this.paramId = paramId; 274 } 275 276 279 protected String paramName = null; 280 281 public String getParamName() { 282 return (this.paramName); 283 } 284 285 public void setParamName(String paramName) { 286 this.paramName = paramName; 287 } 288 289 292 protected String paramProperty = null; 293 294 public String getParamProperty() { 295 return (this.paramProperty); 296 } 297 298 public void setParamProperty(String paramProperty) { 299 this.paramProperty = paramProperty; 300 } 301 302 305 protected String paramScope = null; 306 307 public String getParamScope() { 308 return (this.paramScope); 309 } 310 311 public void setParamScope(String paramScope) { 312 this.paramScope = paramScope; 313 } 314 315 318 protected String property = null; 319 320 public String getProperty() { 321 return (this.property); 322 } 323 324 public void setProperty(String property) { 325 this.property = property; 326 } 327 328 331 protected String scope = null; 332 333 public String getScope() { 334 return (this.scope); 335 } 336 337 public void setScope(String scope) { 338 this.scope = scope; 339 } 340 341 344 protected String src = null; 345 346 public String getSrc() { 347 return (this.src); 348 } 349 350 public void setSrc(String src) { 351 this.src = src; 352 } 353 354 358 protected String srcKey = null; 359 360 public String getSrcKey() { 361 return (this.srcKey); 362 } 363 364 public void setSrcKey(String srcKey) { 365 this.srcKey = srcKey; 366 } 367 368 371 protected String usemap = null; 372 373 public String getUsemap() { 374 return (this.usemap); 375 } 376 377 public void setUsemap(String usemap) { 378 this.usemap = usemap; 379 } 380 381 384 protected String vspace = null; 385 386 public String getVspace() { 387 return (this.vspace); 388 } 389 390 public void setVspace(String vspace) { 391 this.vspace = vspace; 392 } 393 394 397 protected String width = null; 398 399 public String getWidth() { 400 return (this.width); 401 } 402 403 public void setWidth(String width) { 404 this.width = width; 405 } 406 407 protected boolean useLocalEncoding = false; 408 409 public boolean isUseLocalEncoding() { 410 return useLocalEncoding; 411 } 412 413 public void setUseLocalEncoding(boolean b) { 414 useLocalEncoding = b; 415 } 416 417 419 424 public int doStartTag() throws JspException { 425 426 return (EVAL_BODY_TAG); 428 429 } 430 431 436 public int doEndTag() throws JspException { 437 438 HttpServletResponse response = (HttpServletResponse ) pageContext.getResponse(); 440 StringBuffer results = new StringBuffer ("<img"); 441 String tmp = src(); 442 String srcurl = url(tmp); 443 if (srcurl != null) { 444 prepareAttribute(results, "src", response.encodeURL(srcurl)); 445 } 446 String lowsrcurl = url(this.lowsrc); 447 if (lowsrcurl != null) { 448 prepareAttribute(results, "lowsrcurl", response.encodeURL(lowsrcurl)); 449 } 450 451 prepareAttribute(results, "name", getImageName()); 452 prepareAttribute(results, "height", getHeight()); 453 prepareAttribute(results, "width", getWidth()); 454 prepareAttribute(results, "align", getAlign()); 455 prepareAttribute(results, "border", getBorder()); 456 prepareAttribute(results, "hspace", getHspace()); 457 prepareAttribute(results, "vspace", getVspace()); 458 prepareAttribute(results, "ismap", getIsmap()); 459 prepareAttribute(results, "usemap", getUsemap()); 460 results.append(prepareStyles()); 461 results.append(prepareEventHandlers()); 462 prepareOtherAttributes(results); 463 results.append(getElementClose()); 464 465 TagUtils.getInstance().write(pageContext, results.toString()); 466 467 return (EVAL_PAGE); 468 469 } 470 471 474 public void release() { 475 476 super.release(); 477 478 border = null; 479 height = null; 480 hspace = null; 481 imageName = null; 482 ismap = null; 483 lowsrc = null; 484 name = null; 485 page = null; 486 pageKey = null; 487 action = null; 488 paramId = null; 489 paramName = null; 490 paramProperty = null; 491 paramScope = null; 492 property = null; 493 scope = null; 494 src = null; 495 srcKey = null; 496 usemap = null; 497 vspace = null; 498 width = null; 499 500 } 501 502 504 508 private void throwImgTagSrcException() throws JspException { 509 JspException e = new JspException (messages.getMessage("imgTag.src")); 510 TagUtils.getInstance().saveException(pageContext, e); 511 throw e; 512 } 513 514 520 private boolean srcDefaultReference(ModuleConfig config) { 521 return (config == null || isContextRelativeSet()); 522 } 523 524 531 protected String src() throws JspException { 532 533 if (this.page != null) { 535 if ((this.src != null) || (this.srcKey != null) || (this.pageKey != null)) { 536 throwImgTagSrcException(); 537 } 538 ModuleConfig config = 539 ModuleUtils.getInstance().getModuleConfig( 540 this.module, 541 (HttpServletRequest ) pageContext.getRequest(), 542 pageContext.getServletContext()); 543 544 HttpServletRequest request = (HttpServletRequest ) pageContext.getRequest(); 545 String pageValue = this.page; 546 if (!srcDefaultReference(config)) { 547 pageValue = TagUtils.getInstance().pageURL(request, 548 this.page, 549 config); 550 } 551 return (request.getContextPath() + pageValue); 552 } 553 554 if (this.pageKey != null) { 556 if ((this.src != null) || (this.srcKey != null)) { 557 throwImgTagSrcException(); 558 } 559 ModuleConfig config = 560 ModuleUtils.getInstance().getModuleConfig( 561 this.module, 562 (HttpServletRequest ) pageContext.getRequest(), 563 pageContext.getServletContext()); 564 565 HttpServletRequest request = (HttpServletRequest ) pageContext.getRequest(); 566 String pageValue = TagUtils.getInstance().message( 567 pageContext, 568 getBundle(), 569 getLocale(), 570 this.pageKey); 571 if (!srcDefaultReference(config)) { 572 pageValue = TagUtils.getInstance().pageURL(request, 573 pageValue, 574 config); 575 } 576 return (request.getContextPath() + pageValue); 577 } 578 579 if (this.action != null) { 580 if ((this.src != null) || (this.srcKey != null)) { 581 throwImgTagSrcException(); 582 } 583 return TagUtils.getInstance().getActionMappingURL(action, module, pageContext, isContextRelativeSet()); 584 } 585 586 if (this.src != null) { 588 if (this.srcKey != null) { 589 throwImgTagSrcException(); 590 } 591 return (this.src); 592 } 593 594 if (this.srcKey == null) { 596 throwImgTagSrcException(); 597 } 598 599 return TagUtils.getInstance().message( 600 pageContext, 601 getBundle(), 602 getLocale(), 603 this.srcKey); 604 605 } 606 607 615 protected String url(String url) throws JspException { 616 617 if (url == null) { 618 return (url); 619 } 620 621 String charEncoding = "UTF-8"; 622 if(useLocalEncoding){ 623 charEncoding = pageContext.getResponse().getCharacterEncoding(); 624 } 625 626 StringBuffer src = new StringBuffer (url); 628 629 if ((paramId != null) && (paramName != null)) { 631 if (src.toString().indexOf('?') < 0) { 632 src.append('?'); 633 } else { 634 src.append("&"); 635 } 636 src.append(paramId); 637 src.append('='); 638 Object value = TagUtils.getInstance().lookup(pageContext, paramName, paramProperty, paramScope); 639 if (value != null) 640 src.append(TagUtils.getInstance().encodeURL(value.toString(), charEncoding)); 641 } 642 643 if ((property != null) && (name == null)) { 645 JspException e = new JspException (messages.getMessage("getter.name")); 646 TagUtils.getInstance().saveException(pageContext, e); 647 throw e; 648 } 649 650 if (name == null) { 651 return (src.toString()); 652 } 653 654 Object mapObject = TagUtils.getInstance().lookup(pageContext, name, property, scope); 656 Map map = null; 657 try { 658 map = (Map ) mapObject; 659 } catch (ClassCastException e) { 660 TagUtils.getInstance().saveException(pageContext, e); 661 throw new JspException (messages.getMessage("imgTag.type")); 662 } 663 664 boolean question = (src.toString().indexOf("?") >= 0); 666 Iterator keys = map.keySet().iterator(); 667 while (keys.hasNext()) { 668 String key = (String ) keys.next(); 669 Object value = map.get(key); 670 if (value == null) { 671 if (question) { 672 src.append("&"); 673 } else { 674 src.append('?'); 675 question = true; 676 } 677 src.append(key); 678 src.append('='); 679 } else if (value instanceof String []) { 681 String values[] = (String []) value; 682 for (int i = 0; i < values.length; i++) { 683 if (question) { 684 src.append("&"); 685 } else { 686 src.append('?'); 687 question = true; 688 } 689 src.append(key); 690 src.append('='); 691 src.append(TagUtils.getInstance().encodeURL(values[i], charEncoding)); 692 } 693 } else { 694 695 if (question) { 696 src.append("&"); 697 } else { 698 src.append('?'); 699 question = true; 700 } 701 src.append(key); 702 src.append('='); 703 src.append(TagUtils.getInstance().encodeURL(value.toString(), charEncoding)); 704 } 705 } 706 707 return (src.toString()); 709 710 } 711 712 } 713 | Popular Tags |