1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
package org.apache.tapestry.util; |
16 |
|
|
17 |
|
import org.apache.commons.lang.StringUtils; |
18 |
|
import org.apache.hivemind.Locatable; |
19 |
|
import org.apache.hivemind.Location; |
20 |
|
import org.apache.hivemind.Resource; |
21 |
|
import org.apache.hivemind.util.Defense; |
22 |
|
import org.apache.tapestry.*; |
23 |
|
import org.apache.tapestry.asset.AssetFactory; |
24 |
|
import org.apache.tapestry.services.ResponseBuilder; |
25 |
|
|
26 |
|
import java.util.ArrayList; |
27 |
|
import java.util.HashMap; |
28 |
|
import java.util.List; |
29 |
|
import java.util.Map; |
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
public class PageRenderSupportImpl implements Locatable, PageRenderSupport |
39 |
|
{ |
40 |
|
private final AssetFactory _assetFactory; |
41 |
|
|
42 |
|
private Location _location; |
43 |
|
|
44 |
|
private final ResponseBuilder _builder; |
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
private StringBuffer _initializationScript; |
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
private StringBuffer _postInitializationScript; |
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
private StringBuffer _bodyScript; |
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
private StringBuffer _imageInitializations; |
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
private Map _imageMap; |
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
private List _externalScripts; |
75 |
|
|
76 |
|
private final IdAllocator _idAllocator; |
77 |
|
|
78 |
|
private final String _preloadName; |
79 |
|
|
80 |
11 |
private final Map _requires = new HashMap(); |
81 |
|
|
82 |
|
private IRequestCycle _cycle; |
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
public PageRenderSupportImpl(AssetFactory assetFactory, String namespace, |
101 |
|
Location location, ResponseBuilder builder) |
102 |
8 |
{ |
103 |
8 |
Defense.notNull(assetFactory, "assetService"); |
104 |
|
|
105 |
8 |
_assetFactory = assetFactory; |
106 |
8 |
_location = location; |
107 |
8 |
_idAllocator = new IdAllocator(namespace); |
108 |
8 |
_builder = builder; |
109 |
|
|
110 |
8 |
_preloadName = (namespace.equals("") ? "tapestry." : namespace) + "preload"; |
111 |
8 |
} |
112 |
|
|
113 |
|
public PageRenderSupportImpl(AssetFactory assetFactory, String namespace, |
114 |
|
ResponseBuilder builder, IRequestCycle cycle) |
115 |
3 |
{ |
116 |
3 |
Defense.notNull(assetFactory, "assetService"); |
117 |
|
|
118 |
3 |
_assetFactory = assetFactory; |
119 |
3 |
_idAllocator = new IdAllocator(namespace); |
120 |
3 |
_builder = builder; |
121 |
3 |
_cycle = cycle; |
122 |
|
|
123 |
3 |
_preloadName = (namespace.equals("") ? "tapestry." : namespace) + "preload"; |
124 |
3 |
} |
125 |
|
|
126 |
|
|
127 |
|
|
128 |
|
|
129 |
|
|
130 |
|
|
131 |
|
public Location getLocation() |
132 |
|
{ |
133 |
1 |
if (_location != null) |
134 |
|
{ |
135 |
1 |
return _location; |
136 |
|
} |
137 |
|
|
138 |
0 |
if (_cycle != null) |
139 |
|
{ |
140 |
0 |
IRender render = _cycle.renderStackPeek(); |
141 |
|
|
142 |
0 |
if (render != null && IComponent.class.isInstance(render)) |
143 |
|
{ |
144 |
0 |
return ((IComponent)render).getLocation(); |
145 |
|
} |
146 |
|
} |
147 |
|
|
148 |
0 |
return null; |
149 |
|
} |
150 |
|
|
151 |
|
public String getPreloadedImageReference(String URL) |
152 |
|
{ |
153 |
3 |
return getPreloadedImageReference(null, URL); |
154 |
|
} |
155 |
|
|
156 |
|
public String getPreloadedImageReference(IComponent target, IAsset source) |
157 |
|
{ |
158 |
1 |
return getPreloadedImageReference(target, source.buildURL()); |
159 |
|
} |
160 |
|
|
161 |
|
public String getPreloadedImageReference(IComponent target, String URL) |
162 |
|
{ |
163 |
4 |
if (target != null |
164 |
|
&& !_builder.isImageInitializationAllowed(target)) |
165 |
0 |
return URL; |
166 |
|
|
167 |
4 |
if (_imageMap == null) |
168 |
2 |
_imageMap = new HashMap(); |
169 |
|
|
170 |
4 |
String reference = (String) _imageMap.get(URL); |
171 |
|
|
172 |
4 |
if (reference == null) |
173 |
|
{ |
174 |
3 |
int count = _imageMap.size(); |
175 |
3 |
String varName = _preloadName + "[" + count + "]"; |
176 |
3 |
reference = varName + ".src"; |
177 |
|
|
178 |
3 |
if (_imageInitializations == null) |
179 |
2 |
_imageInitializations = new StringBuffer(); |
180 |
|
|
181 |
3 |
_imageInitializations.append(" "); |
182 |
3 |
_imageInitializations.append(varName); |
183 |
3 |
_imageInitializations.append(" = new Image();\n"); |
184 |
3 |
_imageInitializations.append(" "); |
185 |
3 |
_imageInitializations.append(reference); |
186 |
3 |
_imageInitializations.append(" = \""); |
187 |
3 |
_imageInitializations.append(URL); |
188 |
3 |
_imageInitializations.append("\";\n"); |
189 |
|
|
190 |
3 |
_imageMap.put(URL, reference); |
191 |
|
} |
192 |
|
|
193 |
4 |
return reference; |
194 |
|
} |
195 |
|
|
196 |
|
public void addBodyScript(String script) |
197 |
|
{ |
198 |
2 |
addBodyScript(null, script); |
199 |
2 |
} |
200 |
|
|
201 |
|
public void addBodyScript(IComponent target, String script) |
202 |
|
{ |
203 |
2 |
if (!_builder.isBodyScriptAllowed(target)) |
204 |
0 |
return; |
205 |
|
|
206 |
2 |
String val = stripDuplicateIncludes(script); |
207 |
|
|
208 |
2 |
if (_bodyScript == null) |
209 |
2 |
_bodyScript = new StringBuffer(val.length()); |
210 |
|
|
211 |
2 |
_bodyScript.append("\n").append(val); |
212 |
2 |
} |
213 |
|
|
214 |
|
|
215 |
|
|
216 |
|
|
217 |
|
public boolean isBodyScriptAllowed(IComponent target) |
218 |
|
{ |
219 |
0 |
return _builder.isBodyScriptAllowed(target); |
220 |
|
} |
221 |
|
|
222 |
|
|
223 |
|
|
224 |
|
|
225 |
|
public boolean isExternalScriptAllowed(IComponent target) |
226 |
|
{ |
227 |
0 |
return _builder.isExternalScriptAllowed(target); |
228 |
|
} |
229 |
|
|
230 |
|
|
231 |
|
|
232 |
|
|
233 |
|
public boolean isInitializationScriptAllowed(IComponent target) |
234 |
|
{ |
235 |
0 |
return _builder.isInitializationScriptAllowed(target); |
236 |
|
} |
237 |
|
|
238 |
|
public void addInitializationScript(String script) |
239 |
|
{ |
240 |
6 |
addInitializationScript(null, script); |
241 |
6 |
} |
242 |
|
|
243 |
|
public void addInitializationScript(IComponent target, String script) |
244 |
|
{ |
245 |
6 |
if (!_builder.isInitializationScriptAllowed(target)) |
246 |
0 |
return; |
247 |
|
|
248 |
6 |
String val = stripDuplicateIncludes(script); |
249 |
|
|
250 |
6 |
if (_initializationScript == null) |
251 |
1 |
_initializationScript = new StringBuffer(val.length() + 1); |
252 |
|
|
253 |
6 |
_initializationScript.append("\n").append(val); |
254 |
6 |
} |
255 |
|
|
256 |
|
public void addScriptAfterInitialization(IComponent target, String script) |
257 |
|
{ |
258 |
0 |
if (!_builder.isInitializationScriptAllowed(target)) |
259 |
0 |
return; |
260 |
|
|
261 |
0 |
String strippedScript = stripDuplicateIncludes(script); |
262 |
|
|
263 |
0 |
if (_postInitializationScript == null) |
264 |
0 |
_postInitializationScript = new StringBuffer(strippedScript.length() + 1); |
265 |
|
|
266 |
0 |
_postInitializationScript.append("\n").append(strippedScript); |
267 |
0 |
} |
268 |
|
|
269 |
|
|
270 |
|
|
271 |
|
|
272 |
|
|
273 |
|
|
274 |
|
|
275 |
|
|
276 |
|
String stripDuplicateIncludes(String input) |
277 |
|
{ |
278 |
8 |
String[] lines = StringUtils.splitPreserveAllTokens(input, ';'); |
279 |
|
|
280 |
8 |
if (lines == null || lines.length < 1) |
281 |
0 |
return input; |
282 |
|
|
283 |
8 |
String ret = input; |
284 |
|
|
285 |
29 |
for (int i=0; i < lines.length; i++) |
286 |
|
{ |
287 |
21 |
if (lines[i].indexOf("dojo.require") < 0) |
288 |
15 |
continue; |
289 |
|
|
290 |
6 |
String line = StringUtils.stripToEmpty(lines[i]); |
291 |
|
|
292 |
6 |
if (_requires.containsKey(line)) |
293 |
|
{ |
294 |
3 |
ret = StringUtils.replaceOnce(ret, line+";", ""); |
295 |
|
} else |
296 |
|
{ |
297 |
3 |
_requires.put(line, "t"); |
298 |
|
} |
299 |
|
} |
300 |
|
|
301 |
8 |
return StringUtils.stripToEmpty(ret.trim()); |
302 |
|
} |
303 |
|
|
304 |
|
public void addExternalScript(Resource scriptLocation) |
305 |
|
{ |
306 |
3 |
addExternalScript(null, scriptLocation); |
307 |
3 |
} |
308 |
|
|
309 |
|
public void addExternalScript(IComponent target, Resource scriptLocation) |
310 |
|
{ |
311 |
3 |
if (!_builder.isExternalScriptAllowed(target)) |
312 |
0 |
return; |
313 |
|
|
314 |
3 |
if (_externalScripts == null) |
315 |
1 |
_externalScripts = new ArrayList(); |
316 |
|
|
317 |
3 |
if (_externalScripts.contains(scriptLocation)) |
318 |
1 |
return; |
319 |
|
|
320 |
|
|
321 |
|
|
322 |
2 |
_externalScripts.add(scriptLocation); |
323 |
2 |
} |
324 |
|
|
325 |
|
public String getUniqueString(String baseValue) |
326 |
|
{ |
327 |
8 |
return _idAllocator.allocateId(baseValue); |
328 |
|
} |
329 |
|
|
330 |
|
private void writeExternalScripts(IMarkupWriter writer, IRequestCycle cycle) |
331 |
|
{ |
332 |
1 |
int count = Tapestry.size(_externalScripts); |
333 |
3 |
for (int i = 0; i < count; i++) |
334 |
|
{ |
335 |
2 |
Resource scriptLocation = (Resource) _externalScripts.get(i); |
336 |
|
|
337 |
2 |
IAsset asset = _assetFactory.createAsset(scriptLocation, null); |
338 |
|
|
339 |
2 |
String url = asset.buildURL(); |
340 |
|
|
341 |
|
|
342 |
|
|
343 |
|
|
344 |
2 |
_builder.writeExternalScript(writer, url, cycle); |
345 |
|
} |
346 |
1 |
} |
347 |
|
|
348 |
|
|
349 |
|
|
350 |
|
|
351 |
|
|
352 |
|
|
353 |
|
|
354 |
|
|
355 |
|
|
356 |
|
|
357 |
|
|
358 |
|
|
359 |
|
|
360 |
|
|
361 |
|
|
362 |
|
|
363 |
|
public void writeBodyScript(IMarkupWriter writer, IRequestCycle cycle) |
364 |
|
{ |
365 |
4 |
if (!Tapestry.isEmpty(_externalScripts)) |
366 |
1 |
writeExternalScripts(writer, cycle); |
367 |
|
|
368 |
4 |
if (!(any(_bodyScript) || any(_imageInitializations))) |
369 |
1 |
return; |
370 |
|
|
371 |
3 |
_builder.beginBodyScript(writer, cycle); |
372 |
|
|
373 |
3 |
if (any(_imageInitializations)) |
374 |
|
{ |
375 |
2 |
_builder.writeImageInitializations(writer, |
376 |
|
StringUtils.stripToEmpty(_imageInitializations.toString()), |
377 |
|
_preloadName, |
378 |
|
cycle); |
379 |
|
} |
380 |
|
|
381 |
3 |
if (any(_bodyScript)) |
382 |
|
{ |
383 |
2 |
_builder.writeBodyScript(writer, StringUtils.stripToEmpty(_bodyScript.toString()), cycle); |
384 |
|
} |
385 |
|
|
386 |
3 |
_builder.endBodyScript(writer, cycle); |
387 |
3 |
} |
388 |
|
|
389 |
|
|
390 |
|
|
391 |
|
|
392 |
|
|
393 |
|
|
394 |
|
|
395 |
|
|
396 |
|
|
397 |
|
|
398 |
|
public void writeInitializationScript(IMarkupWriter writer) |
399 |
|
{ |
400 |
1 |
if (!any(_initializationScript) && !any(_postInitializationScript)) |
401 |
0 |
return; |
402 |
|
|
403 |
1 |
String script = getContent(_initializationScript) + getContent(_postInitializationScript); |
404 |
|
|
405 |
1 |
_builder.writeInitializationScript(writer, StringUtils.stripToEmpty(script)); |
406 |
1 |
} |
407 |
|
|
408 |
|
public static String getContent(StringBuffer buffer) |
409 |
|
{ |
410 |
8 |
if (buffer == null || buffer.length() < 1) |
411 |
6 |
return ""; |
412 |
|
|
413 |
2 |
return buffer.toString(); |
414 |
|
} |
415 |
|
|
416 |
|
private boolean any(StringBuffer buffer) |
417 |
|
{ |
418 |
13 |
return buffer != null && buffer.length() > 0; |
419 |
|
} |
420 |
|
} |