001    package railo.runtime.op;
002    
003    import java.awt.image.BufferedImage;
004    import java.io.ByteArrayInputStream;
005    import java.io.ByteArrayOutputStream;
006    import java.io.File;
007    import java.io.FileInputStream;
008    import java.io.IOException;
009    import java.io.InputStream;
010    import java.io.OutputStream;
011    import java.io.Reader;
012    import java.io.Serializable;
013    import java.io.UnsupportedEncodingException;
014    import java.lang.reflect.InvocationTargetException;
015    import java.math.BigDecimal;
016    import java.net.MalformedURLException;
017    import java.sql.Blob;
018    import java.sql.Clob;
019    import java.sql.ResultSet;
020    import java.sql.SQLException;
021    import java.text.DecimalFormat;
022    import java.util.ArrayList;
023    import java.util.Calendar;
024    import java.util.Date;
025    import java.util.Enumeration;
026    import java.util.Hashtable;
027    import java.util.Iterator;
028    import java.util.List;
029    import java.util.Locale;
030    import java.util.Map;
031    import java.util.Map.Entry;
032    import java.util.Set;
033    import java.util.TimeZone;
034    import java.util.Vector;
035    import java.util.concurrent.ExecutionException;
036    
037    import org.w3c.dom.Node;
038    import org.w3c.dom.NodeList;
039    
040    import railo.commons.date.JREDateTimeUtil;
041    import railo.commons.date.TimeZoneUtil;
042    import railo.commons.io.FileUtil;
043    import railo.commons.io.IOUtil;
044    import railo.commons.io.res.Resource;
045    import railo.commons.io.res.util.ResourceUtil;
046    import railo.commons.lang.CFTypes;
047    import railo.commons.lang.ClassException;
048    import railo.commons.lang.ClassUtil;
049    import railo.commons.lang.StringUtil;
050    import railo.commons.net.HTTPUtil;
051    import railo.runtime.Component;
052    import railo.runtime.PageContext;
053    import railo.runtime.coder.Base64Coder;
054    import railo.runtime.coder.Coder;
055    import railo.runtime.coder.CoderException;
056    import railo.runtime.config.Config;
057    import railo.runtime.converter.ConverterException;
058    import railo.runtime.converter.ScriptConverter;
059    import railo.runtime.engine.ThreadLocalPageContext;
060    import railo.runtime.exp.CasterException;
061    import railo.runtime.exp.ExpressionException;
062    import railo.runtime.exp.NativeException;
063    import railo.runtime.exp.PageException;
064    import railo.runtime.exp.PageExceptionBox;
065    import railo.runtime.ext.function.Function;
066    import railo.runtime.functions.file.FileStreamWrapper;
067    import railo.runtime.i18n.LocaleFactory;
068    import railo.runtime.img.Image;
069    import railo.runtime.interpreter.VariableInterpreter;
070    import railo.runtime.java.JavaObject;
071    import railo.runtime.op.date.DateCaster;
072    import railo.runtime.op.validators.ValidateCreditCard;
073    import railo.runtime.reflection.Reflector;
074    import railo.runtime.text.xml.XMLCaster;
075    import railo.runtime.text.xml.XMLUtil;
076    import railo.runtime.text.xml.struct.XMLMultiElementArray;
077    import railo.runtime.text.xml.struct.XMLMultiElementStruct;
078    import railo.runtime.text.xml.struct.XMLStruct;
079    import railo.runtime.type.Array;
080    import railo.runtime.type.ArrayImpl;
081    import railo.runtime.type.Collection;
082    import railo.runtime.type.Collection.Key;
083    import railo.runtime.type.CollectionStruct;
084    import railo.runtime.type.FunctionValue;
085    import railo.runtime.type.FunctionValueImpl;
086    import railo.runtime.type.Iteratorable;
087    import railo.runtime.type.KeyImpl;
088    import railo.runtime.type.ObjectWrap;
089    import railo.runtime.type.Objects;
090    import railo.runtime.type.Query;
091    import railo.runtime.type.QueryColumn;
092    import railo.runtime.type.QueryImpl;
093    import railo.runtime.type.Struct;
094    import railo.runtime.type.StructImpl;
095    import railo.runtime.type.UDF;
096    import railo.runtime.type.dt.DateTime;
097    import railo.runtime.type.dt.DateTimeImpl;
098    import railo.runtime.type.dt.TimeSpan;
099    import railo.runtime.type.dt.TimeSpanImpl;
100    import railo.runtime.type.scope.ObjectStruct;
101    import railo.runtime.type.util.ArrayUtil;
102    import railo.runtime.type.util.ComponentUtil;
103    import railo.runtime.type.wrap.ArrayAsList;
104    import railo.runtime.type.wrap.ListAsArray;
105    import railo.runtime.type.wrap.MapAsStruct;
106    import railo.runtime.util.ArrayIterator;
107    import railo.runtime.util.IteratorWrapper;
108    
109    
110    
111    
112    /**
113     * This class can cast object of one type to a other by CFML rules
114     */
115    public final class Caster { 
116        private Caster(){
117        }
118        //static Map calendarsMap=new ReferenceMap(ReferenceMap.SOFT,ReferenceMap.SOFT);
119        
120        private static final int NUMBERS_MIN=0;
121        private static final int NUMBERS_MAX=999;
122        private static final String[] NUMBERS = {
123                "0","1","2","3","4","5","6","7","8","9",
124                "10","11","12","13","14","15","16","17","18","19",
125                "20","21","22","23","24","25","26","27","28","29",
126                "30","31","32","33","34","35","36","37","38","39",
127                "40","41","42","43","44","45","46","47","48","49",
128                "50","51","52","53","54","55","56","57","58","59",
129                "60","61","62","63","64","65","66","67","68","69",
130                "70","71","72","73","74","75","76","77","78","79",
131                "80","81","82","83","84","85","86","87","88","89",
132                "90","91","92","93","94","95","96","97","98","99",
133                "100","101","102","103","104","105","106","107","108","109",
134                "110","111","112","113","114","115","116","117","118","119",
135                "120","121","122","123","124","125","126","127","128","129",
136                "130","131","132","133","134","135","136","137","138","139",
137                "140","141","142","143","144","145","146","147","148","149",
138                "150","151","152","153","154","155","156","157","158","159",
139                "160","161","162","163","164","165","166","167","168","169",
140                "170","171","172","173","174","175","176","177","178","179",
141                "180","181","182","183","184","185","186","187","188","189",
142                "190","191","192","193","194","195","196","197","198","199",
143                "200","201","202","203","204","205","206","207","208","209",
144                "210","211","212","213","214","215","216","217","218","219",
145                "220","221","222","223","224","225","226","227","228","229",
146                "230","231","232","233","234","235","236","237","238","239",
147                "240","241","242","243","244","245","246","247","248","249",
148                "250","251","252","253","254","255","256","257","258","259",
149                "260","261","262","263","264","265","266","267","268","269",
150                "270","271","272","273","274","275","276","277","278","279",
151                "280","281","282","283","284","285","286","287","288","289",
152                "290","291","292","293","294","295","296","297","298","299",
153                "300","301","302","303","304","305","306","307","308","309",
154                "310","311","312","313","314","315","316","317","318","319",
155                "320","321","322","323","324","325","326","327","328","329",
156                "330","331","332","333","334","335","336","337","338","339",
157                "340","341","342","343","344","345","346","347","348","349",
158                "350","351","352","353","354","355","356","357","358","359",
159                "360","361","362","363","364","365","366","367","368","369",
160                "370","371","372","373","374","375","376","377","378","379",
161                "380","381","382","383","384","385","386","387","388","389",
162                "390","391","392","393","394","395","396","397","398","399",
163                "400","401","402","403","404","405","406","407","408","409",
164                "410","411","412","413","414","415","416","417","418","419",
165                "420","421","422","423","424","425","426","427","428","429",
166                "430","431","432","433","434","435","436","437","438","439",
167                "440","441","442","443","444","445","446","447","448","449",
168                "450","451","452","453","454","455","456","457","458","459",
169                "460","461","462","463","464","465","466","467","468","469",
170                "470","471","472","473","474","475","476","477","478","479",
171                "480","481","482","483","484","485","486","487","488","489",
172                "490","491","492","493","494","495","496","497","498","499",
173                "500","501","502","503","504","505","506","507","508","509",
174                "510","511","512","513","514","515","516","517","518","519",
175                "520","521","522","523","524","525","526","527","528","529",
176                "530","531","532","533","534","535","536","537","538","539",
177                "540","541","542","543","544","545","546","547","548","549",
178                "550","551","552","553","554","555","556","557","558","559",
179                "560","561","562","563","564","565","566","567","568","569",
180                "570","571","572","573","574","575","576","577","578","579",
181                "580","581","582","583","584","585","586","587","588","589",
182                "590","591","592","593","594","595","596","597","598","599",
183                "600","601","602","603","604","605","606","607","608","609",
184                "610","611","612","613","614","615","616","617","618","619",
185                "620","621","622","623","624","625","626","627","628","629",
186                "630","631","632","633","634","635","636","637","638","639",
187                "640","641","642","643","644","645","646","647","648","649",
188                "650","651","652","653","654","655","656","657","658","659",
189                "660","661","662","663","664","665","666","667","668","669",
190                "670","671","672","673","674","675","676","677","678","679",
191                "680","681","682","683","684","685","686","687","688","689",
192                "690","691","692","693","694","695","696","697","698","699",
193                "700","701","702","703","704","705","706","707","708","709",
194                "710","711","712","713","714","715","716","717","718","719",
195                "720","721","722","723","724","725","726","727","728","729",
196                "730","731","732","733","734","735","736","737","738","739",
197                "740","741","742","743","744","745","746","747","748","749",
198                "750","751","752","753","754","755","756","757","758","759",
199                "760","761","762","763","764","765","766","767","768","769",
200                "770","771","772","773","774","775","776","777","778","779",
201                "780","781","782","783","784","785","786","787","788","789",
202                "790","791","792","793","794","795","796","797","798","799",
203                "800","801","802","803","804","805","806","807","808","809",
204                "810","811","812","813","814","815","816","817","818","819",
205                "820","821","822","823","824","825","826","827","828","829",
206                "830","831","832","833","834","835","836","837","838","839",
207                "840","841","842","843","844","845","846","847","848","849",
208                "850","851","852","853","854","855","856","857","858","859",
209                "860","861","862","863","864","865","866","867","868","869",
210                "870","871","872","873","874","875","876","877","878","879",
211                "880","881","882","883","884","885","886","887","888","889",
212                "890","891","892","893","894","895","896","897","898","899",
213                "900","901","902","903","904","905","906","907","908","909",
214                "910","911","912","913","914","915","916","917","918","919",
215                "920","921","922","923","924","925","926","927","928","929",
216                "930","931","932","933","934","935","936","937","938","939",
217                "940","941","942","943","944","945","946","947","948","949",
218                "950","951","952","953","954","955","956","957","958","959",
219                "960","961","962","963","964","965","966","967","968","969",
220                "970","971","972","973","974","975","976","977","978","979",
221                "980","981","982","983","984","985","986","987","988","989",
222                "990","991","992","993","994","995","996","997","998","999"
223               };
224        
225        /**
226         * cast a boolean value to a boolean value (do nothing)
227         * @param b boolean value to cast
228         * @return casted boolean value
229         */
230        public static boolean toBooleanValue(boolean b) {
231            return b;
232        }
233        
234        /**
235         * cast a int value to a boolean value (primitive value type)
236         * @param i int value to cast
237         * @return casted boolean value
238         */
239        public static boolean toBooleanValue(int i) {
240            return i!=0;
241        }   
242        
243        /**
244         * cast a long value to a boolean value (primitive value type)
245         * @param l long value to cast
246         * @return casted boolean value
247         */
248        public static boolean toBooleanValue(long l) {
249            return l!=0;
250        }
251        
252        /**
253         * cast a double value to a boolean value (primitive value type)
254         * @param d double value to cast
255         * @return casted boolean value
256         */
257        public static boolean toBooleanValue(double d) {
258            return d!=0;
259        }
260        
261        /**
262         * cast a double value to a boolean value (primitive value type)
263         * @param c char value to cast
264         * @return casted boolean value
265         */
266        public static boolean toBooleanValue(char c) {
267            return c!=0;
268        }
269        
270        /**
271         * cast a Object to a boolean value (primitive value type)
272         * @param o Object to cast
273         * @return casted boolean value
274         * @throws PageException 
275         */
276        public static boolean toBooleanValue(Object o) throws PageException {
277            if(o instanceof Boolean) return ((Boolean)o).booleanValue();
278            else if(o instanceof Double) return toBooleanValue(((Double)o).doubleValue());
279            else if(o instanceof Number) return toBooleanValue(((Number)o).doubleValue());
280            else if(o instanceof String) return toBooleanValue((String)o);
281            else if(o instanceof Castable) return ((Castable)o).castToBooleanValue();
282            else if(o == null) return toBooleanValue("");
283            else if(o instanceof ObjectWrap) return toBooleanValue(((ObjectWrap)o).getEmbededObject());
284            throw new CasterException(o,"boolean");
285        }
286        
287        /**
288         * tranlate a Boolean object to a boolean value
289         * @param b
290         * @return
291         */
292        public static boolean toBooleanValue(Boolean b) {
293            return b.booleanValue();
294        }
295        
296        /**
297         * cast a Object to a boolean value (primitive value type)
298         * @param str String to cast
299         * @return casted boolean value
300         * @throws PageException 
301         */
302        public static boolean toBooleanValue(String str) throws PageException {
303            Boolean b = toBoolean(str,null);
304            if(b!=null) return b.booleanValue();
305            throw new CasterException("Can't cast String ["+str+"] to a boolean");
306        }
307        
308        public static Boolean toBoolean(String str, Boolean defaultValue) {
309            if(str==null) return defaultValue;
310            int i=stringToBooleanValueEL(str);
311            if(i!=-1) return (i==1)?Boolean.TRUE:Boolean.FALSE;
312            
313            double d=toDoubleValue(str,Double.NaN);
314            if(!Double.isNaN(d)) return toBoolean(d);
315            
316            return defaultValue;
317        }
318        
319    
320        /**
321         * cast a Object to a Double Object (reference Type)
322         * @param o Object to cast
323         * @return casted Double Object
324         * @throws PageException
325         */
326        public static Double toDouble(float f) {
327            return new Double(f);
328            
329        }
330        public static Double toDouble(Float f) {
331            return new Double(f.doubleValue());
332        }
333    
334        /**
335         * cast a Object to a Double Object (reference Type)
336         * @param o Object to cast
337         * @return casted Double Object
338         * @throws PageException
339         */
340        public static Double toDouble(Object o) throws PageException {
341            if(o instanceof Double) return (Double)o;
342            return new Double(toDoubleValue(o));
343            
344        }
345    
346        /**
347         * cast a Object to a Double Object (reference Type)
348         * @param str string to cast
349         * @return casted Double Object
350         * @throws PageException
351         */
352        public static Double toDouble(String str) throws PageException {
353            return new Double(toDoubleValue(str));
354            
355        }
356    
357        /**
358         * cast a Object to a Double Object (reference Type)
359         * @param o Object to cast
360         * @param defaultValue 
361         * @return casted Double Object
362         */
363        public static Double toDouble(Object o, Double defaultValue) {
364            if(o instanceof Double) return (Double)o;
365            double dbl = toDoubleValue(o,Double.NaN);
366            if(Double.isNaN(dbl)) return defaultValue;
367            return new Double(dbl);
368            
369        }
370        
371        /**
372         * cast a double value to a Double Object (reference Type)
373         * @param d double value to cast
374         * @return casted Double Object
375         */
376        private static final int MAX_SMALL_DOUBLE=10000;
377            private static final Double[] smallDoubles=new Double[MAX_SMALL_DOUBLE];
378            static {
379                    for(int i=0;i<MAX_SMALL_DOUBLE;i++) smallDoubles[i]=new Double(i);
380            }
381            
382            public static Double toDouble(double d) {
383                    if(d<MAX_SMALL_DOUBLE && d>=0) {
384                            int i;
385                            if((i=((int)d))==d) return smallDoubles[i];
386                    }
387                    return new Double(d);
388            }
389        
390        
391        /**
392         * cast a boolean value to a Double Object (reference Type)
393         * @param b boolean value to cast
394         * @return casted Double Object
395         */
396        public static Double toDouble(boolean b) {
397            return new Double(b?1:0);
398        }
399        
400    
401        /**
402         * cast a Object to a double value (primitive value Type)
403         * @param o Object to cast
404         * @return casted double value
405         * @throws PageException
406         */
407        public static double toDoubleValue(Object o) throws PageException {
408            if(o instanceof Number) return ((Number)o).doubleValue();
409            else if(o instanceof Boolean) return ((Boolean)o).booleanValue()?1:0;
410            else if(o instanceof String) return toDoubleValue(o.toString(),true);
411            //else if(o instanceof Clob) return toDoubleValue(toString(o));
412            else if(o instanceof Castable) return ((Castable)o).castToDoubleValue();
413            else if(o == null) return 0;//toDoubleValue("");
414            else if(o instanceof ObjectWrap) return toDoubleValue(((ObjectWrap)o).getEmbededObject());
415            throw new CasterException(o,"number");
416        }
417    
418        public static double toDoubleValue(Double d) {
419            if(d == null) return 0;
420            return d.doubleValue();
421        }
422        
423        /**
424         * cast a Object to a double value (primitive value Type)
425         * @param str String to cast
426         * @return casted double value
427         * @throws CasterException 
428         */
429        public static double toDoubleValue(String str) throws CasterException { 
430            return toDoubleValue(str,true);
431        }
432        public static double toDoubleValue(String str, boolean alsoFromDate) throws CasterException { 
433            if(str==null) return 0;//throw new CasterException("can't cast empty string to a number value");
434            str=str.trim();
435            double rtn_=0;
436            double _rtn=0;
437            int eCount=0;
438            double deep=1;
439            int pos=0; 
440            int len=str.length(); 
441            
442            
443            if(len==0) throw new CasterException("can't cast empty string to a number value"); 
444            char curr=str.charAt(pos); 
445            boolean isMinus=false;
446            
447            if(curr=='+') { 
448                if(len==++pos) throw new CasterException("can't cast [+] string to a number value");          
449            }
450            if(curr=='-') { 
451                if(len==++pos) throw new CasterException("can't cast [-] string to a number value");  
452                isMinus=true;
453            }
454            boolean hasDot=false; 
455            //boolean hasExp=false; 
456            do { 
457                curr=str.charAt(pos); 
458                if(curr<'0') {
459                    if(curr=='.') { 
460                        if(hasDot) {
461                            if(!alsoFromDate) throw new CasterException("cannot cast ["+str+"] string to a number value");  
462                            return toDoubleValueViaDate(str);
463                        }
464                        hasDot=true; 
465                    } 
466                    else {
467                        if(pos==0 && Decision.isBoolean(str)) return toBooleanValue(str,false)?1.0D:0.0D;
468                        if(!alsoFromDate) throw new CasterException("cannot cast ["+str+"] string to a number value");  
469                            return toDoubleValueViaDate(str);
470                        //throw new CasterException("can't cast ["+str+"] string to a number value"); 
471                    }
472                }
473                else if(curr>'9') {
474                    if(curr == 'e' || curr == 'E') { 
475                            try{
476                                    return Double.parseDouble(str);
477                            }
478                            catch( NumberFormatException e){
479                                    if(!alsoFromDate) throw new CasterException("cannot cast ["+str+"] string to a number value");  
480                            return toDoubleValueViaDate(str);
481                                    //throw new CasterException("can't cast ["+str+"] string to a number value"); 
482                            } 
483                    }
484                    //else {
485                        if(pos==0 && Decision.isBoolean(str)) return toBooleanValue(str,false)?1.0D:0.0D;
486                        if(!alsoFromDate) throw new CasterException("cannot cast ["+str+"] string to a number value");  
487                            return toDoubleValueViaDate(str);
488                        //throw new CasterException("can't cast ["+str+"] string to a number value"); 
489                    //}
490                }
491                else if(!hasDot) {
492                    rtn_*=10;
493                    rtn_+=toDigit(curr);
494                    
495                }
496                /*else if(hasExp) {
497                    eCount*=10;
498                    eCount+=toDigit(curr);
499                }*/
500                else  {               
501                    deep*=10;
502                    _rtn*=10;
503                    _rtn+=toDigit(curr);
504                    
505                    //rtn_+=(toDigit(curr)/deep);
506                    //deep*=10;
507                }
508            } 
509            while(++pos<len);
510            
511    
512            if(deep>1) {
513                rtn_+=(_rtn/=deep);
514            }
515            if(isMinus)rtn_= -rtn_;
516            if(eCount>0)for(int i=0;i<eCount;i++)rtn_*=10;
517            return rtn_;
518        }
519        
520        private static double toDoubleValueViaDate(String str) throws CasterException {
521                    DateTime date = DateCaster.toDateSimple(str, false,false, null, null);// not advanced here, neo also only support simple
522                    if(date==null)throw new CasterException("can't cast ["+str+"] string to a number value");
523            return date.castToDoubleValue(0);
524            }
525        
526        private static double toDoubleValueViaDate(String str,double defaultValue) {
527            DateTime date = DateCaster.toDateSimple(str, false,false, null, null);// not advanced here, neo also only support simple
528                    if(date==null)return defaultValue;
529            return date.castToDoubleValue(0);
530            }
531    
532            /**
533         * cast a Object to a double value (primitive value Type)
534         * @param o Object to cast
535         * @param defaultValue if can't cast return this value
536         * @return casted double value
537         */
538        public static double toDoubleValue(Object o,double defaultValue) {
539            if(o instanceof Number) return ((Number)o).doubleValue();
540            else if(o instanceof Boolean) return ((Boolean)o).booleanValue()?1:0;
541            else if(o instanceof String) return toDoubleValue(o.toString(),defaultValue);
542            else if(o instanceof Castable) {
543                return ((Castable)o).castToDoubleValue(defaultValue);
544                
545            }
546            //else if(o == null) return defaultValue;
547            else if(o instanceof ObjectWrap) return toDoubleValue(((ObjectWrap)o).getEmbededObject(new Double(defaultValue)),defaultValue);
548                            
549            return defaultValue;
550        }
551        
552        /**
553         * cast a Object to a double value (primitive value Type), if can't return Double.NaN
554         * @param str String to cast
555         * @param defaultValue if can't cast return this value
556         * @return casted double value
557         */ 
558        public static double toDoubleValue(String str,double defaultValue) { 
559            return toDoubleValue(str, true,defaultValue);
560        }
561        public static double toDoubleValue(String str,boolean alsoFromDate,double defaultValue) { 
562            if(str==null) return defaultValue; 
563            str=str.trim();
564    
565            int len=str.length(); 
566            if(len==0) return defaultValue; 
567    
568            double rtn_=0;
569            double _rtn=0;
570            int eCount=0;
571    //      double deep=10;
572            double deep=1;
573            int pos=0; 
574            
575            char curr=str.charAt(pos); 
576            boolean isMinus=false;
577            
578            if(curr=='+') { 
579                if(len==++pos) return defaultValue; 
580            }
581            else if(curr=='-') { 
582                if(len==++pos) return defaultValue; 
583                isMinus=true;
584            }
585            
586            boolean hasDot=false; 
587            //boolean hasExp=false; 
588            do { 
589                curr=str.charAt(pos); 
590                
591                
592                if(curr<'0') {
593                    if(curr=='.') { 
594                            if(hasDot) {
595                                    if(!alsoFromDate) return defaultValue;  
596                            return toDoubleValueViaDate(str,defaultValue);
597                            }
598                        hasDot=true; 
599                    } 
600                    else {
601                        if(pos==0 && Decision.isBoolean(str)) return toBooleanValue(str,false)?1.0D:0.0D;
602                        if(!alsoFromDate) return defaultValue;  
603                            return toDoubleValueViaDate(str,defaultValue);
604                    }
605                }
606                else if(curr>'9') {
607                    if(curr=='e' || curr=='E') { 
608                            try{
609                                    return Double.parseDouble(str);
610                            }
611                            catch( NumberFormatException e){
612                                    if(!alsoFromDate) return defaultValue;  
613                            return toDoubleValueViaDate(str,defaultValue);
614                            } 
615                    }
616                    //else {
617                        if(pos==0 && Decision.isBoolean(str)) return toBooleanValue(str,false)?1.0D:0.0D;
618                        if(!alsoFromDate) return defaultValue;  
619                            return toDoubleValueViaDate(str,defaultValue);
620                    //}
621                }
622                else if(!hasDot) {
623                    rtn_*=10;
624                    rtn_+=toDigit(curr);
625                }
626                /*else if(hasExp) {
627                    eCount*=10;
628                    eCount+=toDigit(curr);
629                }*/
630                else  {                
631                    deep*=10;
632                    _rtn*=10;
633                    _rtn+=toDigit(curr);
634                }
635               
636            } 
637            while(++pos<len);
638            
639            
640            if(deep>1) {
641                rtn_+=(_rtn/=deep);
642            }
643            if(isMinus)rtn_= -rtn_;
644            if(eCount>0)for(int i=0;i<eCount;i++)rtn_*=10;
645            return rtn_;
646            
647            
648        }
649        
650        private static int toDigit(char c) {
651            return c-48;
652        } 
653    
654        /**
655         * cast a double value to a double value (do nothing)
656         * @param d double value to cast
657         * @return casted double value
658         */
659        public static double toDoubleValue(double d) {
660            return d;
661        }
662    
663        public static double toDoubleValue(float f) {
664            return f;
665        }
666    
667        public static double toDoubleValue(Float f) {
668            return f.doubleValue();
669        }
670        
671        /**
672         * cast a boolean value to a double value (primitive value type)
673         * @param b boolean value to cast
674         * @return casted double value
675         */
676        public static double toDoubleValue(boolean b) {
677            return b?1:0;
678        }
679        
680        /**
681         * cast a char value to a double value (primitive value type)
682         * @param c char value to cast
683         * @return casted double value
684         */
685        public static double toDoubleValue(char c) {
686            return c;
687        }
688        
689        /**
690         * cast a Object to a int value (primitive value type)
691         * @param o Object to cast
692         * @return casted int value
693         * @throws PageException
694         */
695        public static int toIntValue(Object o) throws PageException {
696            
697            if(o instanceof Number) return ((Number)o).intValue();
698            else if(o instanceof Boolean) return ((Boolean)o).booleanValue()?1:0;
699            else if(o instanceof String) return toIntValue(o.toString().trim());
700            //else if(o instanceof Clob) return toIntValue(toString(o));
701            else if(o instanceof Castable) return (int)((Castable)o).castToDoubleValue();
702            else if(o instanceof Date) return (int)new DateTimeImpl((Date)o).castToDoubleValue();
703            
704            if(o instanceof String)
705                throw new ExpressionException("Can't cast String ["+o.toString()+"] to a number");
706            else if(o instanceof ObjectWrap) return toIntValue(((ObjectWrap)o).getEmbededObject());
707                    
708            
709            throw new CasterException(o,"number");
710        }
711        
712        /**
713         * cast a Object to a int value (primitive value type)
714         * @param o Object to cast
715         * @param defaultValue 
716         * @return casted int value
717         */
718        public static int toIntValue(Object o, int defaultValue) {
719            
720            if(o instanceof Number) return ((Number)o).intValue();
721            else if(o instanceof Boolean) return ((Boolean)o).booleanValue()?1:0;
722            else if(o instanceof String) return toIntValue(o.toString().trim(),defaultValue);
723            //else if(o instanceof Clob) return toIntValue(toString(o));
724            else if(o instanceof Castable) {
725                return (int)((Castable)o).castToDoubleValue(defaultValue);
726                
727            }
728            else if(o instanceof Date) return (int)new DateTimeImpl((Date)o).castToDoubleValue();
729            else if(o instanceof ObjectWrap) return toIntValue(((ObjectWrap)o).getEmbededObject(Integer.valueOf(defaultValue)),defaultValue);
730                    
731            return defaultValue;
732        }
733        
734    
735        /**
736         * cast a String to a int value (primitive value type)
737         * @param str String to cast
738         * @return casted int value
739         * @throws ExpressionException
740         */
741        public static int toIntValue(String str) throws ExpressionException {
742            return (int)toDoubleValue(str,false);
743        }
744    
745        /**
746         * cast a Object to a double value (primitive value Type), if can't return Integer.MIN_VALUE
747         * @param str String to cast
748         * @param defaultValue 
749         * @return casted double value
750         */
751        public static int toIntValue(String str, int defaultValue) { 
752            return (int)toDoubleValue(str,false,defaultValue);
753        }
754        
755        /**
756         * cast a double value to a int value (primitive value type)
757         * @param d double value to cast
758         * @return casted int value
759         */
760        public static int toIntValue(double d) {
761            return (int)d;
762        }
763        
764        /**
765         * cast a int value to a int value (do nothing)
766         * @param i int value to cast
767         * @return casted int value
768         */
769        public static int toIntValue(int i) {
770            return i;
771        }
772        
773        /**
774         * cast a boolean value to a int value (primitive value type)
775         * @param b boolean value to cast
776         * @return casted int value
777         */
778        public static int toIntValue(boolean b) {
779            return b?1:0;
780        }
781        
782        /**
783         * cast a char value to a int value (primitive value type)
784         * @param c char value to cast
785         * @return casted int value
786         */
787        public static int toIntValue(char c) {
788            return c;
789        }
790    
791        /**
792         * cast a double to a decimal value (String:xx.xx)
793         * @param value Object to cast
794         * @return casted decimal value
795         */
796        public static String toDecimal(boolean value) {
797            if(value) return "1.00";
798            return "0.00";
799        }
800    
801        /**
802         * cast a double to a decimal value (String:xx.xx)
803         * @param value Object to cast
804         * @return casted decimal value
805         * @throws PageException
806         */
807        public static String toDecimal(Object value) throws PageException {
808            return toDecimal(Caster.toDoubleValue(value));
809        }
810    
811        /**
812         * cast a double to a decimal value (String:xx.xx)
813         * @param value Object to cast
814         * @return casted decimal value
815         * @throws PageException
816         */
817        public static String toDecimal(String value) throws PageException {
818            return toDecimal(Caster.toDoubleValue(value));
819        }
820    
821        /**
822         * cast a double to a decimal value (String:xx.xx)
823         * @param value Object to cast
824         * @param defaultValue 
825         * @return casted decimal value
826         */
827        public static String toDecimal(Object value, String defaultValue) {
828            double res=toDoubleValue(value,Double.NaN);
829            if(Double.isNaN(res)) return defaultValue;
830            return toDecimal(res);
831        }
832        
833        /**
834         * cast a Oject to a decimal value (String:xx.xx)
835         * @param value Object to cast
836         * @return casted decimal value
837         */
838        public static String toDecimal(double value) {
839            return toDecimal(value,'.',',');
840        }
841        
842        
843        private static String toDecimal(double value,char decDel,char thsDel) {
844            // TODO Caster toDecimal bessere impl.
845            String str=new BigDecimal((StrictMath.round(value*100)/100D)).toString();
846            //str=toDouble(value).toString();
847            String[] arr=str.split("\\.");      
848            
849            //right value
850                String rightValue;
851                if(arr.length==1) {
852                    rightValue="00";
853                }
854                else {
855                    rightValue=arr[1];
856                    rightValue=StrictMath.round(Caster.toDoubleValue("0."+rightValue,0)*100)+"";
857                    if(rightValue.length()<2)rightValue=0+rightValue;
858                }
859                
860            // left value
861                String leftValue=arr[0];
862                int leftValueLen=leftValue.length();
863                int ends=(StringUtil.startsWith(str, '-'))?1:0;
864                if(leftValueLen>3) {
865                    StringBuffer tmp=new StringBuffer();
866                    int i;
867                    for(i=leftValueLen-3;i>0;i-=3) {
868                            tmp.insert(0, leftValue.substring(i,i+3));
869                        if(i!=ends)tmp.insert(0,thsDel);
870                    }
871                    tmp.insert(0, leftValue.substring(0,i+3));
872                    leftValue=tmp.toString();
873                    
874                }
875            
876            return leftValue+decDel+rightValue;
877        }
878        
879        /*public static void main(String[] args) {
880            print.out(toDecimal(12));
881            print.out(toDecimal(123));
882                    print.out(toDecimal(1234));
883                    print.out(toDecimal(12345));
884                    print.out(toDecimal(123456));
885                    print.out(toDecimal(-12));
886                    print.out(toDecimal(-123));
887                    print.out(toDecimal(-1234));
888                    print.out(toDecimal(-12345));
889                    print.out(toDecimal(-123456));
890            }*/
891        
892        /**
893         * cast a boolean value to a Boolean Object(reference type)
894         * @param b boolean value to cast
895         * @return casted Boolean Object
896         */
897        public static Boolean toBoolean(boolean b) {
898            return b?Boolean.TRUE:Boolean.FALSE;
899        }
900        
901        /**
902         * cast a char value to a Boolean Object(reference type)
903         * @param c char value to cast
904         * @return casted Boolean Object
905         */
906        public static Boolean toBoolean(char c) {
907            return c!=0?Boolean.TRUE:Boolean.FALSE;
908        }
909        
910        /**
911         * cast a int value to a Boolean Object(reference type)
912         * @param i int value to cast
913         * @return casted Boolean Object
914         */
915        public static Boolean toBoolean(int i) {
916            return i!=0?Boolean.TRUE:Boolean.FALSE;
917        }
918        
919        /**
920         * cast a long value to a Boolean Object(reference type)
921         * @param l long value to cast
922         * @return casted Boolean Object
923         */
924        public static Boolean toBoolean(long l) {
925            return l!=0?Boolean.TRUE:Boolean.FALSE;
926        }
927        
928        /**
929         * cast a double value to a Boolean Object(reference type)
930         * @param d double value to cast
931         * @return casted Boolean Object
932         */
933        public static Boolean toBoolean(double d) {
934            return d!=0?Boolean.TRUE:Boolean.FALSE;
935        }
936    
937        /**
938         * cast a Object to a Boolean Object(reference type)
939         * @param o Object to cast
940         * @return casted Boolean Object
941         * @throws PageException
942         */
943        public static Boolean toBoolean(Object o) throws PageException {
944            if(o instanceof Boolean) return (Boolean)o;
945            return toBooleanValue(o)?Boolean.TRUE:Boolean.FALSE;
946            
947        }
948    
949        /**
950         * cast a Object to a Boolean Object(reference type)
951         * @param str String to cast
952         * @return casted Boolean Object
953         * @throws PageException
954         */
955        public static Boolean toBoolean(String str) throws PageException {
956            return toBooleanValue(str)?Boolean.TRUE:Boolean.FALSE;
957            
958        }
959        
960        /**
961         * cast a Object to a boolean value (primitive value type), Exception Less
962         * @param o Object to cast
963         * @param defaultValue 
964         * @return casted boolean value
965         */
966        public static boolean toBooleanValue(Object o, boolean defaultValue) {
967            if(o instanceof Boolean) return ((Boolean)o).booleanValue();
968            else if(o instanceof Double) return toBooleanValue(((Double)o).doubleValue());
969            else if(o instanceof Number) return toBooleanValue(((Number)o).doubleValue());
970            else if(o instanceof String) {
971                Boolean b = toBoolean(o.toString(),null);
972                    if(b!=null) return b;
973            }
974            //else if(o instanceof Clob) return toBooleanValueEL(toStringEL(o));
975            else if(o instanceof Castable) {
976                return ((Castable)o).castToBoolean(Caster.toBoolean(defaultValue)).booleanValue();
977                
978            }
979            else if(o == null) return toBooleanValue("",defaultValue);
980            else if(o instanceof ObjectWrap) return toBooleanValue(((ObjectWrap)o).getEmbededObject(toBoolean(defaultValue)),defaultValue);
981                    
982            return defaultValue;
983        }
984        
985        /**
986         * cast a Object to a boolean value (refrence type), Exception Less
987         * @param o Object to cast
988         * @param defaultValue default value
989         * @return casted boolean reference
990         */
991        public static Boolean toBoolean(Object o,Boolean defaultValue) {
992            if(o instanceof Boolean) return ((Boolean)o);
993            else if(o instanceof Number) return ((Number)o).intValue()==0?Boolean.FALSE:Boolean.TRUE;
994            else if(o instanceof String) {
995                int rtn=stringToBooleanValueEL(o.toString());
996                if(rtn==1)return Boolean.TRUE;
997                else if(rtn==0)return Boolean.FALSE;
998                else {
999                    double dbl = toDoubleValue(o.toString(),Double.NaN);
1000                    if(!Double.isNaN(dbl)) return toBooleanValue(dbl)?Boolean.TRUE:Boolean.FALSE;
1001                }
1002                
1003            }
1004            //else if(o instanceof Clob) return toBooleanValueEL(toStringEL(o));
1005            else if(o instanceof Castable) {
1006                return ((Castable)o).castToBoolean(defaultValue);
1007            }
1008            else if(o instanceof ObjectWrap) return toBoolean(((ObjectWrap)o).getEmbededObject(defaultValue),defaultValue);
1009                    
1010            else if(o == null) return toBoolean("",defaultValue);
1011            return defaultValue;
1012        }
1013        
1014        
1015        
1016    
1017        
1018        /**
1019         * cast a boolean value to a char value
1020         * @param b boolean value to cast
1021         * @return casted char value
1022         */
1023        public static char toCharValue(boolean b) {
1024            return (char)(b?1:0);
1025        }
1026        
1027        /**
1028         * cast a double value to a char value (primitive value type)
1029         * @param d double value to cast
1030         * @return casted char value
1031         */
1032        public static char toCharValue(double d) {
1033            return (char)d;
1034        }
1035        
1036        /**
1037         * cast a char value to a char value (do nothing)
1038         * @param c char value to cast
1039         * @return casted char value
1040         */
1041        public static char toCharValue(char c) {
1042            return c;
1043        }
1044    
1045        /**
1046         * cast a Object to a char value (primitive value type)
1047         * @param o Object to cast
1048         * @return casted char value
1049         * @throws PageException
1050         */
1051        public static char toCharValue(Object o) throws PageException {
1052            if(o instanceof Character) return ((Character)o).charValue();
1053            else if(o instanceof Boolean) return (char)((((Boolean)o).booleanValue())?1:0);
1054            else if(o instanceof Double) return (char)(((Double)o).doubleValue());
1055            else if(o instanceof Number) return (char)(((Number)o).doubleValue());
1056            else if(o instanceof String) {
1057                String str = o.toString();
1058                if(str.length()>0)return str.charAt(0);
1059                throw new ExpressionException("can't cast empty string to a char");
1060                
1061            }
1062            else if(o instanceof ObjectWrap) {
1063                return toCharValue(((ObjectWrap)o).getEmbededObject());
1064            }                                                                                                                                                                               
1065            else if(o == null) return toCharValue("");
1066            throw new CasterException(o,"char");
1067        }
1068    
1069        /**
1070         * cast a Object to a char value (primitive value type)
1071         * @param o Object to cast
1072         * @return casted char value
1073         * @throws PageException
1074         */
1075        public static char toCharValue(String str) throws PageException {
1076                if(str.length()>0)return str.charAt(0);
1077                throw new ExpressionException("can't cast empty string to a char");
1078        }
1079        
1080        /**
1081         * cast a Object to a char value (primitive value type)
1082         * @param o Object to cast
1083         * @param defaultValue 
1084         * @return casted char value
1085         */
1086        public static char toCharValue(Object o, char defaultValue) {
1087            if(o instanceof Character) return ((Character)o).charValue();
1088            else if(o instanceof Boolean) return (char)((((Boolean)o).booleanValue())?1:0);
1089            else if(o instanceof Double) return (char)(((Double)o).doubleValue());
1090            else if(o instanceof Number) return (char)(((Number)o).doubleValue());
1091            else if(o instanceof String) {
1092                String str = o.toString();
1093                if(str.length()>0)return str.charAt(0);
1094                return defaultValue;
1095                
1096            }
1097            else if(o instanceof ObjectWrap) {
1098                return toCharValue(((ObjectWrap)o).getEmbededObject(toCharacter(defaultValue)),defaultValue);
1099            }                                                                                                                                                                               
1100            else if(o == null) return toCharValue("",defaultValue);
1101            return defaultValue;
1102        }
1103        
1104        /**
1105         * cast a boolean value to a Character Object(reference type)
1106         * @param b boolean value to cast
1107         * @return casted Character Object
1108         */
1109        public static Character toCharacter(boolean b) {
1110            return new Character(toCharValue(b));
1111        }
1112        
1113        /**
1114         * cast a char value to a Character Object(reference type)
1115         * @param c char value to cast
1116         * @return casted Character Object
1117         */
1118        public static Character toCharacter(char c) {
1119            return new Character(toCharValue(c));
1120        }
1121        
1122        /**
1123         * cast a double value to a Character Object(reference type)
1124         * @param d double value to cast
1125         * @return casted Character Object
1126         */
1127        public static Character toCharacter(double d) {
1128            return new Character(toCharValue(d));
1129        }
1130    
1131        /**
1132         * cast a Object to a Character Object(reference type)
1133         * @param o Object to cast
1134         * @return casted Character Object
1135         * @throws PageException
1136         */
1137        public static Character toCharacter(Object o) throws PageException {
1138            if(o instanceof Character) return (Character)o;
1139            return new Character(toCharValue(o));
1140            
1141        }
1142    
1143        /**
1144         * cast a Object to a Character Object(reference type)
1145         * @param str Object to cast
1146         * @return casted Character Object
1147         * @throws PageException
1148         */
1149        public static Character toCharacter(String str) throws PageException {
1150            return new Character(toCharValue(str));
1151            
1152        }
1153        
1154        /**
1155         * cast a Object to a Character Object(reference type)
1156         * @param o Object to cast
1157         * @param defaultValue 
1158         * @return casted Character Object
1159         */
1160        public static Character toCharacter(Object o, Character defaultValue) {
1161            if(o instanceof Character) return (Character)o;
1162            if(defaultValue!=null)return new Character(toCharValue(o,defaultValue.charValue()));
1163            
1164            char res = toCharValue(o,Character.MIN_VALUE);
1165            if(res==Character.MIN_VALUE) return defaultValue;
1166            return new Character(res);
1167        }
1168        
1169        /**
1170         * cast a boolean value to a byte value
1171         * @param b boolean value to cast
1172         * @return casted byte value
1173         */
1174        public static byte toByteValue(boolean b) {
1175            return (byte)(b?1:0);
1176        }
1177        
1178        /**
1179         * cast a double value to a byte value (primitive value type)
1180         * @param d double value to cast
1181         * @return casted byte value
1182         */
1183        public static byte toByteValue(double d) {
1184            return (byte)d;
1185        }
1186        
1187        /**
1188         * cast a char value to a byte value (do nothing)
1189         * @param c char value to cast
1190         * @return casted byte value
1191         */
1192        public static byte toByteValue(char c) {
1193            return (byte)c;
1194        }
1195        
1196        /**
1197         * cast a Object to a byte value (primitive value type)
1198         * @param o Object to cast
1199         * @return casted byte value
1200         * @throws PageException
1201         * @throws CasterException
1202         */
1203        public static byte toByteValue(Object o) throws PageException {
1204            if(o instanceof Byte) return ((Byte)o).byteValue();
1205            if(o instanceof Character) return (byte)(((Character)o).charValue());
1206            else if(o instanceof Boolean) return (byte)((((Boolean)o).booleanValue())?1:0);
1207            else if(o instanceof Number) return (((Number)o).byteValue());
1208            else if(o instanceof String) return (byte)toDoubleValue(o.toString());      
1209            else if(o instanceof ObjectWrap) {
1210                return toByteValue(((ObjectWrap)o).getEmbededObject());
1211            }
1212            throw new CasterException(o,"byte");
1213        }
1214        
1215        /**
1216         * cast a Object to a byte value (primitive value type)
1217         * @param str Object to cast
1218         * @return casted byte value
1219         * @throws PageException
1220         * @throws CasterException
1221         */
1222        public static byte toByteValue(String str) throws PageException {
1223            return (byte)toDoubleValue(str.toString());
1224        }
1225        
1226        /**
1227         * cast a Object to a byte value (primitive value type)
1228         * @param o Object to cast
1229         * @param defaultValue 
1230         * @return casted byte value
1231         */
1232        public static byte toByteValue(Object o, byte defaultValue) {
1233            if(o instanceof Byte)           return ((Byte)o).byteValue();
1234            if(o instanceof Character)      return (byte)(((Character)o).charValue());
1235            else if(o instanceof Boolean)   return (byte)((((Boolean)o).booleanValue())?1:0);
1236            else if(o instanceof Number)    return (((Number)o).byteValue());
1237            else if(o instanceof String)    return (byte)toDoubleValue(o.toString(),defaultValue);      
1238            else if(o instanceof ObjectWrap) {
1239                return toByteValue(((ObjectWrap)o).getEmbededObject(toByte(defaultValue)),defaultValue);
1240            }
1241            return defaultValue;
1242        }
1243        
1244        /**
1245         * cast a boolean value to a Byte Object(reference type)
1246         * @param b boolean value to cast
1247         * @return casted Byte Object
1248         */
1249        public static Byte toByte(boolean b) {
1250            return new Byte(toByteValue(b));
1251        }
1252        
1253        /**
1254         * cast a char value to a Byte Object(reference type)
1255         * @param c char value to cast
1256         * @return casted Byte Object
1257         */
1258        public static Byte toByte(char c) {
1259            return new Byte(toByteValue(c));
1260        }
1261        
1262        /**
1263         * cast a double value to a Byte Object(reference type)
1264         * @param d double value to cast
1265         * @return casted Byte Object
1266         */
1267        public static Byte toByte(double d) {
1268            return new Byte(toByteValue(d));
1269        }
1270    
1271        /**
1272         * cast a Object to a Byte Object(reference type)
1273         * @param o Object to cast
1274         * @return casted Byte Object
1275         * @throws PageException
1276         */
1277        public static Byte toByte(Object o) throws PageException {
1278            if(o instanceof Byte) return (Byte)o;
1279            return new Byte(toByteValue(o));
1280            
1281        }
1282    
1283        /**
1284         * cast a Object to a Byte Object(reference type)
1285         * @param str String to cast
1286         * @return casted Byte Object
1287         * @throws PageException
1288         */
1289        public static Byte toByte(String str) throws PageException {
1290            return new Byte(toByteValue(str));
1291            
1292        }
1293    
1294        /**
1295         * cast a Object to a Byte Object(reference type)
1296         * @param o Object to cast
1297         * @param defaultValue 
1298         * @return casted Byte Object
1299         */
1300        public static Byte toByte(Object o, Byte defaultValue) {
1301            if(o instanceof Byte) return (Byte)o;
1302            if(defaultValue!=null) return new Byte(toByteValue(o,defaultValue.byteValue()));
1303            byte res=toByteValue(o,Byte.MIN_VALUE);
1304            if(res==Byte.MIN_VALUE) return defaultValue;
1305            return new Byte(res);
1306        }
1307    
1308        /**
1309         * cast a boolean value to a long value
1310         * @param b boolean value to cast
1311         * @return casted long value
1312         */
1313        public static long toLongValue(boolean b) {
1314            return (b?1L:0L);
1315        }
1316        
1317        /**
1318         * cast a double value to a long value (primitive value type)
1319         * @param d double value to cast
1320         * @return casted long value
1321         */
1322        public static long toLongValue(double d) {
1323            return (long)d;
1324        }
1325        
1326        /**
1327         * cast a char value to a long value (do nothing)
1328         * @param c char value to cast
1329         * @return casted long value
1330         */
1331        public static long toLongValue(char c) {
1332            return c;
1333        }
1334        
1335        /**
1336         * cast a Object to a long value (primitive value type)
1337         * @param o Object to cast
1338         * @return casted long value
1339         * @throws PageException
1340         */
1341        public static long toLongValue(Object o) throws PageException {
1342            if(o instanceof Character) return (((Character)o).charValue());
1343            else if(o instanceof Boolean) return ((((Boolean)o).booleanValue())?1L:0L);
1344            else if(o instanceof Number) return (((Number)o).longValue());
1345            else if(o instanceof String) {
1346                    String str=(String)o;
1347                    try{
1348                            return Long.parseLong(str);
1349                    }
1350                    catch(NumberFormatException nfe){
1351                            return (long)toDoubleValue(str);
1352                    }                                                                                                                                                     
1353            }
1354            else if(o instanceof Castable) return (long)((Castable)o).castToDoubleValue();    
1355            else if(o instanceof ObjectWrap) return toLongValue(((ObjectWrap)o).getEmbededObject());
1356                    
1357            throw new CasterException(o,"long");
1358        }
1359        
1360        /**
1361         * cast a Object to a long value (primitive value type)
1362         * @param str Object to cast
1363         * @return casted long value
1364         * @throws PageException
1365         */
1366        public static long toLongValue(String str) throws PageException {
1367            return (long)toDoubleValue(str); 
1368        }
1369        
1370        /**
1371         * cast a Object to a long value (primitive value type)
1372         * @param o Object to cast
1373         * @param defaultValue 
1374         * @return casted long value
1375         */
1376        public static long toLongValue(Object o, long defaultValue) {
1377            if(o instanceof Character) return (((Character)o).charValue());
1378            else if(o instanceof Boolean) return ((((Boolean)o).booleanValue())?1L:0L);
1379            else if(o instanceof Number) return (((Number)o).longValue());
1380            else if(o instanceof String) return (long)toDoubleValue(o.toString(),defaultValue);                                                                                                                                                      
1381            else if(o instanceof Castable) {
1382                return (long)((Castable)o).castToDoubleValue(defaultValue);                                                                                           
1383            }
1384            else if(o instanceof ObjectWrap) return toLongValue(((ObjectWrap)o).getEmbededObject(toLong(defaultValue)),defaultValue);
1385                    
1386            return defaultValue;
1387        }
1388        
1389        /**
1390         * cast a boolean value to a Long Object(reference type)
1391         * @param b boolean value to cast
1392         * @return casted Long Object
1393         */
1394        public static Long toLong(boolean b) {
1395            return Long.valueOf(toLongValue(b));
1396        }
1397        
1398        /**
1399         * cast a char value to a Long Object(reference type)
1400         * @param c char value to cast
1401         * @return casted Long Object
1402         */
1403        public static Long toLong(char c) {
1404            return Long.valueOf(toLongValue(c));
1405        }
1406        
1407        /**
1408         * cast a double value to a Long Object(reference type)
1409         * @param d double value to cast
1410         * @return casted Long Object
1411         */
1412        public static Long toLong(double d) {
1413            return Long.valueOf(toLongValue(d));
1414        }
1415    
1416        /**
1417         * cast a Object to a Long Object(reference type)
1418         * @param o Object to cast
1419         * @return casted Long Object
1420         * @throws PageException
1421         */
1422        public static Long toLong(Object o) throws PageException {
1423            if(o instanceof Long) return (Long)o; 
1424            return Long.valueOf(toLongValue(o));
1425            
1426        }
1427    
1428        /**
1429         * cast a Object to a Long Object(reference type)
1430         * @param str Object to cast
1431         * @return casted Long Object
1432         * @throws PageException
1433         */
1434        public static Long toLong(String str) throws PageException {
1435            return Long.valueOf(toLongValue(str));
1436            
1437        }
1438    
1439        /**
1440         * cast a long to a Long Object(reference type)
1441         * @param l long to cast
1442         * @return casted Long Object
1443         */
1444        public static Long toLong(long l) {
1445            return Long.valueOf(l);
1446            
1447        }
1448    
1449        /**
1450         * cast a Object to a Long Object(reference type)
1451         * @param o Object to cast
1452         * @param defaultValue 
1453         * @return casted Long Object
1454         */
1455        public static Long toLong(Object o, Long defaultValue) {
1456            if(o instanceof Long) return (Long)o;
1457            if(defaultValue!=null) return Long.valueOf(toLongValue(o,defaultValue.longValue()));
1458            
1459            long res=toLongValue(o,Long.MIN_VALUE);
1460            if(res==Long.MIN_VALUE) return defaultValue;
1461            return Long.valueOf(res);
1462        }
1463        
1464        /**
1465         * cast a boolean value to a Float Object(reference type)
1466         * @param b boolean value to cast
1467         * @return casted Float Object
1468         */
1469        public static Float toFloat(boolean b) {
1470            return new Float(toFloatValue(b));
1471        }
1472        
1473        /**
1474         * cast a char value to a Float Object(reference type)
1475         * @param c char value to cast
1476         * @return casted Float Object
1477         */
1478        public static Float toFloat(char c) {
1479            return new Float(toFloatValue(c));
1480        }
1481        
1482        /**
1483         * cast a double value to a Float Object(reference type)
1484         * @param d double value to cast
1485         * @return casted Float Object
1486         */
1487        public static Float toFloat(double d) {
1488            return new Float(toFloatValue(d));
1489        }
1490    
1491        /**
1492         * cast a Object to a Float Object(reference type)
1493         * @param o Object to cast
1494         * @return casted Float Object
1495         * @throws PageException
1496         */
1497        public static Float toFloat(Object o) throws PageException {
1498            if(o instanceof Float) return (Float)o;
1499            return new Float(toFloatValue(o));
1500        }
1501    
1502        /**
1503         * cast a Object to a Float Object(reference type)
1504         * @param str Object to cast
1505         * @return casted Float Object
1506         * @throws PageException
1507         */
1508        public static Float toFloat(String str) throws PageException {
1509            return new Float(toFloatValue(str));
1510        }
1511    
1512        /**
1513         * cast a Object to a Float Object(reference type)
1514         * @param o Object to cast
1515         * @param defaultValue 
1516         * @return casted Float Object
1517         */
1518        public static Float toFloat(Object o, Float defaultValue) {
1519            if(o instanceof Float) return (Float)o;
1520            if(defaultValue!=null) return new Float(toFloatValue(o,defaultValue.floatValue()));
1521            
1522            float res=toFloatValue(o,Float.MIN_VALUE);
1523            if(res==Float.MIN_VALUE) return defaultValue;
1524            return new Float(res);
1525        }
1526        
1527        
1528        
1529        
1530        /**
1531         * cast a boolean value to a float value
1532         * @param b boolean value to cast
1533         * @return casted long value
1534         */
1535        public static float toFloatValue(boolean b) {
1536            return (b?1F:0F);
1537        }
1538        
1539        /**
1540         * cast a double value to a long value (primitive value type)
1541         * @param d double value to cast
1542         * @return casted long value
1543         */
1544        public static float toFloatValue(double d) {
1545            return (float)d;
1546        }
1547        
1548        /**
1549         * cast a char value to a long value (do nothing)
1550         * @param c char value to cast
1551         * @return casted long value
1552         */
1553        public static float toFloatValue(char c) {
1554            return c;
1555        }
1556        
1557        /**
1558         * cast a Object to a long value (primitive value type)
1559         * @param o Object to cast
1560         * @return casted long value
1561         * @throws PageException
1562         */
1563        public static float toFloatValue(Object o) throws PageException {
1564            if(o instanceof Character) return (((Character)o).charValue());
1565            else if(o instanceof Boolean) return ((((Boolean)o).booleanValue())?1F:0F);
1566            else if(o instanceof Number) return (((Number)o).floatValue());
1567            else if(o instanceof String) return (float)toDoubleValue(o.toString());                                                                                                                                                      
1568            else if(o instanceof Castable) return (float)((Castable)o).castToDoubleValue();    
1569            else if(o instanceof ObjectWrap) return toFloatValue(((ObjectWrap)o).getEmbededObject());
1570                    
1571            throw new CasterException(o,"float");
1572        }
1573        
1574        /**
1575         * cast a Object to a long value (primitive value type)
1576         * @param str Object to cast
1577         * @return casted long value
1578         * @throws PageException
1579         */
1580        public static float toFloatValue(String str) throws PageException {
1581            return (float)toDoubleValue(str);  
1582        }
1583        
1584        /**
1585         * cast a Object to a float value (primitive value type)
1586         * @param o Object to cast
1587         * @param defaultValue 
1588         * @return casted float value
1589         */
1590        public static float toFloatValue(Object o, float defaultValue) {
1591            if(o instanceof Character) return (((Character)o).charValue());
1592            else if(o instanceof Boolean) return ((((Boolean)o).booleanValue())?1F:0F);
1593            else if(o instanceof Number) return (((Number)o).floatValue());
1594            else if(o instanceof String) return (float)toDoubleValue(o.toString(),defaultValue);                                                                                                                                                      
1595            else if(o instanceof Castable) {
1596                return (float)((Castable)o).castToDoubleValue(defaultValue);
1597                                                                                                                                                                      
1598            }
1599            else if(o instanceof ObjectWrap) return toFloatValue(((ObjectWrap)o).getEmbededObject(toFloat(defaultValue)),defaultValue);
1600                    
1601            return defaultValue;
1602        }
1603    
1604        /**
1605         * cast a boolean value to a short value
1606         * @param b boolean value to cast
1607         * @return casted short value
1608         */
1609        public static short toShortValue(boolean b) {
1610            return (short)(b?1:0);
1611        }
1612        
1613        /**
1614         * cast a double value to a short value (primitive value type)
1615         * @param d double value to cast
1616         * @return casted short value
1617         */
1618        public static short toShortValue(double d) {
1619            return (short)d;
1620        }
1621        
1622        /**
1623         * cast a char value to a short value (do nothing)
1624         * @param c char value to cast
1625         * @return casted short value
1626         */
1627        public static short toShortValue(char c) {
1628            return (short)c;
1629        }
1630        
1631        /**
1632         * cast a Object to a short value (primitive value type)
1633         * @param o Object to cast
1634         * @return casted short value
1635         * @throws PageException
1636         */
1637        public static short toShortValue(Object o) throws PageException {
1638            if(o instanceof Short) return ((Short)o).shortValue();
1639            if(o instanceof Character) return (short)(((Character)o).charValue());
1640            else if(o instanceof Boolean) return (short)((((Boolean)o).booleanValue())?1:0);
1641            else if(o instanceof Number) return (((Number)o).shortValue());
1642            else if(o instanceof String) return (short)toDoubleValue(o.toString());                                                                                                                                                             
1643            else if(o instanceof Castable) return (short)((Castable)o).castToDoubleValue(); 
1644            else if(o instanceof ObjectWrap) return toShortValue(((ObjectWrap)o).getEmbededObject());
1645                    
1646            throw new CasterException(o,"short");
1647        }
1648        
1649        /**
1650         * cast a Object to a short value (primitive value type)
1651         * @param str Object to cast
1652         * @return casted short value
1653         * @throws PageException
1654         */
1655        public static short toShortValue(String str) throws PageException {
1656            return (short)toDoubleValue(str);     
1657        }
1658        
1659        /**
1660         * cast a Object to a short value (primitive value type)
1661         * @param o Object to cast
1662         * @param defaultValue 
1663         * @return casted short value
1664         */
1665        public static short toShortValue(Object o, short defaultValue) {
1666            if(o instanceof Short) return ((Short)o).shortValue();
1667            if(o instanceof Character) return (short)(((Character)o).charValue());
1668            else if(o instanceof Boolean) return (short)((((Boolean)o).booleanValue())?1:0);
1669            else if(o instanceof Number) return (((Number)o).shortValue());
1670            else if(o instanceof String) return (short)toDoubleValue(o.toString(),defaultValue);                                                                                                                                                             
1671            else if(o instanceof Castable) {
1672                return (short)((Castable)o).castToDoubleValue(defaultValue);
1673                                                                                                                                                
1674            }
1675            else if(o instanceof ObjectWrap) return toShortValue(((ObjectWrap)o).getEmbededObject(toShort(defaultValue)),defaultValue);
1676                    
1677            return defaultValue;
1678        }
1679        
1680        /**
1681         * cast a boolean value to a Short Object(reference type)
1682         * @param b boolean value to cast
1683         * @return casted Short Object
1684         */
1685        public static Short toShort(boolean b) {
1686            return Short.valueOf(toShortValue(b));
1687        }
1688        
1689        /**
1690         * cast a char value to a Short Object(reference type)
1691         * @param c char value to cast
1692         * @return casted Short Object
1693         */
1694        public static Short toShort(char c) {
1695            return Short.valueOf(toShortValue(c));
1696        }
1697        
1698        /**
1699         * cast a double value to a Byte Object(reference type)
1700         * @param d double value to cast
1701         * @return casted Byte Object
1702         */
1703        public static Short toShort(double d) {
1704            return Short.valueOf(toShortValue(d));
1705        }
1706    
1707        /**
1708         * cast a Object to a Byte Object(reference type)
1709         * @param o Object to cast
1710         * @return casted Byte Object
1711         * @throws PageException
1712         */
1713        public static Short toShort(Object o) throws PageException {
1714            if(o instanceof Short) return (Short)o;
1715            return Short.valueOf(toShortValue(o));
1716            
1717        }
1718    
1719        /**
1720         * cast a Object to a Byte Object(reference type)
1721         * @param str Object to cast
1722         * @return casted Byte Object
1723         * @throws PageException
1724         */
1725        public static Short toShort(String str) throws PageException {
1726            return Short.valueOf(toShortValue(str));
1727            
1728        }
1729    
1730        /**
1731         * cast a Object to a Byte Object(reference type)
1732         * @param o Object to cast
1733         * @param defaultValue 
1734         * @return casted Byte Object
1735         */
1736        public static Short toShort(Object o, Short defaultValue) {
1737            if(o instanceof Short) return (Short)o;
1738            if(defaultValue!=null)return Short.valueOf(toShortValue(o,defaultValue.shortValue()));
1739            short res=toShortValue(o,Short.MIN_VALUE);
1740            if(res==Short.MIN_VALUE) return defaultValue;
1741            return Short.valueOf(res);
1742        }
1743        
1744        /**
1745         * cast a String to a boolean value (primitive value type)
1746         * @param str String to cast
1747         * @return casted boolean value
1748         * @throws ExpressionException
1749         */
1750        public static boolean stringToBooleanValue(String str) throws ExpressionException {
1751            str=StringUtil.toLowerCase(str.trim());
1752            if(str.equals("yes") || str.equals("true")) return true;
1753            else if(str.equals("no") || str.equals("false")) return false;
1754            throw new CasterException("Can't cast String ["+str+"] to boolean");
1755        }
1756        
1757        /**
1758         * cast a String to a boolean value (primitive value type), return 1 for true, 0 for false and -1 if can't cast to a boolean type
1759         * @param str String to cast
1760         * @return casted boolean value
1761         */
1762        public static int stringToBooleanValueEL(String str) {
1763            if(str.length()<2) return -1;
1764            switch(str.charAt(0)) {
1765                case 't':
1766                case 'T': return str.equalsIgnoreCase("true")?1:-1;
1767                case 'f':
1768                case 'F': return str.equalsIgnoreCase("false")?0:-1;
1769                case 'y':
1770                case 'Y': return str.equalsIgnoreCase("yes")?1:-1;
1771                case 'n':
1772                case 'N': return str.equalsIgnoreCase("no")?0:-1;
1773            }
1774            return -1;
1775        }
1776    
1777        /**
1778         * cast a Object to a String
1779         * @param o Object to cast
1780         * @return casted String
1781         * @throws PageException
1782         */
1783        public static String toString(Object o) throws PageException {
1784            if(o instanceof String) return (String)o;
1785            else if(o instanceof Number) return toString(((Number)o).doubleValue());
1786            else if(o instanceof Boolean) return toString(((Boolean)o).booleanValue());
1787            else if(o instanceof Castable) return ((Castable)o).castToString();
1788            else if(o instanceof Date) {
1789                if(o instanceof DateTime) return ((DateTime)o).castToString();
1790                return new DateTimeImpl((Date)o).castToString();
1791            }
1792            else if(o instanceof Clob) return toString((Clob)o);
1793            else if(o instanceof Node) return XMLCaster.toString((Node)o);
1794            else if(o instanceof Reader) {
1795                    Reader r=null;
1796                            try {
1797                                    return IOUtil.toString(r=(Reader)o);
1798                            } 
1799                            catch (IOException e) {
1800                                    throw Caster.toPageException(e);
1801                            }
1802                            finally {
1803                                    IOUtil.closeEL(r);
1804                            }
1805            }
1806            else if(o instanceof InputStream) {
1807                    Config config = ThreadLocalPageContext.getConfig();
1808                    InputStream r=null;
1809                            try {
1810                                    return IOUtil.toString(r=(InputStream)o,config.getWebCharset());
1811                            } 
1812                            catch (IOException e) {
1813                                    throw Caster.toPageException(e);
1814                            }
1815                            finally {
1816                                    IOUtil.closeEL(r);
1817                            }
1818            }
1819            else if(o instanceof byte[]) {
1820                    Config config = ThreadLocalPageContext.getConfig();
1821                    
1822                    try {
1823                                    return new String((byte[])o,config.getWebCharset());
1824                            } catch (Throwable t) {
1825                                    return new String((byte[])o);
1826                            }
1827            }
1828            else if(o instanceof char[]) return new String((char[])o);
1829            else if(o instanceof ObjectWrap) return toString(((ObjectWrap)o).getEmbededObject());
1830            else if(o instanceof Calendar) return toString(((Calendar)o).getTime());
1831            else if(o == null) return "";
1832            
1833            // INFO Collection is new of type Castable 
1834            if(o instanceof Map || o instanceof List || o instanceof Function)
1835                throw new CasterException(o,"string");
1836            /*if((x instanceof Query) || 
1837                            (x instanceof RowSet) || 
1838                            (x instanceof coldfusion.runtime.Array) || 
1839                            (x instanceof JavaProxy) || 
1840                            (x instanceof FileStreamWrapper))
1841            */
1842            
1843            
1844            return o.toString();
1845        }
1846        
1847        /**
1848         * cast a String to a String (do Nothing)
1849         * @param o Object to cast
1850         * @return casted String
1851         * @throws PageException
1852         */
1853        public static String toString(String str) {
1854            return str;
1855        }
1856        
1857        public static StringBuffer toStringBuffer(Object obj) throws PageException {
1858            if(obj instanceof StringBuffer) return (StringBuffer) obj;
1859            return new StringBuffer(toString(obj));
1860        }
1861    
1862        public static Collection.Key toKey(Object o) throws CasterException { 
1863            return KeyImpl.toKey(o);
1864        }
1865        public static Collection.Key toKey(Object o,Collection.Key defaultValue) {
1866            return KeyImpl.toKey(o, defaultValue);
1867        }
1868        
1869    
1870        /**
1871         * cast a Object to a String dont throw a exception, if can't cast to a string return a empty string
1872         * @param o Object to cast
1873         * @param defaultValue 
1874         * @return casted String
1875         */
1876        public static String toString(Object o,String defaultValue) {
1877            if(o instanceof String) return (String)o;
1878            else if(o instanceof Boolean) return toString(((Boolean)o).booleanValue());
1879            else if(o instanceof Number) return toString(((Number)o).doubleValue());
1880            else if(o instanceof Castable) return ((Castable)o).castToString(defaultValue);
1881            else if(o instanceof Date) {
1882                if(o instanceof DateTime) {
1883                    return ((DateTime)o).castToString(defaultValue);
1884                    
1885                }
1886                return new DateTimeImpl((Date)o).castToString(defaultValue);
1887            }
1888            else if(o instanceof Clob) {
1889                try {
1890                    return toString((Clob)o);
1891                } catch (ExpressionException e) {
1892                    return defaultValue;
1893                }
1894            }
1895            else if(o instanceof Node) {
1896                try {
1897                    return XMLCaster.toString((Node)o);
1898                } catch (PageException e) {
1899                    return defaultValue;
1900                }
1901            }
1902            else if(o instanceof Map || o instanceof List || o instanceof Function) return defaultValue;
1903            else if(o == null) return "";
1904            else if(o instanceof ObjectWrap) return toString(((ObjectWrap)o).getEmbededObject(defaultValue),defaultValue);
1905                    return o.toString();
1906                    /// TODO diese methode ist nicht gleich wie toString(Object)
1907        }
1908    
1909        private static String toString(Clob clob) throws ExpressionException {
1910            try {
1911            Reader in = clob.getCharacterStream();
1912            StringBuffer buf = new StringBuffer();
1913            for(int c=in.read();c != -1;c = in.read()) {
1914                buf.append((char)c);
1915            }
1916            return buf.toString();
1917            }
1918            catch(Exception e) {
1919                throw ExpressionException.newInstance(e);
1920            }
1921        }
1922        
1923        
1924        /**
1925         * cast a double value to a String
1926         * @param d double value to cast
1927         * @return casted String
1928         */
1929        public static String toString3(double d) {
1930            
1931            long l = (long)d;
1932            if(l == d) return toString(l);
1933            String str = Double.toString(d);
1934            int pos;
1935            if((pos=str.indexOf('E'))!=-1 && pos==str.length()-2){
1936                return new StringBuffer(pos+2).
1937                append(str.charAt(0)).
1938                append(str.substring(2,toDigit(str.charAt(pos+1))+2)).
1939                append('.').
1940                append(str.substring(toDigit(str.charAt(pos+1))+2,pos)).
1941                toString();
1942                
1943            }
1944            return str;
1945        }
1946        private static DecimalFormat df=(DecimalFormat) DecimalFormat.getInstance(Locale.US);//("#.###########");
1947            //public static int count;
1948        static {
1949            df.applyLocalizedPattern("#.############");
1950        }
1951        public static String toString(double d) {
1952            long l = (long)d;
1953            if(l == d) return toString(l);
1954            
1955            if(d>l && (d-l)<0.000000000001)  return toString(l);
1956            if(l>d && (l-d)<0.000000000001)  return toString(l);
1957            return df.format(d);
1958        }
1959        
1960        /**
1961         * cast a long value to a String
1962         * @param l long value to cast
1963         * @return casted String
1964         */
1965        public static String toString(long l) {
1966            if(l<NUMBERS_MIN || l>NUMBERS_MAX) {
1967                return Long.toString(l, 10);
1968            }
1969            return NUMBERS[(int)l];
1970        }
1971        
1972        /**
1973         * cast a int value to a String
1974         * @param i int value to cast
1975         * @return casted String
1976         */
1977        public static String toString(int i) {
1978            if(i<NUMBERS_MIN || i>NUMBERS_MAX) return Integer.toString(i, 10);
1979            return NUMBERS[i];
1980        }
1981    
1982        /**
1983         * cast a boolean value to a String
1984         * @param b boolean value to cast
1985         * @return casted String
1986         */
1987        public static String toString(boolean b) {
1988            return b?"true":"false";
1989        }
1990    
1991        public static UDF toFunction(Object o) throws PageException {
1992            if(o instanceof UDF) return (UDF)o;
1993            
1994            else if(o instanceof ObjectWrap) {
1995                return toFunction(((ObjectWrap)o).getEmbededObject());
1996            }
1997            throw new CasterException(o,"function");
1998        }
1999    
2000        public static UDF toFunction(Object o, UDF defaultValue) {
2001            if(o instanceof UDF) return (UDF)o;
2002            
2003            else if(o instanceof ObjectWrap) {
2004                return toFunction(((ObjectWrap)o).getEmbededObject(defaultValue),defaultValue);
2005            }
2006            return defaultValue;
2007        }
2008        
2009        /**
2010         * cast a Object to a Array Object
2011         * @param o Object to cast
2012         * @return casted Array
2013         * @throws PageException
2014         */
2015        public static List toList(Object o) throws PageException {
2016            return toList(o,false);
2017        }
2018        
2019        /**
2020         * cast a Object to a Array Object
2021         * @param o Object to cast
2022         * @param defaultValue 
2023         * @return casted Array
2024         */
2025        public static List toList(Object o, List defaultValue) {
2026            return toList(o,false,defaultValue);
2027        }
2028        
2029        /**
2030         * cast a Object to a Array Object
2031         * @param o Object to cast
2032         * @param duplicate 
2033         * @param defaultValue 
2034         * @return casted Array
2035         */
2036        public static List toList(Object o, boolean duplicate, List defaultValue) {
2037            try {
2038                return toList(o,duplicate);
2039            } catch (PageException e) {
2040                return defaultValue;
2041            }
2042        }
2043        
2044        /**
2045         * cast a Object to a Array Object
2046         * @param o Object to cast
2047         * @param duplicate 
2048         * @return casted Array
2049         * @throws PageException
2050         */
2051        public static List toList(Object o, boolean duplicate) throws PageException {
2052            if(o instanceof List) {
2053                if(duplicate) {
2054                    List src=(List)o;
2055                    int size=src.size();
2056                    ArrayList trg = new ArrayList();
2057                    
2058                    for(int i=0;i<size;i++) {
2059                        trg.add(i,src.get(i));
2060                    }
2061                    return trg;
2062                    
2063                }
2064                return (List)o;
2065            }
2066            else if(o instanceof Object[]) {
2067                    ArrayList list=new ArrayList();
2068                Object[] arr=(Object[])o;
2069                for(int i=0;i<arr.length;i++)list.add(i,arr[i]);
2070                return list;
2071            }
2072            else if(o instanceof Array) {
2073                if(!duplicate)return ArrayAsList.toList((Array)o);
2074                    ArrayList list=new ArrayList();
2075                Array arr=(Array)o;
2076                for(int i=0;i<arr.size();i++)list.add(i,arr.get(i+1,null));
2077                return list;
2078            }
2079            else if(o instanceof XMLStruct) {
2080                XMLStruct sct=((XMLStruct)o);
2081                if(sct instanceof XMLMultiElementStruct) return toList(new XMLMultiElementArray((XMLMultiElementStruct) o));
2082                ArrayList list=new ArrayList();
2083                list.add(sct);
2084                return list;
2085            }
2086            else if(o instanceof ObjectWrap) {
2087                return toList(((ObjectWrap)o).getEmbededObject());
2088            }
2089            else if(o instanceof Struct) {
2090                Struct sct=(Struct) o;
2091                ArrayList arr=new ArrayList();
2092                
2093                Iterator<Entry<Key, Object>> it = sct.entryIterator();
2094                Entry<Key, Object> e=null;
2095                try {
2096                    while(it.hasNext()) {
2097                            e = it.next();
2098                        arr.add(toIntValue(e.getKey().getString()),e.getValue());
2099                    }
2100                } 
2101                catch (ExpressionException ee) {
2102                    throw new ExpressionException("can't cast struct to a array, key ["+(e!=null?e.getKey():"")+"] is not a number");
2103                }
2104                return arr;
2105            }
2106            else if(o instanceof boolean[])return toList(ArrayUtil.toReferenceType((boolean[])o));
2107            else if(o instanceof byte[])return toList(ArrayUtil.toReferenceType((byte[])o));
2108            else if(o instanceof char[])return toList(ArrayUtil.toReferenceType((char[])o));
2109            else if(o instanceof short[])return toList(ArrayUtil.toReferenceType((short[])o));
2110            else if(o instanceof int[])return toList(ArrayUtil.toReferenceType((int[])o));
2111            else if(o instanceof long[])return toList(ArrayUtil.toReferenceType((long[])o));
2112            else if(o instanceof float[])return toList(ArrayUtil.toReferenceType((float[])o));
2113            else if(o instanceof double[])return toList(ArrayUtil.toReferenceType((double[])o));
2114            
2115            throw new CasterException(o,"List");
2116            
2117    
2118        }
2119        
2120        /**
2121         * cast a Object to a Array Object
2122         * @param o Object to cast
2123         * @return casted Array
2124         * @throws PageException
2125         */
2126        public static Array toArray(Object o) throws PageException {
2127            if(o instanceof Array) return (Array)o;
2128            else if(o instanceof Object[]) {
2129                return new ArrayImpl((Object[])o);
2130            }
2131            else if(o instanceof List) {
2132                return ListAsArray.toArray((List)o);//new ArrayImpl(((List) o).toArray());
2133            }
2134            else if(o instanceof Set) {
2135                    
2136                return toArray(((Set)o).toArray());//new ArrayImpl(((List) o).toArray());
2137            }
2138            else if(o instanceof XMLStruct) {
2139                    XMLMultiElementStruct xmes;
2140                    if(o instanceof XMLMultiElementStruct) {
2141                            xmes=(XMLMultiElementStruct)o;
2142                    }
2143                    else {
2144                            XMLStruct sct=(XMLStruct) o;
2145                        Array a=new ArrayImpl();
2146                        a.append(o);
2147                        xmes=new XMLMultiElementStruct(a, sct.getCaseSensitive());
2148                    }
2149                    return new XMLMultiElementArray(xmes);
2150            }
2151            else if(o instanceof ObjectWrap) {
2152                return toArray(((ObjectWrap)o).getEmbededObject());
2153            }
2154            else if(o instanceof Struct) {
2155                Struct sct=(Struct) o;
2156                Array arr=new ArrayImpl();
2157                
2158                Iterator<Entry<Key, Object>> it = sct.entryIterator();
2159                Entry<Key, Object> e=null;
2160                try {
2161                    while(it.hasNext()) {
2162                            e = it.next();
2163                        arr.setE(toIntValue(e.getKey().getString()),e.getValue());
2164                    }
2165                } 
2166                catch (ExpressionException ee) {
2167                    throw new ExpressionException("can't cast struct to a array, key ["+e.getKey().getString()+"] is not a number");
2168                }
2169                return arr;
2170            }
2171            else if(o instanceof boolean[])return new ArrayImpl(ArrayUtil.toReferenceType((boolean[])o));
2172            else if(o instanceof byte[])return new ArrayImpl(ArrayUtil.toReferenceType((byte[])o));
2173            else if(o instanceof char[])return new ArrayImpl(ArrayUtil.toReferenceType((char[])o));
2174            else if(o instanceof short[])return new ArrayImpl(ArrayUtil.toReferenceType((short[])o));
2175            else if(o instanceof int[])return new ArrayImpl(ArrayUtil.toReferenceType((int[])o));
2176            else if(o instanceof long[])return new ArrayImpl(ArrayUtil.toReferenceType((long[])o));
2177            else if(o instanceof float[])return new ArrayImpl(ArrayUtil.toReferenceType((float[])o));
2178            else if(o instanceof double[])return new ArrayImpl(ArrayUtil.toReferenceType((double[])o));
2179            
2180            throw new CasterException(o,"Array");
2181        }
2182        
2183        public static Object[] toNativeArray(Object o) throws PageException {
2184            if(o instanceof Object[]) {
2185                return (Object[])o;
2186            }
2187            else if(o instanceof Array) {
2188                    Array arr=(Array)o;
2189                    Object[] objs=new Object[arr.size()];
2190                    for(int i=0;i<objs.length;i++) {
2191                            objs[i]=arr.get(i+1, null);
2192                    }
2193                    return objs;
2194            }
2195            else if(o instanceof List) {
2196                return ((List) o).toArray();
2197            }
2198            else if(o instanceof XMLStruct) {
2199                XMLStruct sct=((XMLStruct)o);
2200                if(sct instanceof XMLMultiElementStruct) return toNativeArray((sct));
2201                Object[] a=new Object[1];
2202                a[0]=sct;
2203                return a;
2204            }
2205            else if(o instanceof ObjectWrap) {
2206                return toNativeArray(((ObjectWrap)o).getEmbededObject());
2207            }
2208            else if(o instanceof Struct) {
2209                Struct sct=(Struct) o;
2210                Array arr=new ArrayImpl();
2211                
2212                Iterator<Entry<Key, Object>> it = sct.entryIterator();
2213                Entry<Key, Object> e=null;
2214                try {
2215                    while(it.hasNext()) {
2216                            e=it.next();
2217                        arr.setE(toIntValue(e.getKey().getString()),e.getValue());
2218                    }
2219                } 
2220                catch (ExpressionException ee) {
2221                    throw new ExpressionException("can't cast struct to a array, key ["+e.getKey()+"] is not a number");
2222                }
2223                return toNativeArray(arr);
2224            }
2225            else if(o instanceof boolean[])return ArrayUtil.toReferenceType((boolean[])o);
2226            else if(o instanceof byte[])return ArrayUtil.toReferenceType((byte[])o);
2227            else if(o instanceof char[])return ArrayUtil.toReferenceType((char[])o);
2228            else if(o instanceof short[])return ArrayUtil.toReferenceType((short[])o);
2229            else if(o instanceof int[])return ArrayUtil.toReferenceType((int[])o);
2230            else if(o instanceof long[])return ArrayUtil.toReferenceType((long[])o);
2231            else if(o instanceof float[])return ArrayUtil.toReferenceType((float[])o);
2232            else if(o instanceof double[])return ArrayUtil.toReferenceType((double[])o);
2233            
2234            throw new CasterException(o,"Array");
2235        }
2236        
2237        /**
2238         * cast a Object to a Array Object
2239         * @param o Object to cast
2240         * @param defaultValue 
2241         * @return casted Array
2242         */
2243        public static Array toArray(Object o, Array defaultValue) {
2244            if(o instanceof Array) return (Array)o;
2245            else if(o instanceof Object[]) {
2246                return new ArrayImpl((Object[])o);
2247            }
2248            else if(o instanceof List) {
2249                return new ArrayImpl(((List) o).toArray());
2250            }
2251            else if(o instanceof XMLStruct) {
2252                Array arr = new ArrayImpl();
2253                arr.appendEL(o);
2254                return arr;
2255            }
2256            else if(o instanceof ObjectWrap) {
2257                return toArray(((ObjectWrap)o).getEmbededObject(defaultValue),defaultValue);
2258                //if(io!=null)return toArray(io,defaultValue);
2259            }
2260            else if(o instanceof Struct) {
2261                Struct sct=(Struct) o;
2262                Array arr=new ArrayImpl();
2263                
2264                Iterator<Entry<Key, Object>> it = sct.entryIterator();
2265                Entry<Key, Object> e=null;
2266                try {
2267                    while(it.hasNext()) {
2268                            e=it.next();
2269                        arr.setEL(toIntValue(e.getKey().getString()),e.getValue());
2270                    }
2271                } 
2272                catch (ExpressionException ee) {
2273                    return defaultValue;
2274                }
2275                return arr;
2276            }
2277            else if(o instanceof boolean[])return new ArrayImpl(ArrayUtil.toReferenceType((boolean[])o));
2278            else if(o instanceof byte[])return new ArrayImpl(ArrayUtil.toReferenceType((byte[])o));
2279            else if(o instanceof char[])return new ArrayImpl(ArrayUtil.toReferenceType((char[])o));
2280            else if(o instanceof short[])return new ArrayImpl(ArrayUtil.toReferenceType((short[])o));
2281            else if(o instanceof int[])return new ArrayImpl(ArrayUtil.toReferenceType((int[])o));
2282            else if(o instanceof long[])return new ArrayImpl(ArrayUtil.toReferenceType((long[])o));
2283            else if(o instanceof float[])return new ArrayImpl(ArrayUtil.toReferenceType((float[])o));
2284            else if(o instanceof double[])return new ArrayImpl(ArrayUtil.toReferenceType((double[])o));
2285            
2286            return defaultValue;
2287        }
2288        
2289        /**
2290         * cast a Object to a Map Object
2291         * @param o Object to cast
2292         * @return casted Struct
2293         * @throws PageException
2294         */
2295        public static Map toMap(Object o) throws PageException {
2296            return toMap(o,false);
2297        }
2298        
2299        /**
2300         * cast a Object to a Map Object
2301         * @param o Object to cast
2302         * @param defaultValue 
2303         * @return casted Struct
2304         */
2305        public static Map toMap(Object o, Map defaultValue) {
2306            return toMap(o,false,defaultValue);
2307        }
2308        
2309        /**
2310         * cast a Object to a Map Object
2311         * @param o Object to cast
2312         * @param duplicate 
2313         * @param defaultValue 
2314         * @return casted Struct
2315         */
2316        public static Map toMap(Object o, boolean duplicate, Map defaultValue) {
2317            try {
2318                return toMap(o,duplicate);
2319            } catch (PageException e) {
2320                return defaultValue;
2321            }
2322        }
2323        
2324        /**
2325         * cast a Object to a Map Object
2326         * @param o Object to cast
2327         * @param duplicate 
2328         * @return casted Struct
2329         * @throws PageException
2330         */
2331        public static Map toMap(Object o, boolean duplicate) throws PageException {
2332            if(o instanceof Struct) {
2333                if(duplicate) return (Map) Duplicator.duplicate(o,false);
2334                return ((Struct)o);
2335            }
2336            else if(o instanceof Map){
2337                if(duplicate) return (Map)Duplicator.duplicate(o,false);
2338                return (Map)o;
2339            }
2340            else if(o instanceof Node) {
2341                if(duplicate) {
2342                    return toMap(XMLCaster.toXMLStruct((Node)o,false),duplicate);
2343                }
2344                return (XMLCaster.toXMLStruct((Node)o,false));
2345            }
2346            else if(o instanceof ObjectWrap) {
2347                return toMap(((ObjectWrap)o).getEmbededObject(),duplicate);
2348            }
2349            throw new CasterException(o,"Map");
2350        }
2351        
2352        /**
2353         * cast a Object to a Struct Object
2354         * @param o Object to cast
2355         * @param defaultValue 
2356         * @return casted Struct
2357         */
2358        
2359        
2360        public static Struct toStruct(Object o, Struct defaultValue, boolean caseSensitive) {
2361            if(o instanceof Struct) return (Struct)o;
2362            else if(o instanceof Map) {
2363                return MapAsStruct.toStruct((Map)o,caseSensitive);
2364            }
2365            else if(o instanceof Node)return XMLCaster.toXMLStruct((Node)o,false);
2366            else if(o instanceof ObjectWrap) {
2367                return toStruct(((ObjectWrap)o).getEmbededObject(defaultValue),defaultValue,caseSensitive);
2368            }
2369            return defaultValue;
2370        }
2371        
2372        /**
2373         * cast a Object to a Struct Object
2374         * @param o Object to cast
2375         * @return casted Struct
2376         */
2377        public static Struct toStruct(Object o) throws PageException {
2378            return toStruct(o,true);
2379        }
2380        
2381        public static Struct toStruct(Object o,Struct defaultValue) {
2382            return toStruct(o, defaultValue, true);
2383        }
2384        
2385        public static Struct toStruct(Object o,boolean caseSensitive) throws PageException {
2386            if(o instanceof Struct) return (Struct)o;
2387            else if(o instanceof Map)return MapAsStruct.toStruct((Map)o,caseSensitive);//_toStruct((Map)o,caseSensitive);
2388            else if(o instanceof Node)return XMLCaster.toXMLStruct((Node)o,false);
2389            else if(o instanceof ObjectWrap) {
2390                    if(o instanceof JavaObject ) {
2391                            Struct sct = toStruct(((JavaObject)o).getEmbededObject(null),null,caseSensitive);
2392                    if(sct!=null) return sct;
2393                            
2394                            JavaObject jo = (JavaObject)o;
2395                            return new ObjectStruct(jo);
2396                    }
2397                return toStruct(((ObjectWrap)o).getEmbededObject(),caseSensitive);
2398            }
2399            if(Decision.isSimpleValue(o) || Decision.isArray(o))
2400                    throw new CasterException(o,"Struct");
2401            if(o instanceof Collection) return new CollectionStruct((Collection)o);
2402            return new ObjectStruct(o);
2403        }
2404        
2405        /*private static Struct _toStruct(Map map) {
2406            Struct sct = new StructImpl();
2407            Iterator it=map.keySet().iterator();
2408            while(it.hasNext()) {
2409                Object key=it.next();
2410                sct.set(StringUtil.toLowerCase(Caster.toString(key)),map.get(key));
2411            }
2412            return sct;
2413        }*/
2414    
2415        /**
2416         * cast a Object to a Binary
2417         * @param o Object to cast
2418         * @return casted Binary
2419         * @throws PageException 
2420         */
2421        public static byte[] toBinary(Object o) throws PageException {
2422            if(o instanceof byte[]) return (byte[])o;
2423            else if(o instanceof ObjectWrap) return toBinary(((ObjectWrap)o).getEmbededObject(""));
2424                    
2425            else if(o instanceof InputStream) {
2426                ByteArrayOutputStream barr = new ByteArrayOutputStream();
2427                try {
2428                    IOUtil.copy((InputStream)o,barr,false,true);
2429                } catch (IOException e) {
2430                    throw ExpressionException.newInstance(e);
2431                }
2432                return barr.toByteArray();            
2433            }
2434            else if(o instanceof Image) {
2435                    return ((Image)o).getImageBytes(null);
2436            }
2437            else if(o instanceof BufferedImage) {
2438                    return new Image(((BufferedImage)o)).getImageBytes("png");
2439            }
2440            else if(o instanceof ByteArrayOutputStream) {
2441                    return ((ByteArrayOutputStream)o).toByteArray();
2442            }
2443            else if(o instanceof Blob) {
2444                    InputStream is=null;
2445                    try {
2446                            is=((Blob)o).getBinaryStream();
2447                            return IOUtil.toBytes(is);
2448                    } 
2449                    catch (Exception e) {
2450                            throw new ExpressionException(e.getMessage());
2451                            }
2452                    finally {
2453                            IOUtil.closeEL(is);
2454                    }
2455            }
2456            try {
2457                            return Coder.decode(Coder.ENCODING_BASE64,toString(o));
2458                    } 
2459            catch (CoderException e) {
2460                            throw new CasterException(e.getMessage(),"binary");
2461                    }
2462            catch (PageException e) {
2463                throw new CasterException(o,"binary");
2464            }
2465        }
2466        /**
2467         * cast a Object to a Binary
2468         * @param o Object to cast
2469         * @param defaultValue 
2470         * @return casted Binary
2471         */
2472        public static byte[] toBinary(Object o, byte[] defaultValue) {
2473            try {
2474                return toBinary(o);
2475            } 
2476            catch (PageException e) {
2477                return defaultValue;
2478            }
2479        }
2480        
2481        public static Object toCreditCard(Object o) throws PageException {
2482            return ValidateCreditCard.toCreditcard(toString(o));
2483            }
2484        
2485        public static Object toCreditCard(Object o, String defaultValue) {
2486            //print.out("enter");
2487            String str=toString(o,null);
2488            if(str==null)return defaultValue;
2489            //print.out("enter:"+str+":"+ValidateCreditCard.toCreditcard(str,defaultValue));
2490            
2491            return ValidateCreditCard.toCreditcard(str,defaultValue);
2492            }
2493         
2494        /**
2495         * cast a Object to a Base64 value
2496         * @param o Object to cast
2497         * @return to Base64 String
2498         * @throws PageException
2499         */
2500        public static String toBase64(Object o,String charset) throws PageException {
2501            String str=toBase64(o,charset,null);
2502            if(str==null) throw new CasterException(o,"base 64");
2503            return str;
2504        }
2505        
2506        /**
2507         * cast a Object to a Base64 value
2508         * @param o Object to cast
2509         * @param defaultValue 
2510         * @return to Base64 String
2511         */
2512        public static String toBase64(Object o,String charset,String defaultValue) {
2513            byte[] b;
2514            if(o instanceof byte[])b=(byte[]) o;
2515            else if(o instanceof String)return toB64((String)o, charset,defaultValue);
2516            else if(o instanceof ObjectWrap) {
2517                return toBase64(((ObjectWrap)o).getEmbededObject(defaultValue),charset,defaultValue);
2518            }
2519            else if(o == null) return toBase64("",charset,defaultValue);
2520            else {
2521                    String str = toString(o,null);
2522                    if(str!=null)return toBase64(str,charset,defaultValue);
2523                    
2524                    b=toBinary(o,null);
2525                    if(b==null)return defaultValue;
2526            }
2527            return toB64(b,defaultValue);
2528        }
2529    
2530    
2531        public static String toB64(String str,String charset) throws UnsupportedEncodingException {
2532            return toB64(str.getBytes(charset));
2533        }
2534        
2535        public static String toB64(byte[] b)  {
2536            return Base64Coder.encode(b);
2537        }
2538    
2539        public static String toB64(String str,String charset, String defaultValue) {
2540            if(StringUtil.isEmpty(charset,true))charset="UTF-8";
2541            try {
2542                            return Base64Coder.encodeFromString(str,charset);
2543                    } catch (Throwable t) {
2544                            return defaultValue;
2545                    }
2546        }
2547        
2548        public static String toB64(byte[] b, String defaultValue) {
2549            try {
2550                            return Base64Coder.encode(b);
2551                    } catch (Throwable t) {
2552                            return defaultValue;
2553                    }
2554        }
2555    
2556        /**
2557         * cast a boolean to a DateTime Object
2558         * @param b boolean to cast
2559         * @param tz
2560         * @return casted DateTime Object
2561         */
2562        public static DateTime toDate(boolean b, TimeZone tz) {
2563            return DateCaster.toDateSimple(b,tz);
2564        }
2565    
2566        /**
2567         * cast a char to a DateTime Object
2568         * @param c char to cast
2569         * @param tz
2570         * @return casted DateTime Object
2571         */
2572        public static DateTime toDate(char c, TimeZone tz) {
2573            return DateCaster.toDateSimple(c,tz);
2574        }
2575    
2576        /**
2577         * cast a double to a DateTime Object
2578         * @param d double to cast
2579         * @param tz
2580         * @return casted DateTime Object
2581         */
2582        public static DateTime toDate(double d, TimeZone tz) {
2583            return DateCaster.toDateSimple(d,tz);
2584        }
2585    
2586        /**
2587         * cast a Object to a DateTime Object
2588         * @param o Object to cast
2589         * @param tz
2590         * @return casted DateTime Object
2591         * @throws PageException
2592         */
2593        public static DateTime toDate(Object o, TimeZone tz) throws PageException {
2594            return DateCaster.toDateAdvanced(o,tz);
2595        }
2596        
2597        /**
2598         * cast a Object to a DateTime Object
2599         * @param str String to cast
2600         * @param tz
2601         * @return casted DateTime Object
2602         * @throws PageException
2603         */
2604        public static DateTime toDate(String str, TimeZone tz) throws PageException {
2605            return DateCaster.toDateAdvanced(str,tz);
2606        }
2607    
2608        /**
2609         * cast a Object to a DateTime Object
2610         * @param o Object to cast
2611         * @param alsoNumbers define if also numbers will casted to a datetime value
2612         * @param tz
2613         * @return casted DateTime Object
2614         * @throws PageException 
2615         */
2616        public static DateTime toDate(Object o,boolean alsoNumbers, TimeZone tz) throws PageException {
2617            return DateCaster.toDateAdvanced(o,alsoNumbers,tz);
2618        }
2619        
2620        /**
2621         * cast a Object to a DateTime Object
2622         * @param o Object to cast
2623         * @param alsoNumbers define if also numbers will casted to a datetime value
2624         * @param tz
2625         * @param defaultValue 
2626         * @return casted DateTime Object
2627         */
2628        public static DateTime toDate(Object o,boolean alsoNumbers, TimeZone tz, DateTime defaultValue) {
2629            return DateCaster.toDateAdvanced(o,alsoNumbers,tz,defaultValue);
2630        }
2631    
2632        /**
2633         * cast a Object to a DateTime Object
2634         * @param str String to cast
2635         * @param alsoNumbers define if also numbers will casted to a datetime value
2636         * @param tz
2637         * @param defaultValue 
2638         * @return casted DateTime Object
2639         */
2640        public static DateTime toDate(String str,boolean alsoNumbers, TimeZone tz, DateTime defaultValue) {
2641            return DateCaster.toDateAdvanced(str,alsoNumbers,tz,defaultValue);
2642        }
2643    
2644        /**
2645         * cast a Object to a DateTime Object
2646         * @param o Object to cast
2647         * @param tz
2648         * @return casted DateTime Object
2649         * @throws PageException
2650         */
2651        public static DateTime toDateTime(Object o, TimeZone tz) throws PageException {
2652            return DateCaster.toDateAdvanced(o,tz);
2653        }
2654        
2655        /**
2656         * cast a Object to a DateTime Object (alias for toDateTime)
2657         * @param o Object to cast
2658         * @param tz
2659         * @return casted DateTime Object
2660         * @throws PageException
2661         */
2662        public static DateTime toDatetime(Object o, TimeZone tz) throws PageException {
2663            return DateCaster.toDateAdvanced(o,tz);
2664        }
2665    
2666        /**
2667         * cast a Object to a Query Object
2668         * @param o Object to cast
2669         * @return casted Query Object
2670         * @throws PageException
2671         */
2672        public static Query toQuery(Object o) throws PageException {
2673            if(o instanceof Query) return (Query)o;
2674            if(o instanceof ObjectWrap) {
2675                return toQuery(((ObjectWrap)o).getEmbededObject());
2676            }
2677            if(o instanceof ResultSet) return new QueryImpl((ResultSet)o,"query", ThreadLocalPageContext.getTimeZone());
2678            throw new CasterException(o,"query");
2679        }
2680        
2681        /**
2682         * converts a object to a QueryColumn, if possible
2683         * @param o
2684         * @return
2685         * @throws PageException
2686         */
2687        public static QueryColumn toQueryColumn(Object o) throws PageException {
2688            if(o instanceof QueryColumn) return (QueryColumn)o;
2689            throw new CasterException(o,"querycolumn");
2690        }
2691        
2692        /**
2693         * converts a object to a QueryColumn, if possible, also variable declarations are allowed.
2694         * this method is used within the generated bytecode
2695         * @param o
2696         * @return
2697         * @throws PageException
2698         * @info used in bytecode generation
2699         */
2700        public static QueryColumn toQueryColumn(Object o, PageContext pc) throws PageException { 
2701            if(o instanceof QueryColumn) return (QueryColumn)o;
2702            if(o instanceof String) {
2703                    o=VariableInterpreter.getVariableAsCollection(pc, (String)o);
2704                    if(o instanceof QueryColumn) return (QueryColumn) o;
2705            }
2706            
2707            throw new CasterException(o,"querycolumn");
2708        }
2709    
2710        /**
2711         * cast a Object to a Query Object
2712         * @param o Object to cast
2713         * @param defaultValue 
2714         * @return casted Query Object
2715         */
2716        public static Query toQuery(Object o, Query defaultValue) {
2717            if(o instanceof Query) return (Query)o;
2718            else if(o instanceof ObjectWrap) {
2719                return toQuery(((ObjectWrap)o).getEmbededObject(defaultValue),defaultValue);
2720            }
2721            return defaultValue;
2722        }
2723    
2724        /**
2725         * cast a Object to a Query Object
2726         * @param o Object to cast
2727         * @param duplicate duplicate the object or not
2728         * @param defaultValue 
2729         * @return casted Query Object
2730         */
2731        public static Query toQuery(Object o, boolean duplicate, Query defaultValue) {
2732            try {
2733                return toQuery(o,duplicate);
2734            } catch (PageException e) {
2735                return defaultValue;
2736            }
2737        }
2738        
2739        /**
2740         * cast a Object to a Query Object
2741         * @param o Object to cast
2742         * @param duplicate duplicate the object or not
2743         * @return casted Query Object
2744         * @throws PageException
2745         */
2746        public static Query toQuery(Object o, boolean duplicate) throws PageException {
2747            
2748            if(o instanceof Query) {
2749                if(duplicate) {
2750                    Query src = (Query)o;
2751                    Query trg=new QueryImpl(src.getColumnNames(),src.getRowCount(),"query");
2752    
2753                    Collection.Key[] keys=src.getColumnNames();
2754                    QueryColumn[] columnsSrc=new QueryColumn[keys.length];
2755                    for(int i=0;i<columnsSrc.length;i++) {
2756                        columnsSrc[i]=src.getColumn(keys[i]);
2757                    }
2758    
2759                    keys=trg.getColumnNames();
2760                    QueryColumn[] columnsTrg=new QueryColumn[keys.length];
2761                    for(int i=0;i<columnsTrg.length;i++) {
2762                        columnsTrg[i]=trg.getColumn(keys[i]);
2763                    }
2764                    
2765                    int i;
2766                    for(int row=trg.getRecordcount();row>0;row--) {
2767                        for(i=0;i<columnsTrg.length;i++) {
2768                            columnsTrg[i].set(row,columnsSrc[i].get(row,null));
2769                        }
2770                    }
2771                    return trg;
2772                }
2773                return (Query)o;
2774            }
2775            else if(o instanceof ObjectWrap) {
2776                return toQuery(((ObjectWrap)o).getEmbededObject(),duplicate);
2777            }
2778            throw new CasterException(o,"query");
2779        }
2780        
2781        /**
2782         * cast a Object to a UUID
2783         * @param o Object to cast
2784         * @return casted Query Object
2785         * @throws PageException
2786         */
2787        public static Object toUUId(Object o) throws PageException {
2788            String str=toString(o);
2789            if(!Decision.isUUId(str))
2790                throw new ExpressionException("can't cast ["+str+"] to uuid value");
2791            return str;
2792        }
2793        
2794        /**
2795         * cast a Object to a UUID
2796         * @param o Object to cast
2797         * @param defaultValue 
2798         * @return casted Query Object
2799         */
2800        public static Object toUUId(Object o, Object defaultValue) {
2801            String str=toString(o,null);
2802            if(str==null) return defaultValue;
2803            
2804            if(!Decision.isUUId(str)) return defaultValue;
2805            return str;
2806        }
2807        
2808    
2809        /**
2810         * cast a Object to a GUID
2811         * @param o Object to cast
2812         * @return casted Query Object
2813         * @throws PageException
2814         */
2815        public static Object toGUId(Object o) throws PageException {
2816            String str=toString(o);
2817            if(!Decision.isGUId(str))
2818                throw new ExpressionException("can't cast ["+str+"] to guid value");
2819            return str;
2820        }
2821        
2822        /**
2823         * cast a Object to a GUID
2824         * @param o Object to cast
2825         * @param defaultValue 
2826         * @return casted Query Object
2827         */
2828        public static Object toGUId(Object o, Object defaultValue) {
2829            String str=toString(o,null);
2830            if(str==null) return defaultValue;
2831            
2832            if(!Decision.isGUId(str)) return defaultValue;
2833            return str;
2834        }
2835        
2836        /**
2837         * cast a Object to a Variable Name
2838         * @param o Object to cast
2839         * @return casted Variable Name
2840         * @throws PageException
2841         */
2842        public static String toVariableName(Object o) throws PageException {
2843            String str=toString(o);
2844            if(!Decision.isVariableName(str))
2845                throw new ExpressionException("can't cast ["+str+"] to variable name value");
2846            return str;
2847        }
2848        
2849        /**
2850         * cast a Object to a Variable Name
2851         * @param o Object to cast
2852         * @param defaultValue 
2853         * @return casted Variable Name
2854         */
2855        public static Object toVariableName(Object o, Object defaultValue) {
2856            String str=toString(o,null);
2857            
2858            if(str==null || !Decision.isVariableName(str))
2859                return defaultValue;
2860            return str;
2861        }
2862        
2863        /**
2864         * cast a Object to a TimeSpan Object
2865         * @param o Object to cast
2866         * @return casted TimeSpan Object
2867         * @throws PageException
2868         */
2869        public static TimeSpan toTimeSpan(Object o) throws PageException {
2870            return toTimespan(o);
2871        }
2872        
2873        /**
2874         * cast a Object to a TimeSpan Object (alias for toTimeSpan)
2875         * @param o Object to cast
2876         * @param defaultValue 
2877         * @return casted TimeSpan Object
2878         */
2879        public static TimeSpan toTimespan(Object o, TimeSpan defaultValue) {
2880            try {
2881                return toTimespan(o);
2882            } catch (PageException e) {
2883                return defaultValue;
2884            }
2885        }
2886            
2887        /**
2888         * cast a Object to a TimeSpan Object (alias for toTimeSpan)
2889         * @param o Object to cast
2890         * @return casted TimeSpan Object
2891         * @throws PageException
2892         */
2893        public static TimeSpan toTimespan(Object o) throws PageException {
2894            if(o instanceof TimeSpan) return (TimeSpan)o;
2895            else if(o instanceof String) {
2896                    String[] arr=o.toString().split(",");
2897                    if(arr.length==4) {
2898                        int[] values=new int[4];
2899                        try {
2900                            for(int i=0;i<arr.length;i++) {
2901                                values[i]=toIntValue(arr[i]);
2902                            }
2903                            return new TimeSpanImpl(values[0],values[1],values[2],values[3]);
2904                        }
2905                        catch(ExpressionException e) {}
2906                    }
2907            }
2908            else if(o instanceof ObjectWrap) {
2909                return toTimespan(((ObjectWrap)o).getEmbededObject());
2910            }
2911            
2912            double dbl = toDoubleValue(o,Double.NaN);
2913            if(!Double.isNaN(dbl))return TimeSpanImpl.fromDays(dbl);
2914            
2915            throw new CasterException(o,"timespan");
2916        }
2917        
2918        /**
2919         * cast a Throwable Object to a PageException Object
2920         * @param t Throwable to cast
2921         * @return casted PageException Object
2922         */
2923        public static PageException toPageException(Throwable t) {
2924            if(t instanceof PageException)
2925                return (PageException)t;
2926            else if(t instanceof PageExceptionBox)
2927                return ((PageExceptionBox)t).getPageException();
2928            else if(t instanceof InvocationTargetException){
2929                return toPageException(((InvocationTargetException)t).getTargetException());
2930            }
2931            else if(t instanceof ExceptionInInitializerError){
2932                return toPageException(((ExceptionInInitializerError)t).getCause());
2933            }
2934            else if(t instanceof ExecutionException){
2935                return toPageException(((ExecutionException)t).getCause());
2936            }
2937            else {
2938                    //Throwable cause = t.getCause();
2939                    //if(cause!=null && cause!=t) return toPageException(cause);
2940                    return new NativeException(t);
2941            }
2942        }
2943        
2944        
2945        
2946        /**
2947         * return the type name of a object (string, boolean, int aso.), type is not same like class name
2948         * @param o Object to get type from 
2949         * @return type of the object
2950        */
2951        public static String toTypeName(Object o) {
2952            if(o == null) return "null";
2953            else if(o instanceof String) return "string";
2954            else if(o instanceof Boolean) return "boolean";
2955            else if(o instanceof Number) return "int";
2956            else if(o instanceof Array) return "array";
2957            else if(o instanceof Component) return "component";
2958            else if(o instanceof Struct) return "struct";
2959            else if(o instanceof Query) return "query";
2960            else if(o instanceof DateTime) return "datetime";
2961            else if(o instanceof byte[]) return "binary";
2962            else if(o instanceof ObjectWrap) {
2963                return toTypeName(((ObjectWrap)o).getEmbededObject(null));
2964            }
2965            
2966            Class clazz=o.getClass();
2967            String className=clazz.getName();
2968                    if(className.startsWith("java.lang.")) {
2969                            return className.substring(10);
2970                    }
2971                    return toClassName(clazz);
2972        }
2973        public static String toTypeName(Class clazz) {
2974            if(Reflector.isInstaneOf(clazz,String.class))   return "string";
2975            if(Reflector.isInstaneOf(clazz,Boolean.class))  return "boolean";
2976            if(Reflector.isInstaneOf(clazz,Number.class))   return "numeric";
2977            if(Reflector.isInstaneOf(clazz,Array.class))    return "array";
2978            if(Reflector.isInstaneOf(clazz,Struct.class))   return "struct";
2979            if(Reflector.isInstaneOf(clazz,Query.class))    return "query";
2980            if(Reflector.isInstaneOf(clazz,DateTime.class)) return "datetime";
2981            if(Reflector.isInstaneOf(clazz,byte[].class))   return "binary";
2982           
2983            String className=clazz.getName();
2984                    if(className.startsWith("java.lang.")) {
2985                            return className.substring(10);
2986                    }
2987            return toClassName(clazz);
2988        }
2989    
2990        public static String toClassName(Object o) {
2991            if(o==null)return "null";
2992            return toClassName(o.getClass());
2993        }
2994        public static String toClassName(Class clazz) {
2995            if(clazz.isArray()){
2996                    return toClassName(clazz.getComponentType())+"[]";
2997            }
2998            return clazz.getName();
2999        }
3000        
3001        public static Class cfTypeToClass(String type) throws PageException {
3002            // TODO weitere typen siehe bytecode.cast.Cast
3003            
3004            type=type.trim();
3005            String lcType=StringUtil.toLowerCase(type);
3006            
3007            if(lcType.length()>2) {
3008                char first=lcType.charAt(0);
3009                switch(first) {
3010                    case 'a':
3011                        if(lcType.equals("any")) {
3012                            return Object.class;
3013                        }
3014                        else if(lcType.equals("array")) {
3015                            return Array.class;
3016                        }
3017                        break;
3018                    case 'b':
3019                        if(lcType.equals("boolean") || lcType.equals("bool")) {
3020                            return Boolean.class;
3021                        }
3022                        else if(lcType.equals("binary")) {
3023                            return byte[].class;
3024                        }
3025                        else if(lcType.equals("base64")) {
3026                            return String.class;
3027                        }
3028                        else if(lcType.equals("byte")) {
3029                            return Byte.class;
3030                        }
3031                        break;
3032                    case 'c':
3033                        if(lcType.equals("creditcard")) {
3034                            return String.class;
3035                        }
3036                        else if(lcType.equals("component")) {
3037                            return Component.class;
3038                        }
3039                        break;
3040                    case 'd':
3041                        if(lcType.equals("date")) {
3042                            return Date.class;
3043                        }
3044                        else if(lcType.equals("datetime")) {
3045                            return Date.class;
3046                        }
3047                        break;
3048                    case 'g':
3049                        if(lcType.equals("guid")) {
3050                            return Object.class;
3051                        }
3052                        break;
3053                    case 'n':
3054                        if(lcType.equals("numeric")) {
3055                            return Double.class;
3056                        }
3057                        else if(lcType.equals("number")) {
3058                            return Double.class;
3059                        }
3060                        else if(lcType.equals("node")) {
3061                            return Node.class;
3062                        }
3063                        break;
3064                    case 'o':
3065                        if(lcType.equals("object")) {
3066                            return Object.class;
3067                        }
3068                        break;
3069                    case 'q':
3070                        if(lcType.equals("query")) {
3071                            return Query.class;
3072                        }
3073                        break;
3074                    case 's':
3075                        if(lcType.equals("string")) {
3076                            return String.class;
3077                        }
3078                        else if(lcType.equals("struct")) {
3079                            return Struct.class;
3080                        }
3081                        break;
3082                    case 't':
3083                        if(lcType.equals("timespan")) {
3084                            return TimeSpan.class;
3085                        }
3086                        break;
3087                    case 'u':
3088                        if(lcType.equals("uuid")) {
3089                            return Object.class;
3090                        }
3091                        break;
3092                    case 'v':
3093                        if(lcType.equals("variablename")) {
3094                            return Object.class;
3095                        }
3096                        if(lcType.equals("void")) {
3097                            return Object.class;
3098                        }
3099                        break;
3100                    case 'x':
3101                        if(lcType.equals("xml")) {
3102                            return Node.class;
3103                        }
3104                        break;
3105               }
3106            }
3107            // array
3108            if(type.endsWith("[]")) {
3109                    Class clazz = cfTypeToClass(type.substring(0,type.length()-2));
3110                    clazz=ClassUtil.toArrayClass(clazz);
3111                    return clazz;
3112            }
3113            // check for argument
3114            Class<?> clazz;
3115                    try {
3116                            clazz = otherTypeToClass(type);
3117                    } 
3118                    catch (ClassException e) {
3119                            throw Caster.toPageException(e);
3120                    }
3121            return clazz;
3122        }
3123        
3124        private static Class<?> otherTypeToClass(String type) throws PageException, ClassException{
3125            PageContext pc = ThreadLocalPageContext.get();
3126            PageException pe=null;
3127            // try to load as cfc
3128            if(pc!=null)    {
3129                    try {
3130                            Component c = pc.loadComponent(type);
3131                            return ComponentUtil.getServerComponentPropertiesClass(pc,c);
3132                    } 
3133                catch (PageException e) {
3134                    pe=e;
3135                }
3136            }
3137            // try to load as class
3138            try {
3139                            return ClassUtil.loadClass(type);
3140                    } 
3141            catch (ClassException ce) {
3142                    if(pe!=null) throw pe;
3143                    throw ce;
3144            }
3145        }
3146        
3147    
3148        
3149        /**
3150         * cast a value to a value defined by type argument
3151         * @param pc
3152         * @param type type of the returning Value
3153         * @param o Object to cast
3154         * @return casted Value
3155         * @throws PageException
3156         */ 
3157        public static Object castTo(PageContext pc,String type, Object o, boolean alsoPattern) throws PageException {
3158            type=StringUtil.toLowerCase(type).trim();
3159            if(type.length()>2) {
3160                char first=type.charAt(0);
3161                switch(first) {
3162                    case 'a':
3163                        if(type.equals("any")) {
3164                            return o;
3165                        }
3166                        else if(type.equals("array")) {
3167                            return toArray(o);
3168                        }
3169                        break;
3170                    case 'b':
3171                        if(type.equals("boolean") || type.equals("bool")) {
3172                            return toBoolean(o);
3173                        }
3174                        else if(type.equals("binary")) {
3175                            return toBinary(o);
3176                        }
3177                        else if(type.equals("base64")) {
3178                            return toBase64(o,null);
3179                        }
3180                        break;
3181                    case 'c':
3182                        if(alsoPattern && type.equals("creditcard")) {
3183                            return toCreditCard(o);
3184                        }
3185                        break;
3186                    case 'd':
3187                            if(type.equals("date")) {
3188                            return DateCaster.toDateAdvanced(o,pc.getTimeZone());
3189                        }
3190                        else if(type.equals("datetime")) {
3191                            return DateCaster.toDateAdvanced(o,pc.getTimeZone());
3192                        }
3193                        else if(type.equals("double")) {
3194                            return toDouble(o);
3195                        }
3196                        else if(type.equals("decimal")) {
3197                            return toDecimal(o);
3198                        }
3199                        break;
3200                    case 'e':
3201                        if(type.equals("eurodate")) {
3202                            return DateCaster.toEuroDate(o,pc.getTimeZone());
3203                        }
3204                        else if(alsoPattern && type.equals("email")) {
3205                            return toEmail(o);
3206                        }
3207                        break;
3208                    case 'f':
3209                        if(type.equals("float")) {
3210                            return toDouble(o);
3211                        }
3212                        break;
3213                    case 'g':
3214                        if(type.equals("guid")) {
3215                            return toGUId(o);
3216                        }
3217                        break;
3218                    case 'i':
3219                        if(type.equals("integer") || type.equals("int")) {
3220                            return toInteger(o);
3221                        }
3222                        break;
3223                    case 'l':
3224                        if(type.equals("long")) {
3225                            return toLong(o);
3226                        }
3227                        break;
3228                    case 'n':
3229                        if(type.equals("numeric")) {
3230                            return toDouble(o);
3231                        }
3232                        else if(type.equals("number")) {
3233                            return toDouble(o);
3234                        }
3235                        else if(type.equals("node")) {
3236                            return toXML(o);
3237                        }
3238                        break;
3239                    case 'o':
3240                        if(type.equals("object")) {
3241                            return o;
3242                        }
3243                        else if(type.equals("other")) {
3244                            return o;
3245                        }
3246                        break;
3247                    case 'p':
3248                        if(alsoPattern && type.equals("phone")) {
3249                            return toPhone(o);
3250                        }
3251                        break;
3252                    case 'q':
3253                        if(type.equals("query")) {
3254                            return toQuery(o);
3255                        }
3256                        break;
3257                    case 's':
3258                        if(type.equals("string")) {
3259                            return toString(o);
3260                        }
3261                        else if(type.equals("struct")) {
3262                            return toStruct(o);
3263                        }
3264                        else if(type.equals("short")) {
3265                            return toShort(o);
3266                        }
3267                        else if(alsoPattern && (type.equals("ssn") ||type.equals("social_security_number"))) {
3268                            return toSSN(o);
3269                        }
3270                        break;
3271                    case 't':
3272                        if(type.equals("timespan")) {
3273                            return toTimespan(o);
3274                        }
3275                        if(type.equals("time")) {
3276                            return DateCaster.toDateAdvanced(o,pc.getTimeZone());
3277                        }
3278                        if(alsoPattern && type.equals("telephone")) {
3279                            return toPhone(o);
3280                        }
3281                        break;
3282                    case 'u':
3283                        if(type.equals("uuid")) {
3284                            return toUUId(o);
3285                        }
3286                        if(alsoPattern && type.equals("url")) {
3287                            return toURL(o);
3288                        }
3289                        if(type.equals("usdate")) {
3290                            return DateCaster.toUSDate(o,pc.getTimeZone());
3291                            //return DateCaster.toDate(o,pc.getTimeZone());
3292                        }
3293                        break;
3294                    case 'v':
3295                        if(type.equals("variablename")) {
3296                            return toVariableName(o);
3297                        }
3298                        else if(type.equals("void")) {
3299                            return toVoid(o);
3300                        }
3301                        else if(type.equals("variable_name")) {
3302                            return toVariableName(o);
3303                        }
3304                        else if(type.equals("variable-name")) {
3305                            return toVariableName(o);
3306                        }
3307                        break;
3308                    case 'x':
3309                        if(type.equals("xml")) {
3310                            return toXML(o);
3311                        }
3312                        case 'z':
3313                            if(alsoPattern && (type.equals("zip") || type.equals("zipcode"))) {
3314                                return toZip(o);
3315                            }
3316                        break;
3317               }
3318            }
3319            
3320            // <type>[]
3321            if(type.endsWith("[]")){
3322                    String componentType = type.substring(0,type.length()-2);
3323                    Object[] src = toNativeArray(o);
3324                    Array trg=new ArrayImpl();
3325                    for(int i=0;i<src.length;i++){
3326                            if(src[i]==null){
3327                                    continue;
3328                            }
3329                            trg.setE(i+1,castTo(pc, componentType, src[i],alsoPattern));
3330                    }               
3331                    return trg;     
3332            }
3333            
3334            
3335            if(o instanceof Component) {
3336                Component comp=((Component)o);
3337                if(comp.instanceOf(type)) return o;
3338                // neo batch
3339                throw new ExpressionException("can't cast Component of Type ["+comp.getAbsName()+"] to ["+type+"]");
3340            }
3341            throw new CasterException(o,type);
3342        }
3343    
3344            public static String toZip(Object o) throws PageException {
3345            String str=toString(o);
3346            if(Decision.isZipCode(str)) return str;
3347                    throw new ExpressionException("can't cast value ["+str+"] to a zip code");
3348            }
3349        
3350        public static String toZip(Object o, String defaultValue) {
3351            String str=toString(o,null);
3352            if(str==null) return defaultValue;
3353            if(Decision.isZipCode(str)) return str;
3354            return defaultValue;
3355            }
3356    
3357            public static String toURL(Object o) throws PageException {
3358            String str=toString(o);
3359            if(Decision.isURL(str)) return str;
3360            
3361            try {
3362                            return HTTPUtil.toURL(str).toExternalForm();
3363                    } 
3364            catch (MalformedURLException e) {
3365                    throw new ExpressionException("can't cast value ["+str+"] to a URL",e.getMessage());
3366            }
3367            }
3368        
3369        public static String toURL(Object o, String defaultValue) {
3370            String str=toString(o,null);
3371            if(str==null) return defaultValue;
3372            if(Decision.isURL(str)) return str;
3373            try {
3374                            return HTTPUtil.toURL(str).toExternalForm();
3375                    } 
3376            catch (MalformedURLException e) {
3377                    return defaultValue;
3378            }
3379            }
3380    
3381            public static String toPhone(Object o) throws PageException {
3382            String str=toString(o);
3383            if(Decision.isPhone(str)) return str;
3384                    throw new ExpressionException("can't cast value ["+str+"] to a telephone number");
3385            }
3386        
3387        public static String toPhone(Object o, String defaultValue) {
3388            String str=toString(o,null);
3389            if(str==null) return defaultValue;
3390            if(Decision.isPhone(str)) return str;
3391            return defaultValue;
3392            }
3393    
3394            public static String toSSN(Object o) throws PageException {
3395            String str=toString(o);
3396            if(Decision.isSSN(str)) return str;
3397                    throw new ExpressionException("can't cast value ["+str+"] to a U.S. social security number");
3398            }
3399        
3400        public static String toSSN(Object o, String defaultValue) {
3401            String str=toString(o,null);
3402            if(str==null) return defaultValue;
3403            if(Decision.isSSN(str)) return str;
3404            return defaultValue;
3405            }
3406    
3407            public static String toEmail(Object o) throws PageException {
3408                    String str=toString(o);
3409            if(Decision.isEmail(str)) return str;
3410                    throw new ExpressionException("can't cast value ["+str+"] to a E-Mail Address");
3411            }
3412        
3413        public static String toEmail(Object o, String defaultValue) {
3414            String str=toString(o,null);
3415            if(str==null) return defaultValue;
3416            if(Decision.isEmail(str)) return str;
3417            return defaultValue;
3418            }
3419    
3420            /**
3421         * cast a value to a value defined by type argument
3422         * @param pc
3423         * @param type type of the returning Value
3424         * @param strType type as String
3425         * @param o Object to cast
3426         * @return casted Value
3427         * @throws PageException
3428         */
3429        public static Object castTo(PageContext pc, short type, String strType, Object o) throws PageException {
3430    //       TODO weitere typen siehe bytecode.cast.Cast
3431            if(type==CFTypes.TYPE_ANY)                 return o;
3432            else if(type==CFTypes.TYPE_ARRAY)          return toArray(o);
3433            else if(type==CFTypes.TYPE_BOOLEAN)        return toBoolean(o);
3434            else if(type==CFTypes.TYPE_BINARY)         return toBinary(o);
3435            else if(type==CFTypes.TYPE_DATETIME)       return DateCaster.toDateAdvanced(o,pc.getTimeZone());
3436            else if(type==CFTypes.TYPE_NUMERIC)        return toDouble(o);
3437            else if(type==CFTypes.TYPE_QUERY)          return toQuery(o);
3438            else if(type==CFTypes.TYPE_QUERY_COLUMN)   return toQueryColumn(o);
3439            else if(type==CFTypes.TYPE_STRING)         return toString(o);
3440            else if(type==CFTypes.TYPE_STRUCT)         return toStruct(o);
3441            else if(type==CFTypes.TYPE_TIMESPAN)       return toTimespan(o);
3442            else if(type==CFTypes.TYPE_UUID)           return toUUId(o);
3443            else if(type==CFTypes.TYPE_GUID)           return toGUId(o);
3444            else if(type==CFTypes.TYPE_VARIABLE_NAME)  return toVariableName(o);
3445            else if(type==CFTypes.TYPE_VOID)           return toVoid(o);
3446            else if(type==CFTypes.TYPE_XML)            return toXML(o);
3447            else if(type==CFTypes.TYPE_FUNCTION)       return toFunction(o);
3448    
3449            if(o instanceof Component) {
3450                Component comp=((Component)o);
3451                if(comp.instanceOf(strType)) return o;
3452                throw new ExpressionException("can't cast Component of Type ["+comp.getAbsName()+"] to ["+strType+"]");
3453            }
3454            throw new CasterException(o,strType);
3455        }   
3456        
3457        /**
3458         * cast a value to a value defined by type argument
3459         * @param pc
3460         * @param type type of the returning Value
3461         * @param o Object to cast
3462         * @return casted Value
3463         * @throws PageException
3464         */
3465        public static Object castTo(PageContext pc, short type, Object o) throws PageException {
3466            if(type==CFTypes.TYPE_ANY)                 return o;
3467            else if(type==CFTypes.TYPE_ARRAY)          return toArray(o);
3468            else if(type==CFTypes.TYPE_BOOLEAN)        return toBoolean(o);
3469            else if(type==CFTypes.TYPE_BINARY)         return toBinary(o);
3470            else if(type==CFTypes.TYPE_DATETIME)       return DateCaster.toDateAdvanced(o,pc.getTimeZone());
3471            else if(type==CFTypes.TYPE_NUMERIC)        return toDouble(o);
3472            else if(type==CFTypes.TYPE_QUERY)          return toQuery(o);
3473            else if(type==CFTypes.TYPE_QUERY_COLUMN)   return toQueryColumn(o);
3474            else if(type==CFTypes.TYPE_STRING)         return toString(o);
3475            else if(type==CFTypes.TYPE_STRUCT)         return toStruct(o);
3476            else if(type==CFTypes.TYPE_TIMESPAN)       return toTimespan(o);
3477            else if(type==CFTypes.TYPE_UUID)           return toGUId(o);
3478            else if(type==CFTypes.TYPE_UUID)           return toUUId(o);
3479            else if(type==CFTypes.TYPE_VARIABLE_NAME)  return toVariableName(o);
3480            else if(type==CFTypes.TYPE_VOID)           return toVoid(o);
3481            else if(type==CFTypes.TYPE_FUNCTION)       return toFunction(o);
3482            else if(type==CFTypes.TYPE_XML)            return toXML(o);
3483    
3484            if(type==CFTypes.TYPE_UNDEFINED)
3485                throw new ExpressionException("type isn't defined (TYPE_UNDEFINED)");
3486            throw new ExpressionException("invalid type ["+type+"]");
3487        }
3488        
3489    
3490            /**
3491         * cast a value to void (Empty String)
3492         * @param o
3493         * @return void value
3494         * @throws ExpressionException
3495         */
3496        public static Object toVoid(Object o) throws ExpressionException {
3497            if(o==null)return null;
3498            else if(o instanceof String && o.toString().length()==0)return null;
3499            else if(o instanceof Number && ((Number)o).intValue()==0 ) return null;
3500            else if(o instanceof Boolean && ((Boolean)o).booleanValue()==false ) return null;
3501            else if(o instanceof ObjectWrap) return toVoid(((ObjectWrap)o).getEmbededObject(null));
3502                    throw new CasterException(o,"void");
3503        }
3504        
3505        /**
3506         * cast a value to void (Empty String)
3507         * @param o
3508         * @param defaultValue 
3509         * @return void value
3510         */
3511        public static Object toVoid(Object o, Object defaultValue) {
3512            if(o==null)return null;
3513            else if(o instanceof String && o.toString().length()==0)return null;
3514            else if(o instanceof Number && ((Number)o).intValue()==0 ) return null;
3515            else if(o instanceof Boolean && ((Boolean)o).booleanValue()==false ) return null;
3516            else if(o instanceof ObjectWrap) return toVoid(((ObjectWrap)o).getEmbededObject((defaultValue)),defaultValue);
3517                    return defaultValue;
3518        }
3519    
3520        /**
3521         * cast a Object to a reference type (Object), in that case this method to nothing, because a Object is already a reference type
3522         * @param o Object to cast
3523         * @return casted Object
3524         */
3525        public static Object toRef(Object o) {
3526            return o;
3527        }
3528        
3529        /**
3530         * cast a String to a reference type (Object), in that case this method to nothing, because a String is already a reference type
3531         * @param o Object to cast
3532         * @return casted Object
3533         */
3534        public static String toRef(String o) {
3535            return o;
3536        }
3537        
3538        /**
3539         * cast a Collection to a reference type (Object), in that case this method to nothing, because a Collection is already a reference type
3540         * @param o Collection to cast
3541         * @return casted Object
3542         */
3543        public static Collection toRef(Collection o) {
3544            return o;
3545        }
3546        
3547        /**
3548         * cast a char value to his (CFML) reference type String
3549         * @param c char to cast
3550         * @return casted String
3551         */
3552        public static String toRef(char c) {
3553            return ""+c;
3554        }
3555        
3556        /**
3557         * cast a boolean value to his (CFML) reference type Boolean
3558         * @param b boolean to cast
3559         * @return casted Boolean
3560         */
3561        public static Boolean toRef(boolean b) {
3562            return b?Boolean.TRUE:Boolean.FALSE;
3563        }
3564        
3565        /**
3566         * cast a byte value to his (CFML) reference type Integer
3567         * @param b byte to cast
3568         * @return casted Integer
3569         */
3570        public static Byte toRef(byte b) {
3571            return new Byte(b);
3572        }
3573        
3574        /**
3575         * cast a int value to his (CFML) reference type Integer
3576         * @param i int to cast
3577         * @return casted Integer
3578         */
3579        public static Integer toRef(int i) {
3580            return Integer.valueOf(i);
3581        }
3582        
3583        /**
3584         * cast a float value to his (CFML) reference type Float
3585         * @param f float to cast 
3586         * @return casted Float
3587         */
3588        public static Float toRef(float f) {
3589            return new Float(f);
3590        }
3591        
3592        /**
3593         * cast a long value to his (CFML) reference type Long
3594         * @param l long to cast
3595         * @return casted Long
3596         */
3597        public static Long toRef(long l) {
3598            return Long.valueOf(l);
3599        }
3600        
3601        /**
3602         * cast a double value to his (CFML) reference type Double
3603         * @param d doble to cast
3604         * @return casted Double
3605         */
3606        public static Double toRef(double d) {
3607            return new Double(d);
3608        }
3609        
3610        /**
3611         * cast a double value to his (CFML) reference type Double
3612         * @param s short to cast
3613         * @return casted Short
3614         */
3615        public static Short toRef(short s) {
3616            return Short.valueOf(s);
3617        }
3618    
3619        /**
3620         * cast a Object to a Iterator or get Iterator from Object
3621         * @param o Object to cast
3622         * @return casted Collection
3623         * @throws PageException
3624         */
3625        public static Iterator toIterator(Object o) throws PageException {
3626            if(o instanceof Iterator) return (Iterator)o;
3627            else if(o instanceof Iteratorable) return ((Iteratorable)o).keysAsStringIterator();
3628            else if(o instanceof Enumeration) return new IteratorWrapper((Enumeration)o);
3629            else if(o instanceof JavaObject) {
3630                    String[] names = ClassUtil.getFieldNames(((JavaObject)o).getClazz());
3631                    return new ArrayIterator(names);
3632            }
3633            else if(o instanceof ObjectWrap) return toIterator(((ObjectWrap)o).getEmbededObject());
3634                    return toIterator(toCollection(o));
3635        }
3636        
3637        /**
3638         * cast a Object to a Collection
3639         * @param o Object to cast
3640         * @return casted Collection
3641         * @throws PageException
3642         */
3643        public static Collection toCollection(Object o) throws PageException {
3644            if(o instanceof Collection) return (Collection)o;
3645            else if(o instanceof Node)return XMLCaster.toXMLStruct((Node)o,false);
3646            else if(o instanceof Map) {
3647                return MapAsStruct.toStruct((Map)o,true);//StructImpl((Map)o);
3648            }
3649            else if(o instanceof ObjectWrap) {
3650                return toCollection(((ObjectWrap)o).getEmbededObject());
3651            }
3652            else if(Decision.isArray(o)) {
3653                return toArray(o);
3654            }
3655            throw new CasterException(o,"collection");
3656        }
3657        
3658        public static java.util.Collection toJavaCollection(Object o) throws PageException {
3659            if(o instanceof java.util.Collection) return (java.util.Collection) o;
3660            return toList(o);
3661        }
3662        
3663        
3664        /**
3665         * cast a Object to a Component
3666         * @param o Object to cast
3667         * @return casted Component
3668         * @throws PageException
3669         */
3670        public static Component toComponent(Object o ) throws PageException {
3671            if(o instanceof Component) return (Component)o;
3672            else if(o instanceof ObjectWrap) {
3673                return toComponent(((ObjectWrap)o).getEmbededObject());
3674            }
3675            throw new CasterException(o,"Component");
3676        }
3677        
3678        public static Component toComponent(Object o , Component defaultValue) {
3679            if(o instanceof Component) return (Component)o;
3680            else if(o instanceof ObjectWrap) {
3681                return toComponent(((ObjectWrap)o).getEmbededObject(defaultValue),defaultValue);
3682            }
3683            return defaultValue;
3684        }
3685        
3686        /**
3687         * cast a Object to a Collection, if not returns null
3688         * @param o Object to cast
3689         * @param defaultValue 
3690         * @return casted Collection
3691         */
3692        public static Collection toCollection(Object o, Collection defaultValue) {
3693            if(o instanceof Collection) return (Collection)o;
3694            else if(o instanceof Node)return XMLCaster.toXMLStruct((Node)o,false);
3695            else if(o instanceof Map) {
3696                return MapAsStruct.toStruct((Map)o,true);
3697            }
3698            else if(o instanceof ObjectWrap) {
3699                return toCollection(((ObjectWrap)o).getEmbededObject(defaultValue),defaultValue);
3700            }
3701            else if(Decision.isArray(o)) {
3702                try {
3703                    return toArray(o);
3704                } catch (PageException e) {
3705                    return defaultValue;
3706                }
3707            }
3708            return defaultValue;
3709        }
3710    
3711        /**
3712         * convert a object to a File
3713         * @param obj
3714         * @return File
3715         * @throws PageException 
3716          */ 
3717        public static File toFile(Object obj) throws PageException {
3718            if(obj instanceof File) return (File)obj;
3719            return FileUtil.toFile(Caster.toString(obj));
3720        }
3721        
3722        /**
3723         * convert a object to a File
3724         * @param obj
3725         * @param defaultValue 
3726         * @return File
3727         */ 
3728        public static File toFile(Object obj, File defaultValue) {
3729            if(obj instanceof File) return (File)obj;
3730            String str=Caster.toString(obj,null);
3731            if(str==null) return defaultValue;
3732            return FileUtil.toFile(str);
3733        }
3734        
3735        /**
3736         * convert a object array to a HashMap filled with Function value Objects
3737         * @param args Object array to convert
3738         * @return hashmap containing Function values
3739         * @throws ExpressionException
3740         */
3741        public static Struct toFunctionValues(Object[] args) throws ExpressionException {
3742            return toFunctionValues(args, 0, args.length);
3743        }
3744        
3745        public static Struct toFunctionValues(Object[] args, int offset, int len) throws ExpressionException {
3746            // TODO nicht sehr optimal 
3747            Struct sct=new StructImpl(StructImpl.TYPE_LINKED);
3748            for(int i=offset;i<offset+len;i++) {
3749                if(args[i] instanceof FunctionValueImpl){
3750                    FunctionValueImpl value = (FunctionValueImpl) args[i];
3751                    sct.setEL(value.getNameAsKey(),value.getValue());
3752                }
3753                else throw new ExpressionException("Missing argument name, when using named parameters to a function, every parameter must have a name ["+i+":"+args[i].getClass().getName()+"].");
3754            }
3755            return sct;
3756        }
3757        
3758        
3759        
3760        
3761        public static Object[] toFunctionValues(Struct args) {
3762            // TODO nicht sehr optimal 
3763            Iterator<Entry<Key, Object>> it = args.entryIterator();
3764            Entry<Key, Object> e;
3765            List<FunctionValue> fvalues=new ArrayList<FunctionValue>();
3766            while(it.hasNext()) {
3767                    e=it.next();
3768                    fvalues.add(new FunctionValueImpl(e.getKey().getString(),e.getValue()));
3769            }
3770            return fvalues.toArray(new FunctionValue[fvalues.size()]);
3771        }
3772    
3773        /**
3774         * casts a string to a Locale
3775         * @param strLocale
3776         * @return Locale from String
3777         * @throws ExpressionException
3778         */
3779        public static Locale toLocale(String strLocale) throws ExpressionException {
3780            return LocaleFactory.getLocale(strLocale);
3781        }
3782        
3783        /**
3784         * casts a string to a Locale
3785         * @param strLocale
3786         * @param defaultValue 
3787         * @return Locale from String
3788         */
3789        public static Locale toLocale(String strLocale, Locale defaultValue) {
3790            return LocaleFactory.getLocale(strLocale,defaultValue);
3791        }
3792    
3793        /**
3794         * casts a string to a TimeZone
3795         * @param strTimeZone
3796         * @return TimeZone from String
3797         * @throws ExpressionException
3798         */
3799        public static TimeZone toTimeZone(String strTimeZone) throws ExpressionException {
3800            return TimeZoneUtil.toTimeZone(strTimeZone);
3801        }
3802        
3803        /**
3804         * casts a string to a TimeZone
3805         * @param strTimeZone
3806         * @param defaultValue 
3807         * @return TimeZone from String
3808         */
3809        public static TimeZone toTimeZone(String strTimeZone, TimeZone defaultValue) {
3810            return TimeZoneUtil.toTimeZone(strTimeZone,defaultValue);
3811        }
3812        
3813        
3814        public static TimeZone toTimeZone(Object oTimeZone, TimeZone defaultValue) {
3815            if(oTimeZone instanceof TimeZone) return (TimeZone) oTimeZone;
3816            return TimeZoneUtil.toTimeZone(Caster.toString(oTimeZone,null),defaultValue);
3817        }
3818        
3819        
3820        
3821        
3822        /**
3823         * casts a Object to a Node List
3824         * @param o Object to Cast
3825         * @return NodeList from Object
3826         * @throws PageException
3827         */
3828        public static NodeList toNodeList(Object o) throws PageException {
3829            //print.ln("nodeList:"+o);
3830            if(o instanceof NodeList) {
3831                return (NodeList)o;
3832            }
3833            else if(o instanceof ObjectWrap) {
3834                return toNodeList(((ObjectWrap)o).getEmbededObject());
3835            }
3836            throw new CasterException(o,"NodeList");
3837        }
3838        
3839        /**
3840         * casts a Object to a Node List
3841         * @param o Object to Cast
3842         * @param defaultValue 
3843         * @return NodeList from Object
3844         */
3845        public static NodeList toNodeList(Object o, NodeList defaultValue) {
3846            //print.ln("nodeList:"+o);
3847            if(o instanceof NodeList) {
3848                return (NodeList)o;
3849            }
3850            else if(o instanceof ObjectWrap) {
3851                return toNodeList(((ObjectWrap)o).getEmbededObject(defaultValue),defaultValue);
3852            }
3853            return defaultValue;
3854        }   
3855        
3856        /**
3857         * casts a Object to a XML Node
3858         * @param o Object to Cast
3859         * @return Node from Object
3860         * @throws PageException
3861         */
3862        public static Node toNode(Object o) throws PageException {
3863            return toXML(o);
3864            /*if(o instanceof Node)return (Node)o;
3865            else if(o instanceof String)    {
3866                try {
3867                    return XMLCaster.toXMLStruct(XMLUtil.parse(o.toString(),false),false);
3868                    
3869                } catch (Exception e) {
3870                    throw Caster.toPageException(e);
3871                }
3872            }
3873            else if(o instanceof ObjectWrap) {
3874                return toNode(((ObjectWrap)o).getEmbededObject());
3875            }
3876            throw new CasterException(o,"Node");*/
3877        }   
3878        
3879        /**
3880         * casts a Object to a XML Node
3881         * @param o Object to Cast
3882         * @param defaultValue 
3883         * @return Node from Object
3884         */
3885        public static Node toNode(Object o, Node defaultValue) {
3886            return toXML(o,defaultValue);
3887            /*if(o instanceof Node)return (Node)o;
3888            else if(o instanceof String)    {
3889                try {
3890                    return XMLCaster.toXMLStruct(XMLUtil.parse(o.toString(),false),false);
3891                    
3892                } catch (Exception e) {
3893                    return defaultValue;
3894                }
3895            }
3896            else if(o instanceof ObjectWrap) {
3897                return toNode(((ObjectWrap)o).getEmbededObject(defaultValue),defaultValue);
3898            }
3899            return defaultValue;*/
3900        }
3901    
3902        /**
3903         * casts a boolean to a Integer
3904         * @param b
3905         * @return Integer from boolean
3906         */
3907        public static Integer toInteger(boolean b) {
3908            return b?Constants.INTEGER_1:Constants.INTEGER_0;
3909        }
3910    
3911        /**
3912         * casts a char to a Integer
3913         * @param c
3914         * @return Integer from char
3915         */
3916        public static Integer toInteger(char c) {
3917            return Integer.valueOf(c);
3918        }
3919    
3920        /**
3921         * casts a double to a Integer
3922         * @param d
3923         * @return Integer from double
3924         */
3925        public static Integer toInteger(double d) { 
3926            return Integer.valueOf((int)d);
3927        }
3928        
3929        /**
3930         * casts a Object to a Integer
3931         * @param o Object to cast to Integer
3932         * @return Integer from Object
3933         * @throws PageException
3934         */
3935        public static Integer toInteger(Object o) throws PageException {
3936            return Integer.valueOf(toIntValue(o));
3937        }
3938        
3939        /**
3940         * casts a Object to a Integer
3941         * @param str Object to cast to Integer
3942         * @return Integer from Object
3943         * @throws PageException
3944         */
3945        public static Integer toInteger(String str) throws PageException {
3946            return Integer.valueOf(toIntValue(str));
3947        }
3948        
3949        // used in bytecode genrator 
3950        public static Integer toInteger(int i) {
3951            return Integer.valueOf(i);
3952        }
3953        
3954        /**
3955         * casts a Object to a Integer
3956         * @param o Object to cast to Integer
3957         * @param defaultValue 
3958         * @return Integer from Object
3959         */
3960        public static Integer toInteger(Object o, Integer defaultValue) {
3961            if(defaultValue!=null) return Integer.valueOf(toIntValue(o,defaultValue.intValue()));
3962            int res=toIntValue(o,Integer.MIN_VALUE);
3963            if(res==Integer.MIN_VALUE) return defaultValue;
3964            return Integer.valueOf(res);
3965        }
3966        
3967        /**
3968         * casts a Object to null
3969         * @param value
3970         * @return to null from Object
3971         * @throws PageException
3972         */
3973        public static Object toNull(Object value) throws PageException {
3974           if(value==null)return null;
3975           if(value instanceof String && Caster.toString(value).trim().length()==0) return null;
3976           if(value instanceof Number && ((Number)value).intValue()==0) return null;
3977           throw new CasterException(value,"null");
3978        }
3979        
3980        /**
3981         * casts a Object to null
3982         * @param value
3983         * @param defaultValue 
3984         * @return to null from Object
3985         */
3986        public static Object toNull(Object value, Object defaultValue){
3987           if(value==null)return null;
3988           if(value instanceof String && Caster.toString(value,"").trim().length()==0) return null;
3989           if(value instanceof Number && ((Number)value).intValue()==0) return null;
3990           return defaultValue;
3991        }
3992    
3993        /**
3994         * cast Object to a XML Node
3995         * @param value
3996         * @param defaultValue 
3997         * @return XML Node
3998         */
3999        public static Node toXML(Object value, Node defaultValue) {
4000            try {
4001                return toXML(value);
4002            } catch (PageException e) {
4003                return defaultValue;
4004            }
4005        }
4006    
4007        /**
4008         * cast Object to a XML Node
4009         * @param value
4010         * @return XML Node
4011         * @throws PageException 
4012         */
4013        public static Node toXML(Object value) throws PageException {
4014            if(value instanceof Node) return XMLCaster.toXMLStruct((Node)value,false);
4015            if(value instanceof ObjectWrap) {
4016                return toXML(((ObjectWrap)value).getEmbededObject());
4017            }
4018            try {
4019                return XMLCaster.toXMLStruct(XMLUtil.parse(XMLUtil.toInputSource(null, value),null,false),false);
4020            }
4021            catch(Exception outer) {
4022                throw Caster.toPageException(outer);
4023            }
4024            
4025        }
4026    
4027        public static String toStringForce(Object value, String defaultValue) {
4028            String rtn=toString(value,null);
4029            if(rtn!=null)return rtn;
4030            
4031            try {
4032                if(value instanceof Struct) {
4033                    return new ScriptConverter().serialize(value);
4034                }
4035                else if(value instanceof Array) {
4036                    return new ScriptConverter().serialize(value);
4037                }
4038            } 
4039            catch (ConverterException e) {}
4040            return defaultValue;
4041        }
4042            
4043            public static Resource toResource(PageContext pc,Object src, boolean existing) throws ExpressionException {
4044                    return toResource(pc,src,existing,pc.getConfig().allowRealPath());
4045            }
4046            
4047            public static Resource toResource(PageContext pc,Object src, boolean existing,boolean allowRealpath) throws ExpressionException {
4048                    if(src instanceof Resource) return (Resource) src;
4049                    if(src instanceof File) src=src.toString();
4050                    if(src instanceof String) {
4051                            if(existing)
4052                                    return ResourceUtil.toResourceExisting(pc, (String)src,allowRealpath);
4053                            return ResourceUtil.toResourceNotExisting(pc, (String)src,allowRealpath);
4054                    }
4055                    if(src instanceof FileStreamWrapper) return ((FileStreamWrapper)src).getResource();
4056            throw new CasterException(src,"Resource");
4057            }
4058            
4059    
4060            public static Resource toResource(Config config,Object src, boolean existing) throws ExpressionException {
4061                    if(src instanceof Resource) return (Resource) src;
4062                    if(src instanceof File) src=src.toString();
4063                    if(src instanceof String) {
4064                            if(existing)
4065                                    return ResourceUtil.toResourceExisting(config, (String)src);
4066                            return ResourceUtil.toResourceNotExisting(config, (String)src);
4067                    }
4068                    if(src instanceof FileStreamWrapper) return ((FileStreamWrapper)src).getResource();
4069            throw new CasterException(src,"Resource");
4070            }
4071            
4072    
4073            public static Hashtable toHashtable(Object obj) throws PageException {
4074                    if(obj instanceof Hashtable) return (Hashtable) obj;
4075                    return (Hashtable) Duplicator.duplicateMap(toMap(obj,false), new Hashtable(), false);
4076            }
4077    
4078            public static Vector toVetor(Object obj) throws PageException {
4079                    if(obj instanceof Vector) return (Vector) obj;
4080                    return (Vector) Duplicator.duplicateList(toList(obj,false),new Vector(), false);
4081            } 
4082    
4083            public static Calendar toCalendar(Date date, TimeZone tz) {
4084                    tz=ThreadLocalPageContext.getTimeZone(tz);
4085                    Calendar c = tz==null?JREDateTimeUtil.newInstance():JREDateTimeUtil.newInstance(tz);
4086                    c.setTime(date);
4087                    return c;
4088            }
4089            
4090            public static Calendar toCalendar(long time, TimeZone tz) {
4091                    tz=ThreadLocalPageContext.getTimeZone(tz);
4092                    Calendar c = tz==null?JREDateTimeUtil.newInstance():JREDateTimeUtil.newInstance(tz);
4093                    c.setTimeInMillis(time);
4094                    return c;
4095            }
4096    
4097            public static Serializable toSerializable(Object object) throws CasterException {
4098                    if(object instanceof Serializable)return (Serializable)object;
4099                    throw new CasterException(object,"Serializable");
4100            }
4101    
4102            public static Serializable toSerializable(Object object, Serializable defaultValue) {
4103                    if(object instanceof Serializable)return (Serializable)object;
4104                    return defaultValue;
4105            }
4106    
4107            public static byte[] toBytes(Object obj) throws PageException {
4108                    try {
4109                            if(obj instanceof byte[]) return (byte[]) obj;
4110                            if(obj instanceof InputStream)return IOUtil.toBytes((InputStream)obj);
4111                            if(obj instanceof Resource)return IOUtil.toBytes((Resource)obj);
4112                            if(obj instanceof File)return IOUtil.toBytes((File)obj);
4113                            if(obj instanceof String) return ((String)obj).getBytes();
4114                            if(obj instanceof Blob) {
4115                                    InputStream is=null;
4116                                    try {
4117                                            is=((Blob)obj).getBinaryStream();
4118                                            return IOUtil.toBytes(is);
4119                                    }
4120                                    finally {
4121                                            IOUtil.closeEL(is);
4122                                    }
4123                            }
4124                    }
4125                    catch(IOException ioe) {
4126                            throw toPageException(ioe);
4127                    }
4128                    catch(SQLException ioe) {
4129                            throw toPageException(ioe);
4130                    }
4131                    throw new CasterException(obj,byte[].class);
4132            }
4133    
4134            public static InputStream toInputStream(Object obj) throws PageException {
4135                    try {
4136                            if(obj instanceof InputStream)return (InputStream)obj;
4137                            if(obj instanceof byte[]) return new ByteArrayInputStream((byte[]) obj);
4138                            if(obj instanceof Resource)return ((Resource)obj).getInputStream();
4139                            if(obj instanceof File)return new FileInputStream((File)obj);
4140                            if(obj instanceof String) return new ByteArrayInputStream(((String)obj).getBytes());
4141                            if(obj instanceof Blob) return ((Blob)obj).getBinaryStream();
4142                    }
4143                    catch(IOException ioe) {
4144                            throw toPageException(ioe);
4145                    }
4146                    catch(SQLException ioe) {
4147                            throw toPageException(ioe);
4148                    }
4149                    throw new CasterException(obj,InputStream.class);
4150            }
4151    
4152            public static OutputStream toOutputStream(Object obj) throws PageException {
4153                    if(obj instanceof OutputStream)return (OutputStream)obj;
4154                    throw new CasterException(obj,OutputStream.class);
4155            }
4156    
4157            public static Object castTo(PageContext pc, Class trgClass, Object obj) throws PageException {
4158                    if(trgClass==null)return Caster.toNull(obj); 
4159                    else if(obj.getClass()==trgClass)       return obj;
4160                    
4161                    else if(trgClass==boolean.class)return Caster.toBoolean(obj); 
4162                    else if(trgClass==byte.class)return Caster.toByte(obj); 
4163                    else if(trgClass==short.class)return Caster.toShort(obj); 
4164                    else if(trgClass==int.class)return Integer.valueOf(Caster.toDouble(obj).intValue()); 
4165                    else if(trgClass==long.class)return Caster.toLong(obj);
4166                    else if(trgClass==float.class)return new Float(Caster.toDouble(obj).floatValue()); 
4167                    else if(trgClass==double.class)return Caster.toDouble(obj); 
4168                    else if(trgClass==char.class)return Caster.toCharacter(obj); 
4169                    
4170                    else if(trgClass==Boolean.class)return Caster.toBoolean(obj); 
4171                    else if(trgClass==Byte.class)return Caster.toByte(obj); 
4172                    else if(trgClass==Short.class)return Caster.toShort(obj); 
4173                    else if(trgClass==Integer.class)return Integer.valueOf(Caster.toDouble(obj).intValue()); 
4174                    else if(trgClass==Long.class)return Caster.toLong(obj); 
4175                    else if(trgClass==Float.class)return new Float(Caster.toDouble(obj).floatValue()); 
4176                    else if(trgClass==Double.class)return Caster.toDouble(obj); 
4177                    else if(trgClass==Character.class)return Caster.toCharacter(obj); 
4178                    
4179                    else if(trgClass==Object.class)return obj; 
4180                    else if(trgClass==String.class)return Caster.toString(obj); 
4181                    
4182                    if(Reflector.isInstaneOf(obj.getClass(), trgClass)) return obj;
4183                    
4184                    return Caster.castTo(pc, trgClass.getName(), obj,false);
4185            }
4186    
4187            public static Objects toObjects(PageContext pc,Object obj) throws PageException {
4188                    if(obj instanceof Objects) return (Objects) obj;
4189                    if(obj instanceof ObjectWrap) return toObjects(pc,((ObjectWrap)obj).getEmbededObject());
4190                    return new JavaObject(pc.getVariableUtil(), obj);
4191            }
4192    
4193            public static BigDecimal toBigDecimal(Object o) throws PageException {
4194                    if(o instanceof BigDecimal) return (BigDecimal) o;
4195                    if(o instanceof Number) return new BigDecimal(((Number)o).doubleValue());
4196            else if(o instanceof Boolean) return new BigDecimal(((Boolean)o).booleanValue()?1:0);
4197            else if(o instanceof String) return new BigDecimal(o.toString());
4198            else if(o instanceof Castable) return new BigDecimal(((Castable)o).castToDoubleValue());
4199            else if(o == null) return BigDecimal.ZERO;
4200            else if(o instanceof ObjectWrap) return toBigDecimal(((ObjectWrap)o).getEmbededObject());
4201            throw new CasterException(o,"number");
4202            }
4203    
4204            public static Object unwrap(Object value) throws PageException {
4205                    if(value==null) return null;
4206                    if(value instanceof ObjectWrap) {
4207                            return ((ObjectWrap)value).getEmbededObject();
4208                    }
4209                    return value;
4210            }
4211            
4212            public static Object unwrap(Object value,Object defaultValue) {
4213                    if(value==null) return null;
4214                    if(value instanceof ObjectWrap) {
4215                            return ((ObjectWrap)value).getEmbededObject(defaultValue);
4216                    }
4217                    return value;
4218            }
4219    
4220            public static CharSequence toCharSequence(Object obj) throws PageException {
4221                    if(obj instanceof CharSequence) return (CharSequence) obj;
4222                    return Caster.toString(obj);
4223            }
4224    
4225            public static CharSequence toCharSequence(Object obj, CharSequence defaultValue) {
4226                    if(obj instanceof CharSequence) return (CharSequence) obj;
4227                    String str = Caster.toString(obj,null);
4228                    if(str==null) return defaultValue;
4229                    return str;
4230            }
4231    
4232    
4233            
4234            
4235    }