1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
package org.apache.shale.clay.parser.builder; |
22 | |
|
23 | |
import java.util.ArrayList; |
24 | |
import java.util.Iterator; |
25 | |
import java.util.List; |
26 | |
|
27 | |
import org.apache.commons.logging.Log; |
28 | |
import org.apache.commons.logging.LogFactory; |
29 | |
import org.apache.shale.clay.config.beans.ActionListenerBean; |
30 | |
import org.apache.shale.clay.config.beans.AttributeBean; |
31 | |
import org.apache.shale.clay.config.beans.ComponentBean; |
32 | |
import org.apache.shale.clay.config.beans.ConfigBean; |
33 | |
import org.apache.shale.clay.config.beans.ConfigBeanFactory; |
34 | |
import org.apache.shale.clay.config.beans.ConverterBean; |
35 | |
import org.apache.shale.clay.config.beans.ElementBean; |
36 | |
import org.apache.shale.clay.config.beans.SymbolBean; |
37 | |
import org.apache.shale.clay.config.beans.ValidatorBean; |
38 | |
import org.apache.shale.clay.config.beans.ValueChangeListenerBean; |
39 | |
import org.apache.shale.clay.parser.Node; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | 212 | public class ElementBuilder extends Builder { |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
private static Log log; |
52 | |
static { |
53 | 2 | log = LogFactory.getLog(ElementBuilder.class); |
54 | 1 | } |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
protected String getJsfid(Node node) { |
65 | 38 | String jsfid = (String) node.getAttributes().get("jsfid"); |
66 | 38 | return jsfid; |
67 | |
} |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
protected String getComponentType(Node node) { |
77 | 34 | String componentType = (String) node.getAttributes().get("componentType"); |
78 | 34 | return componentType; |
79 | |
} |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
protected void addConverter(Node node, ElementBean target) { |
90 | 1 | ConverterBean targetConverter = new ConverterBean(); |
91 | |
|
92 | 1 | String jsfid = getJsfid(node); |
93 | 1 | targetConverter.setJsfid(jsfid); |
94 | |
|
95 | |
|
96 | 1 | realizeComponent(node, targetConverter); |
97 | |
|
98 | 1 | target.addConverter(targetConverter); |
99 | |
|
100 | 1 | } |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
protected void addValidator(Node node, ElementBean target) { |
112 | 1 | ValidatorBean targetValidator = new ValidatorBean(); |
113 | |
|
114 | 1 | String jsfid = getJsfid(node); |
115 | 1 | targetValidator.setJsfid(jsfid); |
116 | |
|
117 | |
|
118 | 1 | realizeComponent(node, targetValidator); |
119 | |
|
120 | 1 | target.addValidator(targetValidator); |
121 | |
|
122 | 1 | } |
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
protected void addActionListener(Node node, ElementBean target) { |
133 | 1 | ActionListenerBean targetActionListener = new ActionListenerBean(); |
134 | |
|
135 | 1 | String jsfid = getJsfid(node); |
136 | 1 | targetActionListener.setJsfid(jsfid); |
137 | |
|
138 | |
|
139 | 1 | realizeComponent(node, targetActionListener); |
140 | |
|
141 | 1 | target.addActionListener(targetActionListener); |
142 | |
|
143 | 1 | } |
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
protected void addValueChangeListener(Node node, ElementBean target) { |
154 | 1 | ValueChangeListenerBean targetValueChangeListener = new ValueChangeListenerBean(); |
155 | |
|
156 | 1 | String jsfid = getJsfid(node); |
157 | 1 | targetValueChangeListener.setJsfid(jsfid); |
158 | |
|
159 | |
|
160 | 1 | realizeComponent(node, targetValueChangeListener); |
161 | |
|
162 | 1 | target.addValueChangeListener(targetValueChangeListener); |
163 | 1 | } |
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
protected void realizeComponent(Node node, ComponentBean target) { |
176 | |
|
177 | 16 | ConfigBean config = ConfigBeanFactory.findConfig(target.getJsfid()); |
178 | |
|
179 | |
try { |
180 | |
|
181 | 16 | config.assignParent(target); |
182 | |
|
183 | 16 | config.realizingInheritance(target); |
184 | |
} catch (RuntimeException e) { |
185 | |
log.error(e); |
186 | |
throw new RuntimeException( |
187 | |
messages.getMessage("parser.unresolved", |
188 | |
new Object[] {node.getToken(), node.getToken().getRawText()})); |
189 | 16 | } |
190 | |
|
191 | 16 | assignAttributes(node, target); |
192 | |
|
193 | |
|
194 | 16 | Iterator ci = node.getChildren().iterator(); |
195 | 19 | while (ci.hasNext()) { |
196 | 3 | Node child = (Node) ci.next(); |
197 | 3 | if (child.isWellFormed() && child.getName() != null |
198 | |
&& child.getName().equals("attributes")) { |
199 | |
|
200 | 1 | addAttributes(child, target); |
201 | |
} |
202 | 3 | } |
203 | |
|
204 | 16 | } |
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
protected void addAttributes(Node attributesNode, ComponentBean target) { |
217 | 23 | Iterator ci = attributesNode.getChildren().iterator(); |
218 | 134 | while (ci.hasNext()) { |
219 | 111 | Node child = (Node) ci.next(); |
220 | 111 | if (child.isWellFormed() && child.getName() != null |
221 | |
&& child.getName().equals("set")) { |
222 | |
|
223 | 44 | String name = (String) child.getAttributes().get("name"); |
224 | 44 | String value = (String) child.getAttributes().get("value"); |
225 | 44 | String bindingType = (String) child.getAttributes().get("bindingType"); |
226 | |
|
227 | 44 | AttributeBean attr = target.getAttribute(name); |
228 | 44 | if (attr != null) { |
229 | 44 | createAttribute(attr, value, target); |
230 | 44 | } else { |
231 | |
attr = new AttributeBean(); |
232 | |
attr.setName(name); |
233 | |
attr.setValue(value); |
234 | |
attr.setBindingType(bindingType); |
235 | |
target.addAttribute(attr); |
236 | |
} |
237 | |
} |
238 | 111 | } |
239 | 23 | } |
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
protected void addSymbols(Node symbolsNode, ElementBean target) { |
251 | |
Iterator si = symbolsNode.getChildren().iterator(); |
252 | |
while (si.hasNext()) { |
253 | |
Node child = (Node) si.next(); |
254 | |
if (child.isWellFormed() && child.getName() != null |
255 | |
&& child.getName().equals("set")) { |
256 | |
|
257 | |
String name = (String) child.getAttributes().get("name"); |
258 | |
String value = (String) child.getAttributes().get("value"); |
259 | |
|
260 | |
if (name != null && name.length() > 0) { |
261 | |
SymbolBean symbol = new SymbolBean(); |
262 | |
StringBuffer tmp = new StringBuffer(name); |
263 | |
if (tmp.charAt(0) != '@') { |
264 | |
tmp.insert(0, '@'); |
265 | |
} |
266 | |
|
267 | |
symbol.setName(tmp.toString()); |
268 | |
symbol.setValue(value); |
269 | |
target.addSymbol(symbol); |
270 | |
} |
271 | |
} |
272 | |
} |
273 | |
} |
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
protected void encodeBegin(Node node, ElementBean target, ComponentBean root) { |
287 | 34 | super.encodeBegin(node, target, root); |
288 | |
|
289 | 34 | List deleteList = new ArrayList(); |
290 | 34 | Iterator ci = node.getChildren().iterator(); |
291 | 112 | while (ci.hasNext()) { |
292 | 78 | Node child = (Node) ci.next(); |
293 | 78 | if (child.isWellFormed() && child.getName() != null) { |
294 | 28 | if (child.getName().equals("attributes")) { |
295 | 22 | addAttributes(child, target); |
296 | 22 | deleteList.add(child); |
297 | 22 | } else if (child.getName().equals("symbols")) { |
298 | |
addSymbols(child, target); |
299 | |
deleteList.add(child); |
300 | |
} else if (child.getName().equals("converter")) { |
301 | 1 | addConverter(child, target); |
302 | 1 | deleteList.add(child); |
303 | 1 | } else if (child.getName().equals("validator")) { |
304 | 1 | addValidator(child, target); |
305 | 1 | deleteList.add(child); |
306 | 1 | } else if (child.getName().equals("actionListener")) { |
307 | 1 | addActionListener(child, target); |
308 | 1 | deleteList.add(child); |
309 | 1 | } else if (child.getName().equals("valueChangeListener")) { |
310 | 1 | addValueChangeListener(child, target); |
311 | 1 | deleteList.add(child); |
312 | 1 | } |
313 | |
} else { |
314 | 50 | if (child.isComment() || isNodeWhitespace(child)) { |
315 | |
|
316 | 50 | deleteList.add(child); |
317 | |
} |
318 | |
} |
319 | 78 | } |
320 | |
|
321 | 34 | ci = deleteList.iterator(); |
322 | 110 | while (ci.hasNext()) { |
323 | 76 | node.getChildren().remove(ci.next()); |
324 | 76 | } |
325 | |
|
326 | 34 | } |
327 | |
|
328 | |
|
329 | |
|
330 | |
|
331 | |
|
332 | |
|
333 | |
|
334 | |
|
335 | |
|
336 | |
|
337 | |
|
338 | |
|
339 | |
|
340 | |
|
341 | |
public ElementBean createElement(Node node) { |
342 | 34 | ElementBean target = super.createElement(node); |
343 | 34 | String renderId = (String) node.getAttributes().get("renderId"); |
344 | 34 | if (renderId != null) { |
345 | 2 | Integer id = null; |
346 | |
try { |
347 | 2 | id = Integer.valueOf(renderId); |
348 | |
} catch (NumberFormatException e) { |
349 | |
log.error(e); |
350 | |
throw new RuntimeException( |
351 | |
messages.getMessage("parser.unresolved", |
352 | |
new Object[] {node.getToken(), node.getToken().getRawText()})); |
353 | 2 | } |
354 | 2 | if (id != null) { |
355 | 2 | target.setRenderId(id.intValue()); |
356 | |
} |
357 | |
} |
358 | |
|
359 | 34 | return target; |
360 | |
} |
361 | |
|
362 | |
|
363 | |
|
364 | |
|
365 | |
|
366 | |
|
367 | |
|
368 | |
|
369 | |
|
370 | |
public boolean isChildrenAllowed() { |
371 | 81 | return true; |
372 | |
} |
373 | |
|
374 | |
|
375 | |
|
376 | |
|
377 | |
|
378 | |
|
379 | |
|
380 | |
|
381 | |
|
382 | |
|
383 | |
|
384 | |
protected void assignNode(Node node, ElementBean target) { |
385 | |
|
386 | |
|
387 | 81 | String id = (String) node.getAttributes().get("id"); |
388 | 81 | target.setId(id); |
389 | |
|
390 | |
|
391 | 81 | if (target.getJsfid() != null) { |
392 | |
|
393 | 81 | ConfigBean config = ConfigBeanFactory.findConfig(target.getJsfid()); |
394 | |
|
395 | 81 | target.setComponentType(null); |
396 | |
|
397 | |
|
398 | |
try { |
399 | |
|
400 | 81 | config.assignParent(target); |
401 | |
|
402 | 81 | config.realizingInheritance(target); |
403 | |
} catch (RuntimeException e) { |
404 | |
log.error(e); |
405 | |
throw new RuntimeException( |
406 | |
messages.getMessage("parser.unresolved", |
407 | |
new Object[] {node.getToken(), node.getToken().getRawText()})); |
408 | 81 | } |
409 | |
|
410 | |
|
411 | 81 | if (target.getComponentType() == null) { |
412 | |
target.setComponentType(this.getComponentType(node)); |
413 | |
} |
414 | |
|
415 | |
} |
416 | |
|
417 | |
|
418 | 81 | assignAttributes(node, target); |
419 | |
|
420 | 81 | } |
421 | |
|
422 | |
|
423 | |
|
424 | |
|
425 | |
|
426 | |
|
427 | |
|
428 | |
|
429 | |
|
430 | |
protected boolean isNodeWhitespace(Node node) { |
431 | 80 | StringBuffer document = node.getToken().getDocument(); |
432 | 80 | for (int i = node.getToken().getBeginOffset(); |
433 | 1832 | i < node.getToken().getEndOffset(); i++) { |
434 | 876 | char c = document.charAt(i); |
435 | 876 | if (!Character.isWhitespace(c)) { |
436 | |
return false; |
437 | |
} |
438 | |
} |
439 | 80 | return true; |
440 | |
} |
441 | |
|
442 | |
} |