001    package railo.runtime;
002    
003    import java.util.Iterator;
004    import java.util.Set;
005    
006    import railo.runtime.component.Member;
007    import railo.runtime.dump.DumpData;
008    import railo.runtime.dump.DumpProperties;
009    import railo.runtime.exp.PageException;
010    import railo.runtime.op.Caster;
011    import railo.runtime.type.Collection;
012    import railo.runtime.type.KeyImpl;
013    import railo.runtime.type.Struct;
014    import railo.runtime.type.StructImpl;
015    import railo.runtime.type.UDF;
016    import railo.runtime.type.dt.DateTime;
017    import railo.runtime.type.util.ComponentUtil;
018    import railo.runtime.type.util.StructSupport;
019    import railo.runtime.type.util.StructUtil;
020    
021    /**
022     * 
023     */
024    public final class ComponentScopeThis extends StructSupport implements ComponentScope {
025        
026        private final ComponentImpl component;
027        private static final int access=Component.ACCESS_PRIVATE;
028        
029        /**
030         * constructor of the class
031         * @param component
032         */
033        public ComponentScopeThis(ComponentImpl component) {
034            this.component=component;
035        }
036    
037        /**
038         * @see railo.runtime.type.Scope#initialize(railo.runtime.PageContext)
039         */
040        public void initialize(PageContext pc) {
041            
042        }
043    
044        /**
045         * @see railo.runtime.type.Scope#release()
046         */
047        public void release() {
048            
049        }
050    
051        /**
052         * @see railo.runtime.type.Scope#getType()
053         */
054        public int getType() {
055            return SCOPE_VARIABLES;
056        }
057    
058        /**
059         * @see railo.runtime.type.Scope#getTypeAsString()
060         */
061        public String getTypeAsString() {
062            return "variables";
063        }
064    
065        /**
066         * @see railo.runtime.type.Collection#size()
067         */
068        public int size() {
069            return component.size(access)+1;
070        }
071        
072        /**
073         * @see railo.runtime.type.Collection#keysAsString()
074         */
075        public String[] keysAsString() {
076            Set keySet = component.keySet(access);
077            keySet.add("this");
078            String[] arr = new String[keySet.size()];
079            Iterator it = keySet.iterator();
080            
081            int index=0;
082            while(it.hasNext()){
083                    arr[index++]=Caster.toString(it.next(),null);
084            }
085            
086            return arr;
087        }
088    
089        public Collection.Key[] keys() {
090            Set keySet = component.keySet(access);
091            keySet.add("this");
092            Collection.Key[] arr = new Collection.Key[keySet.size()];
093            Iterator it = keySet.iterator();
094            
095            int index=0;
096            while(it.hasNext()){
097                    arr[index++]=KeyImpl.toKey(it.next(),null);
098            }
099            return arr;
100        }
101        
102        
103        
104    
105            /**
106             * @see railo.runtime.type.Collection#remove(railo.runtime.type.Collection.Key)
107             */
108            public Object remove(Collection.Key key) throws PageException {
109                    return component.remove(key);
110            }
111    
112            /**
113             *
114             * @see railo.runtime.type.Collection#removeEL(railo.runtime.type.Collection.Key)
115             */
116            public Object removeEL(Collection.Key key) {
117                     return component.removeEL(key);
118            }
119    
120        /**
121         * @see railo.runtime.type.Collection#clear()
122         */
123        public void clear() {
124            component.clear();
125        }
126    
127            /**
128             * @see railo.runtime.type.Collection#get(railo.runtime.type.Collection.Key)
129             */
130            public Object get(Key key) throws PageException {
131            if(key.equalsIgnoreCase(KeyImpl.THIS)){
132                return component;
133            }
134            return component.get(access,key);
135            }
136    
137            /**
138             * @see railo.runtime.type.Collection#get(railo.runtime.type.Collection.Key, java.lang.Object)
139             */
140            public Object get(Collection.Key key, Object defaultValue) {
141            if(key.equalsIgnoreCase(KeyImpl.THIS)){
142                return component;
143            }
144            return component.get(access,key,defaultValue);
145            }
146    
147            /**
148             * @see railo.runtime.type.Collection#set(railo.runtime.type.Collection.Key, java.lang.Object)
149             */
150            public Object set(Collection.Key key, Object value) throws PageException {
151                    return component.set(key,value);
152            }
153    
154            /**
155             *
156             * @see railo.runtime.type.Collection#setEL(railo.runtime.type.Collection.Key, java.lang.Object)
157             */
158            public Object setEL(Collection.Key key, Object value) {
159                    return component.setEL(key,value);
160            }
161    
162        /**
163         * @see railo.runtime.type.Iteratorable#keyIterator()
164         */
165        public Iterator keyIterator() {
166            return component.iterator(access);
167        }
168        
169            /**
170             * @see railo.runtime.type.Collection#containsKey(railo.runtime.type.Collection.Key)
171             */
172            public boolean containsKey(Key key) {
173                    return get(key,null)!=null;
174            }
175    
176        /**
177             * @see railo.runtime.dump.Dumpable#toDumpData(railo.runtime.PageContext, int)
178             */
179            public DumpData toDumpData(PageContext pageContext, int maxlevel, DumpProperties dp) {
180                    return StructUtil.toDumpTable(this, "Variable Scope (of Component)", pageContext, maxlevel, dp);
181        }
182    
183        /**
184         * @see railo.runtime.op.Castable#castToString()
185         */
186        public String castToString() throws PageException {
187            return component.castToString();
188        }
189        
190            /**
191             * @see railo.runtime.type.util.StructSupport#castToString(java.lang.String)
192             */
193            public String castToString(String defaultValue) {
194                    return component.castToString(defaultValue);
195            }
196    
197        /**
198         * @see railo.runtime.op.Castable#castToBooleanValue()
199         */
200        public boolean castToBooleanValue() throws PageException {
201            return component.castToBooleanValue();
202        }
203        
204        /**
205         * @see railo.runtime.op.Castable#castToBoolean(java.lang.Boolean)
206         */
207        public Boolean castToBoolean(Boolean defaultValue) {
208            return component.castToBoolean(defaultValue);
209        }
210    
211        /**
212         * @see railo.runtime.op.Castable#castToDoubleValue()
213         */
214        public double castToDoubleValue() throws PageException {
215            return component.castToDoubleValue();
216        }
217        
218        /**
219         * @see railo.runtime.op.Castable#castToDoubleValue(double)
220         */
221        public double castToDoubleValue(double defaultValue) {
222            return component.castToDoubleValue(defaultValue);
223        }
224    
225        /**
226         * @see railo.runtime.op.Castable#castToDateTime()
227         */
228        public DateTime castToDateTime() throws PageException {
229            return component.castToDateTime();
230        }
231        
232        /**
233         * @see railo.runtime.op.Castable#castToDateTime(railo.runtime.type.dt.DateTime)
234         */
235        public DateTime castToDateTime(DateTime defaultValue) {
236            return component.castToDateTime(defaultValue);
237        }
238    
239    
240            /**
241             * @throws PageException 
242             * @see railo.runtime.op.Castable#compare(boolean)
243             */
244            public int compareTo(boolean b) throws PageException {
245                    return component.compareTo(b);
246            }
247    
248            /**
249             * @see railo.runtime.op.Castable#compareTo(railo.runtime.type.dt.DateTime)
250             */
251            public int compareTo(DateTime dt) throws PageException {
252                    return component.compareTo(dt);
253            }
254    
255            /**
256             * @see railo.runtime.op.Castable#compareTo(double)
257             */
258            public int compareTo(double d) throws PageException {
259                    return component.compareTo(d);
260            }
261    
262            /**
263             * @see railo.runtime.op.Castable#compareTo(java.lang.String)
264             */
265            public int compareTo(String str) throws PageException {
266                    return component.compareTo(str);
267            }
268        
269        /**
270         * @see railo.runtime.type.Collection#duplicate(boolean)
271         */
272        public Collection duplicate(boolean deepCopy) {
273    
274                    StructImpl sct = new StructImpl();
275                    StructImpl.copy(this, sct, deepCopy);
276                    return sct;
277        }
278    
279        /**
280         * Returns the value of component.
281         * @return value component
282         */
283        public ComponentPro getComponent() {
284            return component;
285        }
286    
287        /**
288         *
289         * @see railo.runtime.type.Objects#get(railo.runtime.PageContext, java.lang.String, java.lang.Object)
290         */
291        public Object get(PageContext pc, String key, Object defaultValue) {
292            return component.get(access,key,defaultValue);
293        }
294    
295            /**
296             *
297             * @see railo.runtime.type.Objects#get(railo.runtime.PageContext, railo.runtime.type.Collection.Key, java.lang.Object)
298             */
299            public Object get(PageContext pc, Collection.Key key, Object defaultValue) {
300                    return component.get(access,key,defaultValue);
301            }
302    
303        /**
304         * @see railo.runtime.type.Objects#get(railo.runtime.PageContext, java.lang.String)
305         */
306        public Object get(PageContext pc, String key) throws PageException {
307            return component.get(access,key);
308        }
309    
310            /**
311             *
312             * @see railo.runtime.type.Objects#get(railo.runtime.PageContext, railo.runtime.type.Collection.Key)
313             */
314            public Object get(PageContext pc, Collection.Key key) throws PageException {
315                    return component.get(access,key);
316            }
317    
318        /**
319         * @see railo.runtime.type.Objects#set(railo.runtime.PageContext, java.lang.String, java.lang.Object)
320         */
321        public Object set(PageContext pc, String propertyName, Object value) throws PageException {
322            return component.set(propertyName,value);
323        }
324    
325            /**
326             *
327             * @see railo.runtime.type.Objects#set(railo.runtime.PageContext, railo.runtime.type.Collection.Key, java.lang.Object)
328             */
329            public Object set(PageContext pc, Collection.Key propertyName, Object value) throws PageException {
330                    return component.set(propertyName,value);
331            }
332    
333        /**
334         *
335         * @see railo.runtime.type.Objects#setEL(railo.runtime.PageContext, java.lang.String, java.lang.Object)
336         */
337        public Object setEL(PageContext pc, String propertyName, Object value) {
338            return component.setEL(propertyName,value);
339        }
340    
341            /**
342             *
343             * @see railo.runtime.type.Objects#setEL(railo.runtime.PageContext, railo.runtime.type.Collection.Key, java.lang.Object)
344             */
345            public Object setEL(PageContext pc, Collection.Key propertyName, Object value) {
346                    return component.setEL(propertyName,value);
347            }
348    
349        /**
350         * @see railo.runtime.type.Objects#call(railo.runtime.PageContext, java.lang.String, java.lang.Object[])
351         */
352        public Object call(PageContext pc, String key, Object[] arguments) throws PageException {
353            return call(pc, KeyImpl.init(key), arguments);
354        }
355    
356            public Object call(PageContext pc, Collection.Key key, Object[] arguments) throws PageException {
357            Member m = component.getMember(access, key, false,false);
358                    if(m!=null) {
359                            if(m instanceof UDF) return ((UDF)m).call(pc, arguments, false);
360                    throw ComponentUtil.notFunction(component, key, m.getValue(),access);
361                    }
362                    throw ComponentUtil.notFunction(component, key, null,access);
363            }
364    
365        /**
366         * @see railo.runtime.type.Objects#callWithNamedValues(railo.runtime.PageContext, java.lang.String, railo.runtime.type.Struct)
367         */
368        public Object callWithNamedValues(PageContext pc, String key, Struct args) throws PageException {
369            return callWithNamedValues(pc, KeyImpl.init(key), args);
370        }
371    
372            public Object callWithNamedValues(PageContext pc, Collection.Key key, Struct args) throws PageException {
373            Member m = component.getMember(access, key, false,false);
374                    if(m!=null) {
375                            if(m instanceof UDF) return ((UDF)m).callWithNamedValues(pc, args, false);
376                    throw ComponentUtil.notFunction(component, key, m.getValue(),access);
377                    }
378                    throw ComponentUtil.notFunction(component, key, null,access);
379            }
380    
381        /**
382         * @see railo.runtime.type.Objects#isInitalized()
383         */
384        public boolean isInitalized() {
385            return component.isInitalized();
386        }
387    
388            /**
389             * @see railo.runtime.ComponentScope#setComponent(railo.runtime.ComponentImpl)
390             * /
391            public void setComponentd(ComponentImpl c) {
392                    this.component=c;
393            }*/
394    }