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            /**
062             * @see railo.runtime.type.ref.Reference#get(railo.runtime.PageContext)
063             */
064            public Object get(PageContext pc) throws PageException { 
065                return get(); 
066            } 
067            private Object get() throws PageException { 
068                if(coll instanceof Query) {
069                    return ((Query)coll).getColumn(key);
070                }
071                return coll.get(key); 
072            } 
073            
074            /**
075             *
076             * @see railo.runtime.type.ref.Reference#get(railo.runtime.PageContext, java.lang.Object)
077             */
078            public Object get(PageContext pc, Object defaultValue) { 
079                return get(defaultValue); 
080            } 
081            private Object get(Object defaultValue) { 
082                if(coll instanceof Query) {
083                    Object rtn=((Query)coll).getColumn(key,null);
084                    if(rtn!=null)return rtn;
085                    return defaultValue;
086                }
087                return coll.get(key,defaultValue); 
088            } 
089            
090            
091                    /**
092                     * @see railo.runtime.type.ref.Reference#set(railo.runtime.PageContext, java.lang.Object)
093                     */
094                    public Object set(PageContext pc, Object value) throws PageException { 
095                            return coll.set(key,value); 
096                    } 
097                    public void set(double value) throws PageException { 
098                            coll.set(key,Caster.toDouble(value)); 
099                    } 
100    
101                    /**
102                     * @see railo.runtime.type.ref.Reference#setEL(railo.runtime.PageContext, java.lang.Object)
103                     */
104                    public Object setEL(PageContext pc, Object value) { 
105                                    return coll.setEL(key,value); 
106                    } 
107            
108            /**
109             * @see railo.runtime.type.ref.Reference#touch(railo.runtime.PageContext)
110             */
111            public Object touch(PageContext pc) throws PageException {
112                Object o;
113                if(coll instanceof Query) {
114                    o= ((Query)coll).getColumn(key,null);
115                    if(o!=null) return o;
116                    return set(pc,new StructImpl());
117                }
118                o=coll.get(key,null); 
119                if(o!=null) return o;
120                return set(pc,new StructImpl());
121            } 
122            
123            /**
124             * @see railo.runtime.type.ref.Reference#touchEL(railo.runtime.PageContext)
125             */
126            public Object touchEL(PageContext pc) {
127                Object o;
128                if(coll instanceof Query) {
129                    o= ((Query)coll).getColumn(key,null);
130                    if(o!=null) return o;
131                    return setEL(pc,new StructImpl());
132                }
133                o=coll.get(key,null); 
134                if(o!=null) return o;
135                return setEL(pc,new StructImpl());
136            } 
137            
138            /**
139             * @see railo.runtime.type.ref.Reference#remove(PageContext pc)
140             */
141            public Object remove(PageContext pc) throws PageException { 
142                    return coll.remove(key); 
143            } 
144            
145            /**
146             * @see railo.runtime.type.ref.Reference#removeEL(railo.runtime.PageContext)
147             */
148            public Object removeEL(PageContext pc) { 
149                return coll.removeEL(key); 
150            } 
151    
152            /**
153             * @see railo.runtime.type.ref.Reference#getParent()
154             */
155            public Object getParent() { 
156                return coll; 
157                }
158                
159            /**
160             * @return return the parent as Collection
161             */
162            public Collection getCollection() { 
163                return coll; 
164                }
165    
166                    /**
167                     *
168                     * @see railo.runtime.type.ref.Reference#getKeyAsString()
169                     */
170                    public String getKeyAsString() {
171                            return key.getString();
172                    }
173                    
174                    /**
175                     *
176                     * @see railo.runtime.type.ref.Reference#getKey()
177                     */
178                    public Collection.Key getKey() {
179                            return key;
180                    }
181    
182            /**
183             * @see java.lang.Object#toString()
184             */
185            public String toString() {
186                try {
187                    return Caster.toString(get());
188                } 
189                catch (PageException e) {
190                    return super.toString();
191                }
192            }
193    
194            
195    
196            
197    }