001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019/**
020 * Implements the CFML Function array
021 */
022package lucee.runtime.functions.struct;
023
024import lucee.runtime.PageContext;
025import lucee.runtime.exp.ExpressionException;
026import lucee.runtime.exp.PageException;
027import lucee.runtime.ext.function.Function;
028import lucee.runtime.type.Collection.Key;
029import lucee.runtime.type.FunctionValue;
030import lucee.runtime.type.FunctionValueImpl;
031import lucee.runtime.type.KeyImpl;
032import lucee.runtime.type.Struct;
033import lucee.runtime.type.StructImpl;
034
035public class Struct_ implements Function {
036
037        private static final long serialVersionUID = 8708684598035273346L;
038
039        public static Struct call(PageContext pc , Object[] objArr) throws PageException {
040                return _call(objArr, "invalid argument for function struct, only named arguments are allowed like struct(name:\"value\",name2:\"value2\")", StructImpl.TYPE_UNDEFINED);
041        }
042        
043        
044        protected static Struct _call(Object[] objArr,String expMessage, int type) throws PageException {
045                StructImpl sct=type<0?new StructImpl():new StructImpl(type);
046                FunctionValueImpl fv;
047                for(int i=0;i<objArr.length;i++) {
048                        if(objArr[i] instanceof FunctionValue) {
049                                fv=((FunctionValueImpl)objArr[i]);
050                                if(fv.getNames()==null) {
051                                        sct.set(fv.getNameAsKey(),fv.getValue());
052                                }
053                                else {
054                                        String[] arr = fv.getNames();
055                                        Struct s=sct;
056                                        for(int y=0;y<arr.length-1;y++) {
057                                                s=touch(s,arr[y]);
058                                        }
059                                        s.set(KeyImpl.init(arr[arr.length-1]), fv.getValue());  
060                                }
061                        }
062                        else {
063                                throw new ExpressionException(expMessage);
064                        }
065                }
066                return sct;
067        }
068        private static Struct touch(Struct parent,String name) {
069                Key key = KeyImpl.init(name.trim());
070                Object obj=parent.get(key, null); 
071                if(obj instanceof Struct) return (Struct) obj;
072                Struct sct=new StructImpl();
073                parent.setEL(key, sct);
074                return sct;
075        }
076        
077}