001: package org.jicengine.element.impl;
002:
003: import org.jicengine.element.*;
004: import org.jicengine.operation.*;
005: import java.util.*;
006:
007: /**
008: * <p> </p>
009: *
010: * <p> </p>
011: *
012: * <p> </p>
013: *
014: * <p> </p>
015: *
016: * @author timo laitinen
017: */
018: public class SwitchCompiler extends ElementCompiler {
019:
020: public SwitchCompiler(String name, Location location,
021: String[] parameters) throws ElementException {
022: super (name, location);
023:
024: getElement().setConstructor(new Chooser(parameters[0]));
025: }
026:
027: /**
028: *
029: * @param child VariableElement
030: * @throws ElementException
031: * @return ActionElement
032: */
033: protected ActionElement handleLooseVariableElement(
034: final VariableElement child) throws ElementException {
035: // check that the element has no if-attribute
036: // handle the variable as a different case.
037:
038: // we give the element directly to the Chooser
039: ((Chooser) getElement().getConstructor()).addCase(child);
040:
041: // return a dummy action element in return..
042: return new ActionElement() {
043: public void execute(Context context, Object parentInstance) {
044: }
045:
046: public String getName() {
047: return child.getName();
048: }
049:
050: public Location getLocation() {
051: return child.getLocation();
052: }
053:
054: public boolean isExecuted(Context outerContext,
055: Object parentInstance) {
056: return false;
057: }
058: };
059: }
060:
061: public class Chooser implements Operation {
062: private String variable;
063: private List caseNames = new ArrayList();
064: private List cases = new ArrayList();
065: private VariableElement defaultCase;
066:
067: public Chooser(String variable) {
068: this .variable = variable;
069: }
070:
071: public void addCase(VariableElement child) {
072: if (child.getName().equals("default")) {
073: this .defaultCase = child;
074: } else {
075: this .cases.add(child);
076: this .caseNames.add(child.getName());
077: }
078: }
079:
080: public boolean needsParameters() {
081: return true;
082: }
083:
084: public boolean needsParameter(String name) {
085: return name.equals(this .variable);
086: }
087:
088: public Object execute(Context context)
089: throws OperationException {
090: Object variableValue = new VariableValueOperation(
091: this .variable).execute(context);
092:
093: String stringValue = String.valueOf(variableValue);
094:
095: VariableElement selected = null;
096: for (int i = 0; i < this .caseNames.size(); i++) {
097: String candidate = this .caseNames.get(i).toString();
098: if (candidate.equals(stringValue)) {
099: // match
100: selected = (VariableElement) this .cases.get(i);
101: break;
102: }
103: }
104:
105: if (selected == null) {
106: if (this .defaultCase != null) {
107: selected = this .defaultCase;
108: } else {
109: throw new OperationException("Case '" + stringValue
110: + "' not found among " + this .caseNames);
111: }
112: }
113: Context elementContext = ((LocalContext) context)
114: .getParent();
115: try {
116: Object result = selected.getValue(elementContext, null);
117: return result;
118: } catch (ElementException e) {
119: throw new OperationException(e.getMessage(), e);
120: }
121: }
122: }
123:
124: }
|