001    /**
002     * Implements the CFML Function array
003     */
004    package railo.runtime.functions.struct;
005    
006    import railo.runtime.PageContext;
007    import railo.runtime.exp.ExpressionException;
008    import railo.runtime.exp.PageException;
009    import railo.runtime.ext.function.Function;
010    import railo.runtime.type.FunctionValue;
011    import railo.runtime.type.FunctionValueImpl;
012    import railo.runtime.type.Struct;
013    import railo.runtime.type.StructImpl;
014    
015    public class Struct_ implements Function {
016            public static Struct call(PageContext pc , Object[] objArr) throws PageException {
017                    return _call(objArr, "invalid argument for function struct, only named arguments are allowed like struct(name:\"value\",name2:\"value2\")");
018            }
019            
020            
021            protected static Struct _call(Object[] objArr,String expMessage) throws PageException {
022                    Struct sct=new StructImpl();
023                    FunctionValueImpl fv;
024                    for(int i=0;i<objArr.length;i++) {
025                            if(objArr[i] instanceof FunctionValue) {
026                                    fv=((FunctionValueImpl)objArr[i]);
027                                    if(fv.getNames()==null) {
028                                            sct.set(fv.getNameAsKey(),fv.getValue());
029                                    }
030                                    else {
031                                            String[] arr = fv.getNames();
032                                            Struct s=sct;
033                                            for(int y=0;y<arr.length-1;y++) {
034                                                    s=touch(s,arr[y]);
035                                            }
036                                            s.set(arr[arr.length-1], fv.getValue());        
037                                    }
038                            }
039                            else {
040                                    throw new ExpressionException(expMessage);
041                            }
042                    }
043                    return sct;
044            }
045            private static Struct touch(Struct parent,String name) {
046                    name=name.trim();
047                    Object obj=parent.get(name, null); 
048                    if(obj instanceof Struct) return (Struct) obj;
049                    Struct sct=new StructImpl();
050                    parent.setEL(name, sct);
051                    return sct;
052            }
053            
054    }