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.interpreter.ref.var;
020
021import lucee.runtime.PageContext;
022import lucee.runtime.exp.PageException;
023import lucee.runtime.interpreter.InterpreterException;
024import lucee.runtime.interpreter.ref.Ref;
025import lucee.runtime.interpreter.ref.RefSupport;
026import lucee.runtime.interpreter.ref.Set;
027import lucee.runtime.interpreter.ref.literal.LString;
028import lucee.runtime.op.Caster;
029import lucee.runtime.type.Query;
030import lucee.runtime.type.StructImpl;
031
032/**
033 * 
034 */
035public final class Variable extends RefSupport implements Set {
036        
037        private String key;
038        private Ref parent;
039    private Ref refKey;
040        private boolean limited;
041
042    /**
043     * @param pc
044     * @param parent
045     * @param key
046     */
047    public Variable( Ref parent,String key, boolean limited) {
048        this.parent=parent;
049        this.key=key;
050        this.limited=limited;
051    }
052    
053    /**
054     * @param pc
055     * @param parent
056     * @param refKey
057     */
058    public Variable(Ref parent,Ref refKey, boolean limited) {
059        this.parent=parent;
060        this.refKey=refKey;
061        this.limited=limited;
062    }
063    
064    @Override
065    public Object getValue(PageContext pc) throws PageException {
066        if(limited) throw new InterpreterException("invalid syntax, variables are not supported in a json string.");
067        return pc.get(parent.getCollection(pc),getKeyAsString(pc));
068    }
069    
070    @Override
071    public Object touchValue(PageContext pc) throws PageException {
072        if(limited) throw new InterpreterException("invalid syntax, variables are not supported in a json string.");
073        Object p = parent.touchValue(pc);
074        if(p instanceof Query) {
075            Object o= ((Query)p).getColumn(getKeyAsString(pc),null);
076            if(o!=null) return o;
077            return setValue(pc,new StructImpl());
078        }
079        
080        return pc.touch(p,getKeyAsString(pc));
081    }
082    
083    @Override
084    public Object getCollection(PageContext pc) throws PageException {
085        if(limited) throw new InterpreterException("invalid syntax, variables are not supported in a json string.");
086        Object p = parent.getValue(pc);
087        if(p instanceof Query) {
088            return ((Query)p).getColumn(getKeyAsString(pc));
089        }
090        return pc.get(p,getKeyAsString(pc));
091    }
092
093    @Override
094    public Object setValue(PageContext pc,Object obj) throws PageException {
095        if(limited) throw new InterpreterException("invalid syntax, variables are not supported in a json string.");
096        return pc.set(parent.touchValue(pc),getKeyAsString(pc),obj);
097    }
098
099    @Override
100    public String getTypeName() {
101                return "variable";
102        }
103
104    @Override
105    public Ref getKey(PageContext pc) throws PageException {
106        if(key==null)return refKey;
107        return new LString(key);
108    }
109    
110    @Override
111    public String getKeyAsString(PageContext pc) throws PageException {
112        if(key==null)key=Caster.toString(refKey.getValue(pc));
113        return key;
114    }
115
116    @Override
117    public Ref getParent(PageContext pc) throws PageException {
118        return parent;
119    }
120}