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 **/
019package lucee.transformer.cfml.evaluator.func.impl;
020
021import lucee.commons.lang.StringList;
022import lucee.runtime.exp.TemplateException;
023import lucee.runtime.interpreter.VariableInterpreter;
024import lucee.runtime.type.Collection;
025import lucee.runtime.type.scope.Scope;
026import lucee.runtime.type.util.ArrayUtil;
027import lucee.transformer.bytecode.expression.Expression;
028import lucee.transformer.bytecode.expression.type.CollectionKey;
029import lucee.transformer.bytecode.expression.type.CollectionKeyArray;
030import lucee.transformer.bytecode.expression.var.Argument;
031import lucee.transformer.bytecode.expression.var.BIF;
032import lucee.transformer.bytecode.literal.LitDouble;
033import lucee.transformer.bytecode.literal.LitString;
034import lucee.transformer.cfml.evaluator.FunctionEvaluator;
035import lucee.transformer.library.function.FunctionLibFunction;
036
037public class IsDefined implements FunctionEvaluator{
038
039        public void evaluate(BIF bif, FunctionLibFunction flf) throws TemplateException {
040                Argument arg = bif.getArguments()[0];
041                Expression value = arg.getValue();
042                if(value instanceof LitString) {
043                        String str=((LitString)value).getString();
044                        StringList sl = VariableInterpreter.parse(str,false);
045                        if(sl!=null){
046                                // scope
047                                str=sl.next();
048                                int scope = VariableInterpreter.scopeString2Int(str);
049                                if(scope==Scope.SCOPE_UNDEFINED)sl.reset();
050                                
051                                // keys
052                                String[] arr=sl.toArray();
053                                ArrayUtil.trim(arr);
054                                
055                                // update first arg
056                                arg.setValue(LitDouble.toExprDouble(scope),"number");
057                                
058                                // add second argument
059                                
060                                if(arr.length==1){
061                                        Expression expr = new CollectionKey(arr[0]);//LitString.toExprString(str);
062                                        arg=new Argument(expr,Collection.Key.class.getName());
063                                        bif.addArgument(arg);   
064                                }
065                                else {
066                                        CollectionKeyArray expr=new CollectionKeyArray(arr);
067                                        //LiteralStringArray expr = new LiteralStringArray(arr);
068                                        arg=new Argument(expr,Collection.Key[].class.getName());
069                                        bif.addArgument(arg);
070                                }
071                                
072                        }
073                        
074                }
075                //print.out("bif:"+arg.getValue().getClass().getName());
076        }
077
078}