KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jicengine.element.impl;
2
3 import org.jicengine.element.*;
4 import org.jicengine.expression.FactoryInvocationParser;
5 import org.jicengine.operation.*;
6
7 /**
8  * <p> </p>
9  *
10  * <p> </p>
11  *
12  * <p> </p>
13  *
14  * <p> </p>
15  *
16  * @author timo laitinen
17  */

18 public class FactoryElementCompiler extends ElementCompiler {
19
20     public FactoryElementCompiler(String JavaDoc name, Location location) throws ElementException
21     {
22         this(name, location, new String JavaDoc[0]);
23     }
24
25
26     public FactoryElementCompiler(String JavaDoc name, Location location, String JavaDoc[] parameters) throws ElementException
27     {
28         super(name, location);
29
30         String JavaDoc[] parameterNames = new String JavaDoc[parameters.length];
31         Class JavaDoc[] parameterTypes = new Class JavaDoc[parameters.length];
32
33         for (int i = 0; i < parameters.length; i++) {
34             try {
35                 String JavaDoc paramDef = parameters[i].trim();
36                 int spaceIndex = paramDef.indexOf(" ");
37                 parameterTypes[i] = org.jicengine.expression.ClassParser.toClass(paramDef.substring(0, spaceIndex));
38                 parameterNames[i] = paramDef.substring(spaceIndex + 1).trim();
39             } catch (Exception JavaDoc e){
40                 throw new ElementException("Problems with parameter definition '" + parameters[i] + "'. Expected format '[class] [name]'", getName(), getLocation());
41             }
42         }
43         getElement().setAction(new RegisterFactoryOperation(name,parameterNames,parameterTypes, FactoryInvocationParser.FACTORY_NAME_PREFIX + name));
44     }
45
46     /**
47      *
48      * @param child VariableElement
49      * @throws ElementException
50      * @return ActionElement
51      */

52     protected ActionElement handleLooseVariableElement(final VariableElement child) throws ElementException
53     {
54         if( ((RegisterFactoryOperation)getElement().getAction()).getFactoryElement() != null ){
55             throw new ElementException("Illegal child " + child + ". Only single element is allowed inside element of type 'factory'.", getName(), getLocation());
56         }
57
58         ((RegisterFactoryOperation)getElement().getAction()).setFactoryElement(child);
59
60         // return a dummy action element in return..
61
return new ActionElement(){
62             public void execute(Context context, Object JavaDoc parentInstance)
63             {
64             }
65             public String JavaDoc getName()
66             {
67                 return child.getName();
68             }
69             public Location getLocation()
70             {
71                 return child.getLocation();
72             }
73             public boolean isExecuted(Context outerContext, Object JavaDoc parentInstance)
74             {
75                 return false;
76             }
77         };
78
79     }
80
81
82   /*
83     private static Context getRootContext(Context context)
84     {
85         if( context instanceof LocalContext){
86             return getRootContext(((LocalContext)context).getParent());
87         }
88         else {
89             return context;
90         }
91     }
92   */

93 }
94
Popular Tags