KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Jt > examples > patterns > Visitor


1 package Jt.examples.patterns;
2 import java.util.*;
3 import java.lang.reflect.*;
4 import java.beans.*;
5 import java.io.*;
6 import Jt.*;
7
8
9 /**
10  * Demonstrates the use of the Visitor pattern.
11  */

12
13
14 public class Visitor extends JtObject {
15
16
17   public Visitor() {
18   }
19
20    // Encode the object using XML format
21

22    private Object JavaDoc encodeObject (Object JavaDoc obj) {
23
24      ByteArrayOutputStream stream = new ByteArrayOutputStream ();
25      XMLEncoder e;
26      Object JavaDoc result = null;
27
28
29      if (obj == null)
30        return (null);
31
32      try {
33
34        e = new XMLEncoder(
35                           new BufferedOutputStream(stream));
36        e.writeObject(obj);
37        e.close();
38        result = stream.toString ();
39
40      } catch (Exception JavaDoc ex) {
41        handleException (ex);
42        return (null);
43      }
44      return (result);
45
46    }
47
48   // Additional functionality implemented by the Visitor (print the object
49
// using XML format)
50

51   private Object JavaDoc printObject (Object JavaDoc obj) {
52
53     Object JavaDoc aux;
54
55     if (obj == null)
56       return (null);
57
58     aux = encodeObject (obj);
59     System.out.println (aux);
60     return (aux);
61   }
62
63   /**
64     * Process object messages.
65     * <ul>
66     * <li>JtVISIT - visit the object specified by msgContent.
67     * </ul>
68     * @param event Jt Message
69     */

70
71   public Object JavaDoc processMessage (Object JavaDoc event) {
72
73    String JavaDoc msgid = null;
74    JtMessage e = (JtMessage) event;
75    Object JavaDoc content;
76    Object JavaDoc data;
77    JtMessage aux;
78
79
80      if (e == null)
81     return null;
82
83      msgid = (String JavaDoc) e.getMsgId ();
84
85      if (msgid == null)
86     return null;
87
88      content = e.getMsgContent();
89
90      if (msgid.equals ("JtREMOVE")) {
91        return (this);
92      }
93
94      if (msgid.equals ("JtVISIT")) {
95        return (printObject (content));
96      }
97
98      handleError ("JtVisitor.processMessage: invalid message id:" + msgid);
99      return (null);
100
101   }
102
103   /**
104     * Test program
105     */

106
107   public static void main(String JavaDoc[] args) {
108
109     JtObject main = new JtFactory ();
110     JtMessage msg;
111     Visitor visitor;
112     Visitable node;
113
114
115
116     // Create an instance of JtVisitor
117

118     visitor = (Visitor)
119       main.createObject ("Jt.examples.patterns.Visitor",
120       "visitor");
121
122     node = (Visitable) main.createObject ("Jt.examples.patterns.Visitable",
123       "node");
124
125     System.out.println ("This visitor prints the XML representation of the object ...\n");
126
127     // Send an JtACCEPT message to the node. The message contains
128
// a reference to the visitor
129
msg = new JtMessage ("JtACCEPT");
130     msg.setMsgContent (visitor);
131     main.sendMessage (node, msg);
132
133
134     // Remove objects
135

136     main.removeObject (visitor);
137     main.removeObject (node);
138
139
140
141     
142          
143
144   }
145 }
146
Popular Tags