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
018package org.apache.commons.jexl3.scripting;
019
020import java.io.BufferedReader;
021import java.io.File;
022import java.io.FileInputStream;
023import java.io.InputStreamReader;
024
025import java.io.PrintStream;
026import java.nio.charset.Charset;
027import javax.script.ScriptEngine;
028import javax.script.ScriptException;
029
030/**
031 * Test application for JexlScriptEngine (JSR-223 implementation).
032 *
033 * @since 2.0
034 */
035public class Main {
036
037    /**
038     * Reads an input.
039     *
040     * @param charset the charset or null for default charset
041     * @param fileName the file name or null for stdin
042     * @return the reader
043     * @throws Exception if anything goes wrong
044     */
045    static BufferedReader read(final Charset charset, final String fileName) throws Exception {
046        return new BufferedReader(
047            new InputStreamReader(
048                    fileName == null
049                        ? System.in
050                        : new FileInputStream(new File(fileName)),
051                    charset == null
052                        ? Charset.defaultCharset()
053                        : charset));
054    }
055
056    /**
057     * Test application for JexlScriptEngine (JSR-223 implementation).
058     *
059     * If a single argument is present, it is treated as a file name of a JEXL
060     * script to be evaluated. Any exceptions terminate the application.
061     *
062     * Otherwise, lines are read from standard input and evaluated.
063     * ScriptExceptions are logged, and do not cause the application to exit.
064     * This is done so that interactive testing is easier.
065     *
066     * @param args (optional) file name to evaluate. Stored in the args variable.
067     *
068     * @throws Exception if parsing or IO fail
069     */
070    public static void main(final String[] args) throws Exception {
071        final JexlScriptEngineFactory fac = new JexlScriptEngineFactory();
072        final ScriptEngine engine = fac.getScriptEngine();
073        final PrintStream out = System.out;
074        engine.put("args", args);
075        if (args.length == 1){
076            final Object value = engine.eval(read(null, args[0]));
077            out.println("Return value: "+value);
078        } else {
079            final BufferedReader console = read(null, null);
080            String line;
081            System.out.print("> ");
082            while(null != (line=console.readLine())){
083                try {
084                    final Object value = engine.eval(line);
085                    out.println("Return value: "+value);
086                } catch (final ScriptException e) {
087                    out.println(e.getLocalizedMessage());
088                }
089                out.print("> ");
090            }
091        }
092    }
093}