KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jicengine > element > impl > CollectionElementCompiler


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 /**
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 JavaDoc name, Location location) throws ElementException
29   {
30     super(name, location);
31   }
32
33   public void setInstanceClass(String JavaDoc 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 JavaDoc 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 JavaDoc execute(Context context) throws OperationException
58     {
59       Collection parent = (Collection) org.jicengine.operation.VariableValueOperation.lookup(Element.VARIABLE_NAME_PARENT_INSTANCE, context);
60       Object JavaDoc elementInstance = org.jicengine.operation.VariableValueOperation.lookup(Element.VARIABLE_NAME_ELEMENT_INSTANCE, context);
61       return new Boolean JavaDoc(parent.add(elementInstance));
62     }
63
64   };
65 }
66
Popular Tags