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.commons.io.res.util;
020
021import lucee.commons.io.ModeUtil;
022import lucee.commons.io.res.Resource;
023import lucee.runtime.exp.PageException;
024import lucee.runtime.op.Castable;
025import lucee.runtime.op.Caster;
026import lucee.runtime.op.Operator;
027import lucee.runtime.op.date.DateCaster;
028import lucee.runtime.type.ObjectWrap;
029import lucee.runtime.type.dt.DateTime;
030
031public final class ModeObjectWrap implements ObjectWrap,Castable {
032        
033        private final Resource res;
034        private String mode=null;
035        
036        public ModeObjectWrap(Resource res) {
037                this.res=res;
038        }
039
040        @Override
041        public Object getEmbededObject() {
042                return toString();
043        }
044
045        @Override
046        public Object getEmbededObject(Object def) {
047                return toString();
048        }
049
050        @Override
051        public String toString() {
052                //print.dumpStack();
053                if(mode==null) mode=ModeUtil.toStringMode(res.getMode());
054                return mode;
055        }
056
057        public String castString() {
058                return toString();
059        }
060
061        @Override
062        public boolean castToBooleanValue() throws PageException {
063                return Caster.toBooleanValue(toString());
064        }
065    
066    @Override
067    public Boolean castToBoolean(Boolean defaultValue) {
068        return Caster.toBoolean(toString(),defaultValue);
069    }
070
071        @Override
072        public DateTime castToDateTime() throws PageException {
073                return Caster.toDatetime(toString(),null);
074        }
075    
076    @Override
077    public DateTime castToDateTime(DateTime defaultValue) {
078        return DateCaster.toDateAdvanced(toString(),DateCaster.CONVERTING_TYPE_OFFSET,null,defaultValue);
079    }
080
081        @Override
082        public double castToDoubleValue() throws PageException {
083                return Caster.toDoubleValue(toString());
084        }
085    
086    @Override
087    public double castToDoubleValue(double defaultValue) {
088        return Caster.toDoubleValue(toString(),defaultValue);
089    }
090
091        @Override
092        public String castToString() throws PageException {
093                return toString();
094        }
095
096        @Override
097        public String castToString(String defaultValue) {
098                return toString();
099        }
100
101        @Override
102        public int compareTo(String str) throws PageException {
103                return Operator.compare(toString(), str);
104        }
105
106        @Override
107        public int compareTo(boolean b) throws PageException {
108                return Operator.compare(castToBooleanValue(), b);
109        }
110
111        @Override
112        public int compareTo(double d) throws PageException {
113                return Operator.compare(castToDoubleValue(), d);
114        }
115
116        @Override
117        public int compareTo(DateTime dt) throws PageException {
118                return Operator.compare(toString(), dt.castToString());
119        }
120
121}