1 30 package com.genimen.djeneric.tools.scriptengine.core.util; 31 32 import java.util.HashMap ; 33 import java.util.Iterator ; 34 import java.util.Stack ; 35 import java.util.StringTokenizer ; 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 _cIndexStack = new Stack (); 52 FunctionSupport _functionSupport = null; 53 ScriptRunnerContainer _container = null; 54 ControllerNode _controller = null; 55 ActionNode[] _actions = new ActionNode[0]; 56 HashMap _actionsHashed = new HashMap (); 57 protected ScriptNode _scriptNode; 58 private HashMap _knownJavaBeans = new HashMap (); 59 60 public DjScriptCompileTimeScope(ScriptNode scriptNode, DjPersistenceManager mgr) 61 { 62 _mgr = mgr; 63 _scriptNode = scriptNode; 64 } 65 66 public HashMap getVariableTypes() 67 { 68 return _contextStack.getVariableTypes(); 69 } 70 71 public Variable pushVariable(String name, Object 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 (_contextStack.size())); 84 } 85 86 public void releaseToMark() 87 { 88 int c = ((Integer ) _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 actionName) 114 { 115 return (ActionNode) _actionsHashed.get(actionName); 116 } 117 118 public ActionNode getAction(String 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 (); 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 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 name) 166 { 167 return getVariableStack().getVariable(name); 168 } 169 170 public boolean isVariable(String 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 getValue(String name) 187 { 188 return getVariableStack().getValue(name); 189 } 190 191 public ScriptNode getScriptNode() 192 { 193 return _scriptNode; 194 } 195 196 public boolean isKnownJavaBean(String alias) 197 { 198 return _knownJavaBeans.get(alias) != null; 199 } 200 201 public void addJavaBean(String alias, String typeName) 202 { 203 _knownJavaBeans.put(alias, typeName); 204 } 205 206 public String getJavaBeanTypeName(String alias) 207 { 208 return (String ) _knownJavaBeans.get(alias); 209 } 210 211 public String getPropertyType(String 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 st = new StringTokenizer (path, "."); 222 223 String varName = st.nextToken(); 224 225 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 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 typeName = var.getTypeName(); 244 245 if (!st.hasMoreElements()) return typeName; 246 247 String propertyPath = path.substring(path.indexOf(".") + 1); 248 249 DjExtent ext = getPersistenceManager().getExtentByObjectType(typeName); 250 return ext.getPropertyType(propertyPath); 251 252 } 253 catch (Exception e) 254 { 255 throw new DjScriptExecutionException(e, caller); 256 } 257 258 } 259 260 public void declareGlobals(HashMap variables) 261 { 262 Iterator it = variables.keySet().iterator(); 263 while (it.hasNext()) 264 { 265 String key = it.next().toString(); 266 pushVariable(new Variable(key, null, variables.get(key).toString())); 267 } 268 } 269 270 } | Popular Tags |