KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tags > javascript > ScriptBlock


1 /*
2  * Copyright 2005 The Apache Software Foundation.
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  * $Header:$
17  */

18 package org.apache.beehive.netui.tags.javascript;
19
20 import org.apache.beehive.netui.tags.AbstractSimpleTag;
21 import org.apache.beehive.netui.tags.rendering.WriteRenderAppender;
22
23 import javax.servlet.jsp.JspException JavaDoc;
24 import javax.servlet.jsp.PageContext JavaDoc;
25 import java.io.IOException JavaDoc;
26
27 /**
28  * This class will output a script block into the generated HTML. The primary reason
29  * for this class is to add the ability to add JavaScript either before or after the
30  * framework provided JavaScript.
31  * @jsptagref.tagdescription <p>Outputs a script block into the generated HTML. The primary reason
32  * for this tag is to add JavaScript either before or after the
33  * framework provided JavaScript.</p>
34  * @netui:tag name="scriptBlock" body-content="scriptless" description="Used to place JavaScript in relationship to the framework genrated JavaScript"
35  */

36 public class ScriptBlock extends AbstractSimpleTag
37 {
38     private ScriptPlacement _placement = ScriptPlacement.PLACE_INLINE;
39
40     /**
41      * Return the name of the Tag.
42      */

43     public String JavaDoc getTagName()
44     {
45         return "Content";
46     }
47
48     /**
49      * Place the JavaScript inside in relationship to the frameword generated JavaScript.
50      * @param placement The placement of the JavaScript
51      * @jsptagref.attributedescription String value 'after' or 'before'. Places the JavaScript
52      * before or after the JavaScript provided by the framework.
53      * @jsptagref.databindable false
54      * @jsptagref.attributesyntaxvalue <i>string_or_expression_output</i>
55      * @netui:attribute rtexprvalue="true"
56      * description="The String literal or expression used to output the content."
57      */

58     public void setPlacement(String JavaDoc placement)
59     {
60         if (placement.equals("after"))
61             _placement = ScriptPlacement.PLACE_AFTER;
62         else if (placement.equals("before"))
63             _placement = ScriptPlacement.PLACE_BEFORE;
64         else
65             _placement = ScriptPlacement.PLACE_INLINE;
66     }
67
68     /**
69      * Render the content.
70      * @throws javax.servlet.jsp.JspException if a JSP exception has occurred
71      */

72     public void doTag()
73             throws JspException JavaDoc, IOException JavaDoc
74     {
75         // report any errors...
76
if (hasErrors()) {
77             reportErrors();
78             return;
79         }
80
81         PageContext JavaDoc pageContext = getPageContext();
82         WriteRenderAppender writer = new WriteRenderAppender(pageContext);
83         String JavaDoc script = getBufferBody(false);
84         if (script == null)
85             return;
86
87         // make sure that the last character is a new line
88
if (script.length() > 0 && script.charAt(script.length() -1) != '\n') {
89             script = script + "\n";
90         }
91
92         IScriptReporter sr = getScriptReporter();
93
94         // if we are writting the javaScript inline then do it....
95
if (_placement == ScriptPlacement.PLACE_INLINE || sr == null) {
96             ScriptRequestState.writeScriptBlock(pageContext.getRequest(), writer, script);
97             return;
98         }
99
100         sr.addScriptFunction(_placement, script);
101     }
102 }
103
Popular Tags