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