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.type.Collection;
024import lucee.runtime.type.KeyImpl;
025import lucee.runtime.type.StructImpl;
026
027/**
028 * Handle a Reference
029 */
030public final class ReferenceReference implements Reference {
031
032    private Reference reference;
033    private String key;
034
035    /**
036     * @param reference
037     * @param key
038     */
039    public ReferenceReference(Reference reference, String key) {
040        this.reference=reference;
041        this.key=key;
042    }
043
044    @Override
045    public Collection.Key getKey() {
046        return KeyImpl.init(key);
047    }
048
049    @Override
050    public String getKeyAsString() {
051        return key;
052    }
053
054    @Override
055    public Object get(PageContext pc) throws PageException {
056        return pc.getCollection(reference.get(pc),key);       
057    }
058
059    @Override
060    public Object get(PageContext pc, Object defaultValue) {
061        return pc.getCollection(reference.get(pc,null),key,defaultValue);       
062    }
063
064    @Override
065    public Object set(PageContext pc, Object value) throws PageException {
066        return pc.set(reference.touch(pc),key,value);
067    }
068
069    @Override
070    public Object setEL(PageContext pc, Object value) {
071        try {
072                        return set(pc,value);
073                } catch (PageException e) {
074                        return null;
075                }
076    }
077
078    @Override
079    public Object touch(PageContext pc) throws PageException {
080        Object parent=reference.touch(pc);
081        Object o= pc.getCollection(parent,key,null);
082        if(o!=null) return o;
083        return pc.set(parent,key,new StructImpl());
084    }
085    
086    public Object touchEL(PageContext pc) {
087        Object parent=reference.touchEL(pc);
088        Object o= pc.getCollection(parent,key,null);
089        if(o!=null) return o;
090        try {
091                        return pc.set(parent,key,new StructImpl());
092                } catch (PageException e) {
093                        return null;
094                }
095    }
096
097    @Override
098    public Object remove(PageContext pc) throws PageException {
099        return pc.getVariableUtil().remove(reference.get(pc),key);
100    }
101
102    @Override
103    public Object removeEL(PageContext pc) {
104        return pc.getVariableUtil().removeEL(reference.get(pc,null),key);
105    }
106
107    @Override
108    public Object getParent() {
109        return reference;
110    }
111    @Override
112    public String toString() {
113        return "java.util.ReferenceReference(reference:"+reference+";key:"+key+")";
114    }
115}