1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
package org.webmacro; |
25 | |
|
26 | |
import java.util.Collection; |
27 | |
import java.util.HashMap; |
28 | |
import java.util.Map; |
29 | |
import java.util.Set; |
30 | |
|
31 | |
import org.slf4j.Logger; |
32 | |
import org.slf4j.LoggerFactory; |
33 | |
import org.webmacro.engine.EvaluationExceptionHandler; |
34 | |
import org.webmacro.engine.FunctionCall; |
35 | |
import org.webmacro.engine.MethodWrapper; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
public class Context implements Map, Cloneable |
61 | |
{ |
62 | 38 | static Logger _log = LoggerFactory.getLogger(Context.class); |
63 | |
|
64 | |
private final Broker _broker; |
65 | 2011 | private HashMap _funcs = null; |
66 | |
|
67 | |
private EvaluationExceptionHandler _eeHandler; |
68 | |
|
69 | 2011 | private Map _variables = new HashMap(); |
70 | |
|
71 | 2011 | private TemplateEvaluationContext _teContext |
72 | |
= new TemplateEvaluationContext(); |
73 | |
|
74 | 38 | private static final org.webmacro.engine.UndefinedMacro UNDEF |
75 | |
= org.webmacro.engine.UndefinedMacro.getInstance(); |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
public Context() throws InitException { |
81 | 10 | this(Broker.getBroker()); |
82 | 10 | } |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
public Context (Broker broker) |
88 | 2011 | { |
89 | 2011 | _broker = broker; |
90 | 2011 | } |
91 | |
|
92 | |
|
93 | 2011 | public final static class TemplateEvaluationContext |
94 | |
{ |
95 | |
|
96 | |
|
97 | |
public String _templateName; |
98 | |
public int _lineNo; |
99 | |
public int _columnNo; |
100 | |
} |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
public final Object clone () |
107 | |
{ |
108 | 0 | return cloneContext(); |
109 | |
} |
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
public Context cloneContext () |
116 | |
{ |
117 | |
Context c; |
118 | |
try |
119 | |
{ |
120 | 0 | c = (Context) super.clone(); |
121 | |
} |
122 | 0 | catch (CloneNotSupportedException e) |
123 | |
{ |
124 | 0 | e.printStackTrace(); |
125 | 0 | return null; |
126 | 0 | } |
127 | 0 | c._teContext = new TemplateEvaluationContext(); |
128 | 0 | if (_variables instanceof HashMap) |
129 | |
{ |
130 | 0 | c._variables = (Map) ((HashMap) _variables).clone(); |
131 | |
} |
132 | |
else |
133 | |
{ |
134 | 0 | c._variables = new HashMap(_variables); |
135 | |
} |
136 | 0 | return c; |
137 | |
} |
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
public void clear () |
146 | |
{ |
147 | 0 | _variables.clear(); |
148 | 0 | _eeHandler = null; |
149 | 0 | } |
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
public final Broker getBroker () |
156 | |
{ |
157 | 3873 | return _broker; |
158 | |
} |
159 | |
|
160 | |
public final TemplateEvaluationContext getTemplateEvaluationContext () |
161 | |
{ |
162 | 11558 | return _teContext; |
163 | |
} |
164 | |
|
165 | |
public final String getCurrentLocation () |
166 | |
{ |
167 | 110 | StringBuffer loc = new StringBuffer(); |
168 | 110 | loc.append(_teContext._templateName == null ? "(unknown)" : _teContext._templateName); |
169 | 110 | loc.append(":").append(_teContext._lineNo).append(".").append(_teContext._columnNo); |
170 | 110 | return loc.toString(); |
171 | |
} |
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
public final Logger getLog (String type, String description) |
178 | |
{ |
179 | 0 | return _log; |
180 | |
} |
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
public final Logger getLog (String type) |
188 | |
{ |
189 | 892 | return _log; |
190 | |
} |
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
public EvaluationExceptionHandler getEvaluationExceptionHandler () |
196 | |
{ |
197 | 58 | if (_eeHandler != null) |
198 | |
{ |
199 | 50 | return _eeHandler; |
200 | |
} |
201 | |
else |
202 | |
{ |
203 | 8 | return _broker.getEvaluationExceptionHandler(); |
204 | |
} |
205 | |
} |
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
public void setEvaluationExceptionHandler (EvaluationExceptionHandler eeh) |
212 | |
{ |
213 | 163 | _eeHandler = eeh; |
214 | 163 | } |
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
protected Object internalGet (Object name) |
224 | |
throws PropertyException |
225 | |
{ |
226 | 10368 | Object ret = _variables.get(name); |
227 | 10368 | if (ret != null || _variables.containsKey(name)) |
228 | 10268 | return ret; |
229 | |
|
230 | 100 | if (name instanceof String) { |
231 | 47 | Object var = _broker.getAutoContextVariable((String) name, this); |
232 | 47 | if (var != null) { |
233 | 8 | put(name, var); |
234 | 8 | return var; |
235 | |
} |
236 | |
else |
237 | 39 | return UNDEF; |
238 | |
} |
239 | 53 | else if (name instanceof FunctionCall) |
240 | |
{ |
241 | 53 | FunctionCall fc = (FunctionCall) name; |
242 | 53 | String fname = fc.getName(); |
243 | 53 | MethodWrapper func = null; |
244 | 53 | if (_funcs != null) { |
245 | 11 | func = (MethodWrapper) _funcs.get(fname); |
246 | |
} |
247 | 53 | if (func == null) |
248 | |
{ |
249 | 45 | func = _broker.getFunction(fname); |
250 | |
} |
251 | 53 | if (func != null) |
252 | |
{ |
253 | 53 | Object[] args = fc.getArguments(this); |
254 | 53 | ret = func.invoke(args); |
255 | 53 | } |
256 | |
else |
257 | |
{ |
258 | 0 | _log.error("Function " + fname + " was not loaded!"); |
259 | |
} |
260 | 53 | return ret; |
261 | |
} |
262 | |
else |
263 | |
{ |
264 | |
|
265 | 0 | return UNDEF; |
266 | |
} |
267 | |
} |
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
public final Object get (Object name) |
274 | |
{ |
275 | |
try |
276 | |
{ |
277 | 172 | Object o = internalGet(name); |
278 | 172 | if (o == UNDEF) |
279 | |
{ |
280 | 12 | return null; |
281 | |
} |
282 | 160 | return o; |
283 | |
} |
284 | 0 | catch (PropertyException e) |
285 | |
{ |
286 | |
|
287 | 0 | return null; |
288 | |
} |
289 | |
} |
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
public final Object put (Object name, Class c) |
296 | |
{ |
297 | 27 | if (c == null) |
298 | |
{ |
299 | 24 | return _variables.put(name, null); |
300 | |
} |
301 | |
else |
302 | |
{ |
303 | 3 | return _variables.put(name, new org.webmacro.engine.StaticClassWrapper(c)); |
304 | |
} |
305 | |
} |
306 | |
|
307 | |
|
308 | |
|
309 | |
|
310 | |
|
311 | |
|
312 | |
public final void putFunction (String name, Object instance, String methodName) |
313 | |
{ |
314 | 30 | MethodWrapper func = wrapMethod(instance, methodName); |
315 | 30 | if (_funcs == null) |
316 | 6 | _funcs = new HashMap(); |
317 | 30 | _funcs.put(name, func); |
318 | 30 | } |
319 | |
|
320 | |
public final void putGlobalFunction (String name, Object instance, String methodName) |
321 | |
{ |
322 | 0 | MethodWrapper func = wrapMethod(instance, methodName); |
323 | 0 | _broker.putFunction(name, func); |
324 | 0 | } |
325 | |
|
326 | |
private final MethodWrapper wrapMethod (Object instance, String methodName) |
327 | |
{ |
328 | 30 | MethodWrapper func = null; |
329 | |
try |
330 | |
{ |
331 | 30 | func = new MethodWrapper(instance, methodName); |
332 | |
} |
333 | 0 | catch (Exception e) |
334 | |
{ |
335 | 0 | String className = null; |
336 | 0 | if (instance instanceof Class) |
337 | |
{ |
338 | 0 | className = ((Class) instance).getName(); |
339 | |
} |
340 | 0 | else if (instance != null) |
341 | |
{ |
342 | 0 | className = instance.getClass().getName(); |
343 | |
} |
344 | 0 | _log.error("Unable to construct function from method: " |
345 | |
+ methodName + " of class " + className); |
346 | 30 | } |
347 | 30 | return func; |
348 | |
} |
349 | |
|
350 | |
|
351 | |
|
352 | |
|
353 | |
|
354 | |
|
355 | |
public final Object put (Object name, Object value) |
356 | |
{ |
357 | 6301 | return _variables.put(name, value); |
358 | |
} |
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | |
|
364 | |
|
365 | |
|
366 | |
protected Object internalGet (Object[] names) |
367 | |
throws PropertyException |
368 | |
{ |
369 | |
Object instance; |
370 | |
try |
371 | |
{ |
372 | 1457 | instance = internalGet(names[0]); |
373 | |
} |
374 | 0 | catch (ArrayIndexOutOfBoundsException e) |
375 | |
{ |
376 | 0 | throw new PropertyException( |
377 | |
"Attempt to access property with a zero length name array"); |
378 | 1457 | } |
379 | 1457 | if (names.length == 1) |
380 | |
{ |
381 | 51 | return instance; |
382 | |
} |
383 | 1406 | else if (instance == null) |
384 | |
{ |
385 | 0 | throw new PropertyException.NullValueException(names[0].toString()); |
386 | |
} |
387 | |
else |
388 | |
{ |
389 | 1406 | return _broker._propertyOperators.getProperty(this, instance, names, 1); |
390 | |
} |
391 | |
} |
392 | |
|
393 | |
|
394 | |
|
395 | |
|
396 | |
|
397 | |
|
398 | |
|
399 | |
|
400 | |
public final boolean set (Object[] names, Object value) |
401 | |
throws PropertyException |
402 | |
{ |
403 | 564 | if (names.length == 1) |
404 | |
{ |
405 | 0 | put(names[0], value); |
406 | 0 | return true; |
407 | |
} |
408 | |
else |
409 | |
{ |
410 | |
Object instance; |
411 | |
try |
412 | |
{ |
413 | 564 | instance = internalGet(names[0]); |
414 | |
} |
415 | 0 | catch (ArrayIndexOutOfBoundsException e) |
416 | |
{ |
417 | 0 | return false; |
418 | 564 | } |
419 | 564 | return _broker._propertyOperators.setProperty(this, instance, names, 1, value); |
420 | |
} |
421 | |
} |
422 | |
|
423 | |
|
424 | |
|
425 | |
|
426 | |
|
427 | |
public Object getProperty (Object name) throws PropertyException |
428 | |
{ |
429 | 8275 | return internalGet(name); |
430 | |
} |
431 | |
|
432 | |
|
433 | |
|
434 | |
|
435 | |
|
436 | |
public boolean setProperty (Object name, Object value) |
437 | |
throws PropertyException |
438 | |
{ |
439 | 5557 | put(name, value); |
440 | 5557 | return true; |
441 | |
} |
442 | |
|
443 | |
|
444 | |
|
445 | |
|
446 | |
|
447 | |
public Object getProperty (Object[] names) throws PropertyException |
448 | |
{ |
449 | 1457 | return internalGet(names); |
450 | |
} |
451 | |
|
452 | |
|
453 | |
|
454 | |
|
455 | |
|
456 | |
|
457 | |
public boolean setProperty (Object[] names, Object value) |
458 | |
throws PropertyException |
459 | |
{ |
460 | 564 | return set(names, value); |
461 | |
} |
462 | |
|
463 | |
|
464 | |
|
465 | |
|
466 | |
|
467 | |
public final void setMap (Map m) |
468 | |
{ |
469 | 32 | _variables = m; |
470 | 32 | } |
471 | |
|
472 | |
|
473 | |
|
474 | |
|
475 | |
public final Map getMap () |
476 | |
{ |
477 | 1695 | return _variables; |
478 | |
} |
479 | |
|
480 | |
|
481 | |
|
482 | |
|
483 | |
public boolean containsKey (Object key) |
484 | |
{ |
485 | 9200 | return _variables.containsKey(key); |
486 | |
} |
487 | |
|
488 | |
|
489 | |
|
490 | |
|
491 | |
public final boolean containsValue (Object value) |
492 | |
{ |
493 | 0 | return _variables.containsValue(value); |
494 | |
} |
495 | |
|
496 | |
|
497 | |
|
498 | |
|
499 | |
public final Set entrySet () |
500 | |
{ |
501 | 0 | return _variables.entrySet(); |
502 | |
} |
503 | |
|
504 | |
|
505 | |
|
506 | |
|
507 | |
public final boolean isEmpty () |
508 | |
{ |
509 | 0 | return _variables.isEmpty(); |
510 | |
} |
511 | |
|
512 | |
|
513 | |
|
514 | |
|
515 | |
public final Set keySet () |
516 | |
{ |
517 | 0 | return _variables.keySet(); |
518 | |
} |
519 | |
|
520 | |
|
521 | |
|
522 | |
|
523 | |
public final void putAll (Map t) |
524 | |
{ |
525 | 19 | _variables.putAll(t); |
526 | 19 | } |
527 | |
|
528 | |
|
529 | |
|
530 | |
|
531 | |
public final Object remove (Object key) |
532 | |
{ |
533 | 0 | return _variables.remove(key); |
534 | |
} |
535 | |
|
536 | |
|
537 | |
|
538 | |
|
539 | |
public final int size () |
540 | |
{ |
541 | 0 | return _variables.size(); |
542 | |
} |
543 | |
|
544 | |
|
545 | |
|
546 | |
|
547 | |
public final Collection values () |
548 | |
{ |
549 | 0 | return _variables.values(); |
550 | |
} |
551 | |
|
552 | |
|
553 | |
|
554 | |
|
555 | |
|
556 | |
|
557 | |
|
558 | |
|
559 | |
|
560 | |
public String toString () |
561 | |
{ |
562 | 0 | return _variables.toString(); |
563 | |
} |
564 | |
|
565 | |
|
566 | |
|
567 | |
public final void put (Object o, int i) |
568 | |
{ |
569 | 61 | put(o, new Integer(i)); |
570 | 61 | } |
571 | |
|
572 | |
public final void put (Object o, byte b) |
573 | |
{ |
574 | 1 | put(o, new Byte(b)); |
575 | 1 | } |
576 | |
|
577 | |
public final void put (Object o, short s) |
578 | |
{ |
579 | 1 | put(o, new Short(s)); |
580 | 1 | } |
581 | |
|
582 | |
public final void put (Object o, long l) |
583 | |
{ |
584 | 30 | put(o, new Long(l)); |
585 | 30 | } |
586 | |
|
587 | |
public final void put (Object o, char c) |
588 | |
{ |
589 | 1 | put(o, new Character(c)); |
590 | 1 | } |
591 | |
|
592 | |
public final void put (Object o, float f) |
593 | |
{ |
594 | 0 | put(o, new Float(f)); |
595 | 0 | } |
596 | |
|
597 | |
public final void put (Object o, double d) |
598 | |
{ |
599 | 1 | put(o, new Double(d)); |
600 | 1 | } |
601 | |
|
602 | |
public final void put (Object o, boolean b) |
603 | |
{ |
604 | 1 | put(o, new Boolean(b)); |
605 | 1 | } |
606 | |
} |