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