1 16 package com.blandware.atleap.webapp.taglib.core.html; 17 18 import com.blandware.atleap.common.Constants; 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 import org.apache.struts.taglib.TagUtils; 22 23 import javax.servlet.http.HttpServletRequest ; 24 import javax.servlet.jsp.JspException ; 25 import javax.servlet.jsp.JspTagException ; 26 import javax.servlet.jsp.PageContext ; 27 import javax.servlet.jsp.tagext.JspFragment ; 28 import javax.servlet.jsp.tagext.SimpleTagSupport ; 29 import java.io.IOException ; 30 import java.io.PrintWriter ; 31 import java.io.StringWriter ; 32 import java.io.UnsupportedEncodingException ; 33 import java.net.URLEncoder ; 34 35 116 public class IFrameTag extends SimpleTagSupport { 117 118 protected transient final Log log = LogFactory.getLog(IFrameTag.class); 119 120 123 protected static final String NUMBER_KEY = "com.blandware.atleap.taglib.IFRAME_TAG_NUMBER"; 124 125 129 protected static final String DEFAULT_VAR_PREFIX = "com.blandware.atleap.taglib.IFRAME_CONTENT_VAR_"; 130 131 134 protected static final String LOAD_METHOD_JS = "javascript"; 135 136 139 protected static final String LOAD_METHOD_SERVLET = "servlet"; 140 141 142 145 protected String loadMethod = LOAD_METHOD_JS; 146 147 150 protected String var; 151 152 156 protected String contentType = Constants.MIME_TYPE_HTML; 157 158 162 protected String frameId; 163 164 167 protected String frameName; 168 169 172 protected Integer frameBorder; 173 174 179 protected String src; 180 181 184 protected Integer marginWidth; 185 186 189 protected Integer marginHeight; 190 191 194 protected String scrolling; 195 196 199 protected String align; 200 201 204 protected String height; 205 206 209 protected String width; 210 211 214 protected Integer tagNumber; 215 216 217 227 public String getLoadMethod() { 228 return loadMethod; 229 } 230 231 237 public void setLoadMethod(String loadMethod) { 238 this.loadMethod = loadMethod; 239 } 240 241 252 public String getVar() { 253 return var; 254 } 255 256 263 public void setVar(String var) { 264 this.var = var; 265 } 266 267 277 public String getContentType() { 278 return contentType; 279 } 280 281 287 public void setContentType(String contentType) { 288 this.contentType = contentType; 289 } 290 291 301 public String getFrameId() { 302 return frameId; 303 } 304 305 311 public void setFrameId(String frameId) { 312 this.frameId = frameId; 313 } 314 315 324 public String getFrameName() { 325 return frameName; 326 } 327 328 333 public void setFrameName(String frameName) { 334 this.frameName = frameName; 335 } 336 337 346 public Integer getFrameBorder() { 347 return frameBorder; 348 } 349 350 355 public void setFrameBorder(Integer frameBorder) { 356 this.frameBorder = frameBorder; 357 } 358 359 369 public String getSrc() { 370 return src; 371 } 372 373 379 public void setSrc(String src) { 380 this.src = src; 381 } 382 383 392 public Integer getMarginWidth() { 393 return marginWidth; 394 } 395 396 401 public void setMarginWidth(Integer marginWidth) { 402 this.marginWidth = marginWidth; 403 } 404 405 414 public Integer getMarginHeight() { 415 return marginHeight; 416 } 417 418 423 public void setMarginHeight(Integer marginHeight) { 424 this.marginHeight = marginHeight; 425 } 426 427 436 public String getScrolling() { 437 return scrolling; 438 } 439 440 445 public void setScrolling(String scrolling) { 446 this.scrolling = scrolling; 447 } 448 449 458 public String getAlign() { 459 return align; 460 } 461 462 467 public void setAlign(String align) { 468 this.align = align; 469 } 470 471 480 public String getHeight() { 481 return height; 482 } 483 484 489 public void setHeight(String height) { 490 this.height = height; 491 } 492 493 502 public String getWidth() { 503 return width; 504 } 505 506 511 public void setWidth(String width) { 512 this.width = width; 513 } 514 515 521 public void doTag() throws JspException , IOException { 522 523 PageContext pageContext = (PageContext ) getJspContext(); 524 525 if ( loadMethod == null || loadMethod.length() == 0 ) { 526 loadMethod = LOAD_METHOD_JS; 527 } 528 529 if ( contentType == null || contentType.length() == 0 ) { 530 contentType = Constants.MIME_TYPE_HTML; 531 } 532 533 if ( !LOAD_METHOD_JS.equalsIgnoreCase(loadMethod) && !LOAD_METHOD_SERVLET.equalsIgnoreCase(loadMethod) ) { 534 JspTagException e = new JspTagException ("Unrecognized load method: " + loadMethod); 535 throw e; 536 } 537 538 if ( !Constants.MIME_TYPE_PLAIN.equals(contentType) && !Constants.MIME_TYPE_HTML.equals(contentType) ) { 539 JspTagException e = new JspTagException ("Unsupported content type: " + contentType); 540 throw e; 541 } 542 543 tagNumber = (Integer ) pageContext.getAttribute(NUMBER_KEY); 545 546 if ( tagNumber == null ) { 547 tagNumber = new Integer (0); 548 } else { 549 tagNumber = new Integer (tagNumber.intValue() + 1); 550 } 551 552 pageContext.setAttribute(NUMBER_KEY, tagNumber); 553 554 String content = new String (); 555 StringWriter sw = new StringWriter (); 556 JspFragment body = getJspBody(); 557 if ( body != null ) { 558 body.invoke(sw); 559 content = sw.toString(); 560 } 561 562 if ( frameId == null ) { 563 frameId = "__iframe__" + tagNumber; 564 } 565 566 if ( frameName == null ) { 567 frameName = frameId; 568 } 569 570 TagUtils tagUtils = TagUtils.getInstance(); 571 if ( LOAD_METHOD_JS.equals(loadMethod) ) { 572 String contentDivId = frameId + "__content__"; 574 String encodedContent = new String (); 575 try { 576 encodedContent = URLEncoder.encode(content, Constants.DEFAULT_ENCODING).replaceAll("\\+", "%20"); 580 } catch ( UnsupportedEncodingException e ) { 581 if ( log.isErrorEnabled() ) { 582 sw = new StringWriter (); 583 e.printStackTrace(new PrintWriter (sw)); 584 log.error(sw.toString()); 585 } 586 } 587 StringBuffer contentDiv = new StringBuffer ("<div id=\"").append(contentDivId).append("\" style=\"display: none;\">\n").append(encodedContent).append("</div>\n"); 588 if ( src == null ) { 589 String ctxPath = ((HttpServletRequest ) pageContext.getRequest()).getContextPath(); 590 src = ctxPath + "/blank.html"; 591 } 592 StringBuffer onload = new StringBuffer ("loadIFrameContent('") 593 .append(frameId).append("', '").append(contentDivId).append("');"); 594 595 StringBuffer iframe = new StringBuffer ("<iframe id=\"") 596 .append(frameId).append("\" name=\"").append(frameName) 597 .append("\" SRC=\"").append(src).append("\" ") 598 .append(getAttributesStringBuffer()).append("onload=\"") 599 .append(onload).append("\" ").append("></iframe>"); 600 601 tagUtils.write(pageContext, contentDiv.append(iframe).toString()); 602 } else { 603 if ( var == null || var.length() == 0 ) { 605 var = DEFAULT_VAR_PREFIX + frameId; 606 } 607 608 pageContext.setAttribute(var, content, PageContext.SESSION_SCOPE); 609 String contextPath = ((HttpServletRequest ) pageContext.getRequest()).getContextPath(); 610 src = contextPath + "/servlet/iframeContentLoader/?var=" + var + "&type=" + contentType; 611 String iframe = "<iframe id=\"" + frameId + "\" name=\"" + frameName + "\" SRC=\"" + src + "\" " + 612 getAttributesStringBuffer() + "></iframe>"; 613 tagUtils.write(pageContext, iframe); 614 } 615 616 } 617 618 623 protected StringBuffer getAttributesStringBuffer() { 624 StringBuffer result = new StringBuffer (); 625 626 if ( frameBorder != null ) { 627 result.append("frameborder=\"" + frameBorder + "\" "); 628 } 629 630 if ( marginWidth != null ) { 631 result.append("marginwidth=\"" + marginWidth + "\" "); 632 } 633 634 if ( marginHeight != null ) { 635 result.append("marginheight=\"" + marginHeight + "\" "); 636 } 637 638 if ( scrolling != null ) { 639 result.append("scrolling=\"" + scrolling + "\" "); 640 } 641 642 if ( align != null ) { 643 result.append("align=\"" + align + "\" "); 644 } 645 646 if ( width != null ) { 647 result.append("width=\"" + width + "\" "); 648 } 649 650 if ( height != null ) { 651 result.append("height=\"" + height + "\" "); 652 } 653 654 return result; 655 } 656 657 } 658 | Popular Tags |