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