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
018 package org.apache.commons.jexl2;
019
020 import java.io.BufferedReader;
021 import java.io.File;
022 import java.io.InputStreamReader;
023
024 /**
025 * Test application for JEXL.
026 *
027 * @since 2.0
028 */
029 public class Main {
030 /**
031 * Test application for JEXL
032 *
033 * If a single argument is present, it is treated as a filename of a JEXL
034 * script to be executed as a script. Any exceptions terminate the application.
035 *
036 * Otherwise, lines are read from standard input and evaluated.
037 * ParseExceptions and JexlExceptions are logged, and do not cause the application to exit.
038 * This is done so that interactive testing is easier.
039 *
040 * @param args (optional) filename to execute. Stored in the args variable.
041 *
042 * @throws Exception if parsing or IO fail
043 */
044 public static void main(String[] args) throws Exception {
045 JexlEngine engine = new JexlEngine();
046 JexlContext context = new MapContext();
047 context.set("args", args);
048 if (args.length == 1) {
049 Script script = engine.createScript(new File(args[0]));
050 Object value = script.execute(context);
051 System.out.println("Return value: " + value);
052 } else {
053 BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
054 String line;
055 System.out.print("> ");
056 while (null != (line = console.readLine())) {
057 try {
058 Expression expression = engine.createExpression(line);
059 Object value = expression.evaluate(context);
060 System.out.println("Return value: " + value);
061 } catch (JexlException e) {
062 System.out.println(e.getLocalizedMessage());
063 }
064 System.out.print("> ");
065 }
066 }
067 }
068 }