001: /*
002: * Janino - An embedded Java[TM] compiler
003: *
004: * Copyright (c) 2001-2007, 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: Class optionalExpressionType = null;
046: String[] parameterNames = {};
047: Class[] parameterTypes = {};
048: Class[] thrownExceptions = new Class[0];
049: String[] optionalDefaultImports = null;
050:
051: int i;
052: for (i = 0; i < args.length; ++i) {
053: String arg = args[i];
054: if (!arg.startsWith("-"))
055: break;
056: if (arg.equals("-et")) {
057: optionalExpressionType = DemoBase
058: .stringToType(args[++i]);
059: } else if (arg.equals("-pn")) {
060: parameterNames = DemoBase.explode(args[++i]);
061: } else if (arg.equals("-pt")) {
062: parameterTypes = DemoBase.stringToTypes(args[++i]);
063: } else if (arg.equals("-te")) {
064: thrownExceptions = DemoBase.stringToTypes(args[++i]);
065: } else if (arg.equals("-di")) {
066: optionalDefaultImports = DemoBase.explode(args[++i]);
067: } else if (arg.equals("-help")) {
068: System.err
069: .println("Usage: ExpressionDemo { <option> } <expression> { <parameter-value> }");
070: System.err
071: .println("Compiles and evaluates the given expression and prints its value.");
072: System.err.println("Valid options are");
073: System.err
074: .println(" -et <expression-type> (default: any)");
075: System.err
076: .println(" -pn <comma-separated-parameter-names> (default: none)");
077: System.err
078: .println(" -pt <comma-separated-parameter-types> (default: none)");
079: System.err
080: .println(" -te <comma-separated-thrown-exception-types> (default: none)");
081: System.err
082: .println(" -di <comma-separated-default-imports> (default: none)");
083: System.err.println(" -help");
084: System.err
085: .println("The number of parameter names, types and values must be identical.");
086: System.exit(0);
087: } else {
088: System.err.println("Invalid command line option \""
089: + arg + "\"; try \"-help\".");
090: System.exit(0);
091: }
092: }
093:
094: if (i >= args.length) {
095: System.err.println("Expression missing; try \"-help\".");
096: System.exit(1);
097: }
098: String expression = args[i++];
099:
100: if (parameterTypes.length != parameterNames.length) {
101: System.err.println("Parameter type count ("
102: + parameterTypes.length
103: + ") and parameter name count ("
104: + parameterNames.length
105: + ") do not match; try \"-help\".");
106: System.exit(1);
107: }
108:
109: // One command line argument for each parameter.
110: if (args.length - i != parameterNames.length) {
111: System.err.println("Parameter value count ("
112: + (args.length - i)
113: + ") and parameter name count ("
114: + parameterNames.length
115: + ") do not match; try \"-help\".");
116: System.exit(1);
117: }
118:
119: // Convert command line arguments to parameter values.
120: Object[] parameterValues = new Object[parameterNames.length];
121: for (int j = 0; j < parameterNames.length; ++j) {
122: parameterValues[j] = DemoBase.createObject(
123: parameterTypes[j], args[i + j]);
124: }
125:
126: // Create "ExpressionEvaluator" object.
127: ExpressionEvaluator ee = new ExpressionEvaluator();
128: ee.setExpressionType(optionalExpressionType);
129: ee.setDefaultImports(optionalDefaultImports);
130: ee.setParameters(parameterNames, parameterTypes);
131: ee.setThrownExceptions(thrownExceptions);
132: ee.cook(expression);
133:
134: // Evaluate expression with actual parameter values.
135: Object res = ee.evaluate(parameterValues);
136:
137: // Print expression result.
138: System.out.println("Result = " + DemoBase.toString(res));
139: }
140:
141: private ExpressionDemo() {
142: }
143: }
|