KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > webharvest > runtime > scripting > ScriptEngine


1 /* Copyright (c) 2006-2007, Vladimir Nikic
2     All rights reserved.
3
4     Redistribution and use of this software in source and binary forms,
5     with or without modification, are permitted provided that the following
6     conditions are met:
7
8     * Redistributions of source code must retain the above
9       copyright notice, this list of conditions and the
10       following disclaimer.
11
12     * Redistributions in binary form must reproduce the above
13       copyright notice, this list of conditions and the
14       following disclaimer in the documentation and/or other
15       materials provided with the distribution.
16
17     * The name of Web-Harvest may not be used to endorse or promote
18       products derived from this software without specific prior
19       written permission.
20
21     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24     ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25     LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31     POSSIBILITY OF SUCH DAMAGE.
32
33     You can contact Vladimir Nikic by sending e-mail to
34     nikic_vladimir@yahoo.com. Please include the word "Web-Harvest" in the
35     subject line.
36 */

37 package org.webharvest.runtime.scripting;
38
39 import bsh.EvalError;
40 import bsh.Interpreter;
41 import org.webharvest.exception.ScriptException;
42
43 import java.util.Iterator JavaDoc;
44 import java.util.Map JavaDoc;
45
46 /**
47  * Wrapper for scripting engine.
48  */

49 public class ScriptEngine extends Interpreter {
50
51     public static final String JavaDoc CONTEXT_VARIABLE_NAME = "___web_harvest_context___";
52
53     private Map JavaDoc context;
54
55     /**
56      * Constructor - initializes context used in engine.
57      * @param context
58      */

59     public ScriptEngine(Map JavaDoc context) {
60         this.getNameSpace().importCommands("org.webharvest.runtime.scripting");
61         this.context = context;
62         try {
63             this.set(CONTEXT_VARIABLE_NAME, this.context);
64         } catch (EvalError e) {
65             throw new ScriptException("Cannot set Web-Harvest context in scripter: " + e.getMessage(), e);
66         }
67     }
68
69     /**
70      * Sets variable in scripter context.
71      * @param name
72      * @param value
73      */

74     public void setVariable(String JavaDoc name, Object JavaDoc value) {
75         try {
76             super.set(name, value);
77         } catch (EvalError e) {
78             throw new ScriptException("Cannot set variable in scripter: " + e.getMessage(), e);
79         }
80     }
81
82     /**
83      * Evaluates specified expression or code block.
84      * @return value of evaluation or null if there is nothing.
85      */

86     public Object JavaDoc eval(String JavaDoc expression) {
87         // push all variables from context to the scripter
88
if (this.context != null) {
89             Iterator JavaDoc it = this.context.entrySet().iterator();
90             while (it.hasNext()) {
91                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
92                 String JavaDoc name = (String JavaDoc) entry.getKey();
93                 setVariable( name, entry.getValue() );
94             }
95         }
96         
97         try {
98             return super.eval(expression);
99         } catch (EvalError e) {
100             throw new ScriptException("Error during script execution: " + e.getMessage(), e);
101         }
102     }
103
104     /**
105      * Sets the specified variable in the context.
106      * @param name
107      * @param value
108      */

109     public void setInContext(String JavaDoc name, Object JavaDoc value) {
110         if (this.context != null) {
111             this.context.put(name, value);
112         }
113     }
114     
115 }
Popular Tags