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 /** 046 * @see railo.runtime.type.ref.Reference#getParent() 047 */ 048 public Object getParent() { 049 return o; 050 } 051 052 public Collection.Key getKey() { 053 return KeyImpl.init(key); 054 } 055 056 /** 057 * @see railo.runtime.type.ref.Reference#getKeyAsString() 058 */ 059 public String getKeyAsString() { 060 return key; 061 } 062 063 /** 064 * @see railo.runtime.type.ref.Reference#get(railo.runtime.PageContext) 065 */ 066 public Object get(PageContext pc) throws PageException { 067 return pc.getCollection(o,key); 068 } 069 070 /** 071 * 072 * @see railo.runtime.type.ref.Reference#get(railo.runtime.PageContext, java.lang.Object) 073 */ 074 public Object get(PageContext pc, Object defaultValue) { 075 return pc.getCollection(o,key,null); 076 } 077 078 /** 079 * @see railo.runtime.type.ref.Reference#touch(railo.runtime.PageContext) 080 */ 081 public Object touch(PageContext pc) throws PageException { 082 Object rtn=pc.getCollection(o,key,null); 083 if(rtn!=null) return rtn; 084 return pc.set(o,key,new StructImpl()); 085 } 086 public Object touchEL(PageContext pc) { 087 Object rtn=pc.getCollection(o,key,null); 088 if(rtn!=null) return rtn; 089 try { 090 return pc.set(o,key,new StructImpl()); 091 } catch (PageException e) { 092 return null; 093 } 094 } 095 096 /** 097 * @see railo.runtime.type.ref.Reference#set(railo.runtime.PageContext, java.lang.Object) 098 */ 099 public Object set(PageContext pc,Object value) throws PageException { 100 return pc.set(o,key,value); 101 } 102 103 public Object setEL(PageContext pc,Object value) { 104 try { 105 return pc.set(o,key,value); 106 } catch (PageException e) { 107 return null; 108 } 109 } 110 111 /** 112 * @see railo.runtime.type.ref.Reference#remove(PageContext pc) 113 */ 114 public Object remove(PageContext pc) throws PageException { 115 return pc.getVariableUtil().remove(o,key); 116 } 117 118 /** 119 * 120 * @see railo.runtime.type.ref.Reference#removeEL(railo.runtime.PageContext) 121 */ 122 public Object removeEL(PageContext pc) { 123 return pc.getVariableUtil().removeEL(o,key); 124 } 125 126 127 }