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