001 package railo.runtime.type.ref; 002 003 import railo.runtime.PageContext; 004 import railo.runtime.exp.PageException; 005 import railo.runtime.op.Caster; 006 import railo.runtime.type.Collection; 007 import railo.runtime.type.KeyImpl; 008 import railo.runtime.type.StructImpl; 009 010 /** 011 * represent a reference to a Object 012 */ 013 public final class NativeReference implements Reference { 014 015 private Object o; 016 private String key; 017 018 019 /** 020 * Constructor of the class 021 * @param o 022 * @param key 023 */ 024 private NativeReference(Object o, String key) { 025 this.o=o; 026 this.key=key; 027 } 028 029 030 /** 031 * returns a Reference Instance 032 * @param o 033 * @param key 034 * @return Reference Instance 035 */ 036 public static Reference getInstance(Object o, String key) { 037 if(o instanceof Reference) { 038 return new ReferenceReference((Reference)o,key); 039 } 040 Collection coll = Caster.toCollection(o,null); 041 if(coll!=null) return new VariableReference(coll,key); 042 return new NativeReference(o,key); 043 } 044 045 @Override 046 public Object getParent() { 047 return o; 048 } 049 050 public Collection.Key getKey() { 051 return KeyImpl.init(key); 052 } 053 054 @Override 055 public String getKeyAsString() { 056 return key; 057 } 058 059 @Override 060 public Object get(PageContext pc) throws PageException { 061 return pc.getCollection(o,key); 062 } 063 064 @Override 065 public Object get(PageContext pc, Object defaultValue) { 066 return pc.getCollection(o,key,null); 067 } 068 069 @Override 070 public Object touch(PageContext pc) throws PageException { 071 Object rtn=pc.getCollection(o,key,null); 072 if(rtn!=null) return rtn; 073 return pc.set(o,key,new StructImpl()); 074 } 075 public Object touchEL(PageContext pc) { 076 Object rtn=pc.getCollection(o,key,null); 077 if(rtn!=null) return rtn; 078 try { 079 return pc.set(o,key,new StructImpl()); 080 } catch (PageException e) { 081 return null; 082 } 083 } 084 085 @Override 086 public Object set(PageContext pc,Object value) throws PageException { 087 return pc.set(o,key,value); 088 } 089 090 public Object setEL(PageContext pc,Object value) { 091 try { 092 return pc.set(o,key,value); 093 } catch (PageException e) { 094 return null; 095 } 096 } 097 098 @Override 099 public Object remove(PageContext pc) throws PageException { 100 return pc.getVariableUtil().remove(o,key); 101 } 102 103 @Override 104 public Object removeEL(PageContext pc) { 105 return pc.getVariableUtil().removeEL(o,key); 106 } 107 108 109 }