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