1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.commons.jexl3.scripting;
19
20 import java.io.BufferedReader;
21 import java.io.IOException;
22 import java.io.InputStreamReader;
23 import java.io.OutputStreamWriter;
24 import java.io.PrintWriter;
25 import java.nio.charset.Charset;
26 import java.nio.file.Files;
27 import java.nio.file.Path;
28 import java.nio.file.Paths;
29
30 import javax.script.ScriptEngine;
31 import javax.script.ScriptException;
32
33 /**
34 * Test application for JexlScriptEngine (JSR-223 implementation).
35 *
36 * @since 2.0
37 */
38 public class Main {
39
40 /** Default constructor */
41 public Main() { } // Keep Javadoc happy
42
43 /**
44 * Test application for JexlScriptEngine (JSR-223 implementation).
45 * <p>
46 * If a single argument is present, it is treated as a file name of a JEXL
47 * script to be evaluated. Any exceptions terminate the application.
48 * </p>
49 * Otherwise, lines are read from standard input and evaluated.
50 * ScriptExceptions are logged, and do not cause the application to exit.
51 * This is done so that interactive testing is easier.
52 * The line //q! ends the loop.
53 *
54 * @param args (optional) file name to evaluate. Stored in the args variable.
55 * @throws Exception if parsing or IO fail
56 */
57 public static void main(final String[] args) throws Exception {
58 try(BufferedReader in = args.length == 1? read(Paths.get(args[0])) : read(null);
59 PrintWriter out = new PrintWriter(
60 new OutputStreamWriter(System.out, Charset.defaultCharset()),true)) {
61 run(in, out, args);
62 }
63 }
64
65 static void run(final BufferedReader in, final PrintWriter out, final Object[] args) throws Exception {
66 final JexlScriptEngineFactory fac = new JexlScriptEngineFactory();
67 final ScriptEngine engine = fac.getScriptEngine();
68 if (args != null && args.length > 0) {
69 engine.put("args", args);
70 final Object value = engine.eval(in);
71 out.println(">>: " + value);
72 } else {
73 String line;
74 out.print("> ");
75 while (null != (line = in.readLine())) {
76 if ("//q!".equals(line)) {
77 break;
78 }
79 try {
80 final Object value = engine.eval(line);
81 out.println(">> " + value);
82 } catch (final ScriptException e) {
83 out.println("!!>" + e.getLocalizedMessage());
84 }
85 out.print("> ");
86 }
87 }
88 }
89
90 /**
91 * Reads an input.
92 *
93 * @param path the file path or null for stdin
94 * @return the reader
95 * @throws IOException if anything goes wrong
96 */
97 static BufferedReader read(final Path path) throws IOException {
98 return new BufferedReader(new InputStreamReader(path == null
99 ? System.in
100 : Files.newInputStream(path), Charset.defaultCharset()));
101 }
102 }