KickJava   Java API By Example, From Geeks To Geeks.

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


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