01: package org.jicengine.element.impl;
02:
03: import org.jicengine.expression.SyntaxException;
04: import org.jicengine.operation.Operation;
05: import org.jicengine.operation.Context;
06: import org.jicengine.operation.OperationException;
07: import org.jicengine.element.*;
08:
09: import java.util.Collection;
10: import java.util.Map;
11:
12: /**
13: * <p>
14: * Copyright (C) 2004 Timo Laitinen
15: * </p>
16: *
17: * @author .timo
18: */
19:
20: public class MapCompiler extends ElementCompiler {
21:
22: public MapCompiler(String name, Location location) {
23: super (name, location);
24: }
25:
26: public void setInstanceClass(String className)
27: throws ElementException {
28: super .setInstanceClass(className);
29:
30: if (!Map.class
31: .isAssignableFrom(getElement().getInstanceClass())) {
32: throw new ElementException("Class '"
33: + getElement().getInstanceClass().getName()
34: + "' is not a Map.", getElement().getName(),
35: getLocation());
36: }
37: }
38:
39: protected ActionElement handleLooseVariableElement(
40: VariableElement child)
41: throws org.jicengine.element.ElementException {
42: final String childName = child.getName();
43:
44: // construct an Operation object corresponding expression:
45: // parent.put(childName,this)
46: //
47: Operation putToMapOperation = new Operation() {
48: public boolean needsParameter(String name) {
49: return (name
50: .equals(Element.VARIABLE_NAME_PARENT_INSTANCE) || name
51: .equals(Element.VARIABLE_NAME_ELEMENT_INSTANCE));
52: }
53:
54: public boolean needsParameters() {
55: return true;
56: }
57:
58: public Object execute(Context context)
59: throws OperationException {
60: Map parent = (Map) org.jicengine.operation.VariableValueOperation
61: .lookup(Element.VARIABLE_NAME_PARENT_INSTANCE,
62: context);
63: Object elementInstance = org.jicengine.operation.VariableValueOperation
64: .lookup(Element.VARIABLE_NAME_ELEMENT_INSTANCE,
65: context);
66: return parent.put(childName, elementInstance);
67: }
68:
69: };
70:
71: return new WrapperActionElement(child, child.getLocation(),
72: putToMapOperation);
73: }
74: }
|