| 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 | 2 | static Logger _log = LoggerFactory.getLogger(Context.class); |
| 63 | |
|
| 64 | |
private final Broker _broker; |
| 65 | 2141 | private HashMap _funcs = null; |
| 66 | |
|
| 67 | |
private EvaluationExceptionHandler _eeHandler; |
| 68 | |
|
| 69 | 2141 | private Map _variables = new HashMap(); |
| 70 | |
|
| 71 | 2141 | private TemplateEvaluationContext _teContext |
| 72 | |
= new TemplateEvaluationContext(); |
| 73 | |
|
| 74 | 2 | private static final org.webmacro.engine.UndefinedMacro UNDEF |
| 75 | |
= org.webmacro.engine.UndefinedMacro.getInstance(); |
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
public Context() throws InitException { |
| 81 | 0 | this(Broker.getBroker()); |
| 82 | 0 | } |
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
public Context (Broker broker) |
| 88 | 2141 | { |
| 89 | 2141 | _broker = broker; |
| 90 | 2141 | } |
| 91 | |
|
| 92 | |
|
| 93 | 2141 | 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 | 2170 | return _broker; |
| 158 | |
} |
| 159 | |
|
| 160 | |
public final TemplateEvaluationContext getTemplateEvaluationContext () |
| 161 | |
{ |
| 162 | 57550 | return _teContext; |
| 163 | |
} |
| 164 | |
|
| 165 | |
public final String getCurrentLocation () |
| 166 | |
{ |
| 167 | 62 | StringBuffer loc = new StringBuffer(); |
| 168 | 62 | loc.append(_teContext._templateName == null ? "(unknown)" : _teContext._templateName); |
| 169 | 62 | loc.append(":").append(_teContext._lineNo).append(".").append(_teContext._columnNo); |
| 170 | 62 | 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 | 4 | return _log; |
| 190 | |
} |
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
public EvaluationExceptionHandler getEvaluationExceptionHandler () |
| 196 | |
{ |
| 197 | 78 | if (_eeHandler != null) |
| 198 | |
{ |
| 199 | 48 | return _eeHandler; |
| 200 | |
} |
| 201 | |
else |
| 202 | |
{ |
| 203 | 30 | return _broker.getEvaluationExceptionHandler(); |
| 204 | |
} |
| 205 | |
} |
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
public void setEvaluationExceptionHandler (EvaluationExceptionHandler eeh) |
| 212 | |
{ |
| 213 | 2823 | _eeHandler = eeh; |
| 214 | 2823 | } |
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
protected Object internalGet (Object name) |
| 224 | |
throws PropertyException |
| 225 | |
{ |
| 226 | 166694 | Object ret = _variables.get(name); |
| 227 | 166694 | if (ret != null || _variables.containsKey(name)) |
| 228 | 154490 | return ret; |
| 229 | |
|
| 230 | 12204 | if (name instanceof String) { |
| 231 | 12204 | Object var = _broker.getAutoContextVariable((String) name, this); |
| 232 | 12204 | if (var != null) { |
| 233 | 1160 | put(name, var); |
| 234 | 1160 | return var; |
| 235 | |
} |
| 236 | |
else |
| 237 | 11044 | return UNDEF; |
| 238 | |
} |
| 239 | 0 | else if (name instanceof FunctionCall) |
| 240 | |
{ |
| 241 | 0 | FunctionCall fc = (FunctionCall) name; |
| 242 | 0 | String fname = fc.getName(); |
| 243 | 0 | MethodWrapper func = null; |
| 244 | 0 | if (_funcs != null) { |
| 245 | 0 | func = (MethodWrapper) _funcs.get(fname); |
| 246 | |
} |
| 247 | 0 | if (func == null) |
| 248 | |
{ |
| 249 | 0 | func = _broker.getFunction(fname); |
| 250 | |
} |
| 251 | 0 | if (func != null) |
| 252 | |
{ |
| 253 | 0 | Object[] args = fc.getArguments(this); |
| 254 | 0 | ret = func.invoke(args); |
| 255 | 0 | } |
| 256 | |
else |
| 257 | |
{ |
| 258 | 0 | _log.error("Function " + fname + " was not loaded!"); |
| 259 | |
} |
| 260 | 0 | 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 | 8 | Object o = internalGet(name); |
| 278 | 8 | if (o == UNDEF) |
| 279 | |
{ |
| 280 | 0 | return null; |
| 281 | |
} |
| 282 | 8 | 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 | 0 | if (c == null) |
| 298 | |
{ |
| 299 | 0 | return _variables.put(name, null); |
| 300 | |
} |
| 301 | |
else |
| 302 | |
{ |
| 303 | 0 | 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 | 0 | MethodWrapper func = wrapMethod(instance, methodName); |
| 315 | 0 | if (_funcs == null) |
| 316 | 0 | _funcs = new HashMap(); |
| 317 | 0 | _funcs.put(name, func); |
| 318 | 0 | } |
| 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 | 0 | MethodWrapper func = null; |
| 329 | |
try |
| 330 | |
{ |
| 331 | 0 | 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 | 0 | } |
| 347 | 0 | return func; |
| 348 | |
} |
| 349 | |
|
| 350 | |
|
| 351 | |
|
| 352 | |
|
| 353 | |
|
| 354 | |
|
| 355 | |
public final Object put (Object name, Object value) |
| 356 | |
{ |
| 357 | 35984 | 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 | 106792 | 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 | 106792 | } |
| 379 | 106792 | if (names.length == 1) |
| 380 | |
{ |
| 381 | 0 | return instance; |
| 382 | |
} |
| 383 | 106792 | else if (instance == null) |
| 384 | |
{ |
| 385 | 0 | throw new PropertyException.NullValueException(names[0].toString()); |
| 386 | |
} |
| 387 | |
else |
| 388 | |
{ |
| 389 | 106792 | 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 | 192 | 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 | 192 | instance = internalGet(names[0]); |
| 414 | |
} |
| 415 | 0 | catch (ArrayIndexOutOfBoundsException e) |
| 416 | |
{ |
| 417 | 0 | return false; |
| 418 | 192 | } |
| 419 | 192 | 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 | 59702 | return internalGet(name); |
| 430 | |
} |
| 431 | |
|
| 432 | |
|
| 433 | |
|
| 434 | |
|
| 435 | |
|
| 436 | |
public boolean setProperty (Object name, Object value) |
| 437 | |
throws PropertyException |
| 438 | |
{ |
| 439 | 28206 | put(name, value); |
| 440 | 28206 | return true; |
| 441 | |
} |
| 442 | |
|
| 443 | |
|
| 444 | |
|
| 445 | |
|
| 446 | |
|
| 447 | |
public Object getProperty (Object[] names) throws PropertyException |
| 448 | |
{ |
| 449 | 106792 | return internalGet(names); |
| 450 | |
} |
| 451 | |
|
| 452 | |
|
| 453 | |
|
| 454 | |
|
| 455 | |
|
| 456 | |
|
| 457 | |
public boolean setProperty (Object[] names, Object value) |
| 458 | |
throws PropertyException |
| 459 | |
{ |
| 460 | 192 | return set(names, value); |
| 461 | |
} |
| 462 | |
|
| 463 | |
|
| 464 | |
|
| 465 | |
|
| 466 | |
|
| 467 | |
public final void setMap (Map m) |
| 468 | |
{ |
| 469 | 0 | _variables = m; |
| 470 | 0 | } |
| 471 | |
|
| 472 | |
|
| 473 | |
|
| 474 | |
|
| 475 | |
public final Map getMap () |
| 476 | |
{ |
| 477 | 224 | return _variables; |
| 478 | |
} |
| 479 | |
|
| 480 | |
|
| 481 | |
|
| 482 | |
|
| 483 | |
public boolean containsKey (Object key) |
| 484 | |
{ |
| 485 | 5834 | 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 | 0 | _variables.putAll(t); |
| 526 | 0 | } |
| 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 | 0 | put(o, new Integer(i)); |
| 570 | 0 | } |
| 571 | |
|
| 572 | |
public final void put (Object o, byte b) |
| 573 | |
{ |
| 574 | 0 | put(o, new Byte(b)); |
| 575 | 0 | } |
| 576 | |
|
| 577 | |
public final void put (Object o, short s) |
| 578 | |
{ |
| 579 | 0 | put(o, new Short(s)); |
| 580 | 0 | } |
| 581 | |
|
| 582 | |
public final void put (Object o, long l) |
| 583 | |
{ |
| 584 | 0 | put(o, new Long(l)); |
| 585 | 0 | } |
| 586 | |
|
| 587 | |
public final void put (Object o, char c) |
| 588 | |
{ |
| 589 | 0 | put(o, new Character(c)); |
| 590 | 0 | } |
| 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 | 0 | put(o, new Double(d)); |
| 600 | 0 | } |
| 601 | |
|
| 602 | |
public final void put (Object o, boolean b) |
| 603 | |
{ |
| 604 | 0 | put(o, new Boolean(b)); |
| 605 | 0 | } |
| 606 | |
} |