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