View Javadoc

1   /*
2    * Copyright (c) 2004, RV Test Team
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    *
8    * Redistributions of source code must retain the above copyright notice, this
9    * list of conditions and the following disclaimer.
10   *
11   * Redistributions in binary form must reproduce the above copyright notice,
12   * this list of conditions and the following disclaimer in the documentation
13   * and/or other materials provided with the distribution.
14   *
15   * Neither the name of the "RV Test Team" nor the names of its contributors may
16   * be used to endorse or promote products derived from this software without
17   * specific prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29   * THE POSSIBILITY OF SUCH DAMAGE.
30   *
31   */
32  /*
33   * Copyright (c) 2002, Reuters
34   * All rights reserved.
35   *
36   * Redistribution and use in source and binary forms, with or without
37   * modification, are permitted provided that the following conditions are met:
38   *
39   * Redistributions of source code must retain the above copyright notice, this
40   * list of conditions and the following disclaimer.
41   * Redistributions in binary form must reproduce the above copyright notice,
42   * this list of conditions and the following disclaimer in the documentation
43   * and/or other materials provided with the distribution.
44   * Neither the name of the Reuters nor the names of its contributors may be
45   * used to endorse or promote products derived from this software without
46   * specific prior written permission.
47   *
48   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
49   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
52   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
53   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
54   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
55   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
56   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
57   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
58   * THE POSSIBILITY OF SUCH DAMAGE.
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                     //fieldnames.add(msg.getFieldByIndex(i).name);
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                                     //buffer = buffer + "[" + abyte0.length + "
176                                     // opaque bytes]";
177                                 } catch (TibrvException e) {
178                                     //e.printStackTrace();
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                 } //end loop over tibrvfields.
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 }