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 | 0 | 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 | 0 | Map result = new HashMap(); |
67 | |
|
68 | 0 | Map baseSymbols = getBaseSymbols(); |
69 | |
|
70 | 0 | if (baseSymbols != null) result.putAll(baseSymbols); |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | 0 | Iterator i = getBindingNames().iterator(); |
77 | 0 | while(i.hasNext()) |
78 | |
{ |
79 | 0 | String bindingName = (String) i.next(); |
80 | |
|
81 | |
|
82 | |
|
83 | 0 | if (getSpecification().getParameter(bindingName) != null) continue; |
84 | |
|
85 | 0 | IBinding binding = getBinding(bindingName); |
86 | |
|
87 | 0 | Object value = binding.getObject(); |
88 | |
|
89 | 0 | result.put(bindingName, value); |
90 | 0 | } |
91 | |
|
92 | 0 | return result; |
93 | |
} |
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
private IScript getParsedScript() |
100 | |
{ |
101 | 0 | IAsset scriptAsset = getScriptAsset(); |
102 | 0 | String scriptPath = getScriptPath(); |
103 | |
|
104 | |
|
105 | 0 | if (scriptAsset != null && scriptPath != null) |
106 | 0 | throw new ApplicationRuntimeException(HTMLMessages.multiAssetParameterError(getBinding("scriptAsset"), |
107 | |
getBinding("script"))); |
108 | |
|
109 | 0 | if (scriptPath == null && scriptAsset == null) |
110 | 0 | throw new ApplicationRuntimeException(HTMLMessages.noScriptPathError()); |
111 | |
|
112 | 0 | IScriptSource source = getScriptSource(); |
113 | |
|
114 | 0 | if (scriptPath != null) |
115 | |
{ |
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | 0 | Resource rootLocation = getContainer().getSpecification().getSpecificationLocation(); |
121 | |
|
122 | 0 | scriptAsset = getAssetSource().findAsset(rootLocation, getContainer().getSpecification(), scriptPath, getPage().getLocale(), getScriptLocation()); |
123 | |
} |
124 | |
|
125 | 0 | Defense.notNull(scriptAsset, "script"); |
126 | |
|
127 | |
try |
128 | |
{ |
129 | 0 | return source.getScript(scriptAsset.getResourceLocation()); |
130 | |
} |
131 | 0 | catch (RuntimeException ex) |
132 | |
{ |
133 | 0 | throw new ApplicationRuntimeException(ex.getMessage(), this, getScriptLocation(), ex); |
134 | |
} |
135 | |
|
136 | |
} |
137 | |
|
138 | |
Location getScriptLocation() |
139 | |
{ |
140 | 0 | Location location = null; |
141 | |
|
142 | 0 | if (getBinding("script")!=null) |
143 | 0 | location = getBinding("script").getLocation(); |
144 | 0 | else if (getBinding("scriptAsset")!=null) |
145 | 0 | location = getBinding("scriptAsset").getLocation(); |
146 | |
|
147 | 0 | return location; |
148 | |
} |
149 | |
|
150 | |
protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) |
151 | |
{ |
152 | 0 | if (!cycle.isRewinding()) |
153 | |
{ |
154 | 0 | PageRenderSupport pageRenderSupport = TapestryUtils |
155 | |
.getPageRenderSupport(cycle, this); |
156 | |
|
157 | 0 | _symbols = getInputSymbols(); |
158 | |
|
159 | 0 | getParsedScript().execute(this, cycle, pageRenderSupport, _symbols); |
160 | |
} |
161 | |
|
162 | |
|
163 | 0 | renderBody(writer, cycle); |
164 | 0 | } |
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 | 0 | 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 | |
} |