KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > micronova > util > codec > CodecJavascript


1 /*
2
3 Copyright 2003-2007 MicroNova (R)
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or
7 without modification, are permitted provided that the following
8 conditions are met:
9
10     * Redistributions of source code must retain the above copyright
11     notice, this list of conditions and the following disclaimer.
12
13     * Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions and the following disclaimer in the
15     documentation and/or other materials provided with the distribution.
16
17     * Neither the name of MicroNova nor the names of its contributors
18     may be used to endorse or promote products derived from this
19     software without specific prior 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 */

34
35
36 package com.micronova.util.codec;
37
38 import java.util.*;
39 import com.micronova.util.*;
40 import org.mozilla.javascript.*;
41
42 public class CodecJavascript extends Codec
43 {
44     /** evaluates/compiles Javascript expression using rhino */
45
46     public static final Object JavaDoc eval(Object JavaDoc object, Object JavaDoc environment) throws Exception JavaDoc
47     {
48         if (object != null)
49         {
50             NestedMap envMap = TypeUtil.isNestedMap(environment);
51             Context cx = Context.enter();
52
53             try
54             {
55                 Scriptable scope = cx.initStandardObjects();
56
57                 if (envMap != null)
58                 {
59                     Iterator iterator = envMap.entrySet().iterator();
60
61                     while (iterator.hasNext())
62                     {
63                         Map.Entry entry = (Map.Entry)iterator.next();
64                         ScriptableObject.putProperty(scope, entry.getKey().toString(), Context.javaToJS(entry.getValue(), scope));
65                     }
66                 }
67
68                 if (object instanceof Script)
69                 {
70                     object = ((Script)object).exec(cx, scope);
71                 }
72                 else
73                 {
74                     object = cx.evaluateString(scope, object.toString(), "<jsp>", 1, null);
75                 }
76                     
77                 object = cx.jsToJava(object, java.lang.Object JavaDoc.class);
78             }
79             catch (Exception JavaDoc e)
80             {
81                 throw e;
82             }
83             finally
84             {
85                 Context.exit();
86             }
87         }
88         
89         return object;
90     }
91
92     public static final Object JavaDoc eval(Object JavaDoc object) throws Exception JavaDoc
93     {
94         return eval(object, null);
95     }
96
97     /** compiles a script. Returned object can be used as "value" in eval. */
98
99     public static final Object JavaDoc compile(Object JavaDoc object) throws Exception JavaDoc
100     {
101         if (object != null)
102         {
103             Context cx = Context.enter();
104             try
105             {
106                 Scriptable scope = cx.initStandardObjects();
107         
108                 object = cx.compileString(object.toString(), "<jsp>", 1, null);
109             }
110             catch (Exception JavaDoc e)
111             {
112                 throw e;
113             }
114             finally
115             {
116                 cx.exit();
117             }
118         }
119         return object;
120     }
121 }
122
Popular Tags