KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Jt > JtInterpreter


1 package Jt;
2 import java.util.*;
3 import java.lang.reflect.*;
4 import java.beans.*;
5 import java.io.*;
6
7
8 /**
9  * Jt Implementation of the Interpreter pattern.
10  */

11
12
13 public class JtInterpreter extends JtObject {
14
15
16   private Object JavaDoc context = null;
17
18   public JtInterpreter() {
19   }
20
21 /**
22   * Specifies the interpreter context.
23   *
24   * @param context context
25   */

26
27   public void setContext (Object JavaDoc context) {
28      this.context = context;
29
30   }
31
32 /**
33   * Returns the interpreter context.
34   */

35
36   public Object JavaDoc getContext () {
37      return (context);
38   }
39
40
41
42
43   /**
44     * Process object messages. Subclasses need to override this method and implement
45     * the JtINTERPRET message.
46     */

47
48   public Object JavaDoc processMessage (Object JavaDoc event) {
49
50    String JavaDoc msgid = null;
51    JtMessage e = (JtMessage) event;
52    Object JavaDoc content;
53
54  
55      if (e == null)
56     return null;
57
58      msgid = (String JavaDoc) e.getMsgId ();
59
60      if (msgid == null)
61     return null;
62
63      content = e.getMsgContent();
64
65
66      // Remove this object
67
if (msgid.equals ("JtREMOVE")) {
68        return (this);
69      }
70
71      if (msgid.equals ("JtINTERPRET")) {
72        // subclass needs to implement/process this message
73
return (this);
74      }
75
76      handleError ("processMessage: invalid message id:" + msgid);
77      return (null);
78
79
80   }
81
82   /**
83     * Unit Tests the messages processed by JtInterpreter.
84     */

85
86   public static void main(String JavaDoc[] args) {
87
88     JtObject main = new JtFactory ();
89     JtMessage msg;
90     JtInterpreter interpreter;
91
92
93
94     // Create an instance of JtInterpreter
95

96     interpreter = (JtInterpreter)
97       main.createObject ("Jt.JtInterpreter",
98       "interpreter");
99
100
101     main.sendMessage (interpreter, new JtMessage ("JtINTERPRET"));
102
103     main.removeObject ("interpreter");
104          
105
106   }
107
108 }
Popular Tags