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 12 13 14 public class Visitor extends JtObject { 15 16 17 public Visitor() { 18 } 19 20 22 private Object encodeObject (Object obj) { 23 24 ByteArrayOutputStream stream = new ByteArrayOutputStream (); 25 XMLEncoder e; 26 Object 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 ex) { 41 handleException (ex); 42 return (null); 43 } 44 return (result); 45 46 } 47 48 51 private Object printObject (Object obj) { 52 53 Object 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 70 71 public Object processMessage (Object event) { 72 73 String msgid = null; 74 JtMessage e = (JtMessage) event; 75 Object content; 76 Object data; 77 JtMessage aux; 78 79 80 if (e == null) 81 return null; 82 83 msgid = (String ) 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 106 107 public static void main(String [] args) { 108 109 JtObject main = new JtFactory (); 110 JtMessage msg; 111 Visitor visitor; 112 Visitable node; 113 114 115 116 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 msg = new JtMessage ("JtACCEPT"); 130 msg.setMsgContent (visitor); 131 main.sendMessage (node, msg); 132 133 134 136 main.removeObject (visitor); 137 main.removeObject (node); 138 139 140 141 142 143 144 } 145 } 146 | Popular Tags |