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