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    
022    /**
023     * Instances of ExpressionImpl are created by the {@link JexlEngine},
024     * and this is the default implementation of the {@link Expression} and
025     * {@link Script} interface.
026     * @since 1.0
027     * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
028     * @version $Id: ExpressionImpl.java 885754 2009-12-01 12:47:00Z henrib $
029     */
030    public class ExpressionImpl implements Expression, Script {
031        /** The engine for this expression. */
032        protected final JexlEngine jexl;
033        /**
034         * Original expression stripped from leading & trailing spaces.
035         */
036        protected final String expression;
037        /**
038         * The resulting AST we can interpret.
039         */
040        protected final ASTJexlScript script;
041    
042    
043        /**
044         * Do not let this be generally instantiated with a 'new'.
045         *
046         * @param engine the interpreter to evaluate the expression
047         * @param expr the expression.
048         * @param ref the parsed expression.
049         */
050        protected ExpressionImpl(JexlEngine engine, String expr, ASTJexlScript ref) {
051            jexl = engine;
052            expression = expr;
053            script = ref;
054        }
055    
056        /**
057         * {@inheritDoc}
058         */
059        public Object evaluate(JexlContext context) {
060            if (script.jjtGetNumChildren() < 1) {
061                return null;
062            }
063            Interpreter interpreter = jexl.createInterpreter(context);
064            return interpreter.interpret(script.jjtGetChild(0));
065        }
066    
067        /**
068         * {@inheritDoc}
069         */
070        public String dump() {
071            Debugger debug = new Debugger();
072            return debug.debug(script)? debug.toString() : "/*?*/";
073        }
074    
075        /**
076         * {@inheritDoc}
077         */
078        public String getExpression() {
079            return expression;
080        }
081    
082        /**
083         * Provide a string representation of the expression.
084         *
085         * @return the expression or blank if it's null.
086         */
087        @Override
088        public String toString() {
089            String expr = getExpression();
090            return expr == null ? "" : expr;
091        }
092    
093        /**
094         * {@inheritDoc}
095         */
096        public String getText() {
097            return toString();
098        }
099    
100        /**
101         * {@inheritDoc}
102         */
103        public Object execute(JexlContext context) {
104            Interpreter interpreter = jexl.createInterpreter(context);
105            return interpreter.interpret(script);
106        }
107    
108    }