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.scripting;
019    
020    import java.io.BufferedReader;
021    import java.io.FileReader;
022    import java.io.InputStreamReader;
023    
024    import javax.script.ScriptEngine;
025    import javax.script.ScriptException;
026    
027    /**
028     * Test application for JexlScriptEngine (JSR-223 implementation).
029     * @since 2.0
030     */
031    public class Main {
032    
033        /**
034         * Test application for JexlScriptEngine (JSR-223 implementation).
035         * 
036         * If a single argument is present, it is treated as a filename of a JEXL
037         * script to be evaluated. Any exceptions terminate the application.
038         * 
039         * Otherwise, lines are read from standard input and evaluated.
040         * ScriptExceptions are logged, and do not cause the application to exit.
041         * This is done so that interactive testing is easier.
042         * 
043         * @param args (optional) filename to evaluate. Stored in the args variable.
044         * 
045         * @throws Exception if parsing or IO fail
046         */
047        public static void main(String[] args) throws Exception {
048            JexlScriptEngineFactory fac = new JexlScriptEngineFactory();
049            ScriptEngine engine = fac.getScriptEngine();
050            engine.put("args", args);
051            if (args.length == 1){
052                Object value = engine.eval(new FileReader(args[0]));
053                System.out.println("Return value: "+value);
054            } else {
055                BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
056                String line;
057                System.out.print("> ");
058                while(null != (line=console.readLine())){
059                    try {
060                        Object value = engine.eval(line);
061                        System.out.println("Return value: "+value);
062                    } catch (ScriptException e) {
063                        System.out.println(e.getLocalizedMessage());
064                    }
065                    System.out.print("> ");
066                }
067            }
068        }
069    }