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.scope;
020
021import java.util.Iterator;
022
023import lucee.runtime.PageContext;
024import lucee.runtime.dump.DumpData;
025import lucee.runtime.dump.DumpProperties;
026import lucee.runtime.engine.ThreadLocalPageContext;
027import lucee.runtime.exp.ExpressionException;
028import lucee.runtime.exp.PageException;
029import lucee.runtime.exp.PageRuntimeException;
030import lucee.runtime.java.JavaObject;
031import lucee.runtime.reflection.Reflector;
032import lucee.runtime.type.Collection;
033import lucee.runtime.type.KeyImpl;
034import lucee.runtime.type.Objects;
035import lucee.runtime.type.Struct;
036import lucee.runtime.type.dt.DateTime;
037import lucee.runtime.type.it.EntryIterator;
038import lucee.runtime.type.it.KeyIterator;
039import lucee.runtime.type.it.StringIterator;
040import lucee.runtime.type.it.ValueIterator;
041import lucee.runtime.type.util.StructSupport;
042
043public final class ObjectStruct extends StructSupport implements Struct,Objects {
044
045
046        private JavaObject jo;
047
048        public ObjectStruct(Object o) {
049                if(o instanceof JavaObject) this.jo=(JavaObject) o;
050                else this.jo=new JavaObject(ThreadLocalPageContext.get().getVariableUtil(),o);
051        }
052
053        public ObjectStruct(JavaObject jo) {
054                this.jo=jo;
055        }
056
057        @Override
058        public Object call(PageContext pc, Key methodName, Object[] arguments) throws PageException {
059                return jo.call(pc, methodName, arguments);
060        }
061
062        @Override
063        public Object callWithNamedValues(PageContext pc, Key methodName, Struct args) throws PageException {
064                return jo.callWithNamedValues(pc, methodName, args);
065        }
066
067        @Override
068        public Object get(PageContext pc, Key key) throws PageException {
069                return jo.get(pc, key);
070        }
071
072        @Override
073        public Object get(PageContext pc, Key key, Object defaultValue) {
074                return jo.get(pc, key, defaultValue);
075        }
076
077        public boolean isInitalized() {
078                return jo.isInitalized();
079        }
080
081        @Override
082        public Object set(PageContext pc, Key propertyName, Object value) throws PageException {
083                return jo.set(pc, propertyName, value);
084        }
085
086        @Override
087        public Object setEL(PageContext pc, Key propertyName, Object value) {
088                return jo.setEL(pc, propertyName, value);
089        }
090        
091        @Override
092        public void clear() {
093                //throw new PageRuntimeException(new ExpressionException("can't clear fields from object ["+objects.getClazz().getName()+"]"));
094        }
095
096        public Collection duplicate(boolean deepCopy) {
097                throw new PageRuntimeException(new ExpressionException("can't clone object of type ["+jo.getClazz().getName()+"]"));
098                //return null;
099        }
100
101        
102
103        @Override
104        public boolean containsKey(Key key) {
105                return Reflector.hasPropertyIgnoreCase(jo.getClazz(), key.getString());
106        }
107
108        @Override
109        public Object get(Key key) throws PageException {
110                return jo.get(ThreadLocalPageContext.get(), key);
111        }
112
113        @Override
114        public Object get(Key key, Object defaultValue) {
115                return jo.get(ThreadLocalPageContext.get(), key,defaultValue);
116        }
117
118        @Override
119        public Key[] keys() {
120                String[] strKeys = Reflector.getPropertyKeys(jo.getClazz());
121                Key[] keys=new Key[strKeys.length];
122                for(int i=0;i<strKeys.length;i++) {
123                        keys[i]=KeyImpl.init(strKeys[i]);
124                }
125                return keys;
126        }
127
128        @Override
129        public Object remove(Key key) throws PageException {
130                throw new ExpressionException("can't remove field ["+key.getString()+"] from object ["+jo.getClazz().getName()+"]");
131        }
132
133        @Override
134        public Object removeEL(Key key) {
135                return null;
136        }
137
138        @Override
139        public Object set(Key key, Object value) throws PageException {
140                return jo.set(ThreadLocalPageContext.get(), key, value);
141        }
142
143        @Override
144        public Object setEL(Key key, Object value) {
145                return jo.setEL(ThreadLocalPageContext.get(), key, value);
146        }
147
148        @Override
149        public int size() {
150                return keys().length;
151        }
152
153        @Override
154        public DumpData toDumpData(PageContext pageContext, int maxlevel, DumpProperties dp) {
155                return jo.toDumpData(pageContext, maxlevel,dp);
156        }
157
158        @Override
159        public Iterator<Collection.Key> keyIterator() {
160                return new KeyIterator(keys());
161        }
162    
163    @Override
164        public Iterator<String> keysAsStringIterator() {
165        return new StringIterator(keys());
166    }
167        
168        @Override
169        public Iterator<Entry<Key, Object>> entryIterator() {
170                return new EntryIterator(this,keys());
171        }
172        
173        @Override
174        public Iterator<Object> valueIterator() {
175                return new ValueIterator(this,keys());
176        }
177
178        @Override
179        public boolean castToBooleanValue() throws PageException {
180                return jo.castToBooleanValue();
181        }
182    
183    @Override
184    public Boolean castToBoolean(Boolean defaultValue) {
185        return jo.castToBoolean(defaultValue);
186    }
187
188        @Override
189        public DateTime castToDateTime() throws PageException {
190                return jo.castToDateTime();
191        }
192    
193    @Override
194    public DateTime castToDateTime(DateTime defaultValue) {
195        return jo.castToDateTime(defaultValue);
196    }
197
198        @Override
199        public double castToDoubleValue() throws PageException {
200                return jo.castToDoubleValue();
201        }
202    
203    @Override
204    public double castToDoubleValue(double defaultValue) {
205        return jo.castToDoubleValue(defaultValue);
206    }
207
208        @Override
209        public String castToString() throws PageException {
210                return jo.castToString();
211        }
212        
213        @Override
214        public String castToString(String defaultValue) {
215                return jo.castToString(defaultValue);
216        }
217
218        @Override
219        public int compareTo(String str) throws PageException {
220                return jo.compareTo(str);
221        }
222
223        @Override
224        public int compareTo(boolean b) throws PageException {
225                return jo.compareTo(b);
226        }
227
228        @Override
229        public int compareTo(double d) throws PageException {
230                return jo.compareTo(d);
231        }
232
233        @Override
234        public int compareTo(DateTime dt) throws PageException {
235                return jo.compareTo(dt);
236        }
237}