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 PageContext pc;
021        private Ref refKey;
022    
023        /**
024         * @param pc
025         * @param parent
026         * @param key
027         */
028        public Variable(PageContext pc, Ref parent,String key) {
029            this.pc=pc;
030            this.parent=parent;
031            this.key=key;
032        }
033        
034        /**
035         * @param pc
036         * @param parent
037         * @param refKey
038         */
039        public Variable(PageContext pc, Ref parent,Ref refKey) {
040            this.pc=pc;
041            this.parent=parent;
042            this.refKey=refKey;
043        }
044    
045        /**
046         * @see railo.runtime.interpreter.ref.Ref#getValue()
047         */
048        public Object getValue() throws PageException {
049            return pc.get(parent.getCollection(),getKeyAsString());
050        }
051        
052        /**
053         * @see railo.runtime.interpreter.ref.Ref#touchValue()
054         */
055        public Object touchValue() throws PageException {
056            Object p = parent.touchValue();
057            if(p instanceof Query) {
058                Object o= ((Query)p).getColumn(getKeyAsString(),null);
059                if(o!=null) return o;
060                return setValue(new StructImpl());
061            }
062            
063            return pc.touch(p,getKeyAsString());
064        }
065        
066        /**
067         * @see railo.runtime.interpreter.ref.Ref#getCollection()
068         */
069        public Object getCollection() throws PageException {
070            Object p = parent.getValue();
071            if(p instanceof Query) {
072                return ((Query)p).getColumn(getKeyAsString());
073            }
074            return pc.get(p,getKeyAsString());
075        }
076    
077        /**
078         * @see railo.runtime.interpreter.ref.Set#setValue(java.lang.Object)
079         */
080        public Object setValue(Object obj) throws PageException {
081            return pc.set(parent.touchValue(),getKeyAsString(),obj);
082        }
083    
084            /**
085             * @see railo.runtime.interpreter.ref.Ref#getTypeName()
086             */
087            public String getTypeName() {
088                    return "variable";
089            }
090    
091        /**
092         * @see railo.runtime.interpreter.ref.Set#getKey()
093         */
094        public Ref getKey() throws PageException {
095            if(key==null)return refKey;
096            return new LString(key);
097        }
098        
099        public String getKeyAsString() throws PageException {
100            if(key==null)key=Caster.toString(refKey.getValue());
101            return key;
102        }
103    
104        /**
105         * @see railo.runtime.interpreter.ref.Set#getParent()
106         */
107        public Ref getParent() throws PageException {
108            return parent;
109        }
110    }