1 package org.jicengine.element.impl; 2 3 import org.jicengine.expression.SyntaxException; 4 import org.jicengine.operation.Context; 5 import org.jicengine.operation.Operation; 6 import org.jicengine.operation.OperationException; 7 8 import org.jicengine.element.*; 9 10 import java.util.*; 11 12 23 24 public class CollectionElementCompiler extends ElementCompiler { 25 26 private static Operation addToCollectionOperation = new AddToCollectionOperation(); 27 28 public CollectionElementCompiler(String name, Location location) throws ElementException 29 { 30 super(name, location); 31 } 32 33 public void setInstanceClass(String className) throws ElementException 34 { 35 super.setInstanceClass(className); 36 37 if( !Collection.class.isAssignableFrom(getElement().getInstanceClass())){ 38 throw new ElementException("Class '" + getElement().getInstanceClass().getName() + "' is not a Collection.",getElement().getName(), getLocation()); 39 } 40 } 41 42 protected ActionElement handleLooseVariableElement(VariableElement child) throws ElementException 43 { 44 return new org.jicengine.element.WrapperActionElement(child, getLocation(), addToCollectionOperation); 45 } 46 47 static class AddToCollectionOperation implements Operation { 48 public boolean needsParameter(String name) 49 { 50 return (name.equals(Element.VARIABLE_NAME_PARENT_INSTANCE) || name.equals(Element.VARIABLE_NAME_ELEMENT_INSTANCE) ); 51 } 52 public boolean needsParameters() 53 { 54 return true; 55 } 56 57 public Object execute(Context context) throws OperationException 58 { 59 Collection parent = (Collection) org.jicengine.operation.VariableValueOperation.lookup(Element.VARIABLE_NAME_PARENT_INSTANCE, context); 60 Object elementInstance = org.jicengine.operation.VariableValueOperation.lookup(Element.VARIABLE_NAME_ELEMENT_INSTANCE, context); 61 return new Boolean (parent.add(elementInstance)); 62 } 63 64 }; 65 } 66 | Popular Tags |