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;
020
021import java.util.Iterator;
022import java.util.Set;
023
024import lucee.commons.lang.CFTypes;
025import lucee.runtime.component.Member;
026import lucee.runtime.dump.DumpData;
027import lucee.runtime.dump.DumpProperties;
028import lucee.runtime.exp.PageException;
029import lucee.runtime.type.Collection;
030import lucee.runtime.type.Struct;
031import lucee.runtime.type.StructImpl;
032import lucee.runtime.type.UDFPlus;
033import lucee.runtime.type.dt.DateTime;
034import lucee.runtime.type.util.KeyConstants;
035import lucee.runtime.type.util.MemberUtil;
036import lucee.runtime.type.util.StructSupport;
037import lucee.runtime.type.util.StructUtil;
038
039/**
040 * 
041 */
042public final class ComponentScopeThis extends StructSupport implements ComponentScope {
043    
044    private final ComponentImpl component;
045    private static final int access=Component.ACCESS_PRIVATE;
046    
047    /**
048     * constructor of the class
049     * @param component
050     */
051    public ComponentScopeThis(ComponentImpl component) {
052        this.component=component;
053    }
054
055    @Override
056    public void initialize(PageContext pc) {
057        
058    }
059
060    @Override
061    public void release() {}
062    
063    @Override
064    public void release(PageContext pc) {}
065
066    @Override
067    public int getType() {
068        return SCOPE_VARIABLES;
069    }
070
071    @Override
072    public String getTypeAsString() {
073        return "variables";
074    }
075
076    @Override
077    public int size() {
078        return component.size(access)+1;
079    }
080    
081    @Override
082    public Collection.Key[] keys() {
083        Set<Key> keySet = component.keySet(access);
084        keySet.add(KeyConstants._this);
085        Collection.Key[] arr = new Collection.Key[keySet.size()];
086        Iterator<Key> it = keySet.iterator();
087        
088        int index=0;
089        while(it.hasNext()){
090                arr[index++]=it.next();
091        }
092        return arr;
093    }
094    
095    @Override
096    public Object remove(Collection.Key key) throws PageException {
097                return component.remove(key);
098        }
099
100    @Override
101    public Object removeEL(Collection.Key key) {
102                 return component.removeEL(key);
103        }
104
105    @Override
106    public void clear() {
107        component.clear();
108    }
109
110        @Override
111        public Object get(Key key) throws PageException {
112        if(key.equalsIgnoreCase(KeyConstants._THIS)){
113            return component.top;
114        }
115        return component.get(access,key);
116        }
117
118        @Override
119        public Object get(Collection.Key key, Object defaultValue) {
120        if(key.equalsIgnoreCase(KeyConstants._THIS)){
121            return component.top;
122        }
123        return component.get(access,key,defaultValue);
124        }
125
126        @Override
127        public Object set(Collection.Key key, Object value) throws PageException {
128                return component.set(key,value);
129        }
130
131        @Override
132        public Object setEL(Collection.Key key, Object value) {
133                return component.setEL(key,value);
134        }
135
136        @Override
137        public Iterator<Collection.Key> keyIterator() {
138        return component.keyIterator(access);
139    }
140    
141        @Override
142        public Iterator<String> keysAsStringIterator() {
143        return component.keysAsStringIterator(access);
144    }
145        
146        @Override
147        public Iterator<Entry<Key, Object>> entryIterator() {
148                return component.entryIterator(access);
149        }
150        
151        @Override
152        public Iterator<Object> valueIterator() {
153                return component.valueIterator(access);
154        }
155    
156        @Override
157        public boolean containsKey(Key key) {
158                return get(key,null)!=null;
159        }
160
161    @Override
162        public DumpData toDumpData(PageContext pageContext, int maxlevel, DumpProperties dp) {
163                return StructUtil.toDumpTable(this, "Variable Scope (of Component)", pageContext, maxlevel, dp);
164    }
165
166    @Override
167    public String castToString() throws PageException {
168        return component.castToString();
169    }
170    
171        @Override
172        public String castToString(String defaultValue) {
173                return component.castToString(defaultValue);
174        }
175
176    @Override
177    public boolean castToBooleanValue() throws PageException {
178        return component.castToBooleanValue();
179    }
180    
181    @Override
182    public Boolean castToBoolean(Boolean defaultValue) {
183        return component.castToBoolean(defaultValue);
184    }
185
186    @Override
187    public double castToDoubleValue() throws PageException {
188        return component.castToDoubleValue();
189    }
190    
191    @Override
192    public double castToDoubleValue(double defaultValue) {
193        return component.castToDoubleValue(defaultValue);
194    }
195
196    @Override
197    public DateTime castToDateTime() throws PageException {
198        return component.castToDateTime();
199    }
200    
201    @Override
202    public DateTime castToDateTime(DateTime defaultValue) {
203        return component.castToDateTime(defaultValue);
204    }
205
206
207        @Override
208        public int compareTo(boolean b) throws PageException {
209                return component.compareTo(b);
210        }
211
212        @Override
213        public int compareTo(DateTime dt) throws PageException {
214                return component.compareTo(dt);
215        }
216
217        @Override
218        public int compareTo(double d) throws PageException {
219                return component.compareTo(d);
220        }
221
222        @Override
223        public int compareTo(String str) throws PageException {
224                return component.compareTo(str);
225        }
226    
227    @Override
228    public Collection duplicate(boolean deepCopy) {
229
230                StructImpl sct = new StructImpl();
231                StructImpl.copy(this, sct, deepCopy);
232                return sct;
233    }
234
235    /**
236     * Returns the value of component.
237     * @return value component
238     */
239    public Component getComponent() {
240        return component.top;
241    }
242
243    /*public Object get(PageContext pc, String key, Object defaultValue) {
244        return component.get(access,key,defaultValue);
245    }*/
246
247        @Override
248        public Object get(PageContext pc, Collection.Key key, Object defaultValue) {
249                return component.get(access,key,defaultValue);
250        }
251
252    /*public Object get(PageContext pc, String key) throws PageException {
253        return component.get(access,key);
254    }*/
255
256        @Override
257        public Object get(PageContext pc, Collection.Key key) throws PageException {
258                return component.get(access,key);
259        }
260
261    /*public Object set(PageContext pc, String propertyName, Object value) throws PageException {
262        return component.set(propertyName,value);
263    }*/
264
265        @Override
266        public Object set(PageContext pc, Collection.Key propertyName, Object value) throws PageException {
267                return component.set(propertyName,value);
268        }
269
270    /*public Object setEL(PageContext pc, String propertyName, Object value) {
271        return component.setEL(propertyName,value);
272    }*/
273
274        @Override
275        public Object setEL(PageContext pc, Collection.Key propertyName, Object value) {
276                return component.setEL(propertyName,value);
277        }
278
279    /*public Object call(PageContext pc, String key, Object[] arguments) throws PageException {
280        return call(pc, KeyImpl.init(key), arguments);
281    }*/
282
283        public Object call(PageContext pc, Collection.Key key, Object[] arguments) throws PageException {
284        Member m = component.getMember(access, key, false,false);
285                if(m!=null) {
286                        if(m instanceof UDFPlus) return ((UDFPlus)m).call(pc,key, arguments, false);
287                        return MemberUtil.call(pc, this, key, arguments, CFTypes.TYPE_STRUCT, "struct");
288                        //throw ComponentUtil.notFunction(component, key, m.getValue(),access);
289                }
290                return MemberUtil.call(pc, this, key, arguments, CFTypes.TYPE_STRUCT, "struct");
291                //throw ComponentUtil.notFunction(component, key, null,access);
292        }
293
294    /*public Object callWithNamedValues(PageContext pc, String key, Struct args) throws PageException {
295        return callWithNamedValues(pc, KeyImpl.init(key), args);
296    }*/
297
298        public Object callWithNamedValues(PageContext pc, Collection.Key key, Struct args) throws PageException {
299        Member m = component.getMember(access, key, false,false);
300                if(m!=null) {
301                        if(m instanceof UDFPlus) return ((UDFPlus)m).callWithNamedValues(pc,key, args, false);
302                        return MemberUtil.callWithNamedValues(pc, this, key, args, CFTypes.TYPE_STRUCT, "struct");
303                        //throw ComponentUtil.notFunction(component, key, m.getValue(),access);
304                }
305                return MemberUtil.callWithNamedValues(pc, this, key, args, CFTypes.TYPE_STRUCT, "struct");
306                //throw ComponentUtil.notFunction(component, key, null,access);
307        }
308
309    @Override
310    public boolean isInitalized() {
311        return component.isInitalized();
312    }
313
314        @Override
315        public void setBind(boolean bind) {}
316
317        @Override
318        public boolean isBind() {
319                return true;
320        }
321}