001    package railo.runtime.interpreter.ref.var;
002    
003    import railo.runtime.PageContext;
004    import railo.runtime.exp.PageException;
005    import railo.runtime.interpreter.ref.Ref;
006    import railo.runtime.interpreter.ref.RefSupport;
007    import railo.runtime.interpreter.ref.Set;
008    import railo.runtime.interpreter.ref.literal.LString;
009    import railo.runtime.op.Caster;
010    import railo.runtime.type.Query;
011    import railo.runtime.type.StructImpl;
012    
013    /**
014     * 
015     */
016    public final class Variable extends RefSupport implements Set {
017            
018            private String key;
019            private Ref parent;
020        private Ref refKey;
021    
022        /**
023         * @param pc
024         * @param parent
025         * @param key
026         */
027        public Variable( Ref parent,String key) {
028            this.parent=parent;
029            this.key=key;
030        }
031        
032        /**
033         * @param pc
034         * @param parent
035         * @param refKey
036         */
037        public Variable(Ref parent,Ref refKey) {
038            this.parent=parent;
039            this.refKey=refKey;
040        }
041        
042        @Override
043        public Object getValue(PageContext pc) throws PageException {
044            return pc.get(parent.getCollection(pc),getKeyAsString(pc));
045        }
046        
047        @Override
048        public Object touchValue(PageContext pc) throws PageException {
049            Object p = parent.touchValue(pc);
050            if(p instanceof Query) {
051                Object o= ((Query)p).getColumn(getKeyAsString(pc),null);
052                if(o!=null) return o;
053                return setValue(pc,new StructImpl());
054            }
055            
056            return pc.touch(p,getKeyAsString(pc));
057        }
058        
059        @Override
060        public Object getCollection(PageContext pc) throws PageException {
061            Object p = parent.getValue(pc);
062            if(p instanceof Query) {
063                return ((Query)p).getColumn(getKeyAsString(pc));
064            }
065            return pc.get(p,getKeyAsString(pc));
066        }
067    
068        @Override
069        public Object setValue(PageContext pc,Object obj) throws PageException {
070            return pc.set(parent.touchValue(pc),getKeyAsString(pc),obj);
071        }
072    
073        @Override
074        public String getTypeName() {
075                    return "variable";
076            }
077    
078        @Override
079        public Ref getKey(PageContext pc) throws PageException {
080            if(key==null)return refKey;
081            return new LString(key);
082        }
083        
084        @Override
085        public String getKeyAsString(PageContext pc) throws PageException {
086            if(key==null)key=Caster.toString(refKey.getValue(pc));
087            return key;
088        }
089    
090        @Override
091        public Ref getParent(PageContext pc) throws PageException {
092            return parent;
093        }
094    }