001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019package lucee.runtime.type.ref; 020 021import lucee.runtime.PageContext; 022import lucee.runtime.exp.PageException; 023import lucee.runtime.op.Caster; 024import lucee.runtime.type.Collection; 025import lucee.runtime.type.KeyImpl; 026import lucee.runtime.type.Query; 027import lucee.runtime.type.StructImpl; 028 029/** 030 * represent a reference to a variable 031 */ 032public final class VariableReference implements Reference { 033 034 private Collection coll; 035 private Collection.Key key; 036 037 038 039 /** 040 * constructor of the class 041 * @param coll Collection where variable is 042 * @param key key to the value inside the collection 043 */ 044 public VariableReference(Collection coll, String key) { 045 this.coll=coll; 046 this.key=KeyImpl.init(key); 047 } 048 049 /** 050 * constructor of the class 051 * @param coll Collection where variable is 052 * @param key key to the value inside the collection 053 */ 054 public VariableReference(Collection coll, Collection.Key key) { 055 this.coll=coll; 056 this.key=key; 057 } 058 059 /** 060 * constructor of the class 061 * @param o Object will be casted to Collection 062 * @param key key to the value inside the collection 063 * @throws PageException 064 */ 065 public VariableReference(Object o, String key) throws PageException { 066 this(Caster.toCollection(o),key); 067 } 068 069 /** 070 * constructor of the class 071 * @param o Object will be casted to Collection 072 * @param key key to the value inside the collection 073 * @throws PageException 074 */ 075 public VariableReference(Object o, Collection.Key key) throws PageException { 076 this(Caster.toCollection(o),key); 077 } 078 079 @Override 080 public Object get(PageContext pc) throws PageException { 081 return get(); 082 } 083 private Object get() throws PageException { 084 if(coll instanceof Query) { 085 return ((Query)coll).getColumn(key); 086 } 087 return coll.get(key); 088 } 089 090 @Override 091 public Object get(PageContext pc, Object defaultValue) { 092 return get(defaultValue); 093 } 094 private Object get(Object defaultValue) { 095 if(coll instanceof Query) { 096 Object rtn=((Query)coll).getColumn(key,null); 097 if(rtn!=null)return rtn; 098 return defaultValue; 099 } 100 return coll.get(key,defaultValue); 101 } 102 103 104 @Override 105 public Object set(PageContext pc, Object value) throws PageException { 106 return coll.set(key,value); 107 } 108 public void set(double value) throws PageException { 109 coll.set(key,Caster.toDouble(value)); 110 } 111 112 @Override 113 public Object setEL(PageContext pc, Object value) { 114 return coll.setEL(key,value); 115 } 116 117 @Override 118 public Object touch(PageContext pc) throws PageException { 119 Object o; 120 if(coll instanceof Query) { 121 o= ((Query)coll).getColumn(key,null); 122 if(o!=null) return o; 123 return set(pc,new StructImpl()); 124 } 125 o=coll.get(key,null); 126 if(o!=null) return o; 127 return set(pc,new StructImpl()); 128 } 129 130 @Override 131 public Object touchEL(PageContext pc) { 132 Object o; 133 if(coll instanceof Query) { 134 o= ((Query)coll).getColumn(key,null); 135 if(o!=null) return o; 136 return setEL(pc,new StructImpl()); 137 } 138 o=coll.get(key,null); 139 if(o!=null) return o; 140 return setEL(pc,new StructImpl()); 141 } 142 143 @Override 144 public Object remove(PageContext pc) throws PageException { 145 return coll.remove(key); 146 } 147 148 @Override 149 public Object removeEL(PageContext pc) { 150 return coll.removeEL(key); 151 } 152 153 @Override 154 public Object getParent() { 155 return coll; 156 } 157 158 /** 159 * @return return the parent as Collection 160 */ 161 public Collection getCollection() { 162 return coll; 163 } 164 165 @Override 166 public String getKeyAsString() { 167 return key.getString(); 168 } 169 170 @Override 171 public Collection.Key getKey() { 172 return key; 173 } 174 175 @Override 176 public String toString() { 177 try { 178 return Caster.toString(get()); 179 } 180 catch (PageException e) { 181 return super.toString(); 182 } 183 } 184 185 186 187 188}