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.jexl2;
19
20 import java.io.BufferedReader;
21 import java.io.File;
22 import java.io.InputStreamReader;
23
24 /**
25 * Test application for JEXL.
26 *
27 * @since 2.0
28 */
29 public class Main {
30 /**
31 * Test application for JEXL
32 *
33 * If a single argument is present, it is treated as a filename of a JEXL
34 * script to be executed as a script. Any exceptions terminate the application.
35 *
36 * Otherwise, lines are read from standard input and evaluated.
37 * ParseExceptions and JexlExceptions are logged, and do not cause the application to exit.
38 * This is done so that interactive testing is easier.
39 *
40 * @param args (optional) filename to execute. Stored in the args variable.
41 *
42 * @throws Exception if parsing or IO fail
43 */
44 public static void main(String[] args) throws Exception {
45 JexlEngine engine = new JexlEngine();
46 JexlContext context = new MapContext();
47 context.set("args", args);
48 if (args.length == 1) {
49 Script script = engine.createScript(new File(args[0]));
50 Object value = script.execute(context);
51 System.out.println("Return value: " + value);
52 } else {
53 BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
54 String line;
55 System.out.print("> ");
56 while (null != (line = console.readLine())) {
57 try {
58 Expression expression = engine.createExpression(line);
59 Object value = expression.evaluate(context);
60 System.out.println("Return value: " + value);
61 } catch (JexlException e) {
62 System.out.println(e.getLocalizedMessage());
63 }
64 System.out.print("> ");
65 }
66 }
67 }
68 }