KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > taglib > core > html > IFrameTag


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
24 import javax.servlet.jsp.JspException JavaDoc;
25 import javax.servlet.jsp.JspTagException JavaDoc;
26 import javax.servlet.jsp.PageContext JavaDoc;
27 import javax.servlet.jsp.tagext.JspFragment JavaDoc;
28 import javax.servlet.jsp.tagext.SimpleTagSupport JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.PrintWriter JavaDoc;
31 import java.io.StringWriter JavaDoc;
32 import java.io.UnsupportedEncodingException JavaDoc;
33 import java.net.URLEncoder JavaDoc;
34
35 /**
36  * <p>Renders an HTML IFRAME element and loads content to it.
37  * Content to be loaded is body of this tag.
38  * </p>
39  * <p>
40  * There are two methods to load content. Method to be used is specified with
41  * <b>loadMethod</b> attribute.
42  * </p>
43  * <p>
44  * First method uses JavaScript. Here, content will be placed to invisible element
45  * (currently DIV) that will be loaded to IFRAME on its load. To use this method,
46  * you must include script files /scripts/core/klayers.js and
47  * /scripts/core/global.jsp somewhere in page. <b>src</b> attribute (if specified)
48  * will point to page that will be used as 'blank' page: it's content
49  * (everything inside BODY element) will be replaced with loaded content.
50  * <b>var</b> attribute is ignored.
51  * </p>
52  * <p>
53  * Second method uses servlet to load content. Content is put to variable with
54  * name <b>var</b> and then loaded by special servlet. Here, <b>src</b>
55  * is ignored.
56  * </p>
57  * <p>
58  * Allowed attributes are:
59  * <ul>
60  * <li>
61  * <b>loadMethod</b> - load method to use. This can be 'javascript' or 'servlet',
62  * default is 'javascript'
63  * </li>
64  * <li>
65  * <b>var</b> - name of session scope variable that will be used to store
66  * tag content. Used only when <code>loadMethod="servlet"</code>. If not
67  * specified, this will be calculated automatically.
68  * </li>
69  * <li>
70  * <b>src</b> - location of page that will be used as 'blank' page. Used only
71  * when <code>loadMethod="javascript"</code>. If not specified, link to default
72  * blank page is used.
73  * </li>
74  * <li>
75  * <b>contentType</b> - content type of content, this can be 'text/plain' or
76  * 'text/html'. Default is 'text/html'. Used only when <code>loadMethod="servlet"</code>.
77  * </li>
78  * <li>
79  * <b>frameId</b> - ID of generated IFRAME element. If not specified, unique ID
80  * is generated automatically.
81  * </li>
82  * <li>
83  * <b>frameName</b> - name of generated IFRAME element. If not specified, this
84  * is same as <b>frameId</b>.
85  * </li>
86  * <li>
87  * <b>frameBorder</b> - frameBorder attribute value for generated IFRAME element
88  * </li>
89  * <li>
90  * <b>marginWidth</b> - marginWidth attribute value for generated IFRAME element
91  * </li>
92  * <li>
93  * <b>marginHeight</b> - marginHeight attribute value for generated IFRAME element
94  * </li>
95  * <li>
96  * <b>scrolling</b> - scrolling attribute value for generated IFRAME element
97  * </li>
98  * <li>
99  * <b>align</b> - align attribute value for generated IFRAME element
100  * </li>
101  * <li>
102  * <b>width</b> - width attribute value for generated IFRAME element
103  * </li>
104  * <li>
105  * <b>height</b> - height attribute value for generated IFRAME element
106  * </li>
107  * </ul>
108  * </p>
109  * <p><a HREF="IFrameTag.java.htm"><i>View Source</i></a></p>
110  *
111  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
112  * @version $Revision: 1.13 $ $Date: 2005/10/31 07:19:34 $
113  * @jsp.tag name="iframe"
114  * body-content="scriptless"
115  */

116 public class IFrameTag extends SimpleTagSupport JavaDoc {
117
118     protected transient final Log log = LogFactory.getLog(IFrameTag.class);
119
120     /**
121      * Page context key under which ordinal number of this tag is saved
122      */

123     protected static final String JavaDoc NUMBER_KEY = "com.blandware.atleap.taglib.IFRAME_TAG_NUMBER";
124
125     /**
126      * Prefix for default variable to store content if loadMethod="servlet" is used. Variable will be
127      * complemented with frame ID.
128      */

129     protected static final String JavaDoc DEFAULT_VAR_PREFIX = "com.blandware.atleap.taglib.IFRAME_CONTENT_VAR_";
130
131     /**
132      * Javascript load method
133      */

134     protected static final String JavaDoc LOAD_METHOD_JS = "javascript";
135
136     /**
137      * Servlet load method
138      */

139     protected static final String JavaDoc LOAD_METHOD_SERVLET = "servlet";
140
141
142     /**
143      * Load method. Can be "javascript" or "servlet"
144      */

145     protected String JavaDoc loadMethod = LOAD_METHOD_JS;
146
147     /**
148      * Variable to save content in session if loadMethod="servlet" is used
149      */

150     protected String JavaDoc var;
151
152     /**
153      * Type of content. Can be either <code>text/plain</code> or </code>text/html</code>.
154      * If loadMethod="javascript" is used, this attribute is ignored
155      */

156     protected String JavaDoc contentType = Constants.MIME_TYPE_HTML;
157
158     /**
159      * ID of this frame element. If not specified, it will be
160      * '__iframe__N' where N is an ordinal number of this tag on page.
161      */

162     protected String JavaDoc frameId;
163
164     /**
165      * Name of this iframe element
166      */

167     protected String JavaDoc frameName;
168
169     /**
170      * Frame border
171      */

172     protected Integer JavaDoc frameBorder;
173
174     /**
175      * Source of frame. If not specified, blank.html from the root directory will be used.
176      * Note, that all content between &lt;body&gt;&lt;/body&gt; tags will be replaced by
177      * body content of this tag, even if it is an empty string
178      */

179     protected String JavaDoc src;
180
181     /**
182      * Margin width in pixels
183      */

184     protected Integer JavaDoc marginWidth;
185
186     /**
187      * Margin height in pixels
188      */

189     protected Integer JavaDoc marginHeight;
190
191     /**
192      * Whether or not srollbar must be shown in frame
193      */

194     protected String JavaDoc scrolling;
195
196     /**
197      * Align of this frame element
198      */

199     protected String JavaDoc align;
200
201     /**
202      * Height of this frame element
203      */

204     protected String JavaDoc height;
205
206     /**
207      * Width of this frame element
208      */

209     protected String JavaDoc width;
210
211     /**
212      * Ordinal number of this tag on page
213      */

214     protected Integer JavaDoc tagNumber;
215
216
217     /**
218      * Returns load method
219      *
220      * @return load method
221      * @see #loadMethod
222      * @jsp.attribute required="false"
223      * rtexprvalue="true"
224      * type="java.lang.String"
225      * description="Load method. Can be 'javascript' or 'servlet'"
226      */

227     public String JavaDoc getLoadMethod() {
228         return loadMethod;
229     }
230
231     /**
232      * Sets load method
233      *
234      * @param loadMethod load method to set
235      * @see #loadMethod
236      */

237     public void setLoadMethod(String JavaDoc loadMethod) {
238         this.loadMethod = loadMethod;
239     }
240
241     /**
242      * Returns name of variable that will accept loaded content if load method
243      * is 'servlet'
244      *
245      * @return variable name
246      * @see #var
247      * @jsp.attribute required="false"
248      * rtexprvalue="true"
249      * type="java.lang.String"
250      * description="Variable to save content in session if loadMethod='servlet' is used"
251      */

252     public String JavaDoc getVar() {
253         return var;
254     }
255
256     /**
257      * Sets name of variable that will accept loaded content if load method
258      * is 'servlet'
259      *
260      * @param var variable name to set
261      * @see #var
262      */

263     public void setVar(String JavaDoc var) {
264         this.var = var;
265     }
266
267     /**
268      * Returns content-type of content
269      *
270      * @return content-type of content
271      * @see #contentType
272      * @jsp.attribute required="false"
273      * rtexprvalue="true"
274      * type="java.lang.String"
275      * description="Type of content. Usable only if loadMethod='servlet' is used, otherwise is ignored"
276      */

277     public String JavaDoc getContentType() {
278         return contentType;
279     }
280
281     /**
282      * Sets content-type of content
283      *
284      * @param contentType content-type of content to set
285      * @see #contentType
286      */

287     public void setContentType(String JavaDoc contentType) {
288         this.contentType = contentType;
289     }
290
291     /**
292      * Returns IFrame element ID
293      *
294      * @return IFrame element ID
295      * @see #frameId
296      * @jsp.attribute required="false"
297      * rtexprvalue="true"
298      * type="java.lang.String"
299      * description="ID of this frame element."
300      */

301     public String JavaDoc getFrameId() {
302         return frameId;
303     }
304
305     /**
306      * Sets IFrame element ID
307      *
308      * @param frameId IFrame element ID to set
309      * @see #frameId
310      */

311     public void setFrameId(String JavaDoc frameId) {
312         this.frameId = frameId;
313     }
314
315     /**
316      * Returns IFrame element name
317      *
318      * @return IFrame element name
319      * @jsp.attribute required="false"
320      * rtexprvalue="true"
321      * type="java.lang.String"
322      * description="Name of this frame element."
323      */

324     public String JavaDoc getFrameName() {
325         return frameName;
326     }
327
328     /**
329      * Sets IFrame element name
330      *
331      * @param frameName IFrame element name to set
332      */

333     public void setFrameName(String JavaDoc frameName) {
334         this.frameName = frameName;
335     }
336
337     /**
338      * Returns IFrame element border
339      *
340      * @return IFrame element border
341      * @jsp.attribute required="false"
342      * rtexprvalue="true"
343      * type="java.lang.Integer"
344      * description="Frame border"
345      */

346     public Integer JavaDoc getFrameBorder() {
347         return frameBorder;
348     }
349
350     /**
351      * Sets IFrame element border
352      *
353      * @param frameBorder
354      */

355     public void setFrameBorder(Integer JavaDoc frameBorder) {
356         this.frameBorder = frameBorder;
357     }
358
359     /**
360      * Returns frame source
361      *
362      * @return frame source
363      * @see #src
364      * @jsp.attribute required="false"
365      * rtexprvalue="true"
366      * type="java.lang.String"
367      * description="Source for this frame"
368      */

369     public String JavaDoc getSrc() {
370         return src;
371     }
372
373     /**
374      * Sets frame source
375      *
376      * @param src frame source to set
377      * @see #src
378      */

379     public void setSrc(String JavaDoc src) {
380         this.src = src;
381     }
382
383     /**
384      * Returns margin width in pixels
385      *
386      * @return margin width
387      * @jsp.attribute required="false"
388      * rtexprvalue="true"
389      * type="java.lang.Integer"
390      * description="Margin width"
391      */

392     public Integer JavaDoc getMarginWidth() {
393         return marginWidth;
394     }
395
396     /**
397      * Sets margin width in pixels
398      *
399      * @param marginWidth margin width to set
400      */

401     public void setMarginWidth(Integer JavaDoc marginWidth) {
402         this.marginWidth = marginWidth;
403     }
404
405     /**
406      * Returns margin height in pixels
407      *
408      * @return margin height
409      * @jsp.attribute required="false"
410      * rtexprvalue="true"
411      * type="java.lang.Integer"
412      * description="Margin height"
413      */

414     public Integer JavaDoc getMarginHeight() {
415         return marginHeight;
416     }
417
418     /**
419      * Sets margin height in pixels
420      *
421      * @param marginHeight margin height to set
422      */

423     public void setMarginHeight(Integer JavaDoc marginHeight) {
424         this.marginHeight = marginHeight;
425     }
426
427     /**
428      * Returns whether or not srollbar must be shown in frame
429      *
430      * @return whether srollbar must be shown in frame
431      * @jsp.attribute required="false"
432      * rtexprvalue="true"
433      * type="java.lang.String"
434      * description="Whether or not scrollbar must be shown"
435      */

436     public String JavaDoc getScrolling() {
437         return scrolling;
438     }
439
440     /**
441      * Sets whether or not srollbar must be shown in frame
442      *
443      * @param scrolling whether srollbar must be shown in frame
444      */

445     public void setScrolling(String JavaDoc scrolling) {
446         this.scrolling = scrolling;
447     }
448
449     /**
450      * Returns align of IFrame element
451      *
452      * @return align
453      * @jsp.attribute required="false"
454      * rtexprvalue="true"
455      * type="java.lang.String"
456      * description="Align of this frame element"
457      */

458     public String JavaDoc getAlign() {
459         return align;
460     }
461
462     /**
463      * Sets align of IFrame element
464      *
465      * @param align align to set
466      */

467     public void setAlign(String JavaDoc align) {
468         this.align = align;
469     }
470
471     /**
472      * Returns height of IFrame element
473      *
474      * @return height
475      * @jsp.attribute required="false"
476      * rtexprvalue="true"
477      * type="java.lang.String"
478      * description="Height of this frame element"
479      */

480     public String JavaDoc getHeight() {
481         return height;
482     }
483
484     /**
485      * Sets height of IFrame element
486      *
487      * @param height height to set
488      */

489     public void setHeight(String JavaDoc height) {
490         this.height = height;
491     }
492
493     /**
494      * Returns width of IFrame element
495      *
496      * @return width
497      * @jsp.attribute required="false"
498      * rtexprvalue="true"
499      * type="java.lang.String"
500      * description="Width of this frame element"
501      */

502     public String JavaDoc getWidth() {
503         return width;
504     }
505
506     /**
507      * Sets width of IFrame element
508      *
509      * @param width width to set
510      */

511     public void setWidth(String JavaDoc width) {
512         this.width = width;
513     }
514
515     /**
516      * Processes the tag
517      *
518      * @throws JspException
519      * @throws IOException
520      */

521     public void doTag() throws JspException JavaDoc, IOException JavaDoc {
522
523         PageContext JavaDoc pageContext = (PageContext JavaDoc) 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 JavaDoc e = new JspTagException JavaDoc("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 JavaDoc e = new JspTagException JavaDoc("Unsupported content type: " + contentType);
540             throw e;
541         }
542
543         // get ordinal number of this tag
544
tagNumber = (Integer JavaDoc) pageContext.getAttribute(NUMBER_KEY);
545
546         if ( tagNumber == null ) {
547             tagNumber = new Integer JavaDoc(0);
548         } else {
549             tagNumber = new Integer JavaDoc(tagNumber.intValue() + 1);
550         }
551
552         pageContext.setAttribute(NUMBER_KEY, tagNumber);
553
554         String JavaDoc content = new String JavaDoc();
555         StringWriter JavaDoc sw = new StringWriter JavaDoc();
556         JspFragment JavaDoc 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             // make an undisplayable DIV that will contain content for load
573
String JavaDoc contentDivId = frameId + "__content__";
574             String JavaDoc encodedContent = new String JavaDoc();
575             try {
576                 // URL encoder is used here to encode content which is not
577
// actually URL because it's quite enough for our purposes and
578
// rather convenient: decoding is pretty simple
579
encodedContent = URLEncoder.encode(content, Constants.DEFAULT_ENCODING).replaceAll("\\+", "%20");
580             } catch ( UnsupportedEncodingException JavaDoc e ) {
581                 if ( log.isErrorEnabled() ) {
582                     sw = new StringWriter JavaDoc();
583                     e.printStackTrace(new PrintWriter JavaDoc(sw));
584                     log.error(sw.toString());
585                 }
586             }
587             StringBuffer JavaDoc contentDiv = new StringBuffer JavaDoc("<div id=\"").append(contentDivId).append("\" style=\"display: none;\">\n").append(encodedContent).append("</div>\n");
588             if ( src == null ) {
589                 String JavaDoc ctxPath = ((HttpServletRequest JavaDoc) pageContext.getRequest()).getContextPath();
590                 src = ctxPath + "/blank.html";
591             }
592             StringBuffer JavaDoc onload = new StringBuffer JavaDoc("loadIFrameContent('")
593                     .append(frameId).append("', '").append(contentDivId).append("');");
594
595             StringBuffer JavaDoc iframe = new StringBuffer JavaDoc("<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             // loading using servlet
604
if ( var == null || var.length() == 0 ) {
605                 var = DEFAULT_VAR_PREFIX + frameId;
606             }
607
608             pageContext.setAttribute(var, content, PageContext.SESSION_SCOPE);
609             String JavaDoc contextPath = ((HttpServletRequest JavaDoc) pageContext.getRequest()).getContextPath();
610             src = contextPath + "/servlet/iframeContentLoader/?var=" + var + "&type=" + contentType;
611             String JavaDoc iframe = "<iframe id=\"" + frameId + "\" name=\"" + frameName + "\" SRC=\"" + src + "\" " +
612                     getAttributesStringBuffer() + "></iframe>";
613             tagUtils.write(pageContext, iframe);
614         }
615
616     }
617
618     /**
619      * Returns a StringBuffer with attributes set to their values
620      *
621      * @return StringBuffer with attributes set to their values
622      */

623     protected StringBuffer JavaDoc getAttributesStringBuffer() {
624         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
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