1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61 package com.reuters.msgtest;
62
63 import com.tibco.tibrv.TibrvException;
64 import com.tibco.tibrv.TibrvMsg;
65 import com.tibco.tibrv.TibrvMsgField;
66 import java.util.ArrayList;
67 import java.util.Date;
68 import java.util.Iterator;
69 import java.util.List;
70 import java.util.Map;
71 import java.util.TreeMap;
72
73
74 /***
75 * Clean up the standard tibrvlisten program to make it easier to read. Field
76 * names are put in alphabetical order before printing and the type of the data
77 * field is printed as well. The printing of nested messages is controlled using
78 * the static boolean field NESTEDON. The toString methods will not be thread
79 * safe if this option is turned on due to the use of static variables. <p/>
80 * When a message of type byte[] is encountered a new TibrvMsg is created using
81 * this array. <p/>Remove extranious syncronized objects. <p/>Created: Mon Dec
82 * 18 10:32:21 2000
83 *
84 * @author Mark Pollack
85 * @version @VSS_FLAG_VERSION@
86 */
87 public class PrettyPrint {
88 public static boolean NESTEDON = false;
89 private static int recursion_count = 0;
90 private static String QUOTE = "\"";
91
92 public PrettyPrint() {
93 }
94
95 /***
96 * Initial Implementation take from TibrvMsg.toString()
97 *
98 * @param msg
99 * a value of type 'TibrvMsg'
100 * @return a value of type 'String'
101 */
102 public static String toString(TibrvMsg msg) {
103 {
104 Map fieldmap = new TreeMap();
105
106 try {
107 for (int i = 0; i < msg.getNumFields(); i++) {
108
109 String fieldname = msg.getFieldByIndex(i).name;
110
111 List l = (List) fieldmap.get(fieldname);
112
113 if (l == null) {
114 fieldmap.put(fieldname, l = new ArrayList());
115 }
116
117 l.add(msg.getFieldByIndex(i));
118 }
119 } catch (TibrvException e) {
120 e.printStackTrace();
121 }
122
123 StringBuffer buffer = new StringBuffer(1024).append("{");
124
125 for (Iterator i = fieldmap.values().iterator(); i.hasNext();) {
126 List l = (List) i.next();
127
128 for (Iterator j = l.iterator(); j.hasNext();) {
129 buffer.append("\n" + spaces(recursion_count));
130
131 TibrvMsgField tibrvmsgfield = (TibrvMsgField) j.next();
132
133 Object data = tibrvmsgfield.data;
134 short type = tibrvmsgfield.type;
135
136 if (tibrvmsgfield.name != null) {
137 buffer.append(" " + tibrvmsgfield.name);
138 } else if (tibrvmsgfield.id == 0) {
139 buffer.append(" <null>");
140 }
141
142 if (tibrvmsgfield.id != 0) {
143 buffer.append("(" + String.valueOf(tibrvmsgfield.id) +
144 ")");
145 }
146
147 buffer.append("=");
148
149 if (data == null) {
150 buffer.append("[NO DECODER OR DECODER FAILED FOR TYPE " +
151 (type & 0xff) + "]");
152
153 continue;
154 }
155
156 if (data instanceof String) {
157 buffer.append("\"" + data.toString() + "\"" +
158 " [string]");
159
160 continue;
161 }
162
163 if (data instanceof byte[]) {
164 byte[] abyte0 = (byte[]) data;
165
166 if ((type == 0) || (type == 7)) {
167 if (NESTEDON) {
168 try {
169 TibrvMsg amsg = new TibrvMsg(abyte0);
170 recursion_count++;
171 buffer.append(PrettyPrint.toString(amsg) +
172 " [TibrvMsg(byte[])]");
173 recursion_count--;
174
175
176
177 } catch (TibrvException e) {
178
179 buffer.append("[" + abyte0.length +
180 " opaque bytes]");
181 }
182 } else {
183 buffer.append("[" + abyte0.length +
184 " opaque bytes]");
185 }
186 } else {
187 if (type == 34) {
188 buffer.append("[I8: " + abyte0.length +
189 " elements]");
190 } else if (type == 35) {
191 buffer.append("[I8: " + abyte0.length +
192 " elements]");
193 } else {
194 buffer.append("[NO CONVERSION]");
195 }
196 }
197
198 continue;
199 }
200
201 if (data instanceof short[]) {
202 short[] aword0 = (short[]) data;
203
204 if ((type == 0) || (type == 36)) {
205 buffer.append("[I16: " + aword0.length +
206 " elements]");
207 } else if (type == 37) {
208 buffer.append("[U16: " + aword0.length +
209 " elements]");
210 } else {
211 buffer.append("[NO CONVERSION]");
212 }
213
214 continue;
215 }
216
217 if (data instanceof int[]) {
218 int[] ai = (int[]) data;
219
220 if ((type == 0) || (type == 38)) {
221 buffer.append("[I32: " + ai.length + " elements]");
222 } else if (type == 39) {
223 buffer.append("[U32: " + ai.length + " elements]");
224 } else {
225 buffer.append("[NO CONVERSION]");
226 }
227
228 continue;
229 }
230
231 if (data instanceof long[]) {
232 long[] al = (long[]) data;
233
234 if ((type == 0) || (type == 40)) {
235 buffer.append("[I64: " + al.length + " elements]");
236 } else if (type == 41) {
237 buffer.append("[U64: " + al.length + " elements]");
238 } else {
239 buffer.append("[NO CONVERSION]");
240 }
241
242 continue;
243 }
244
245 if (data instanceof float[]) {
246 float[] af = (float[]) data;
247
248 if ((type == 0) || (type == 44)) {
249 buffer.append("[F32: " + af.length + " elements]");
250 } else if (type == 45) {
251 buffer.append("[F64: " + af.length + " elements]");
252 } else {
253 buffer.append("[NO CONVERSION]");
254 }
255
256 continue;
257 }
258
259 if (data instanceof double[]) {
260 double[] ad = (double[]) data;
261
262 if ((type == 0) || (type == 45)) {
263 buffer.append("[F64: " + ad.length + " elements]");
264 } else if (type == 44) {
265 buffer.append("[F32: " + ad.length + " elements]");
266 } else {
267 buffer.append("[NO CONVERSION]");
268 }
269
270 continue;
271 }
272
273 if (data instanceof TibrvMsg) {
274 if (NESTEDON) {
275 recursion_count++;
276 buffer.append(PrettyPrint.toString((TibrvMsg) data) +
277 " [TibrvMsg]");
278 recursion_count--;
279 } else {
280 buffer.append(data.toString() + " [TibrvMsg]");
281 }
282
283 continue;
284 }
285
286 if (data instanceof Boolean) {
287 buffer.append(QUOTE + data.toString() + QUOTE +
288 " [boolean]");
289
290 continue;
291 }
292
293 if (data instanceof Integer) {
294 buffer.append(QUOTE + data.toString() + QUOTE +
295 " [int]");
296
297 continue;
298 }
299
300 if (data instanceof Float) {
301 buffer.append(QUOTE + data.toString() + QUOTE +
302 " [float]");
303
304 continue;
305 }
306
307 if (data instanceof Long) {
308 buffer.append(QUOTE + data.toString() + QUOTE +
309 " [long]");
310
311 continue;
312 }
313
314 if (data instanceof Byte) {
315 buffer.append(QUOTE + data.toString() + QUOTE +
316 " [byte]");
317
318 continue;
319 }
320
321 if (data instanceof Date) {
322 buffer.append(QUOTE + data.toString() + QUOTE +
323 " [date]");
324
325 continue;
326 }
327
328 if (data instanceof Double) {
329 buffer.append(QUOTE + data.toString() + QUOTE +
330 " [double]");
331
332 continue;
333 }
334
335 if (data instanceof Short) {
336 buffer.append(QUOTE + data.toString() + QUOTE +
337 " [short]");
338
339 continue;
340 }
341
342 buffer.append(QUOTE + data.toString() + QUOTE +
343 " [not yet impl: " + data.getClass() + " ]");
344
345 continue;
346 }
347 }
348
349 if (recursion_count != 0) {
350 buffer.append("\n" + spaces(recursion_count - 1) + " }");
351 } else {
352 buffer.append("\n}");
353 }
354
355 return buffer.toString();
356 }
357 }
358
359 private static String spaces(int nspaces) {
360 StringBuffer sb = new StringBuffer();
361
362 for (int i = 0; i < nspaces; i++) {
363 sb.append(" ");
364 }
365
366 return sb.toString();
367 }
368 }