001    package railo.runtime.interpreter.ref.literal;
002    
003    import java.util.ArrayList;
004    import java.util.List;
005    
006    import railo.runtime.PageContext;
007    import railo.runtime.exp.ExpressionException;
008    import railo.runtime.exp.PageException;
009    import railo.runtime.interpreter.ref.Ref;
010    import railo.runtime.interpreter.ref.RefSupport;
011    import railo.runtime.interpreter.ref.Set;
012    import railo.runtime.interpreter.ref.var.Variable;
013    import railo.runtime.type.FunctionValueImpl;
014    
015    /**
016     * ref for a functionValue
017     */
018    public final class LFunctionValue extends RefSupport implements Ref {
019    
020    
021        private Ref name;
022        private Ref refValue;
023        private Object objValue;
024    
025        /**
026         * constructor of the class
027         * @param name
028         * @param value
029         */
030        public LFunctionValue(Ref name, Ref value) {
031            this.name=name;
032            this.refValue=value;
033        }
034        public LFunctionValue(Ref name, Object value) {
035            this.name=name;
036            this.objValue=value;
037        }
038        
039    
040        /*public String getName(){
041            if(name instanceof Variable){
042                    return new FunctionValueImpl(toStringArray((Set)name),value.getValue());
043            }
044            if(name instanceof Literal) {
045                    return ((Literal)name).getString();
046            }
047            
048            // TODO no idea if this is ever used
049            if(name instanceof Set){
050                    return new FunctionValueImpl(railo.runtime.type.List.arrayToList(toStringArray((Set)name),"."),value.getValue());
051            }
052            throw new ExpressionException("invalid syntax in named argument");
053            
054            
055            
056        }*/
057        
058        @Override
059        public Object getValue(PageContext pc) throws PageException {
060            
061            if(name instanceof Variable){
062                    return new FunctionValueImpl(toStringArray(pc,(Set)name),refValue==null?objValue:refValue.getValue(pc));
063            }
064            if(name instanceof Literal) {
065                    return new FunctionValueImpl(((Literal)name).getString(pc),refValue==null?objValue:refValue.getValue(pc));
066            }
067            
068            // TODO no idea if this is ever used
069            if(name instanceof Set){
070                    return new FunctionValueImpl(railo.runtime.type.util.ListUtil.arrayToList(toStringArray(pc,(Set)name),"."),refValue==null?objValue:refValue.getValue(pc));
071            }
072            throw new ExpressionException("invalid syntax in named argument");
073            //return new FunctionValueImpl(key,value.getValue());
074        }
075    
076        public static String[] toStringArray(PageContext pc,Set set) throws PageException {
077            Ref ref=set;
078            String str;
079            List<String> arr=new ArrayList<String>();
080            do {
081                set=(Set) ref;
082                str=set.getKeyAsString(pc);
083                if(str!=null)arr.add(0, str);
084                else break;
085                ref=set.getParent(pc);
086            }while(ref instanceof Set);
087            return arr.toArray(new String[arr.size()]);
088            }
089    
090        @Override
091        public String getTypeName() {
092            return "function value";
093        }
094    
095    }