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