KickJava   Java API By Example, From Geeks To Geeks.

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


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.StringTokenizer JavaDoc;
34
35 import com.genimen.djeneric.language.Messages;
36 import com.genimen.djeneric.repository.DjExtent;
37 import com.genimen.djeneric.repository.DjList;
38 import com.genimen.djeneric.repository.DjObject;
39 import com.genimen.djeneric.repository.DjPersistenceManager;
40 import com.genimen.djeneric.repository.DjSession;
41 import com.genimen.djeneric.repository.DjValueObject;
42 import com.genimen.djeneric.repository.exceptions.DjenericException;
43 import com.genimen.djeneric.repository.exceptions.ObjectNotDefinedException;
44 import com.genimen.djeneric.tools.scriptengine.core.SimpleNode;
45 import com.genimen.djeneric.tools.scriptengine.core.nodes.ScriptNode;
46
47 public class DjScriptExecutionTimeScope extends DjScriptCompileTimeScope implements DjScriptExecutionContext
48 {
49   protected DjSession _session;
50   protected HashMap JavaDoc _parameters = new HashMap JavaDoc();
51
52   public DjScriptExecutionTimeScope(ScriptNode scriptNode, DjPersistenceManager mgr, DjSession thisSession)
53       throws DjenericException
54   {
55     super(scriptNode, mgr);
56     if (thisSession == null) _session = mgr.createSession();
57     else _session = thisSession;
58   }
59
60   public DjSession getSession()
61   {
62     return _session;
63   }
64
65   public void setParameter(String JavaDoc name, Object JavaDoc value)
66   {
67     _parameters.put(name, value);
68   }
69
70   public void setParameters(HashMap JavaDoc parameters)
71   {
72     _parameters = (HashMap JavaDoc) parameters.clone();
73   }
74
75   public Object JavaDoc getParameter(String JavaDoc name)
76   {
77     return _parameters.get(name);
78   }
79
80   public void clearPointerVariables(Object JavaDoc o)
81   {
82     VariableStack stack = getVariableStack();
83     for (int i = 0; i < stack.size(); i++)
84     {
85       Variable v = (Variable) stack.elementAt(i);
86       if (v.getObject() == o) v.setObject(null);
87     }
88   }
89
90   public Object JavaDoc getPropertyValue(String JavaDoc path, SimpleNode contextNode) throws DjScriptExecutionException
91   {
92     StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(path, ".");
93
94     String JavaDoc curId = st.nextToken();
95
96     String JavaDoc varName = curId;
97
98     // Ignore the index specification if there is one
99
int brackIdx = varName.indexOf('[');
100     int idxValue = 0;
101     boolean isIndexed = brackIdx != -1;
102     if (isIndexed)
103     {
104       idxValue = getIndexValue(varName);
105       varName = varName.substring(0, brackIdx);
106     }
107
108     Variable var = lookupVariable(varName);
109
110     if (var == null)
111     {
112       if (st.hasMoreElements()) throw new DjScriptExecutionException(Messages
113           .getString("ScriptExecutionContext.VariableNames", varName), contextNode);
114
115       return addAllObjectsFromExtent(path, contextNode, curId);
116     }
117
118     Object JavaDoc obj;
119
120     if (isIndexed) obj = var.getObject(idxValue);
121     else obj = var.getObject();
122
123     if (!st.hasMoreElements()) return obj;
124
125     String JavaDoc propertyPath = path.substring(path.indexOf(".") + 1);
126
127     if (obj == null) throw new DjScriptExecutionException(Messages.getString("ScriptExecutionContext.ValueIsNull",
128                                                                              varName, propertyPath), contextNode);
129
130     if (obj instanceof DjValueObject || obj instanceof DjList) return evaluateDjenericPath(varName, contextNode, obj,
131                                                                                            propertyPath);
132     else return getJavaProperty(contextNode, obj, propertyPath);
133
134   }
135
136   private Object JavaDoc getJavaProperty(SimpleNode contextNode, Object JavaDoc obj, String JavaDoc propertyPath)
137       throws DjScriptExecutionException
138   {
139     try
140     {
141       throw new DjScriptExecutionException("Java properties currently not supported", contextNode);
142       // return PropertyUtils.getNestedProperty(obj, propertyPath);
143
}
144     catch (Exception JavaDoc e)
145     {
146       throw new DjScriptExecutionException(e, contextNode);
147     }
148   }
149
150   private Object JavaDoc evaluateDjenericPath(String JavaDoc varName, SimpleNode contextNode, Object JavaDoc obj, String JavaDoc property)
151       throws DjScriptExecutionException
152   {
153     try
154     {
155       if (obj instanceof DjList) throw new DjScriptExecutionException(Messages.getString("ObjectPath.IdentifierSet",
156                                                                                          varName), contextNode);
157
158       if (!(obj instanceof DjValueObject)) throw new DjScriptExecutionException(Messages
159           .getString("ObjectPath.IdentifierNotModel", property), contextNode);
160
161       DjValueObject djo = (DjValueObject) obj;
162       return djo.get(property);
163
164     }
165     catch (DjenericException onde)
166     {
167       throw new DjScriptExecutionException(onde.getMessage(), contextNode);
168     }
169   }
170
171   private Object JavaDoc addAllObjectsFromExtent(String JavaDoc path, SimpleNode contextNode, String JavaDoc curId)
172       throws DjScriptExecutionException
173   {
174     try
175     {
176       DjExtent extent = getPersistenceManager().getExtent(curId);
177       return getSession().getObjects(extent);
178     }
179     catch (ObjectNotDefinedException onde)
180     {
181       // ingore; throw a more descriptive exception below
182
}
183     catch (DjenericException de)
184     {
185       throw new DjScriptExecutionException(de.getMessage());
186     }
187     String JavaDoc msg;
188     if (path.indexOf('.') != -1) msg = Messages.getString("ObjectPath.Identifier", curId, path);
189     else msg = Messages.getString("ObjectPath.NotDefined", curId);
190     throw new DjScriptExecutionException(msg, contextNode);
191   }
192
193   public void setPropertyValue(String JavaDoc path, Object JavaDoc value, SimpleNode contextNode) throws DjScriptExecutionException
194   {
195     StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(path, ".");
196
197     String JavaDoc curId = st.nextToken();
198
199     String JavaDoc varName = curId;
200
201     // Ignore the index specification if there is one
202
int brackIdx = varName.indexOf('[');
203     int idxValue = 0;
204     boolean isIndexed = brackIdx != -1;
205     if (isIndexed)
206     {
207       idxValue = getIndexValue(varName);
208       varName = varName.substring(0, brackIdx);
209     }
210
211     Variable variable = getVariable(varName, contextNode);
212
213     if (!st.hasMoreElements())
214     {
215       if (isIndexed) variable.assign(idxValue, value, contextNode);
216       else variable.assign(value, contextNode);
217     }
218     else
219     {
220
221       String JavaDoc propertyPath = path.substring(path.indexOf(".") + 1);
222
223       Object JavaDoc obj = getValue(varName);
224       if (obj == null)
225       {
226         throw new DjScriptExecutionException(Messages.getString("ScriptExecutionContext.ValueIsNull", varName,
227                                                                 propertyPath), contextNode);
228       }
229
230       // Is this an assigment of normal stuff or assignment of a Java bean?
231
if (!(obj instanceof DjObject) && !(obj instanceof DjList))
232       {
233         setJavaProperty(contextNode, obj, propertyPath, value);
234       }
235       else
236       {
237         setProperty(contextNode, obj, propertyPath, value);
238       }
239     }
240   }
241
242   private void setProperty(SimpleNode contextNode, Object JavaDoc obj, String JavaDoc propertyPath, Object JavaDoc value)
243       throws DjScriptExecutionException
244   {
245     try
246     {
247       if (obj instanceof DjList) obj = getFirstOfList(obj);
248
249       if (!(obj instanceof DjValueObject)) throw new DjScriptExecutionException(Messages
250           .getString("ObjectPath.IdentifierNotModel", propertyPath), contextNode);
251
252       DjValueObject djo = (DjValueObject) obj;
253       djo.set(propertyPath, value);
254     }
255     catch (DjenericException onde)
256     {
257       throw new DjScriptExecutionException(onde, contextNode);
258     }
259   }
260
261   private void setJavaProperty(SimpleNode contextNode, Object JavaDoc obj, String JavaDoc propertyPath, Object JavaDoc value)
262       throws DjScriptExecutionException
263   {
264     try
265     {
266       throw new DjScriptExecutionException("Java properties currently not supported", contextNode);
267
268       // BeanUtils.setProperty(obj, propertyPath.toString(), value);
269
}
270     catch (Exception JavaDoc x)
271     {
272       throw new DjScriptExecutionException(x, contextNode);
273     }
274   }
275
276   private DjObject getFirstOfList(Object JavaDoc obj)
277   {
278     DjList lst = (DjList) obj;
279     if (lst.size() > 0) obj = lst.get(0);
280     else obj = null;
281     return (DjObject) obj;
282   }
283
284   private int getIndexValue(String JavaDoc varName)
285   {
286     int brackIdx = varName.indexOf('[');
287     int brackEndIdx = varName.indexOf(']');
288     return Integer.parseInt(varName.substring(brackIdx + 1, brackEndIdx));
289   }
290 }
Popular Tags