1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package netscape.javascript;
16
17 import java.applet.Applet;
18 import java.util.Arrays;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.htmlunit.Page;
23 import org.htmlunit.ScriptResult;
24 import org.htmlunit.corejs.javascript.ScriptableObject;
25 import org.htmlunit.html.HtmlPage;
26 import org.htmlunit.javascript.JavaScriptEngine;
27 import org.htmlunit.javascript.host.Element;
28 import org.htmlunit.javascript.host.Window;
29
30
31
32
33
34
35
36 public class JSObject {
37
38 private static final Log LOG = LogFactory.getLog(JSObject.class);
39
40 private static Window Window_;
41 private final ScriptableObject scriptableObject_;
42
43
44
45
46
47
48 public JSObject(final ScriptableObject scriptableObject) {
49 scriptableObject_ = scriptableObject;
50 }
51
52
53
54
55
56
57
58
59
60
61 public Object call(final String methodName, final Object[] args) throws JSException {
62 if (LOG.isInfoEnabled()) {
63 LOG.info("JSObject call '" + methodName + "(" + Arrays.toString(args) + ")'");
64 }
65
66 final Object jsResult = ScriptableObject.callMethod(scriptableObject_, methodName, args);
67 if (jsResult instanceof ScriptableObject) {
68 return new JSObject((ScriptableObject) jsResult);
69 }
70 if (jsResult instanceof CharSequence) {
71 return jsResult.toString();
72 }
73 return jsResult;
74 }
75
76
77
78
79
80
81
82
83
84 public Object eval(final String expression) throws JSException {
85 if (LOG.isInfoEnabled()) {
86 LOG.info("JSObject eval '" + expression + "'");
87 }
88
89 final Page page = Window_.getWebWindow().getEnclosedPage();
90 if (page instanceof HtmlPage) {
91 final HtmlPage htmlPage = (HtmlPage) page;
92 final ScriptResult result = htmlPage.executeJavaScript(expression);
93 final Object jsResult = result.getJavaScriptResult();
94 if (jsResult instanceof ScriptableObject) {
95 return new JSObject((ScriptableObject) jsResult);
96 }
97 if (jsResult instanceof CharSequence) {
98 return jsResult.toString();
99 }
100 return jsResult;
101 }
102 return null;
103 }
104
105
106
107
108
109
110
111
112
113 public Object getMember(final String name) throws JSException {
114 if (LOG.isInfoEnabled()) {
115 LOG.info("JSObject getMember '" + name + "'");
116 }
117
118 if (scriptableObject_ instanceof Element) {
119 return ((Element) scriptableObject_).getAttribute(name, null);
120 }
121 return scriptableObject_.get(name, scriptableObject_);
122 }
123
124
125
126
127
128
129
130
131
132 public void setMember(final String name, final Object value) throws JSException {
133 String stringValue = "";
134 if (value != null) {
135 stringValue = JavaScriptEngine.toString(value);
136 }
137
138 if (LOG.isInfoEnabled()) {
139 LOG.info("JSObject setMember '" + name + "' to '" + stringValue + "'");
140 }
141
142 if (scriptableObject_ instanceof Element) {
143 ((Element) scriptableObject_).setAttribute(name, stringValue);
144 return;
145 }
146 scriptableObject_.put(name, scriptableObject_, value);
147 }
148
149
150
151
152
153
154
155
156 public void removeMember(final String name) throws JSException {
157 LOG.error("Not yet implemented (netscape.javascript.JSObject.removeMember(String)).");
158 throw new RuntimeException("Not yet implemented (netscape.javascript.JSObject.removeMember(String)).");
159 }
160
161
162
163
164
165
166
167
168
169 public Object getSlot(final int index) throws JSException {
170 LOG.error("Not yet implemented (netscape.javascript.JSObject.getSlot(int)).");
171 throw new RuntimeException("Not yet implemented (netscape.javascript.JSObject.getSlot(int)).");
172 }
173
174
175
176
177
178
179
180
181
182 public void setSlot(final int index, final Object value) throws JSException {
183 LOG.error("Not yet implemented (netscape.javascript.JSObject.setSlot(int, Object)).");
184 throw new RuntimeException("Not yet implemented (netscape.javascript.JSObject.setSlot(int, Object)).");
185 }
186
187
188
189
190
191
192
193
194 public static JSObject getWindow(final Applet paramApplet) throws JSException {
195 return new JSObject(Window_);
196 }
197
198
199
200
201
202
203
204
205 public static void setWindow(final Window window) {
206 Window_ = window;
207 }
208 }