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