KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Jt > xml > JtScriptInterpreter


1
2
3 package Jt.xml;
4 import java.util.*;
5 import java.lang.reflect.*;
6 import java.beans.*;
7 import java.io.*;
8 import org.xml.sax.*;
9 import org.xml.sax.helpers.*;
10 import Jt.*;
11
12 /**
13   * Jt Script interpreter
14   */

15
16
17 public class JtScriptInterpreter extends JtObject {
18   private XMLReader reader = null;
19   private String JavaDoc filename;
20   private String JavaDoc content;
21   private boolean remoteInvocation = false;
22 //Object output = null;
23
JtXMLHelper xmlHelper = null;
24   Object JavaDoc scriptOutput = null; // script output. Output of the last
25
// sendMessage, getValue or createObject
26
// operation
27

28   
29
30   public JtScriptInterpreter () {
31   }
32
33
34  /**
35   * Specifies the script filename.
36   * @param filename script filename
37   */

38
39   public void setFilename (String JavaDoc filename) {
40      this.filename = filename;
41   }
42
43
44  /**
45    * Returns the script filename.
46    */

47
48   public String JavaDoc getFilename () {
49      return (filename);
50   }
51
52  /**
53   * Specifies the script content.
54   * @param content script content
55   */

56
57   public void setContent (String JavaDoc content) {
58      this.content = content;
59   }
60
61
62  /**
63    * Returns the script content.
64    */

65   public String JavaDoc getContent () {
66      return (content);
67   }
68
69
70
71
72   public void setRemoteInvocation (boolean remoteInvocation) {
73      this.remoteInvocation = remoteInvocation;
74   }
75
76
77   public boolean getRemoteInvocation () {
78      return (remoteInvocation);
79   }
80
81   // propagateException:
82

83   private Exception JavaDoc propagateException (Object JavaDoc obj)
84   {
85     Exception JavaDoc ex;
86
87     if (obj == null)
88       return null;
89
90     ex = (Exception JavaDoc)
91      getValue (obj, "objException");
92
93     if (ex != null)
94       setValue (this, "objException", ex);
95
96     return (ex);
97   }
98  
99
100    // Interpret Jt Message: update scriptOutput for JtCREATE_OBJECT, JtSET_VALUE
101
// and JtSEND_MESSAGE
102

103    private Object JavaDoc interpretMsg (Object JavaDoc event) {
104
105      String JavaDoc msgid;
106      JtMessage msg = (JtMessage) event;
107      Object JavaDoc result = null;
108      String JavaDoc ctype;
109
110      if (msg == null)
111        return (null);
112
113      msgid = (String JavaDoc) msg.getMsgId ();
114
115      if (msgid == null)
116        return (null);
117
118      if (msgid.equals ("JtSET_VALUE")) {
119
120        if ("this".equals (msg.getMsgSubject ()))
121          setValue (this,
122                  msg.getMsgContent (),
123                  msg.getMsgData());
124        else
125          setValue (msg.getMsgSubject (),
126                  msg.getMsgContent (),
127                  msg.getMsgData());
128
129        //propagateException (msg.getMsgSubject ());
130
return (null);
131      }
132
133      if (msgid.equals ("JtCREATE_OBJECT")) {
134        //System.out.println ("JtScriptIterpreter(JtCREATE_OBJECT):" + msg.getMsgContent()
135
// + "," + msg.getMsgData());
136

137        ctype = (String JavaDoc) msg.getMsgContent();
138
139        if (remoteInvocation) {
140          if ("Jt.JtOSCommand".equals (ctype) || "Jt.JtFile".equals (ctype)) {
141            handleError ("Security violation: unable to create a remote instance of " +
142              ctype);
143
144            return (null);
145          }
146
147        }
148        result = createObject (msg.getMsgContent (),
149                  msg.getMsgData());
150
151        if (remoteInvocation)
152          return (null);
153
154        scriptOutput = result;
155        return (result);
156      }
157
158      if (msgid.equals ("JtGET_VALUE")) {
159
160
161        result = getValue (msg.getMsgContent (),
162                  msg.getMsgData());
163
164        scriptOutput = result;
165        return (result);
166      }
167
168      if (msgid.equals ("JtREMOVE_OBJECT")) {
169        removeObject (msg.getMsgContent ());
170        return (null);
171      }
172
173
174      if (msgid.equals ("JtSEND_MESSAGE")) {
175
176        result = sendMessage (msg.getMsgContent (),
177                     msg.getMsgData ());
178
179        //propagateException (msg.getMsgContent ());
180

181        scriptOutput = result;
182        return (result);
183
184      }
185
186      if (msgid.equals ("JtREMOVE")) {
187        return (null);
188      }
189
190      handleError ("JtScriptInterpreter.interpretMessage: invalid msg ID:"
191      + msg.getMsgId ());
192      return (null);
193    }
194
195
196   private Object JavaDoc parse () {
197     
198
199     JtMessage msg;
200     JtList col;
201     JtMessage msg1;
202     JtIterator iterator;
203     String JavaDoc str;
204     JtFile file;
205     Object JavaDoc result = null;
206     Exception JavaDoc ex;
207
208     if (filename != null && content != null) {
209       handleError ("JtScriptInterpreter.parse: set uri or string (only one)");
210       return null;
211     }
212
213     if (filename == null && content == null) {
214       handleError ("JtScriptInterpreter.parse: both uri and string are null");
215       return null;
216     }
217
218     scriptOutput = null;
219
220     if (filename != null) {
221
222       file = new JtFile ();
223
224       setValue (file, "name", filename);
225
226       str = (String JavaDoc) sendMessage (file, new JtMessage ("JtCONVERT_TO_STRING"));
227     
228
229     } else
230       str = content;
231
232
233     if (str == null)
234       return (null);
235
236     if (xmlHelper == null)
237       xmlHelper = (JtXMLHelper) createObject ("Jt.xml.JtXMLHelper", "xmlHelper");
238
239
240     msg = new JtMessage ("JtCONVERT_XML_TO_OBJECT");
241
242     msg.setMsgContent (str);
243
244     col = (JtList) sendMessage (xmlHelper, msg);
245
246     //destroyObject ("xmlHelper");
247

248     if (col == null)
249       return (null);
250
251     msg = new JtMessage ("JtNEXT");
252
253     iterator = (JtIterator) getValue (col, "iterator");
254
255     if (iterator == null)
256       return (null);
257
258     for (;;) {
259       //msg1 = (JtMessage) sendMessage (iterator, new JtMessage ("JtNEXT"));
260
msg1 = (JtMessage) iterator.processMessage (new JtMessage ("JtNEXT"));
261
262       if (msg1 == null)
263         break;
264
265       interpretMsg (msg1);
266
267       ex = (Exception JavaDoc)
268        getValue (this, "objException");
269
270       if (ex != null)
271         break; // stop processing the script
272
// if an exception is detected
273
}
274
275     return (scriptOutput);
276   }
277
278
279   // Process object messages
280

281   public Object JavaDoc processMessage (Object JavaDoc event) {
282
283    String JavaDoc msgid = null;
284    JtMessage e = (JtMessage) event;
285    Object JavaDoc content;
286
287      if (e == null)
288     return null;
289
290      msgid = (String JavaDoc) e.getMsgId ();
291
292      if (msgid == null)
293     return null;
294
295 // content = e.getMsgContent();
296

297      if (msgid.equals ("JtPARSE")) {
298         return (parse ());
299      }
300
301           
302      handleError ("JtScriptInterpreter.processMessage: invalid message id:" + msgid);
303      return (null);
304
305   }
306
307
308
309  /**
310    * Unit tests all the messages processed by JtScriptInterpreter.
311    */

312
313   public static void main(String JavaDoc[] args) {
314
315     JtObject main = new JtObject ();
316     JtMessage msg, msg1;
317     Integer JavaDoc count;
318     String JavaDoc str;
319
320     main.setObjTrace (1);
321     //main.setLogFile ("log.txt");
322

323
324     if (args.length < 1) {
325       System.err.println ("Usage: java JtScriptInterpreter uri");
326       System.exit (1);
327     }
328
329     // Create message reader
330

331     main.createObject ("Jt.xml.JtScriptInterpreter", "reader");
332
333     msg = (JtMessage) main.createObject ("Jt.JtMessage", "message");
334     main.setValue ("message", "msgId", "JtPARSE");
335     main.setValue ("reader", "filename", args[0]);
336
337
338     main.setValue ("message", "msgId", "JtPARSE");
339
340
341     main.sendMessage ("reader", "message");
342
343
344   }
345
346 }
347
348
349
Popular Tags