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