1 30 package com.genimen.djeneric.tools.scriptengine.core.nodes; 31 32 import java.math.BigDecimal ; 33 import java.util.Date ; 34 import java.util.HashMap ; 35 36 import com.genimen.djeneric.language.Messages; 37 import com.genimen.djeneric.repository.DjExtent; 38 import com.genimen.djeneric.repository.exceptions.ObjectNotDefinedException; 39 import com.genimen.djeneric.tools.scriptengine.core.DjScriptParserEngine; 40 import com.genimen.djeneric.tools.scriptengine.core.SimpleNode; 41 import com.genimen.djeneric.tools.scriptengine.core.util.DjScriptCompileTimeScope; 42 import com.genimen.djeneric.tools.scriptengine.core.util.DjScriptExecutionException; 43 import com.genimen.djeneric.tools.scriptengine.core.util.DjScriptExecutionTimeScope; 44 import com.genimen.djeneric.tools.scriptengine.core.util.Variable; 45 46 public class DeclarationNode extends SimpleNode 47 { 48 49 String _typeName; 50 String _variableName; 51 52 public DeclarationNode(int i) 53 { 54 super(i); 55 } 56 57 public DeclarationNode(DjScriptParserEngine p, int i) 58 { 59 super(p, i); 60 } 61 62 public String getName() 63 { 64 return _typeName + " " + _variableName; 65 } 66 67 public String toString() 68 { 69 return getName(); 70 } 71 72 public String getTypeName() 73 { 74 return _typeName; 75 } 76 77 public String getVariableName() 78 { 79 return _variableName; 80 } 81 82 public void setTypeName(String string) 83 { 84 _typeName = string; 85 } 86 87 public void setVariableName(String string) 88 { 89 _variableName = string; 90 } 91 92 public void execute(DjScriptExecutionTimeScope ctxt) throws DjScriptExecutionException 93 { 94 ValueExpression valueExpression = null; 95 if (getChildCount() > 0) valueExpression = (ValueExpression) getChild(0); 96 97 Variable variable = createVariable(ctxt); 98 99 if (valueExpression != null) variable.assign(valueExpression.getValue(ctxt), this); 100 101 ctxt.pushVariable(variable); 102 } 103 104 private Variable createVariable(DjScriptCompileTimeScope ctxt) throws DjScriptExecutionException 105 { 106 Variable variable; 107 108 if (getTypeName().equals("int") || getTypeName().equals("Integer") || getTypeName().equals("long") 109 || getTypeName().equals("Long")) 110 { 111 variable = new Variable(getVariableName(), new Long (0)); 112 } 113 else if (getTypeName().equals("BigDecimal") || getTypeName().equals("double") || getTypeName().equals("float")) 114 { 115 variable = new Variable(getVariableName(), new BigDecimal (0)); 116 } 117 else if (getTypeName().equals("String")) 118 { 119 variable = new Variable(getVariableName(), ""); 120 } 121 else if (getTypeName().equals("Boolean")) 122 { 123 variable = new Variable(getVariableName(), new Boolean (false)); 124 } 125 else if (getTypeName().equals("Date") || getTypeName().equals("Timestamp")) 126 { 127 variable = new Variable(getVariableName(), new Date ()); 128 } 129 else 130 { 132 try 133 { 134 DjExtent extent = ctxt.getPersistenceManager().getExtentByObjectType(getTypeName()); 135 variable = new Variable(getVariableName(), null, extent.getName()); 136 } 137 catch (ObjectNotDefinedException e) 138 { 139 throw new DjScriptExecutionException("Type " + getTypeName() + " is not defined", this); 140 } 141 } 142 return variable; 143 } 144 145 public void validateScript(DjScriptCompileTimeScope ctxt) throws DjScriptExecutionException 146 { 147 if (getTypeName().equals("int") || getTypeName().equals("Integer") || getTypeName().equals("long") 148 || getTypeName().equals("Long") || getTypeName().equals("BigDecimal") || getTypeName().equals("double") 149 || getTypeName().equals("float") || getTypeName().equals("String") || getTypeName().equals("Boolean") 150 || getTypeName().equals("Date") || getTypeName().equals("Timestamp")) return; 151 try 152 { 153 ctxt.getPersistenceManager().getExtentByObjectType(getTypeName()); 154 } 155 catch (ObjectNotDefinedException e) 156 { 157 throw new DjScriptExecutionException(Messages.getString("DeclarationNode.Type", getTypeName()), this); 158 } 159 Variable variable = createVariable(ctxt); 160 ctxt.pushVariable(variable); 161 } 162 163 public void collectVariables(DjScriptCompileTimeScope ctxt, HashMap variables, int stopAtLine) 164 throws DjScriptExecutionException 165 { 166 if (stopAtLine >= 0 && getLine() > stopAtLine) return; 167 168 variables.put(_variableName, _typeName); 169 } 170 171 } | Popular Tags |