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