View Javadoc
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    *      http://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.File;
22  import java.io.FileInputStream;
23  import java.io.InputStreamReader;
24  import java.io.PrintStream;
25  import java.nio.charset.Charset;
26  
27  import javax.script.ScriptEngine;
28  import javax.script.ScriptException;
29  
30  /**
31   * Test application for JexlScriptEngine (JSR-223 implementation).
32   *
33   * @since 2.0
34   */
35  public class Main {
36  
37      /**
38       * Test application for JexlScriptEngine (JSR-223 implementation).
39       *
40       * If a single argument is present, it is treated as a file name of a JEXL
41       * script to be evaluated. Any exceptions terminate the application.
42       *
43       * Otherwise, lines are read from standard input and evaluated.
44       * ScriptExceptions are logged, and do not cause the application to exit.
45       * This is done so that interactive testing is easier.
46       *
47       * @param args (optional) file name to evaluate. Stored in the args variable.
48       *
49       * @throws Exception if parsing or IO fail
50       */
51      public static void main(final String[] args) throws Exception {
52          final JexlScriptEngineFactory fac = new JexlScriptEngineFactory();
53          final ScriptEngine engine = fac.getScriptEngine();
54          final PrintStream out = System.out;
55          engine.put("args", args);
56          if (args.length == 1){
57              final Object value = engine.eval(read(null, args[0]));
58              out.println("Return value: "+value);
59          } else {
60              final BufferedReader console = read(null, null);
61              String line;
62              System.out.print("> ");
63              while(null != (line=console.readLine())){
64                  try {
65                      final Object value = engine.eval(line);
66                      out.println("Return value: "+value);
67                  } catch (final ScriptException e) {
68                      out.println(e.getLocalizedMessage());
69                  }
70                  out.print("> ");
71              }
72          }
73      }
74  
75      /**
76       * Reads an input.
77       *
78       * @param charset the charset or null for default charset
79       * @param fileName the file name or null for stdin
80       * @return the reader
81       * @throws Exception if anything goes wrong
82       */
83      static BufferedReader read(final Charset charset, final String fileName) throws Exception {
84          return new BufferedReader(
85              new InputStreamReader(
86                      fileName == null
87                          ? System.in
88                          : new FileInputStream(new File(fileName)),
89                      charset == null
90                          ? Charset.defaultCharset()
91                          : charset));
92      }
93  }