001: /*
002: * Janino - An embedded Java[TM] compiler
003: *
004: * Copyright (c) 2006, Arno Unkrig
005: * All rights reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * 2. Redistributions in binary form must reproduce the above
014: * copyright notice, this list of conditions and the following
015: * disclaimer in the documentation and/or other materials
016: * provided with the distribution.
017: * 3. The name of the author may not be used to endorse or promote
018: * products derived from this software without specific prior
019: * written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
022: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
023: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
024: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
025: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
026: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
027: * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
028: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
029: * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
030: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
031: * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
032: */
033:
034: package org.codehaus.janino.samples;
035:
036: import org.codehaus.janino.*;
037:
038: /**
039: * A test program that allows you to play around with the
040: * {@link org.codehaus.janino.ExpressionEvaluator ExpressionEvaluator} class.
041: */
042:
043: public class ExpressionDemo extends DemoBase {
044: public static void main(String[] args) throws Exception {
045: String expression = "total >= 100.0 ? 0.0 : 7.95";
046: Class optionalExpressionType = null;
047: String[] parameterNames = { "total", };
048: Class[] parameterTypes = { double.class, };
049: Class[] thrownExceptions = new Class[0];
050: String[] optionalDefaultImports = null;
051:
052: int i;
053: for (i = 0; i < args.length; ++i) {
054: String arg = args[i];
055: if (!arg.startsWith("-"))
056: break;
057: if (arg.equals("-e")) {
058: expression = args[++i];
059: } else if (arg.equals("-et")) {
060: optionalExpressionType = DemoBase
061: .stringToType(args[++i]);
062: } else if (arg.equals("-pn")) {
063: parameterNames = DemoBase.explode(args[++i]);
064: } else if (arg.equals("-pt")) {
065: parameterTypes = DemoBase.stringToTypes(args[++i]);
066: } else if (arg.equals("-te")) {
067: thrownExceptions = DemoBase.stringToTypes(args[++i]);
068: } else if (arg.equals("-di")) {
069: optionalDefaultImports = DemoBase.explode(args[++i]);
070: } else if (arg.equals("-help")) {
071: ExpressionDemo.usage();
072: System.exit(0);
073: } else {
074: System.err.println("Invalid command line option \""
075: + arg + "\".");
076: ExpressionDemo.usage();
077: System.exit(0);
078: }
079: }
080:
081: if (parameterTypes.length != parameterNames.length) {
082: System.err
083: .println("Parameter type count and parameter name count do not match.");
084: ExpressionDemo.usage();
085: System.exit(1);
086: }
087:
088: // One command line argument for each parameter.
089: if (args.length - i != parameterNames.length) {
090: System.err
091: .println("Parameter value count and parameter name count do not match.");
092: ExpressionDemo.usage();
093: System.exit(1);
094: }
095:
096: // Convert command line arguments to parameter values.
097: Object[] parameterValues = new Object[parameterNames.length];
098: for (int j = 0; j < parameterNames.length; ++j) {
099: parameterValues[j] = DemoBase.createObject(
100: parameterTypes[j], args[i + j]);
101: }
102:
103: // Create "ExpressionEvaluator" object.
104: ExpressionEvaluator ee = new ExpressionEvaluator();
105: ee.setReturnType(optionalExpressionType);
106: ee.setDefaultImports(optionalDefaultImports);
107: ee.setParameters(parameterNames, parameterTypes);
108: ee.setThrownExceptions(thrownExceptions);
109: ee.cook(expression);
110:
111: // Evaluate expression with actual parameter values.
112: Object res = ee.evaluate(parameterValues);
113:
114: // Print expression result.
115: System.out.println("Result = " + DemoBase.toString(res));
116: }
117:
118: private ExpressionDemo() {
119: }
120:
121: private static void usage() {
122: System.err
123: .println("Usage: ExpressionDemo { <option> } { <parameter-value> }");
124: System.err.println("Valid options are");
125: System.err.println(" -e <expression>");
126: System.err.println(" -et <expression-type>");
127: System.err.println(" -pn <comma-separated-parameter-names>");
128: System.err.println(" -pt <comma-separated-parameter-types>");
129: System.err
130: .println(" -te <comma-separated-thrown-exception-types>");
131: System.err.println(" -di <comma-separated-default-imports>");
132: System.err.println(" -help");
133: System.err
134: .println("The number of parameter names, types and values must be identical.");
135: }
136: }
|