001    package railo.commons.io.res.util;
002    
003    import railo.commons.io.ModeUtil;
004    import railo.commons.io.res.Resource;
005    import railo.runtime.exp.PageException;
006    import railo.runtime.op.Castable;
007    import railo.runtime.op.Caster;
008    import railo.runtime.op.Operator;
009    import railo.runtime.op.date.DateCaster;
010    import railo.runtime.type.ObjectWrap;
011    import railo.runtime.type.dt.DateTime;
012    
013    public final class ModeObjectWrap implements ObjectWrap,Castable {
014            
015            private final Resource res;
016            private String mode=null;
017            
018            public ModeObjectWrap(Resource res) {
019                    this.res=res;
020            }
021    
022            /**
023             * @see railo.runtime.type.ObjectWrap#getEmbededObject()
024             */
025            public Object getEmbededObject() {
026                    return toString();
027            }
028    
029            /**
030             * @see railo.runtime.type.ObjectWrap#getEmbededObject(java.lang.Object)
031             */
032            public Object getEmbededObject(Object def) {
033                    return toString();
034            }
035    
036            /**
037             *
038             * @see java.lang.Object#toString()
039             */
040            public String toString() {
041                    //print.dumpStack();
042                    if(mode==null) mode=ModeUtil.toStringMode(res.getMode());
043                    return mode;
044            }
045    
046            public String castString() {
047                    return toString();
048            }
049    
050            /**
051             * @see railo.runtime.op.Castable#castToBooleanValue()
052             */
053            public boolean castToBooleanValue() throws PageException {
054                    return Caster.toBooleanValue(toString());
055            }
056        
057        /**
058         * @see railo.runtime.op.Castable#castToBoolean(java.lang.Boolean)
059         */
060        public Boolean castToBoolean(Boolean defaultValue) {
061            return Caster.toBoolean(toString(),defaultValue);
062        }
063    
064            /**
065             * @see railo.runtime.op.Castable#castToDateTime()
066             */
067            public DateTime castToDateTime() throws PageException {
068                    return Caster.toDatetime(toString(),null);
069            }
070        
071        /**
072         * @see railo.runtime.op.Castable#castToDateTime(railo.runtime.type.dt.DateTime)
073         */
074        public DateTime castToDateTime(DateTime defaultValue) {
075            return DateCaster.toDateAdvanced(toString(),true,null,defaultValue);
076        }
077    
078            /**
079             * @see railo.runtime.op.Castable#castToDoubleValue()
080             */
081            public double castToDoubleValue() throws PageException {
082                    return Caster.toDoubleValue(toString());
083            }
084        
085        /**
086         * @see railo.runtime.op.Castable#castToDoubleValue(double)
087         */
088        public double castToDoubleValue(double defaultValue) {
089            return Caster.toDoubleValue(toString(),defaultValue);
090        }
091    
092            /**
093             * @see railo.runtime.op.Castable#castToString()
094             */
095            public String castToString() throws PageException {
096                    return toString();
097            }
098    
099            /**
100             * @see railo.runtime.op.Castable#castToString(java.lang.String)
101             */
102            public String castToString(String defaultValue) {
103                    return toString();
104            }
105    
106            /**
107             * @see railo.runtime.op.Castable#compareTo(java.lang.String)
108             */
109            public int compareTo(String str) throws PageException {
110                    return Operator.compare(toString(), str);
111            }
112    
113            /**
114             * @see railo.runtime.op.Castable#compareTo(boolean)
115             */
116            public int compareTo(boolean b) throws PageException {
117                    return Operator.compare(castToBooleanValue(), b);
118            }
119    
120            /**
121             * @see railo.runtime.op.Castable#compareTo(double)
122             */
123            public int compareTo(double d) throws PageException {
124                    return Operator.compare(castToDoubleValue(), d);
125            }
126    
127            /**
128             * @see railo.runtime.op.Castable#compareTo(railo.runtime.type.dt.DateTime)
129             */
130            public int compareTo(DateTime dt) throws PageException {
131                    return Operator.compare(toString(), dt.castToString());
132            }
133    
134    }