001    /**
002     * Implements the CFML Function isdefined
003     */
004    package railo.runtime.functions.decision;
005    
006    import railo.runtime.PageContext;
007    import railo.runtime.config.NullSupportHelper;
008    import railo.runtime.ext.function.Function;
009    import railo.runtime.interpreter.VariableInterpreter;
010    import railo.runtime.type.Collection;
011    import railo.runtime.type.KeyImpl;
012    import railo.runtime.type.Null;
013    import railo.runtime.type.scope.Scope;
014    import railo.runtime.util.VariableUtilImpl;
015    
016    public final class IsDefined implements Function {
017            
018            private static final long serialVersionUID = -6477602189364145523L;
019    
020            public static boolean call(PageContext pc , String varName) {
021                    return VariableInterpreter.isDefined(pc,varName);
022                    //return pc.isDefined(varName);
023            }
024            
025            public static boolean call(PageContext pc , double scope,Collection.Key key) {
026                    try {
027                            Object coll = VariableInterpreter.scope(pc, (int)scope, false);
028                            if(coll==null) return false;
029                            coll=((VariableUtilImpl)pc.getVariableUtil()).get(pc,coll,key,NullSupportHelper.NULL());
030                            if(coll==NullSupportHelper.NULL())return false;
031                            //return pc.scope((int)scope).get(key,null)!=null; 
032                    } catch (Throwable t) {
033                    return false;
034                }
035                    return true;
036            }
037    
038            public static boolean call(PageContext pc , double scope,Collection.Key[] varNames) {
039                    Object defVal=NullSupportHelper.NULL();
040                    try {
041                            Object coll =VariableInterpreter.scope(pc, (int)scope, false); 
042                            //Object coll =pc.scope((int)scope);
043                            VariableUtilImpl vu = ((VariableUtilImpl)pc.getVariableUtil());
044                            for(int i=0;i<varNames.length;i++) {
045                                    coll=vu.getCollection(pc,coll,varNames[i],defVal);
046                                    if(coll==defVal)return false;
047                            }
048                    } catch (Throwable t) {
049                    return false;
050                }
051                    return true; 
052            }
053            
054            // used for older compiled code in ra files
055            public static boolean invoke(PageContext pc , String[] varNames, boolean allowNull) {
056                    int scope = VariableInterpreter.scopeString2Int(varNames[0]);
057                    
058                    
059                    Object defVal=allowNull?Null.NULL:null;
060                    try {
061                            Object coll =VariableInterpreter.scope(pc, scope, false); 
062                            //Object coll =pc.scope((int)scope); 
063                            for(int i=scope==Scope.SCOPE_UNDEFINED?0:1;i<varNames.length;i++) {
064                                    coll=pc.getVariableUtil().getCollection(pc,coll,varNames[i],defVal);
065                                    if(coll==defVal)return false;
066                            }
067                    } catch (Throwable t) {
068                    return false;
069                }
070                    return true; 
071            }
072                    
073            // used for older compiled code in ra files
074            public static boolean call(PageContext pc , double scope,String key) {
075                    return call(pc, scope, KeyImpl.getInstance(key));
076            }
077    
078            // used for older compiled code in ra files
079            public static boolean call(PageContext pc , double scope,String[] varNames) {
080                    return call(pc, scope, KeyImpl.toKeyArray(varNames));
081            }
082    }