KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > scriptengine > core > util > DjScriptCompileTimeScope


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, is permitted provided that the following conditions are met: -
6  * Redistributions of source code must retain the above copyright notice, this
7  * list of conditions and the following disclaimer. - Redistributions in binary
8  * form must reproduce the above copyright notice, this list of conditions and
9  * the following disclaimer in the documentation and/or other materials
10  * provided with the distribution. - All advertising materials mentioning
11  * features or use of this software must display the following acknowledgment:
12  * "This product includes Djeneric." - Products derived from this software may
13  * not be called "Djeneric" nor may "Djeneric" appear in their names without
14  * prior written permission of Genimen BV. - Redistributions of any form
15  * whatsoever must retain the following acknowledgment: "This product includes
16  * Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG, OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.tools.scriptengine.core.util;
31
32 import java.util.HashMap JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.Stack JavaDoc;
35 import java.util.StringTokenizer JavaDoc;
36
37 import com.genimen.djeneric.language.Messages;
38 import com.genimen.djeneric.repository.DjExtent;
39 import com.genimen.djeneric.repository.DjPersistenceManager;
40 import com.genimen.djeneric.tools.scriptengine.core.ScriptRunnerContainer;
41 import com.genimen.djeneric.tools.scriptengine.core.SimpleNode;
42 import com.genimen.djeneric.tools.scriptengine.core.nodes.ActionNode;
43 import com.genimen.djeneric.tools.scriptengine.core.nodes.ControllerNode;
44 import com.genimen.djeneric.tools.scriptengine.core.nodes.ScriptNode;
45
46 public class DjScriptCompileTimeScope
47 {
48
49   VariableStack _contextStack = new VariableStack();
50   protected DjPersistenceManager _mgr;
51   Stack JavaDoc _cIndexStack = new Stack JavaDoc();
52   FunctionSupport _functionSupport = null;
53   ScriptRunnerContainer _container = null;
54   ControllerNode _controller = null;
55   ActionNode[] _actions = new ActionNode[0];
56   HashMap JavaDoc _actionsHashed = new HashMap JavaDoc();
57   protected ScriptNode _scriptNode;
58   private HashMap JavaDoc _knownJavaBeans = new HashMap JavaDoc();
59
60   public DjScriptCompileTimeScope(ScriptNode scriptNode, DjPersistenceManager mgr)
61   {
62     _mgr = mgr;
63     _scriptNode = scriptNode;
64   }
65
66   public HashMap JavaDoc getVariableTypes()
67   {
68     return _contextStack.getVariableTypes();
69   }
70
71   public Variable pushVariable(String JavaDoc name, Object JavaDoc obj)
72   {
73     return _contextStack.push(name, obj);
74   }
75
76   public Variable pushVariable(Variable co)
77   {
78     return (Variable) _contextStack.push(co);
79   }
80
81   public void mark()
82   {
83     _cIndexStack.push(new Integer JavaDoc(_contextStack.size()));
84   }
85
86   public void releaseToMark()
87   {
88     int c = ((Integer JavaDoc) _cIndexStack.pop()).intValue();
89
90     _contextStack.setSize(c);
91   }
92
93   public VariableStack getVariableStack()
94   {
95     return _contextStack;
96   }
97
98   public DjPersistenceManager getPersistenceManager()
99   {
100     return _mgr;
101   }
102
103   public ControllerNode getController()
104   {
105     return _controller;
106   }
107
108   public void setController(ControllerNode node)
109   {
110     _controller = node;
111   }
112
113   public ActionNode findAction(String JavaDoc actionName)
114   {
115     return (ActionNode) _actionsHashed.get(actionName);
116   }
117
118   public ActionNode getAction(String JavaDoc actionName, SimpleNode someContextNode) throws DjScriptExecutionException
119   {
120     ActionNode action = findAction(actionName);
121     if (action == null) throw new DjScriptExecutionException(Messages
122         .getString("ScriptExecutionContext.ActionWithName", actionName), someContextNode);
123     return action;
124   }
125
126   public ActionNode[] getActions()
127   {
128     return _actions;
129   }
130
131   public void setActions(ActionNode[] nodes)
132   {
133     _actionsHashed = new HashMap JavaDoc();
134     for (int i = 0; i < nodes.length; i++)
135     {
136       _actionsHashed.put(nodes[i].getActionName(), nodes[i]);
137     }
138     _actions = nodes;
139   }
140
141   public void setContainer(ScriptRunnerContainer container)
142   {
143     _container = container;
144   }
145
146   public ScriptRunnerContainer getContainer()
147   {
148     return _container;
149   }
150
151   public FunctionSupport getFunctionSupport()
152   {
153     if (_functionSupport == null) _functionSupport = new DefaultFunctions();
154     return _functionSupport;
155   }
156
157   public Variable getVariable(String JavaDoc name, SimpleNode ctxtNode) throws DjScriptExecutionException
158   {
159     Variable o = getVariableStack().getVariable(name);
160     if (o == null) throw new DjScriptExecutionException(Messages
161         .getString("ScriptExecutionContext.VariableNames", name), ctxtNode);
162     return o;
163   }
164
165   public Variable lookupVariable(String JavaDoc name)
166   {
167     return getVariableStack().getVariable(name);
168   }
169
170   public boolean isVariable(String JavaDoc name)
171   {
172     int idx = name.indexOf(".");
173     if (idx != -1)
174     {
175       name = name.substring(0, idx);
176     }
177     idx = name.indexOf("[");
178     if (idx != -1)
179     {
180       name = name.substring(0, idx);
181     }
182
183     return getVariableStack().getVariable(name) != null;
184   }
185
186   public Object JavaDoc getValue(String JavaDoc name)
187   {
188     return getVariableStack().getValue(name);
189   }
190
191   public ScriptNode getScriptNode()
192   {
193     return _scriptNode;
194   }
195
196   public boolean isKnownJavaBean(String JavaDoc alias)
197   {
198     return _knownJavaBeans.get(alias) != null;
199   }
200
201   public void addJavaBean(String JavaDoc alias, String JavaDoc typeName)
202   {
203     _knownJavaBeans.put(alias, typeName);
204   }
205
206   public String JavaDoc getJavaBeanTypeName(String JavaDoc alias)
207   {
208     return (String JavaDoc) _knownJavaBeans.get(alias);
209   }
210
211   public String JavaDoc getPropertyType(String JavaDoc path, SimpleNode caller) throws DjScriptExecutionException
212   {
213
214     try
215     {
216       if (getFunctionSupport().isStaticCall(path))
217       {
218         return "java.lang.Object";
219       }
220
221       StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(path, ".");
222
223       String JavaDoc varName = st.nextToken();
224
225       // Ignore the index specification if there is one
226
int brackIdx = varName.indexOf('[');
227       boolean isIndexed = brackIdx != -1;
228       if (isIndexed)
229       {
230         varName = varName.substring(0, brackIdx);
231       }
232
233       Variable var = lookupVariable(varName);
234       if (var == null)
235       {
236         String JavaDoc jt = getJavaBeanTypeName(varName);
237         if (jt != null && !st.hasMoreElements()) return jt;
238         if (jt != null) return "java.lang.Object";
239         throw new DjScriptExecutionException(Messages.getString("ScriptExecutionContext.VariableNames", varName),
240             caller);
241       }
242
243       String JavaDoc typeName = var.getTypeName();
244
245       if (!st.hasMoreElements()) return typeName;
246
247       String JavaDoc propertyPath = path.substring(path.indexOf(".") + 1);
248
249       DjExtent ext = getPersistenceManager().getExtentByObjectType(typeName);
250       return ext.getPropertyType(propertyPath);
251
252     }
253     catch (Exception JavaDoc e)
254     {
255       throw new DjScriptExecutionException(e, caller);
256     }
257
258   }
259
260   public void declareGlobals(HashMap JavaDoc variables)
261   {
262     Iterator JavaDoc it = variables.keySet().iterator();
263     while (it.hasNext())
264     {
265       String JavaDoc key = it.next().toString();
266       pushVariable(new Variable(key, null, variables.get(key).toString()));
267     }
268   }
269
270 }
Popular Tags