|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| package org.apache.tapestry.util; |
|
16 |
| |
|
17 |
| import java.util.ArrayList; |
|
18 |
| import java.util.HashMap; |
|
19 |
| import java.util.List; |
|
20 |
| import java.util.Map; |
|
21 |
| |
|
22 |
| import org.apache.hivemind.Locatable; |
|
23 |
| import org.apache.hivemind.Location; |
|
24 |
| import org.apache.hivemind.Resource; |
|
25 |
| import org.apache.hivemind.util.Defense; |
|
26 |
| import org.apache.tapestry.IAsset; |
|
27 |
| import org.apache.tapestry.IMarkupWriter; |
|
28 |
| import org.apache.tapestry.IRequestCycle; |
|
29 |
| import org.apache.tapestry.PageRenderSupport; |
|
30 |
| import org.apache.tapestry.Tapestry; |
|
31 |
| import org.apache.tapestry.asset.AssetFactory; |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| public class PageRenderSupportImpl implements Locatable, PageRenderSupport |
|
41 |
| { |
|
42 |
| private final AssetFactory _assetFactory; |
|
43 |
| |
|
44 |
| private final Location _location; |
|
45 |
| |
|
46 |
| |
|
47 |
| private StringBuffer _initializationScript; |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| private StringBuffer _bodyScript; |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| private StringBuffer _imageInitializations; |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| private Map _imageMap; |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| private List _externalScripts; |
|
70 |
| |
|
71 |
| private final IdAllocator _idAllocator; |
|
72 |
| |
|
73 |
| private final String _preloadName; |
|
74 |
| |
|
75 |
231
| public PageRenderSupportImpl(AssetFactory assetFactory, String namespace, Location location)
|
|
76 |
| { |
|
77 |
231
| Defense.notNull(assetFactory, "assetService");
|
|
78 |
| |
|
79 |
231
| _assetFactory = assetFactory;
|
|
80 |
231
| _location = location;
|
|
81 |
231
| _idAllocator = new IdAllocator(namespace);
|
|
82 |
| |
|
83 |
231
| _preloadName = (namespace.equals("") ? "tapestry" : namespace) + "_preload";
|
|
84 |
| } |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
3
| public Location getLocation()
|
|
92 |
| { |
|
93 |
3
| return _location;
|
|
94 |
| } |
|
95 |
| |
|
96 |
12
| public String getPreloadedImageReference(String URL)
|
|
97 |
| { |
|
98 |
12
| if (_imageMap == null)
|
|
99 |
6
| _imageMap = new HashMap();
|
|
100 |
| |
|
101 |
12
| String reference = (String) _imageMap.get(URL);
|
|
102 |
| |
|
103 |
12
| if (reference == null)
|
|
104 |
| { |
|
105 |
9
| int count = _imageMap.size();
|
|
106 |
9
| String varName = _preloadName + "[" + count + "]";
|
|
107 |
9
| reference = varName + ".src";
|
|
108 |
| |
|
109 |
9
| if (_imageInitializations == null)
|
|
110 |
6
| _imageInitializations = new StringBuffer();
|
|
111 |
| |
|
112 |
9
| _imageInitializations.append(" ");
|
|
113 |
9
| _imageInitializations.append(varName);
|
|
114 |
9
| _imageInitializations.append(" = new Image();\n");
|
|
115 |
9
| _imageInitializations.append(" ");
|
|
116 |
9
| _imageInitializations.append(reference);
|
|
117 |
9
| _imageInitializations.append(" = \"");
|
|
118 |
9
| _imageInitializations.append(URL);
|
|
119 |
9
| _imageInitializations.append("\";\n");
|
|
120 |
| |
|
121 |
9
| _imageMap.put(URL, reference);
|
|
122 |
| } |
|
123 |
| |
|
124 |
12
| return reference;
|
|
125 |
| } |
|
126 |
| |
|
127 |
6
| public void addBodyScript(String script)
|
|
128 |
| { |
|
129 |
6
| if (_bodyScript == null)
|
|
130 |
6
| _bodyScript = new StringBuffer(script.length());
|
|
131 |
| |
|
132 |
6
| _bodyScript.append(script);
|
|
133 |
| } |
|
134 |
| |
|
135 |
147
| public void addInitializationScript(String script)
|
|
136 |
| { |
|
137 |
147
| if (_initializationScript == null)
|
|
138 |
96
| _initializationScript = new StringBuffer(script.length() + 1);
|
|
139 |
| |
|
140 |
147
| _initializationScript.append(script);
|
|
141 |
147
| _initializationScript.append('\n');
|
|
142 |
| } |
|
143 |
| |
|
144 |
126
| public void addExternalScript(Resource scriptLocation)
|
|
145 |
| { |
|
146 |
126
| if (_externalScripts == null)
|
|
147 |
96
| _externalScripts = new ArrayList();
|
|
148 |
| |
|
149 |
126
| if (_externalScripts.contains(scriptLocation))
|
|
150 |
27
| return;
|
|
151 |
| |
|
152 |
| |
|
153 |
| |
|
154 |
99
| _externalScripts.add(scriptLocation);
|
|
155 |
| |
|
156 |
| } |
|
157 |
| |
|
158 |
24
| public String getUniqueString(String baseValue)
|
|
159 |
| { |
|
160 |
24
| return _idAllocator.allocateId(baseValue);
|
|
161 |
| } |
|
162 |
| |
|
163 |
78
| private void writeExternalScripts(IMarkupWriter writer, IRequestCycle cycle)
|
|
164 |
| { |
|
165 |
78
| int count = Tapestry.size(_externalScripts);
|
|
166 |
78
| for (int i = 0; i < count; i++)
|
|
167 |
| { |
|
168 |
81
| Resource scriptLocation = (Resource) _externalScripts.get(i);
|
|
169 |
| |
|
170 |
81
| IAsset asset = _assetFactory.createAsset(scriptLocation, null);
|
|
171 |
| |
|
172 |
81
| String url = asset.buildURL();
|
|
173 |
| |
|
174 |
| |
|
175 |
| |
|
176 |
| |
|
177 |
81
| writer.begin("script");
|
|
178 |
81
| writer.attribute("type", "text/javascript");
|
|
179 |
81
| writer.attribute("src", url);
|
|
180 |
81
| writer.end();
|
|
181 |
81
| writer.println();
|
|
182 |
| } |
|
183 |
| } |
|
184 |
| |
|
185 |
| |
|
186 |
| |
|
187 |
| |
|
188 |
| |
|
189 |
| |
|
190 |
| |
|
191 |
| |
|
192 |
| |
|
193 |
| |
|
194 |
| |
|
195 |
| |
|
196 |
201
| public void writeBodyScript(IMarkupWriter writer, IRequestCycle cycle)
|
|
197 |
| { |
|
198 |
201
| if (!Tapestry.isEmpty(_externalScripts))
|
|
199 |
78
| writeExternalScripts(writer, cycle);
|
|
200 |
| |
|
201 |
201
| if (!(any(_bodyScript) || any(_imageInitializations)))
|
|
202 |
192
| return;
|
|
203 |
| |
|
204 |
9
| writer.begin("script");
|
|
205 |
9
| writer.attribute("type", "text/javascript");
|
|
206 |
9
| writer.printRaw("<!--");
|
|
207 |
| |
|
208 |
9
| if (any(_imageInitializations))
|
|
209 |
| { |
|
210 |
6
| writer.printRaw("\n\nvar " + _preloadName + " = new Array();\n");
|
|
211 |
6
| writer.printRaw("if (document.images)\n");
|
|
212 |
6
| writer.printRaw("{\n");
|
|
213 |
6
| writer.printRaw(_imageInitializations.toString());
|
|
214 |
6
| writer.printRaw("}\n");
|
|
215 |
| } |
|
216 |
| |
|
217 |
9
| if (any(_bodyScript))
|
|
218 |
| { |
|
219 |
6
| writer.printRaw("\n\n");
|
|
220 |
6
| writer.printRaw(_bodyScript.toString());
|
|
221 |
| } |
|
222 |
| |
|
223 |
9
| writer.printRaw("\n\n// -->");
|
|
224 |
9
| writer.end();
|
|
225 |
| } |
|
226 |
| |
|
227 |
| |
|
228 |
| |
|
229 |
| |
|
230 |
| |
|
231 |
| |
|
232 |
| |
|
233 |
192
| public void writeInitializationScript(IMarkupWriter writer)
|
|
234 |
| { |
|
235 |
192
| if (!any(_initializationScript))
|
|
236 |
114
| return;
|
|
237 |
| |
|
238 |
78
| writer.begin("script");
|
|
239 |
78
| writer.attribute("language", "JavaScript");
|
|
240 |
78
| writer.attribute("type", "text/javascript");
|
|
241 |
78
| writer.printRaw("<!--\n");
|
|
242 |
| |
|
243 |
78
| writer.printRaw(_initializationScript.toString());
|
|
244 |
| |
|
245 |
78
| writer.printRaw("\n// -->");
|
|
246 |
78
| writer.end();
|
|
247 |
| } |
|
248 |
| |
|
249 |
606
| private boolean any(StringBuffer buffer)
|
|
250 |
| { |
|
251 |
606
| return buffer != null && buffer.length() > 0;
|
|
252 |
| } |
|
253 |
| } |