|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
Body.java | - | 91.3% | 80% | 87.9% |
|
1 |
// Copyright 2004, 2005 The Apache Software Foundation
|
|
2 |
//
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
5 |
// You may obtain a copy of the License at
|
|
6 |
//
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8 |
//
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12 |
// See the License for the specific language governing permissions and
|
|
13 |
// limitations under the License.
|
|
14 |
|
|
15 |
package org.apache.tapestry.html;
|
|
16 |
|
|
17 |
import org.apache.hivemind.Resource;
|
|
18 |
import org.apache.tapestry.AbstractComponent;
|
|
19 |
import org.apache.tapestry.IMarkupWriter;
|
|
20 |
import org.apache.tapestry.IRequestCycle;
|
|
21 |
import org.apache.tapestry.PageRenderSupport;
|
|
22 |
import org.apache.tapestry.TapestryUtils;
|
|
23 |
import org.apache.tapestry.engine.IEngineService;
|
|
24 |
import org.apache.tapestry.util.PageRenderSupportImpl;
|
|
25 |
import org.apache.tapestry.web.WebResponse;
|
|
26 |
|
|
27 |
/**
|
|
28 |
* The body of a Tapestry page. This is used since it allows components on the page access to an
|
|
29 |
* initialization script (that is written the start, just inside the <body> tag). This is
|
|
30 |
* currently used by {@link Rollover}and {@link Script}components. [ <a
|
|
31 |
* href="../../../../../ComponentReference/Body.html">Component Reference </a>]
|
|
32 |
*
|
|
33 |
* @author Howard Lewis Ship
|
|
34 |
*/
|
|
35 |
|
|
36 |
public abstract class Body extends AbstractComponent implements PageRenderSupport |
|
37 |
{ |
|
38 |
private PageRenderSupportImpl _pageRenderSupport;
|
|
39 |
|
|
40 |
/**
|
|
41 |
* Adds to the script an initialization for the named variable as an Image(), to the given URL.
|
|
42 |
* <p>
|
|
43 |
* Returns a reference, a string that can be used to represent the preloaded image in a
|
|
44 |
* JavaScript function.
|
|
45 |
*
|
|
46 |
* @since 1.0.2
|
|
47 |
*/
|
|
48 |
|
|
49 | 0 |
public String getPreloadedImageReference(String URL)
|
50 |
{ |
|
51 | 0 |
return _pageRenderSupport.getPreloadedImageReference(URL);
|
52 |
} |
|
53 |
|
|
54 |
/**
|
|
55 |
* Adds other initialization, in the form of additional JavaScript code to execute from the
|
|
56 |
* <body>'s <code>onLoad</code> event handler. The caller is responsible for adding a
|
|
57 |
* semicolon (statement terminator). This method will add a newline after the script.
|
|
58 |
*/
|
|
59 |
|
|
60 | 6 |
public void addInitializationScript(String script) |
61 |
{ |
|
62 | 6 |
_pageRenderSupport.addInitializationScript(script); |
63 |
} |
|
64 |
|
|
65 |
/**
|
|
66 |
* Adds additional scripting code to the page. This code will be added to a large block of
|
|
67 |
* scripting code at the top of the page (i.e., the before the <body> tag).
|
|
68 |
* <p>
|
|
69 |
* This is typically used to add some form of JavaScript event handler to a page. For example,
|
|
70 |
* the {@link Rollover}component makes use of this.
|
|
71 |
* <p>
|
|
72 |
* Another way this is invoked is by using the {@link Script}component.
|
|
73 |
* <p>
|
|
74 |
* The string will be added, as-is, within the <script> block generated by this
|
|
75 |
* <code>Body</code> component. The script should <em>not</em> contain HTML comments, those
|
|
76 |
* will be supplied by this Body component.
|
|
77 |
* <p>
|
|
78 |
* A frequent use is to add an initialization function using this method, then cause it to be
|
|
79 |
* executed using {@link #addInitializationScript(String)}.
|
|
80 |
*/
|
|
81 |
|
|
82 | 5 |
public void addBodyScript(String script) |
83 |
{ |
|
84 | 5 |
_pageRenderSupport.addBodyScript(script); |
85 |
} |
|
86 |
|
|
87 |
/**
|
|
88 |
* Used to include a script from an outside URL (the scriptLocation is a URL, probably obtained
|
|
89 |
* from an asset. This adds an <script src="..."> tag before the main <script> tag.
|
|
90 |
* The Body component ensures that each URL is included only once.
|
|
91 |
*
|
|
92 |
* @since 1.0.5
|
|
93 |
*/
|
|
94 |
|
|
95 | 4 |
public void addExternalScript(Resource scriptLocation) |
96 |
{ |
|
97 | 4 |
_pageRenderSupport.addExternalScript(scriptLocation); |
98 |
} |
|
99 |
|
|
100 |
/**
|
|
101 |
* Retrieves the <code>Body</code> that was stored into the request cycle. This allows
|
|
102 |
* components wrapped by the <code>Body</code> to locate it and access the services it
|
|
103 |
* provides.
|
|
104 |
*
|
|
105 |
* @deprecated To be removed in 4.1. Use
|
|
106 |
* {@link org.apache.tapestry.TapestryUtils#getPageRenderSupport(IRequestCycle)}
|
|
107 |
* instead.
|
|
108 |
*/
|
|
109 |
|
|
110 | 0 |
public static Body get(IRequestCycle cycle) |
111 |
{ |
|
112 | 0 |
return (Body) TapestryUtils.getPageRenderSupport(cycle, null); |
113 |
} |
|
114 |
|
|
115 | 93 |
protected void prepareForRender(IRequestCycle cycle) |
116 |
{ |
|
117 | 93 |
super.prepareForRender(cycle);
|
118 |
|
|
119 | 93 |
_pageRenderSupport = new PageRenderSupportImpl(getAssetService(), getResponse()
|
120 |
.getNamespace(), getLocation()); |
|
121 |
} |
|
122 |
|
|
123 | 93 |
protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) |
124 |
{ |
|
125 | 93 |
TapestryUtils.storePageRenderSupport(cycle, this);
|
126 |
|
|
127 | 93 |
IMarkupWriter nested = writer.getNestedWriter(); |
128 |
|
|
129 | 93 |
renderBody(nested, cycle); |
130 |
|
|
131 |
// Start the body tag.
|
|
132 | 84 |
writer.println(); |
133 | 84 |
writer.begin(getElement()); |
134 | 84 |
renderInformalParameters(writer, cycle); |
135 |
|
|
136 | 84 |
writer.println(); |
137 |
|
|
138 |
// Write the page's scripting. This is included scripts
|
|
139 |
// and dynamic JavaScript.
|
|
140 |
|
|
141 | 84 |
_pageRenderSupport.writeBodyScript(writer, cycle); |
142 |
|
|
143 |
// Close the nested writer, which dumps its buffered content
|
|
144 |
// into its parent.
|
|
145 |
|
|
146 | 84 |
nested.close(); |
147 |
|
|
148 |
// Any initialization should go at the very end of the document
|
|
149 |
// just before the close body tag. Older version of Tapestry
|
|
150 |
// would create a window.onload event handler, but this is better
|
|
151 |
// (it doesn't have to wait for external images to load).
|
|
152 |
|
|
153 | 84 |
_pageRenderSupport.writeInitializationScript(writer); |
154 |
|
|
155 | 84 |
writer.end(); // <body>
|
156 |
} |
|
157 |
|
|
158 | 93 |
protected void cleanupAfterRender(IRequestCycle cycle) |
159 |
{ |
|
160 | 93 |
super.cleanupAfterRender(cycle);
|
161 |
|
|
162 | 93 |
_pageRenderSupport = null;
|
163 |
|
|
164 | 93 |
TapestryUtils.removePageRenderSupport(cycle); |
165 |
} |
|
166 |
|
|
167 |
public abstract String getElement();
|
|
168 |
|
|
169 |
public abstract void setElement(String element); |
|
170 |
|
|
171 |
/**
|
|
172 |
* Injected
|
|
173 |
*
|
|
174 |
* @since 4.0
|
|
175 |
*/
|
|
176 |
public abstract IEngineService getAssetService();
|
|
177 |
|
|
178 |
/**
|
|
179 |
* Injected
|
|
180 |
*
|
|
181 |
* @since 4.0
|
|
182 |
*/
|
|
183 |
|
|
184 |
public abstract WebResponse getResponse();
|
|
185 |
|
|
186 |
/**
|
|
187 |
* Sets the element parameter property to its default, "body".
|
|
188 |
*
|
|
189 |
* @since 3.0
|
|
190 |
*/
|
|
191 | 60 |
protected void finishLoad() |
192 |
{ |
|
193 | 60 |
setElement("body");
|
194 |
} |
|
195 |
|
|
196 |
/** @since 3.0 */
|
|
197 |
|
|
198 | 5 |
public String getUniqueString(String baseValue)
|
199 |
{ |
|
200 | 5 |
return _pageRenderSupport.getUniqueString(baseValue);
|
201 |
} |
|
202 |
|
|
203 |
} |
|