001    /**
002     * Implements the Cold Fusion Function javacast
003     */
004    package railo.runtime.functions.string;
005    
006    import java.math.BigDecimal;
007    import java.math.BigInteger;
008    
009    import railo.commons.lang.ClassException;
010    import railo.commons.lang.ClassUtil;
011    import railo.commons.lang.StringUtil;
012    import railo.runtime.PageContext;
013    import railo.runtime.exp.ExpressionException;
014    import railo.runtime.exp.PageException;
015    import railo.runtime.ext.function.Function;
016    import railo.runtime.op.Caster;
017    import railo.runtime.type.Array;
018    
019    public final class JavaCast implements Function {
020            public static Object calls(PageContext pc , String string, Object object) throws PageException {
021                    throw new ExpressionException("method javacast not implemented yet"); // MUST ????
022            }
023            public static Object call(PageContext pc , String type, Object obj) throws PageException {
024                    type=type.trim();
025                    String lcType=StringUtil.toLowerCase(type);
026                    
027                    if(type.endsWith("[]")){
028                            return toArray(pc,type, lcType, obj);
029                            
030                    }
031                    Class clazz = toClass(pc, lcType, type);
032                    return to(pc,obj,clazz);
033                    
034            }
035            
036            public static Object toArray(PageContext pc,String type,String lcType, Object obj) throws PageException {
037                    lcType=lcType.substring(0,lcType.length()-2);
038                    type=type.substring(0,type.length()-2);
039                    
040                    
041                    
042                    Array arr = Caster.toArray(obj);
043                    Class clazz = toClass(pc, lcType, type);
044                    Object trg= java.lang.reflect.Array.newInstance(clazz, arr.size());
045                    
046                    
047                    for(int i=arr.size()-1;i>=0;i--) {
048                            java.lang.reflect.Array.set(trg, i,to(pc,arr.getE(i+1),clazz));
049                            
050                    }
051                    return trg;
052            }
053            
054            
055            private static Object to(PageContext pc, Object obj,Class trgClass) throws PageException {
056                    if(trgClass==null)return Caster.toNull(obj); 
057                    else if(trgClass==BigDecimal.class)return new BigDecimal(Caster.toString(obj)); 
058                    else if(trgClass==BigInteger.class)return new BigInteger(Caster.toString(obj)); 
059                    return Caster.castTo(pc, trgClass, obj);
060                    //throw new ExpressionException("can't cast only to the following data types (bigdecimal,int, long, float ,double ,boolean ,string,null ), "+lcType+" is invalid");
061            }
062            
063            private static Class toClass(PageContext pc,String lcType, String type) throws PageException {
064                     
065                    if(lcType.equals("null")){
066                            return null; 
067                    }  
068                    if(lcType.equals("biginteger")){
069                            return BigInteger.class; 
070                    }  
071                    if(lcType.equals("bigdecimal")){
072                            return BigDecimal.class; 
073                    } 
074                    try {
075                            return ClassUtil.toClass(type);
076                    } catch (ClassException e) {
077                            throw Caster.toPageException(e);
078                    }
079            }
080            
081    }