1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
package org.apache.tapestry.html; |
16 |
|
|
17 |
|
import org.apache.hivemind.ApplicationRuntimeException; |
18 |
|
import org.apache.hivemind.Location; |
19 |
|
import org.apache.hivemind.Resource; |
20 |
|
import org.apache.hivemind.util.Defense; |
21 |
|
import org.apache.tapestry.*; |
22 |
|
import org.apache.tapestry.asset.AssetSource; |
23 |
|
import org.apache.tapestry.engine.IScriptSource; |
24 |
|
|
25 |
|
import java.util.HashMap; |
26 |
|
import java.util.Iterator; |
27 |
|
import java.util.Map; |
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
8 |
public abstract class Script extends AbstractComponent |
39 |
|
{ |
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
private Map _symbols; |
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
public abstract IScriptSource getScriptSource(); |
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
private Map getInputSymbols() |
65 |
|
{ |
66 |
7 |
Map result = new HashMap(); |
67 |
|
|
68 |
7 |
Map baseSymbols = getBaseSymbols(); |
69 |
|
|
70 |
7 |
if (baseSymbols != null) result.putAll(baseSymbols); |
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
7 |
Iterator i = getBindingNames().iterator(); |
77 |
8 |
while(i.hasNext()) |
78 |
|
{ |
79 |
1 |
String bindingName = (String) i.next(); |
80 |
|
|
81 |
|
|
82 |
|
|
83 |
1 |
if (getSpecification().getParameter(bindingName) != null) continue; |
84 |
|
|
85 |
1 |
IBinding binding = getBinding(bindingName); |
86 |
|
|
87 |
1 |
Object value = binding.getObject(); |
88 |
|
|
89 |
1 |
result.put(bindingName, value); |
90 |
1 |
} |
91 |
|
|
92 |
7 |
return result; |
93 |
|
} |
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
private IScript getParsedScript() |
100 |
|
{ |
101 |
7 |
IAsset scriptAsset = getScriptAsset(); |
102 |
7 |
String scriptPath = getScriptPath(); |
103 |
|
|
104 |
|
|
105 |
7 |
if (scriptAsset != null && scriptPath != null) |
106 |
1 |
throw new ApplicationRuntimeException(HTMLMessages.multiAssetParameterError(getBinding("scriptAsset"), |
107 |
|
getBinding("script"))); |
108 |
|
|
109 |
6 |
if (scriptPath == null && scriptAsset == null) |
110 |
1 |
throw new ApplicationRuntimeException(HTMLMessages.noScriptPathError()); |
111 |
|
|
112 |
5 |
IScriptSource source = getScriptSource(); |
113 |
|
|
114 |
5 |
if (scriptPath != null) |
115 |
|
{ |
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
|
120 |
3 |
Resource rootLocation = getContainer().getSpecification().getSpecificationLocation(); |
121 |
|
|
122 |
3 |
scriptAsset = getAssetSource().findAsset(rootLocation, getContainer().getSpecification(), scriptPath, getPage().getLocale(), getScriptLocation()); |
123 |
|
} |
124 |
|
|
125 |
5 |
Defense.notNull(scriptAsset, "script"); |
126 |
|
|
127 |
|
try |
128 |
|
{ |
129 |
5 |
return source.getScript(scriptAsset.getResourceLocation()); |
130 |
|
} |
131 |
1 |
catch (RuntimeException ex) |
132 |
|
{ |
133 |
1 |
throw new ApplicationRuntimeException(ex.getMessage(), this, getScriptLocation(), ex); |
134 |
|
} |
135 |
|
|
136 |
|
} |
137 |
|
|
138 |
|
Location getScriptLocation() |
139 |
|
{ |
140 |
4 |
Location location = null; |
141 |
|
|
142 |
4 |
if (getBinding("script")!=null) |
143 |
0 |
location = getBinding("script").getLocation(); |
144 |
4 |
else if (getBinding("scriptAsset")!=null) |
145 |
0 |
location = getBinding("scriptAsset").getLocation(); |
146 |
|
|
147 |
4 |
return location; |
148 |
|
} |
149 |
|
|
150 |
|
protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) |
151 |
|
{ |
152 |
8 |
if (!cycle.isRewinding()) |
153 |
|
{ |
154 |
7 |
PageRenderSupport pageRenderSupport = TapestryUtils |
155 |
|
.getPageRenderSupport(cycle, this); |
156 |
|
|
157 |
7 |
_symbols = getInputSymbols(); |
158 |
|
|
159 |
7 |
getParsedScript().execute(this, cycle, pageRenderSupport, _symbols); |
160 |
|
} |
161 |
|
|
162 |
|
|
163 |
5 |
renderBody(writer, cycle); |
164 |
5 |
} |
165 |
|
|
166 |
|
public abstract String getScriptPath(); |
167 |
|
|
168 |
|
public abstract IAsset getScriptAsset(); |
169 |
|
|
170 |
|
|
171 |
|
public abstract AssetSource getAssetSource(); |
172 |
|
|
173 |
|
|
174 |
|
|
175 |
|
public abstract Map getBaseSymbols(); |
176 |
|
|
177 |
|
|
178 |
|
|
179 |
|
|
180 |
|
|
181 |
|
|
182 |
|
|
183 |
|
|
184 |
|
|
185 |
|
public Map getSymbols() |
186 |
|
{ |
187 |
2 |
return _symbols; |
188 |
|
} |
189 |
|
|
190 |
|
protected void cleanupAfterRender(IRequestCycle cycle) |
191 |
|
{ |
192 |
0 |
_symbols = null; |
193 |
|
|
194 |
0 |
super.cleanupAfterRender(cycle); |
195 |
0 |
} |
196 |
|
|
197 |
|
} |