1 30 package com.genimen.djeneric.tools.scriptengine.core.util; 31 32 import java.lang.reflect.Array ; 33 import java.math.BigDecimal ; 34 import java.text.ParseException ; 35 import java.text.SimpleDateFormat ; 36 import java.util.Date ; 37 38 import com.genimen.djeneric.language.Messages; 39 import com.genimen.djeneric.repository.DjList; 40 import com.genimen.djeneric.repository.DjObject; 41 import com.genimen.djeneric.repository.exceptions.ObjectNotDefinedException; 42 import com.genimen.djeneric.tools.scriptengine.core.SimpleNode; 43 44 public class Variable 45 { 46 SimpleDateFormat _sdf = new SimpleDateFormat ("dd-MM-yyyy"); 47 48 Object _obj; 49 String _name; 50 String _typeName = String .class.getName(); 51 boolean _isList = false; 52 53 public Variable(String name, Object obj, String typeName) 54 { 55 _name = name; 56 setObject(obj); 57 _typeName = typeName; 58 } 59 60 public Variable(String name, Object obj) 61 { 62 _name = name; 63 setObject(obj); 64 } 65 66 public String toString() 67 { 68 return getName() + "=" + getObject(); 69 } 70 71 public String getName() 72 { 73 return _name; 74 } 75 76 public Object getObject() 77 { 78 return _obj; 79 } 80 81 public Object getObject(int idx) throws DjScriptExecutionException 82 { 83 assertList(); 84 85 if (_obj instanceof DjList) return ((DjList) _obj).get(idx); 86 87 return Array.get(_obj, idx); 88 } 89 90 public void setObject(Object obj) 91 { 92 if (obj != null) _typeName = obj.getClass().getName(); 93 _obj = obj; 94 _isList = obj instanceof DjList; 95 if (_isList) 96 { 97 _typeName = ((DjList) _obj).getStoredType().getName(); 98 } 99 100 } 101 102 public void assign(int idx, Object value, SimpleNode context) throws DjScriptExecutionException 103 { 104 assertList(); 105 106 if (_obj instanceof DjList) ((DjList) _obj).set(idx, coerse(value, context)); 107 else Array.set(_obj, idx, value); 108 } 109 110 private void assertList() throws DjScriptExecutionException 111 { 112 boolean l = _obj != null && (_obj instanceof DjList || _obj.getClass().isArray()); 113 if (!l) throw new DjScriptExecutionException(Messages.getString("Variable.NotListOrArray", getName())); 114 } 115 116 public void assign(Object value, SimpleNode context) throws DjScriptExecutionException 117 { 118 _obj = coerse(value, context); 119 } 120 121 private Object coerse(Object value, SimpleNode context) throws DjScriptExecutionException 122 { 123 Object result = null; 124 125 if (value == null) 126 { 127 result = value; 128 } 129 else if (_typeName.equals(value.getClass().getName())) 130 { 131 result = value; 132 } 133 else if (_typeName.equals(Long .class.getName())) 134 { 135 result = new Long (value.toString()); 136 } 137 else if (_typeName.equals(BigDecimal .class.getName())) 138 { 139 result = new BigDecimal (value.toString()); 140 } 141 else if (_typeName.equals(Boolean .class.getName())) 142 { 143 result = new Boolean (value.toString()); 144 } 145 else if (_typeName.equals(String .class.getName())) 146 { 147 result = value.toString(); 148 } 149 else if (_typeName.equals(Date .class.getName())) 150 { 151 if (value instanceof Long ) 152 { 153 result = new Date (((Long ) value).longValue()); 154 } 155 else 156 { 157 try 158 { 159 result = _sdf.parse(value.toString()); 160 } 161 catch (ParseException e) 162 { 163 throw new DjScriptExecutionException(e.getMessage(), context); 164 } 165 } 166 } 167 else if (value instanceof DjObject) 168 { 169 DjObject v = (DjObject) value; 170 try 171 { 172 if (v.isInstanceOf(_typeName)) result = value; 173 else throw new DjScriptExecutionException(Messages.getString("Variable.CannotAssign", v.getExtent() 174 .getObjectType(), _typeName), context); 175 } 176 catch (ObjectNotDefinedException e) 177 { 178 throw new DjScriptExecutionException(Messages.getString("Variable.UndefinedType", _typeName), context); 179 } 180 } 181 else if (value instanceof DjList) 182 { 183 DjList lst = (DjList) value; 184 if (_isList) 185 { 186 if (lst.getStoredType() != null && lst.getStoredType().isInstanceof(_typeName)) result = value; 187 else 188 { 189 String typeName = "[untyped]"; 190 if (lst.getStoredType() != null) typeName = lst.getStoredType().getObjectType(); 191 192 throw new DjScriptExecutionException(Messages.getString("Variable.CannotAssign", typeName, _typeName), 193 context); 194 } 195 } 196 else 197 { 198 if (lst.size() == 0) result = null; 199 else if (lst.size() == 1) result = lst.getDjenericObjectAt(0); 200 else throw new DjScriptExecutionException(Messages.getString("Variable.NotOneElement", getName()), context); 201 } 202 } 203 else 204 { 205 String val = "null"; 206 if (value != null) val = value.getClass().getName(); 207 208 throw new DjScriptExecutionException(Messages.getString("Variable.CannotAssign", val, _typeName), context); 209 } 210 211 return result; 212 213 } 214 215 public String getTypeName() 216 { 217 return _typeName; 218 } 219 220 public boolean isList() 221 { 222 return _isList; 223 } 224 } | Popular Tags |