001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.jexl2;
019    
020    import org.apache.commons.jexl2.parser.ASTJexlScript;
021    import org.apache.commons.jexl2.parser.JexlNode;
022    
023    /**
024     * Instances of ExpressionImpl are created by the {@link JexlEngine},
025     * and this is the default implementation of the {@link Expression} and
026     * {@link Script} interface.
027     * @since 1.0
028     * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
029     * @version $Id: ExpressionImpl.java 885754 2009-12-01 12:47:00Z henrib $
030     */
031    public class ExpressionImpl implements Expression, Script {
032        /** The engine for this expression. */
033        protected final JexlEngine jexl;
034        /**
035         * Original expression stripped from leading & trailing spaces.
036         */
037        protected final String expression;
038        /**
039         * The resulting AST we can interpret.
040         */
041        protected final ASTJexlScript script;
042    
043    
044        /**
045         * Do not let this be generally instantiated with a 'new'.
046         *
047         * @param engine the interpreter to evaluate the expression
048         * @param expr the expression.
049         * @param ref the parsed expression.
050         */
051        protected ExpressionImpl(JexlEngine engine, String expr, ASTJexlScript ref) {
052            jexl = engine;
053            expression = expr;
054            script = ref;
055        }
056    
057        /**
058         * {@inheritDoc}
059         */
060        public Object evaluate(JexlContext context) {
061            if (script.jjtGetNumChildren() < 1) {
062                return null;
063            }
064            Interpreter interpreter = jexl.createInterpreter(context);
065            return interpreter.interpret((JexlNode)script.jjtGetChild(0));
066        }
067    
068        /**
069         * {@inheritDoc}
070         */
071        public String dump() {
072            Debugger debug = new Debugger();
073            return debug.debug(script)? debug.toString() : "/*?*/";
074        }
075    
076        /**
077         * {@inheritDoc}
078         */
079        public String getExpression() {
080            return expression;
081        }
082    
083        /**
084         * Provide a string representation of the expression.
085         *
086         * @return the expression or blank if it's null.
087         */
088        @Override
089        public String toString() {
090            String expr = getExpression();
091            return expr == null ? "" : expr;
092        }
093    
094        /**
095         * {@inheritDoc}
096         */
097        public String getText() {
098            return toString();
099        }
100    
101        /**
102         * {@inheritDoc}
103         */
104        public Object execute(JexlContext context) {
105            Interpreter interpreter = jexl.createInterpreter(context);
106            return interpreter.interpret(script);
107        }
108    
109    }