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.Query; 009 import railo.runtime.type.StructImpl; 010 011 /** 012 * represent a reference to a variable 013 */ 014 public final class VariableReference implements Reference { 015 016 private Collection coll; 017 private Collection.Key key; 018 019 020 021 /** 022 * constructor of the class 023 * @param coll Collection where variable is 024 * @param key key to the value inside the collection 025 */ 026 public VariableReference(Collection coll, String key) { 027 this.coll=coll; 028 this.key=KeyImpl.init(key); 029 } 030 031 /** 032 * constructor of the class 033 * @param coll Collection where variable is 034 * @param key key to the value inside the collection 035 */ 036 public VariableReference(Collection coll, Collection.Key key) { 037 this.coll=coll; 038 this.key=key; 039 } 040 041 /** 042 * constructor of the class 043 * @param o Object will be casted to Collection 044 * @param key key to the value inside the collection 045 * @throws PageException 046 */ 047 public VariableReference(Object o, String key) throws PageException { 048 this(Caster.toCollection(o),key); 049 } 050 051 /** 052 * constructor of the class 053 * @param o Object will be casted to Collection 054 * @param key key to the value inside the collection 055 * @throws PageException 056 */ 057 public VariableReference(Object o, Collection.Key key) throws PageException { 058 this(Caster.toCollection(o),key); 059 } 060 061 @Override 062 public Object get(PageContext pc) throws PageException { 063 return get(); 064 } 065 private Object get() throws PageException { 066 if(coll instanceof Query) { 067 return ((Query)coll).getColumn(key); 068 } 069 return coll.get(key); 070 } 071 072 @Override 073 public Object get(PageContext pc, Object defaultValue) { 074 return get(defaultValue); 075 } 076 private Object get(Object defaultValue) { 077 if(coll instanceof Query) { 078 Object rtn=((Query)coll).getColumn(key,null); 079 if(rtn!=null)return rtn; 080 return defaultValue; 081 } 082 return coll.get(key,defaultValue); 083 } 084 085 086 @Override 087 public Object set(PageContext pc, Object value) throws PageException { 088 return coll.set(key,value); 089 } 090 public void set(double value) throws PageException { 091 coll.set(key,Caster.toDouble(value)); 092 } 093 094 @Override 095 public Object setEL(PageContext pc, Object value) { 096 return coll.setEL(key,value); 097 } 098 099 @Override 100 public Object touch(PageContext pc) throws PageException { 101 Object o; 102 if(coll instanceof Query) { 103 o= ((Query)coll).getColumn(key,null); 104 if(o!=null) return o; 105 return set(pc,new StructImpl()); 106 } 107 o=coll.get(key,null); 108 if(o!=null) return o; 109 return set(pc,new StructImpl()); 110 } 111 112 @Override 113 public Object touchEL(PageContext pc) { 114 Object o; 115 if(coll instanceof Query) { 116 o= ((Query)coll).getColumn(key,null); 117 if(o!=null) return o; 118 return setEL(pc,new StructImpl()); 119 } 120 o=coll.get(key,null); 121 if(o!=null) return o; 122 return setEL(pc,new StructImpl()); 123 } 124 125 @Override 126 public Object remove(PageContext pc) throws PageException { 127 return coll.remove(key); 128 } 129 130 @Override 131 public Object removeEL(PageContext pc) { 132 return coll.removeEL(key); 133 } 134 135 @Override 136 public Object getParent() { 137 return coll; 138 } 139 140 /** 141 * @return return the parent as Collection 142 */ 143 public Collection getCollection() { 144 return coll; 145 } 146 147 @Override 148 public String getKeyAsString() { 149 return key.getString(); 150 } 151 152 @Override 153 public Collection.Key getKey() { 154 return key; 155 } 156 157 @Override 158 public String toString() { 159 try { 160 return Caster.toString(get()); 161 } 162 catch (PageException e) { 163 return super.toString(); 164 } 165 } 166 167 168 169 170 }