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 package com.reuters.msgtest.digester;
33
34 import com.reuters.msgtest.ArrayUtility;
35 import com.tibco.tibrv.TibrvException;
36 import com.tibco.tibrv.TibrvMsg;
37 import java.text.DateFormat;
38 import java.text.ParseException;
39 import java.util.Iterator;
40 import java.util.LinkedList;
41 import java.util.List;
42 import java.util.Locale;
43 import java.util.StringTokenizer;
44
45
46 /***
47 * @author Michael Ward
48 */
49 public class RvMessage {
50 public static final String ARRAY_STRING_DELIMITER = ",";
51 private String subject;
52 private List rvFields;
53
54 public RvMessage() {
55 rvFields = new LinkedList();
56 }
57
58 public void addField(Object rvField) {
59 rvFields.add(rvField);
60 }
61
62 public List getRvFields() {
63 return rvFields;
64 }
65
66 public void setRvFields(List rvFields) {
67 this.rvFields = rvFields;
68 }
69
70 public String getSubject() {
71 return subject;
72 }
73
74 public void setSubject(String subject) {
75 this.subject = subject;
76 }
77
78 /***
79 * converts this RvMessage into an actual TibrvMsg
80 */
81 public TibrvMsg convert() {
82 try {
83 TibrvMsg tibrvMsg = new TibrvMsg();
84 tibrvMsg.setSendSubject(subject);
85
86 for (Iterator iter = rvFields.iterator(); iter.hasNext();) {
87 addField(tibrvMsg, (RvField) iter.next());
88 }
89
90 return tibrvMsg;
91 } catch (Exception e) {
92 throw new RuntimeException("Unable to process RvMessage", e);
93 }
94 }
95
96 protected void addField(TibrvMsg tibrvMsg, RvField rvField)
97 throws TibrvException, ParseException {
98 String name = rvField.getName();
99 String value = rvField.getValue();
100
101 switch (rvField.getType()) {
102 case TibrvMsg.BOOL:
103 tibrvMsg.add(name, new Boolean(value));
104
105 break;
106
107 case TibrvMsg.DATETIME:
108
109 DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT,
110 DateFormat.SHORT, new Locale("en", "US"));
111 tibrvMsg.add(name, dateFormat.parse(value));
112
113 break;
114
115 case TibrvMsg.I8:
116 tibrvMsg.add(name, new Byte(value));
117
118 break;
119
120 case TibrvMsg.U8:
121 tibrvMsg.addU8(name, Byte.parseByte(value));
122
123 break;
124
125 case TibrvMsg.I16:
126 tibrvMsg.add(name, new Short(value));
127
128 break;
129
130 case TibrvMsg.U16:
131 tibrvMsg.addU16(name, Short.parseShort(value));
132
133 break;
134
135 case TibrvMsg.F32:
136 tibrvMsg.add(name, new Float(value));
137
138 break;
139
140 case TibrvMsg.I32:
141 tibrvMsg.add(name, new Integer(value));
142
143 break;
144
145 case TibrvMsg.U32:
146 tibrvMsg.addU32(name, Integer.parseInt(value));
147
148 break;
149
150 case TibrvMsg.I64:
151 tibrvMsg.add(name, new Long(value));
152
153 break;
154
155 case TibrvMsg.U64:
156 tibrvMsg.addU64(name, Long.parseLong(value));
157
158 break;
159
160 case TibrvMsg.STRING:
161 tibrvMsg.add(name, new String(value));
162
163 break;
164
165 case TibrvMsg.F64:
166 tibrvMsg.add(name, new Double(value));
167
168 break;
169
170 case TibrvMsg.MSG:
171
172 TibrvMsg nestedMsg = new TibrvMsg();
173 addMessage(nestedMsg, ((RvFieldParent) rvField).getChildren());
174 tibrvMsg.add(name, nestedMsg, TibrvMsg.MSG);
175
176 break;
177
178 case TibrvMsg.I8ARRAY:
179 case TibrvMsg.I16ARRAY:
180 case TibrvMsg.I32ARRAY:
181 case TibrvMsg.I64ARRAY:
182 case TibrvMsg.F32ARRAY:
183 case TibrvMsg.F64ARRAY:
184 addArrayField(tibrvMsg, name, value, rvField.getType());
185
186 break;
187
188 default:
189 throw new RuntimeException("Error in XML");
190 }
191 }
192
193 private void addArrayField(TibrvMsg tibrvMsg, String name, String value,
194 short type) throws TibrvException {
195 StringTokenizer st = new StringTokenizer(value, ARRAY_STRING_DELIMITER);
196 int len = st.countTokens();
197 Object array = null;
198
199 switch (type) {
200 case TibrvMsg.I8ARRAY:
201 array = new byte[len];
202
203 break;
204
205 case TibrvMsg.I16ARRAY:
206 array = new short[len];
207
208 break;
209
210 case TibrvMsg.I32ARRAY:
211 array = new int[len];
212
213 break;
214
215 case TibrvMsg.I64ARRAY:
216 array = new long[len];
217
218 break;
219
220 case TibrvMsg.F32ARRAY:
221 array = new float[len];
222
223 break;
224
225 case TibrvMsg.F64ARRAY:
226 array = new double[len];
227
228 break;
229 }
230
231 ArrayUtility.buildPrimitiveArray(array, value.split(","));
232 tibrvMsg.add(name, array);
233 }
234
235 private void addMessage(TibrvMsg tibrvMsg, List rvfields)
236 throws TibrvException, ParseException {
237 for (int i = 0; i < rvfields.size(); i++) {
238 addField(tibrvMsg, (RvField) rvfields.get(i));
239 }
240 }
241 }