1 30 package com.genimen.djeneric.tools.scriptengine.core.util; 31 32 import java.util.HashMap ; 33 import java.util.StringTokenizer ; 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 _parameters = new HashMap (); 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 name, Object value) 66 { 67 _parameters.put(name, value); 68 } 69 70 public void setParameters(HashMap parameters) 71 { 72 _parameters = (HashMap ) parameters.clone(); 73 } 74 75 public Object getParameter(String name) 76 { 77 return _parameters.get(name); 78 } 79 80 public void clearPointerVariables(Object 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 getPropertyValue(String path, SimpleNode contextNode) throws DjScriptExecutionException 91 { 92 StringTokenizer st = new StringTokenizer (path, "."); 93 94 String curId = st.nextToken(); 95 96 String varName = curId; 97 98 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 obj; 119 120 if (isIndexed) obj = var.getObject(idxValue); 121 else obj = var.getObject(); 122 123 if (!st.hasMoreElements()) return obj; 124 125 String 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 getJavaProperty(SimpleNode contextNode, Object obj, String propertyPath) 137 throws DjScriptExecutionException 138 { 139 try 140 { 141 throw new DjScriptExecutionException("Java properties currently not supported", contextNode); 142 } 144 catch (Exception e) 145 { 146 throw new DjScriptExecutionException(e, contextNode); 147 } 148 } 149 150 private Object evaluateDjenericPath(String varName, SimpleNode contextNode, Object obj, String 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 addAllObjectsFromExtent(String path, SimpleNode contextNode, String 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 } 183 catch (DjenericException de) 184 { 185 throw new DjScriptExecutionException(de.getMessage()); 186 } 187 String 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 path, Object value, SimpleNode contextNode) throws DjScriptExecutionException 194 { 195 StringTokenizer st = new StringTokenizer (path, "."); 196 197 String curId = st.nextToken(); 198 199 String varName = curId; 200 201 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 propertyPath = path.substring(path.indexOf(".") + 1); 222 223 Object obj = getValue(varName); 224 if (obj == null) 225 { 226 throw new DjScriptExecutionException(Messages.getString("ScriptExecutionContext.ValueIsNull", varName, 227 propertyPath), contextNode); 228 } 229 230 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 obj, String propertyPath, Object 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 obj, String propertyPath, Object value) 262 throws DjScriptExecutionException 263 { 264 try 265 { 266 throw new DjScriptExecutionException("Java properties currently not supported", contextNode); 267 268 } 270 catch (Exception x) 271 { 272 throw new DjScriptExecutionException(x, contextNode); 273 } 274 } 275 276 private DjObject getFirstOfList(Object 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 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 |