1 7 package java.beans; 8 9 import java.io.*; 10 import java.util.*; 11 12 13 28 29 public class Encoder { 30 private Map bindings = new IdentityHashMap(); 31 private ExceptionListener exceptionListener; 32 boolean executeStatements = true; 33 private Map attributes; 34 35 49 protected void writeObject(Object o) { 50 if (o == this) { 51 return; 52 } 53 PersistenceDelegate info = getPersistenceDelegate(o == null ? null : o.getClass()); 54 info.writeObject(o, this); 55 } 56 57 67 public void setExceptionListener(ExceptionListener exceptionListener) { 68 this.exceptionListener = exceptionListener; 69 } 70 71 79 public ExceptionListener getExceptionListener() { 80 return (exceptionListener != null) ? exceptionListener : Statement.defaultExceptionListener; 81 } 82 83 Object getValue(Expression exp) { 84 try { 85 return (exp == null) ? null : exp.getValue(); 86 } 87 catch (Exception e) { 88 getExceptionListener().exceptionThrown(e); 89 throw new RuntimeException ("failed to evaluate: " + exp.toString()); 90 } 91 } 92 93 128 public PersistenceDelegate getPersistenceDelegate(Class <?> type) { 129 return MetaData.getPersistenceDelegate(type); 130 } 131 132 143 public void setPersistenceDelegate(Class <?> type, 144 PersistenceDelegate persistenceDelegate) 145 { 146 MetaData.setPersistenceDelegate(type, persistenceDelegate); 147 } 148 149 157 public Object remove(Object oldInstance) { 158 Expression exp = (Expression )bindings.remove(oldInstance); 159 return getValue(exp); 160 } 161 162 174 public Object get(Object oldInstance) { 175 if (oldInstance == null || oldInstance == this || 176 oldInstance.getClass() == String .class) { 177 return oldInstance; 178 } 179 Expression exp = (Expression )bindings.get(oldInstance); 180 return getValue(exp); 181 } 182 183 private Object writeObject1(Object oldInstance) { 184 Object o = get(oldInstance); 185 if (o == null) { 186 writeObject(oldInstance); 187 o = get(oldInstance); 188 } 189 return o; 190 } 191 192 private Statement cloneStatement(Statement oldExp) { 193 Object oldTarget = oldExp.getTarget(); 194 Object newTarget = writeObject1(oldTarget); 195 196 Object [] oldArgs = oldExp.getArguments(); 197 Object [] newArgs = new Object [oldArgs.length]; 198 for (int i = 0; i < oldArgs.length; i++) { 199 newArgs[i] = writeObject1(oldArgs[i]); 200 } 201 if (oldExp.getClass() == Statement .class) { 202 return new Statement (newTarget, oldExp.getMethodName(), newArgs); 203 } 204 else { 205 return new Expression (newTarget, oldExp.getMethodName(), newArgs); 206 } 207 } 208 209 228 public void writeStatement(Statement oldStm) { 229 Statement newStm = cloneStatement(oldStm); 231 if (oldStm.getTarget() != this && executeStatements) { 232 try { 233 newStm.execute(); 234 } catch (Exception e) { 235 getExceptionListener().exceptionThrown(new Exception ("Encoder: discarding statement " 236 + newStm, e)); 237 } 238 } 239 } 240 241 252 public void writeExpression(Expression oldExp) { 253 Object oldValue = getValue(oldExp); 255 if (get(oldValue) != null) { 256 return; 257 } 258 bindings.put(oldValue, (Expression )cloneStatement(oldExp)); 259 writeObject(oldValue); 260 } 261 262 void clear() { 263 bindings.clear(); 264 } 265 266 void setAttribute(Object key, Object value) { 268 if (attributes == null) { 269 attributes = new HashMap(); 270 } 271 attributes.put(key, value); 272 } 273 274 Object getAttribute(Object key) { 275 if (attributes == null) { 276 return null; 277 } 278 return attributes.get(key); 279 } 280 } 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 | Popular Tags |