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.ScriptEvaluator ScriptEvaluator} class.
041: */
042:
043: public class ScriptDemo extends DemoBase {
044: public static void main(String[] args) throws Exception {
045: Class returnType = void.class;
046: String[] parameterNames = {};
047: Class[] parameterTypes = {};
048:
049: int i;
050: for (i = 0; i < args.length; ++i) {
051: String arg = args[i];
052: if (!arg.startsWith("-"))
053: break;
054: if (arg.equals("-rt")) {
055: returnType = DemoBase.stringToType(args[++i]);
056: } else if (arg.equals("-pn")) {
057: parameterNames = DemoBase.explode(args[++i]);
058: } else if (arg.equals("-pt")) {
059: parameterTypes = DemoBase.stringToTypes(args[++i]);
060: } else if (arg.equals("-help")) {
061: ScriptDemo.usage();
062: System.exit(0);
063: } else {
064: System.err.println("Invalid command line option \""
065: + arg + "\".");
066: ScriptDemo.usage();
067: System.exit(0);
068: }
069: }
070:
071: if (i == args.length) {
072: System.err
073: .println("Script missing on command line; try \"-help\".");
074: System.exit(1);
075: }
076: String script = args[i++];
077:
078: if (parameterTypes.length != parameterNames.length) {
079: System.err
080: .println("Parameter type count and parameter name count do not match.");
081: ScriptDemo.usage();
082: System.exit(1);
083: }
084:
085: // One command line argument for each parameter.
086: if (args.length - i != parameterNames.length) {
087: System.err
088: .println("Argument and parameter count do not match.");
089: ScriptDemo.usage();
090: System.exit(1);
091: }
092:
093: // Convert command line arguments to parameter values.
094: Object[] parameterValues = new Object[parameterNames.length];
095: for (int j = 0; j < parameterNames.length; ++j) {
096: parameterValues[j] = DemoBase.createObject(
097: parameterTypes[j], args[i + j]);
098: }
099:
100: // Create "ScriptEvaluator" object.
101: ScriptEvaluator se = new ScriptEvaluator(script, returnType,
102: parameterNames, parameterTypes);
103:
104: // Evaluate script with actual parameter values.
105: Object res = se.evaluate(parameterValues);
106:
107: // Print script return value.
108: System.out.println("Result = " + DemoBase.toString(res));
109: }
110:
111: private ScriptDemo() {
112: }
113:
114: private static void usage() {
115: System.err
116: .println("Usage: ScriptDemo { <option> } <script> { <parameter-value> }");
117: System.err.println("Valid options are");
118: System.err.println(" -rt <return-type>");
119: System.err.println(" -pn <comma-separated-parameter-names>");
120: System.err.println(" -pt <comma-separated-parameter-types>");
121: System.err.println(" -help");
122: System.err
123: .println("The number of parameter names, types and values must be identical.");
124: System.err.println("The default is no parameters.");
125: }
126: }
|