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.op;
020
021import java.awt.Color;
022import java.io.File;
023import java.io.Serializable;
024import java.util.Iterator;
025import java.util.List;
026import java.util.Locale;
027import java.util.Map;
028import java.util.TimeZone;
029
030import lucee.commons.color.ColorCaster;
031import lucee.commons.io.res.Resource;
032import lucee.commons.io.res.util.ResourceUtil;
033import lucee.runtime.PageContext;
034import lucee.runtime.engine.ThreadLocalPageContext;
035import lucee.runtime.exp.CasterException;
036import lucee.runtime.exp.PageException;
037import lucee.runtime.op.date.DateCaster;
038import lucee.runtime.type.Array;
039import lucee.runtime.type.Collection;
040import lucee.runtime.type.Query;
041import lucee.runtime.type.Struct;
042import lucee.runtime.type.dt.DateTime;
043import lucee.runtime.type.dt.TimeSpan;
044import lucee.runtime.util.Cast;
045
046import org.w3c.dom.Node;
047import org.w3c.dom.NodeList;
048
049/**
050 * Implementation of the cast interface
051 */
052public final class CastImpl implements Cast {
053
054    private static CastImpl singelton;
055
056    @Override
057    public Object castTo(PageContext pc, short type, Object o) throws PageException {
058        return Caster.castTo(pc,type,o);
059    }
060
061    @Override
062    public Object castTo(PageContext pc, short type, String strType, Object o) throws PageException {
063        return Caster.castTo(pc,type,strType,o);
064    }
065
066    @Override
067    public Object castTo(PageContext pc, String type, Object o) throws PageException {
068        return Caster.castTo(pc,type,o,false);
069    }
070    public Object castTo(PageContext pc, String type, Object o, boolean alsoPattern) throws PageException {
071        return Caster.castTo(pc,type,o,alsoPattern);
072    }
073
074    @Override
075    public Array toArray(Object obj, Array defaultValue) {
076        return Caster.toArray(obj,defaultValue);
077    }
078
079    @Override
080    public Array toArray(Object obj) throws PageException {
081        return Caster.toArray(obj);
082    }
083
084    @Override
085    public String toBase64(Object o, String defaultValue) {
086        return Caster.toBase64(o,null,defaultValue);
087    }
088
089    @Override
090    public String toBase64(Object o) throws PageException {
091        return Caster.toBase64(o,null);
092    }
093
094    @Override
095    public byte[] toBinary(Object obj, byte[] defaultValue) {
096        return Caster.toBinary(obj,defaultValue);
097    }
098
099    @Override
100    public byte[] toBinary(Object obj) throws PageException {
101        return Caster.toBinary(obj);
102    }
103
104    @Override
105    public Boolean toBoolean(boolean b) {
106        return Caster.toBoolean(b);
107    }
108
109    @Override
110    public Boolean toBoolean(char c) {
111        return Caster.toBoolean(c);
112    }
113
114    @Override
115    public Boolean toBoolean(double d) {
116        return Caster.toBoolean(d);
117    }
118
119    @Override
120    public Boolean toBoolean(Object o, Boolean defaultValue) {
121        return Caster.toBoolean(o,defaultValue);
122    }
123
124    @Override
125    public Boolean toBoolean(Object o) throws PageException {
126        return Caster.toBoolean(o);
127    }
128
129    @Override
130    public Boolean toBoolean(String str, Boolean defaultValue) {
131        return Caster.toBoolean(str,defaultValue);
132    }
133
134    @Override
135    public Boolean toBoolean(String str) throws PageException {
136        return Caster.toBoolean(str);
137    }
138
139    @Override
140    public boolean toBooleanValue(boolean b) {
141        return Caster.toBooleanValue(b);
142    }
143
144    @Override
145    public boolean toBooleanValue(char c) {
146        return Caster.toBooleanValue(c);
147    }
148
149    @Override
150    public boolean toBooleanValue(double d) {
151        return Caster.toBooleanValue(d);
152    }
153
154    @Override
155    public boolean toBooleanValue(Object o, boolean defaultValue) {
156        return Caster.toBooleanValue(o,defaultValue);
157    }
158
159    @Override
160    public boolean toBooleanValue(Object o) throws PageException {
161        return Caster.toBooleanValue(o);
162    }
163
164    @Override
165    public boolean toBooleanValue(String str, boolean defaultValue) {
166        return Caster.toBooleanValue(str,defaultValue);
167    }
168
169    @Override
170    public boolean toBooleanValue(String str) throws PageException {
171        return Caster.toBooleanValue(str);
172    }
173
174    @Override
175    public Byte toByte(boolean b) {
176        return Caster.toByte(b);
177    }
178
179    @Override
180    public Byte toByte(char c) {
181        return Caster.toByte(c);
182    }
183
184    @Override
185    public Byte toByte(double d) {
186        return Caster.toByte(d);
187    }
188
189    @Override
190    public Byte toByte(Object o, Byte defaultValue) {
191        return Caster.toByte(o,defaultValue);
192    }
193
194    @Override
195    public Byte toByte(Object o) throws PageException {
196        return Caster.toByte(o);
197    }
198
199    @Override
200    public byte toByteValue(boolean b) {
201        return Caster.toByteValue(b);
202    }
203
204    @Override
205    public byte toByteValue(char c) {
206        return Caster.toByteValue(c);
207    }
208
209    @Override
210    public byte toByteValue(double d) {
211        return Caster.toByteValue(d);
212    }
213
214    @Override
215    public byte toByteValue(Object o, byte defaultValue) {
216        return Caster.toByteValue(o,defaultValue);
217    }
218
219    @Override
220    public byte toByteValue(Object o) throws PageException {
221        return Caster.toByteValue(o);
222    }
223
224    @Override
225    public Character toCharacter(boolean b) {
226        return Caster.toCharacter(b);
227    }
228
229    @Override
230    public Character toCharacter(char c) {
231        return Caster.toCharacter(c);
232    }
233
234    @Override
235    public Character toCharacter(double d) {
236        return Caster.toCharacter(d);
237    }
238
239    @Override
240    public Character toCharacter(Object o, Character defaultValue) {
241        return Caster.toCharacter(o,defaultValue);
242    }
243
244    @Override
245    public Character toCharacter(Object o) throws PageException {
246        return Caster.toCharacter(o);
247    }
248
249    @Override
250    public char toCharValue(boolean b) {
251        return Caster.toCharValue(b);
252    }
253
254    @Override
255    public char toCharValue(char c) {
256        return Caster.toCharValue(c);
257    }
258
259    @Override
260    public char toCharValue(double d) {
261        return Caster.toCharValue(d);
262    }
263
264    @Override
265    public char toCharValue(Object o, char defaultValue) {
266        return Caster.toCharValue(o,defaultValue);
267    }
268
269    @Override
270    public char toCharValue(Object o) throws PageException {
271        return Caster.toCharValue(o);
272    }
273
274    @Override
275    public Collection toCollection(Object o, Collection defaultValue) {
276        return Caster.toCollection(o,defaultValue);
277    }
278
279    @Override
280    public Collection toCollection(Object o) throws PageException {
281        return Caster.toCollection(o);
282    }
283    
284    @Override
285    public Color toColor(Object o) throws PageException {
286        if(o instanceof Color) return (Color) o;
287        else if (o instanceof String) return ColorCaster.toColor((String)o);
288        else if (o instanceof Number) return ColorCaster.toColor(Integer.toHexString(((Number)o).intValue()));
289        throw new CasterException(o,Color.class);
290    }
291
292    
293    @Override
294    public DateTime toDate(boolean b, TimeZone tz) {
295        return Caster.toDate(b,tz);
296    }
297
298    @Override
299    public DateTime toDate(char c, TimeZone tz) {
300        return Caster.toDate(c,tz);
301    }
302
303    @Override
304    public DateTime toDate(double d, TimeZone tz) {
305        return Caster.toDate(d,tz);
306    }
307
308    @Override
309    public DateTime toDate(Locale locale, String str, TimeZone tz, DateTime defaultValue) {
310        return DateCaster.toDateTime(locale,str,tz,defaultValue,true);
311    }
312
313    @Override
314    public DateTime toDate(Locale locale, String str, TimeZone tz) throws PageException {
315        return DateCaster.toDateTime(locale,str,tz,true);
316    }
317
318    @Override
319    public DateTime toDate(Object o, boolean alsoNumbers, TimeZone tz, DateTime defaultValue) {
320        return Caster.toDate(o,alsoNumbers,tz,defaultValue);
321    }
322
323    @Override
324    public DateTime toDate(Object o, TimeZone tz) throws PageException {
325        return Caster.toDate(o,tz);
326    }
327
328    @Override
329    public DateTime toDate(String str, boolean alsoNumbers, TimeZone tz, DateTime defaultValue) {
330        return Caster.toDate(str,alsoNumbers,tz,defaultValue);
331    }
332
333    @Override
334    public DateTime toDate(String str, TimeZone tz) throws PageException {
335        return Caster.toDate(str,tz);
336    }
337
338    @Override
339    public DateTime toDatetime(Object o, TimeZone tz) throws PageException {
340        return Caster.toDate(o,tz);
341    }
342
343    @Override
344    public DateTime toDateTime(Object o, TimeZone tz) throws PageException {
345        return Caster.toDate(o,tz);
346    }
347
348    @Override
349    public String toDecimal(boolean b) {
350        return Caster.toDecimal(b);
351    }
352
353    @Override
354    public String toDecimal(char c) {
355        return Caster.toDecimal(c);
356    }
357
358    @Override
359    public String toDecimal(double d) {
360        return Caster.toDecimal(d);
361    }
362
363    @Override
364    public String toDecimal(Object value, String defaultValue) {
365        return Caster.toDecimal(value,defaultValue);
366    }
367
368    @Override
369    public String toDecimal(Object value) throws PageException {
370        return Caster.toDecimal(value);
371    }
372
373    @Override
374    public Double toDouble(boolean b) {
375        return Caster.toDouble(b);
376    }
377
378    @Override
379    public Double toDouble(char c) {
380        return Caster.toDouble(c);
381    }
382
383    @Override
384    public Double toDouble(double d) {
385        return Caster.toDouble(d);
386    }
387
388    @Override
389    public Double toDouble(Object o, Double defaultValue) {
390        return Caster.toDouble(o,defaultValue);
391    }
392
393    @Override
394    public Double toDouble(Object o) throws PageException {
395        return Caster.toDouble(o);
396    }
397
398    @Override
399    public Double toDouble(String str, Double defaultValue) {
400        return Caster.toDouble(str,defaultValue);
401    }
402
403    @Override
404    public Double toDouble(String str) throws PageException {
405        return Caster.toDouble(str);
406    }
407
408    @Override
409    public double toDoubleValue(boolean b) {
410        return Caster.toDoubleValue(b);
411    }
412
413    @Override
414    public double toDoubleValue(char c) {
415        return Caster.toDoubleValue(c);
416    }
417
418    @Override
419    public double toDoubleValue(double d) {
420        return Caster.toDoubleValue(d);
421    }
422
423    @Override
424    public double toDoubleValue(Object o, double defaultValue) {
425        return Caster.toDoubleValue(o,true,defaultValue);
426    }
427
428    @Override
429    public double toDoubleValue(Object o) throws PageException {
430        return Caster.toDoubleValue(o);
431    }
432
433    @Override
434    public double toDoubleValue(String str, double defaultValue) {
435        return Caster.toDoubleValue(str,true,defaultValue);
436    }
437
438    @Override
439    public double toDoubleValue(String str) throws PageException {
440        return Caster.toDoubleValue(str);
441    }
442
443    @Override
444    public File toFile(Object obj, File defaultValue) {
445        return Caster.toFile(obj,defaultValue);
446    }
447
448    @Override
449    public File toFile(Object obj) throws PageException {
450        return Caster.toFile(obj);
451    }
452
453    @Override
454    public Integer toInteger(boolean b) {
455        return Caster.toInteger(b);
456    }
457
458    @Override
459    public Integer toInteger(char c) {
460        return Caster.toInteger(c);
461    }
462
463    @Override
464    public Integer toInteger(double d) {
465        return Caster.toInteger(d);
466    }
467
468    @Override
469    public Integer toInteger(Object o, Integer defaultValue) {
470        return Caster.toInteger(o,defaultValue);
471    }
472
473    @Override
474    public Integer toInteger(Object o) throws PageException {
475        return Caster.toInteger(o);
476    }
477
478    @Override
479    public int toIntValue(boolean b) {
480        return Caster.toIntValue(b);
481    }
482
483    @Override
484    public int toIntValue(char c) {
485        return Caster.toIntValue(c);
486    }
487
488    @Override
489    public int toIntValue(double d) {
490        return Caster.toIntValue(d);
491    }
492
493    @Override
494    public int toIntValue(Object o, int defaultValue) {
495        return Caster.toIntValue(o,defaultValue);
496    }
497
498    @Override
499    public int toIntValue(Object o) throws PageException {
500        return Caster.toIntValue(o);
501    }
502
503    @Override
504    public int toIntValue(String str, int defaultValue) {
505        return Caster.toIntValue(str,defaultValue);
506    }
507
508    @Override
509    public int toIntValue(String str) throws PageException {
510        return Caster.toIntValue(str);
511    }
512
513    @Override
514    public Iterator toIterator(Object o) throws PageException {
515        return Caster.toIterator(o);
516    }
517
518    @Override
519    public List toList(Object o, boolean duplicate, List defaultValue) {
520        return Caster.toList(o,duplicate,defaultValue);
521    }
522
523    @Override
524    public List toList(Object o, boolean duplicate) throws PageException {
525        return Caster.toList(o,duplicate);
526    }
527
528    @Override
529    public List toList(Object o, List defaultValue) {
530        return Caster.toList(o,defaultValue);
531    }
532
533    @Override
534    public List toList(Object o) throws PageException {
535        return Caster.toList(o);
536    }
537
538    @Override
539    public Locale toLocale(String strLocale, Locale defaultValue) {
540        return Caster.toLocale(strLocale,defaultValue);
541    }
542
543    @Override
544    public Locale toLocale(String strLocale) throws PageException {
545        return Caster.toLocale(strLocale);
546    }
547
548    @Override
549    public Long toLong(boolean b) {
550        return Caster.toLong(b);
551    }
552
553    @Override
554    public Long toLong(char c) {
555        return Caster.toLong(c);
556    }
557
558    @Override
559    public Long toLong(double d) {
560        return Caster.toLong(d);
561    }
562
563    @Override
564    public Long toLong(Object o, Long defaultValue) {
565        return Caster.toLong(o,defaultValue);
566    }
567
568    @Override
569    public Long toLong(Object o) throws PageException {
570        return Caster.toLong(o);
571    }
572
573    @Override
574    public long toLongValue(boolean b) {
575        return Caster.toLongValue(b);
576    }
577
578    @Override
579    public long toLongValue(char c) {
580        return Caster.toLongValue(c);
581    }
582
583    @Override
584    public long toLongValue(double d) {
585        return Caster.toLongValue(d);
586    }
587
588    @Override
589    public long toLongValue(Object o, long defaultValue) {
590        return Caster.toLongValue(o,defaultValue);
591    }
592
593    @Override
594    public long toLongValue(Object o) throws PageException {
595        return Caster.toLongValue(o);
596    }
597
598    @Override
599    public Map toMap(Object o, boolean duplicate, Map defaultValue) {
600        return Caster.toMap(o,duplicate,defaultValue);
601    }
602
603    @Override
604    public Map toMap(Object o, boolean duplicate) throws PageException {
605        return Caster.toMap(o,duplicate);
606    }
607
608    @Override
609    public Map toMap(Object o, Map defaultValue) {
610        return Caster.toMap(o,defaultValue);
611    }
612
613    @Override
614    public Map toMap(Object o) throws PageException {
615        return Caster.toMap(o);
616    }
617
618    @Override
619    public Node toNode(Object o, Node defaultValue) {
620        return Caster.toNode(o,defaultValue);
621    }
622
623    @Override
624    public Node toNode(Object o) throws PageException {
625        return Caster.toNode(o);
626    }
627
628    @Override
629    public NodeList toNodeList(Object o, NodeList defaultValue) {
630        return Caster.toNodeList(o,defaultValue);
631    }
632
633    @Override
634    public NodeList toNodeList(Object o) throws PageException {
635        return Caster.toNodeList(o);
636    }
637
638    @Override
639    public Object toNull(Object value, Object defaultValue) {
640        return Caster.toNull(value,defaultValue);
641    }
642
643    @Override
644    public Object toNull(Object value) throws PageException {
645        return Caster.toNull(value);
646    }
647
648    public Collection.Key toKey(Object o) throws PageException{
649        return Caster.toKey(o);
650    }
651    
652    public Collection.Key toKey(Object o, Collection.Key defaultValue){
653        return Caster.toKey(o,defaultValue);
654    }
655    
656    @Override
657    public PageException toPageException(Throwable t) {
658        return Caster.toPageException(t);
659    }
660
661    @Override
662    public Query toQuery(Object o, boolean duplicate, Query defaultValue) {
663        return Caster.toQuery(o,duplicate,defaultValue);
664    }
665
666    @Override
667    public Query toQuery(Object o, boolean duplicate) throws PageException {
668        return Caster.toQuery(o,duplicate);
669    }
670
671    @Override
672    public Query toQuery(Object o, Query defaultValue) {
673        return Caster.toQuery(o,defaultValue);
674    }
675
676    @Override
677    public Query toQuery(Object o) throws PageException {
678        return Caster.toQuery(o);
679    }
680
681    @Override
682    public Boolean toRef(boolean b) {
683        return Caster.toRef(b);
684    }
685
686    @Override
687    public Byte toRef(byte b) {
688        return Caster.toRef(b);
689    }
690
691    @Override
692    public String toRef(char c) {
693        return Caster.toRef(c);
694    }
695
696    @Override
697    public Collection toRef(Collection o) {
698        return Caster.toRef(o);
699    }
700
701    @Override
702    public Double toRef(double d) {
703        return Caster.toRef(d);
704    }
705
706    @Override
707    public Float toRef(float f) {
708        return Caster.toRef(f);
709    }
710
711    @Override
712    public Integer toRef(int i) {
713        return Caster.toRef(i);
714    }
715
716    @Override
717    public Long toRef(long l) {
718        return Caster.toRef(l);
719    }
720
721    @Override
722    public Object toRef(Object o) {
723        return Caster.toRef(o);
724    }
725
726    @Override
727    public Short toRef(short s) {
728        return Caster.toRef(s);
729    }
730
731    @Override
732    public String toRef(String str) {
733        return Caster.toRef(str);
734    }
735
736    @Override
737    public Short toShort(boolean b) {
738        return Caster.toShort(b);
739    }
740
741    @Override
742    public Short toShort(char c) {
743        return Caster.toShort(c);
744    }
745
746    @Override
747    public Short toShort(double d) {
748        return Caster.toShort(d);
749    }
750
751    @Override
752    public Short toShort(Object o, Short defaultValue) {
753        return Caster.toShort(o,defaultValue);
754    }
755
756    @Override
757    public Short toShort(Object o) throws PageException {
758        return Caster.toShort(o);
759    }
760
761    @Override
762    public short toShortValue(boolean b) {
763        return Caster.toShortValue(b);
764    }
765
766    @Override
767    public short toShortValue(char c) {
768        return Caster.toShortValue(c);
769    }
770
771    @Override
772    public short toShortValue(double d) {
773        return Caster.toShortValue(d);
774    }
775
776    @Override
777    public short toShortValue(Object o, short defaultValue) {
778        return Caster.toShortValue(o,defaultValue);
779    }
780
781    @Override
782    public short toShortValue(Object o) throws PageException {
783        return Caster.toShortValue(o);
784    }
785
786    @Override
787    public String toString(boolean b) {
788        return Caster.toString(b);
789    }
790
791    @Override
792    public String toString(double d) {
793        return Caster.toString(d);
794    }
795
796    @Override
797    public String toString(int i) {
798        return Caster.toString(i);
799    }
800
801    @Override
802    public String toString(long l) {
803        return Caster.toString(l);
804    }
805
806    @Override
807    public String toString(Object o, String defaultValue) {
808        return Caster.toString(o,defaultValue);
809    }
810
811    @Override
812    public String toString(Object o) throws PageException {
813        return Caster.toString(o);
814    }
815    
816    @Override
817    public Struct toStruct(Object o, Struct defaultValue,boolean caseSensitive) {
818        return Caster.toStruct(o,defaultValue,caseSensitive);
819    }
820
821    @Override
822    public Struct toStruct(Object o, Struct defaultValue) {
823        return Caster.toStruct(o,defaultValue,true);
824    }
825
826    @Override
827    public Struct toStruct(Object o) throws PageException {
828        return Caster.toStruct(o);
829    }
830
831    @Override
832    public TimeSpan toTimespan(Object o, TimeSpan defaultValue) {
833        return Caster.toTimespan(o,defaultValue);
834    }
835
836    @Override
837    public TimeSpan toTimespan(Object o) throws PageException {
838        return Caster.toTimespan(o);
839    }
840
841    @Override
842    public String toTypeName(Object o) {
843        return Caster.toTypeName(o);
844    }
845
846    @Override
847    public Object toUUId(Object o, Object defaultValue) {
848        return Caster.toUUId(o,defaultValue);
849    }
850
851    @Override
852    public Object toUUId(Object o) throws PageException {
853        return Caster.toUUId(o);
854    }
855
856    @Override
857    public Object toVariableName(Object obj, Object defaultValue) {
858        return Caster.toVariableName(obj,defaultValue);
859    }
860
861    @Override
862    public Object toVariableName(Object o) throws PageException {
863        return Caster.toVariableName(o);
864    }
865
866    @Override
867    public Object toVoid(Object o, Object defaultValue) {
868        return Caster.toVoid(o,defaultValue);
869    }
870
871    @Override
872    public Object toVoid(Object o) throws PageException {
873        return Caster.toVoid(o);
874    }
875
876    @Override
877    public Node toXML(Object value, Node defaultValue) {
878        return Caster.toXML(value,defaultValue);
879    }
880
881    @Override
882    public Node toXML(Object value) throws PageException {
883        return Caster.toXML(value);
884    }
885
886    public static Cast getInstance() {
887        if(singelton==null)singelton=new CastImpl();
888        return singelton;
889    }
890
891        @Override
892        public Resource toResource(Object obj) throws PageException {
893                if(obj instanceof Resource) return (Resource) obj;
894                if(obj instanceof File) return ResourceUtil.toResource((File) obj);
895                return ResourceUtil.toResourceNotExisting(ThreadLocalPageContext.get(), toString(obj));
896        }
897
898        @Override
899        public Resource toResource(Object obj, Resource defaultValue) {
900                if(obj instanceof Resource) return (Resource) obj;
901                String path=toString(obj,null);
902                if(path==null)return defaultValue;
903                return ResourceUtil.toResourceNotExisting(ThreadLocalPageContext.get(), path);
904        }
905
906        @Override
907        public Object to(String type, Object o,boolean alsoPattern) throws PageException {
908                return Caster.castTo(ThreadLocalPageContext.get(), type, o,alsoPattern);
909        }
910
911        @Override
912        public Serializable toSerializable(Object obj) throws PageException {
913                return Caster.toSerializable(obj);
914        }
915
916        @Override
917        public Serializable toSerializable(Object object, Serializable defaultValue) {
918                return Caster.toSerializable(object, defaultValue);
919        }
920
921}