001    package railo.runtime.functions.other;
002    
003    import railo.runtime.Component;
004    import railo.runtime.PageContext;
005    import railo.runtime.exp.ExpressionException;
006    import railo.runtime.exp.PageException;
007    import railo.runtime.op.Caster;
008    import railo.runtime.type.Collection;
009    import railo.runtime.type.FunctionValue;
010    import railo.runtime.type.KeyImpl;
011    import railo.runtime.type.Struct;
012    import railo.runtime.type.UDF;
013    
014    public class _CreateComponent {
015            
016            private static final Collection.Key INIT = KeyImpl.intern("init");
017            private static final Object[] EMPTY = new Object[0]; 
018    
019            public static Object call(PageContext pc , Object[] objArr) throws PageException {
020                    
021                    String path = Caster.toString(objArr[objArr.length-1]);
022                    Component cfc = CreateObject.doComponent(pc, path);
023                    
024                    
025                    // no init method
026                    if(!(cfc.get(INIT,null) instanceof UDF)){
027                            return cfc;
028                    }
029                    
030                    Object rtn;
031                    // no arguments
032                    if(objArr.length==1) {
033                            rtn = cfc.call(pc, INIT, EMPTY);
034                    }       
035                    // named arguments
036                    else if(objArr[0] instanceof FunctionValue) {
037                            Struct args=Caster.toFunctionValues(objArr,0,objArr.length-1);
038                            rtn = cfc.callWithNamedValues(pc, INIT, args);
039                    }
040                    // no name arguments
041                    else {
042                            Object[] args = new Object[objArr.length-1];
043                            for(int i=0;i<objArr.length-1;i++) {
044                                    args[i]=objArr[i];
045                                    if(args[i] instanceof FunctionValue) 
046                                            throw new ExpressionException("invalid argument defintion,when using named parameters to a function, every parameter must have a name.");
047                            }
048                            rtn = cfc.call(pc, INIT, args);
049                    }
050                    if(rtn==null)return cfc;
051                    return rtn;
052            }
053    
054    }