Source code: rcs/nml/DISPFormatConverter.java
1 package rcs.nml;
2
3 import rcs.nml.NMLFormatConverter;
4 import java.util.StringTokenizer;
5 import java.io.IOException;
6
7 /**
8 * The DISPFormatConverter converts NML message classes to simple
9 * text strings. Most users should not use it directly.
10 *
11 * <pre>
12 * Related Documentation:
13 * <A HREF="http://isd.cme.nist.gov/proj/rcs_lib">RCS Library</a>, <A HREF="http://isd.cme.nist.gov/proj/rcs_lib/NMLjava.html">NML Programmers Guide (Java Version)</a>
14 *
15 * Source Code:
16 * <A HREF="DISPFormatConverter.java">DISPFormatConverter.java</a>
17 *
18 * </pre>
19 *
20 * @author Will Shackleford -- <A HREF="mailto:shackle@cme.nist.gov">shackle@cme.nist.gov</a>
21 */
22 public class DISPFormatConverter extends NMLFormatConverterBase
23 {
24 protected byte temp_input_bytes[] = null;
25 String token;
26
27 public String toString()
28 {
29 return super.toString()+ " = "+getClass().getName()+" {\n"+
30 "temp_input_bytes="+temp_input_bytes+";\n"+
31 "token="+token+";\n}";
32 }
33
34 protected void write_string_without_zero_end(String str) throws IOException
35 {
36 if(debug_on)
37 {
38 System.out.println("write_string_without_zero_end("+str+") called.");
39 }
40 byte b[] = str.getBytes();
41 output_stream.write(b);
42 }
43
44 protected long improved_parse_long(String str)
45 {
46 try
47 {
48 while(str.length() > 1 &&
49 (str.charAt(0) == ' ' || str.charAt(0) == '\t' || str.charAt(0) == '+')
50 )
51 {
52 str = str.substring(1);
53 }
54 if(str.length() < 1)
55 {
56 throw new Exception();
57 }
58 Long L = Long.valueOf(str);
59 return (long) L.longValue();
60 }
61 catch(Exception e)
62 {
63 System.err.println("Can't parse "+str +" in "+input_string);
64 e.printStackTrace(System.out);
65 }
66 return 0;
67 }
68
69 protected int improved_parse_int(String str)
70 {
71 try
72 {
73 while(str.length() > 1 &&
74 (str.charAt(0) == ' ' || str.charAt(0) == '\t' || str.charAt(0) == '+')
75 )
76 {
77 str = str.substring(1);
78 }
79 if(str.length() < 1)
80 {
81 throw new Exception();
82 }
83 Integer I = Integer.valueOf(str);
84 return (int) I.intValue();
85 }
86 catch(Exception e)
87 {
88 if(debug_on)
89 {
90 System.out.println("token = "+token);
91 System.out.println("tokens_taken = "+tokens_taken);
92 System.out.println("tokens left = "+input_string_tokenizer.countTokens());
93 e.printStackTrace(System.out);
94 }
95 else
96 {
97 if(!error_in_update)
98 {
99 System.err.println("Can't parse "+str +" in "+input_string);
100 e.printStackTrace();
101 error_in_update = true;
102 }
103 }
104 }
105 return 0;
106 }
107
108 private int tokens_taken = 0;
109 protected void make_it_use_string()
110 {
111 try
112 {
113 if(!use_string)
114 {
115 tokens_taken = 0;
116 if(decoding)
117 {
118 if(bytes_in_input_stream_known && bytes_in_input_stream > 0)
119 {
120 temp_input_bytes = new byte[bytes_in_input_stream];
121 if(debug_on)
122 {
123 System.out.println("DISPFormatConverter.make_it_use_string() reading "+bytes_in_input_stream+" bytes from input_stream.");
124 }
125 input_stream.readFully(temp_input_bytes, 0, bytes_in_input_stream);
126 bytes_in_input_stream = 0;
127 input_string = new String(temp_input_bytes);
128 }
129 else
130 {
131 temp_input_bytes = new byte[2048];
132 input_string = "";
133 if(debug_on)
134 {
135 System.out.println("DISPFormatConverter.make_it_use_string() reading ? bytes from input_stream.");
136 }
137 while(input_stream.read(temp_input_bytes,0,2048) > 0)
138 {
139 input_string += new String(temp_input_bytes);
140 }
141 }
142 if(debug_on)
143 {
144 System.out.println("Checking for double commas");
145 System.out.println(input_string);
146 }
147 int dc_index = input_string.indexOf(",,");
148 while(dc_index > 0)
149 {
150 if(debug_on)
151 {
152 System.out.println("Breaking String");
153 System.out.println(input_string.substring(0,dc_index));
154 System.out.println(input_string.substring(dc_index+2));
155 }
156 input_string = input_string.substring(0,dc_index)+",(null),"+input_string.substring(dc_index+2);
157 if(debug_on)
158 {
159 System.out.println(input_string);
160 }
161 dc_index = input_string.indexOf(",,");
162 }
163 input_string_tokenizer = new StringTokenizer(input_string,",");
164 }
165 }
166 if(!decoding)
167 {
168 if(null == output_string)
169 {
170 output_string = "";
171 }
172 }
173 }
174 catch(Exception e)
175 {
176 if(debug_on)
177 {
178 System.out.println("token = "+token);
179 System.out.println("tokens_taken = "+tokens_taken);
180 System.out.println("tokens left = "+input_string_tokenizer.countTokens());
181 e.printStackTrace(System.out);
182 }
183 else
184 {
185 if(!error_in_update)
186 {
187 e.printStackTrace();
188 error_in_update = true;
189 }
190 }
191 }
192 use_string = true;
193 if(decoding)
194 {
195 if(debug_on)
196 {
197 System.out.println(input_string);
198 System.out.println("token = "+token);
199 System.out.println("tokens_taken = "+tokens_taken);
200 System.out.println("tokens left = "+input_string_tokenizer.countTokens());
201 }
202 tokens_taken++;
203 }
204 else
205 {
206 if(debug_on)
207 {
208 System.out.println("output_string = "+output_string);
209 }
210 }
211
212 }
213
214 public byte update(byte x)
215 {
216 try
217 {
218 if(error_in_update)
219 {
220 return x;
221 }
222 make_it_use_string();
223 if(decoding)
224 {
225 token = input_string_tokenizer.nextToken();
226 if(token != null)
227 {
228 if(token.charAt(0) == '\\')
229 {
230 x = (byte) Integer.valueOf(token.substring(1)).intValue();
231 }
232 else
233 {
234 x = (byte) token.charAt(0);
235 }
236 }
237 }
238 else
239 {
240 if(null != output_stream)
241 {
242 if(x < 100)
243 {
244 write_string_without_zero_end("\\0"+Integer.toString(x)+",");
245 output_string += "\\0"+Integer.toString(x)+",";
246 }
247 else
248 {
249 write_string_without_zero_end("\\"+Integer.toString(x)+",");
250 output_string += "\\0"+Integer.toString(x)+",";
251 }
252 raw_data_size = output_string.length();
253 }
254 }
255 }
256 catch(Exception e)
257 {
258 if(debug_on)
259 {
260 System.out.println("token = "+token);
261 System.out.println("tokens_taken = "+tokens_taken);
262 System.out.println("tokens left = "+input_string_tokenizer.countTokens());
263 e.printStackTrace(System.out);
264 }
265 else
266 {
267 if(!error_in_update)
268 {
269 e.printStackTrace();
270 error_in_update = true;
271 }
272 }
273 }
274
275 return x;
276 }
277
278
279 public void update(byte x[],int num_elements)
280 {
281 int i;
282 try
283 {
284 if(error_in_update)
285 {
286 return;
287 }
288 make_it_use_string();
289 if(decoding)
290 {
291 token = input_string_tokenizer.nextToken();
292 if(token != null)
293 {
294 int bytes_to_get = token.length();
295 if(bytes_to_get >=num_elements)
296 {
297 bytes_to_get = num_elements;
298 }
299 byte temp_bytes[] = token.getBytes();
300 for(i = 0; i < temp_bytes.length && i < x.length; i++)
301 {
302 x[i] = temp_bytes[i];
303 }
304 if(bytes_to_get < num_elements)
305 {
306 x[bytes_to_get] = 0;
307 }
308 }
309 }
310 else
311 {
312 for(i = 0; i < num_elements && i < x.length; i++)
313 {
314 if(x[i] == 0)
315 {
316 break;
317 }
318 }
319 if(i > 0)
320 {
321 if(debug_on)
322 {
323 System.out.println("updating string: length ="+i);
324 }
325 token = new String(x,0,i);
326 if(debug_on)
327 {
328 System.out.println("updating string ="+token);
329 }
330 output_string += token+",";
331 if(null != output_stream)
332 {
333 write_string_without_zero_end(token+",");
334 raw_data_size = output_string.length();
335 }
336 }
337 else
338 {
339 output_string += ",";
340 if(null != output_stream)
341 {
342 write_string_without_zero_end(",");
343 raw_data_size = output_string.length();
344 }
345 }
346 }
347 }
348 catch(Exception e)
349 {
350 if(debug_on)
351 {
352 System.out.println("token = "+token);
353 System.out.println("tokens_taken = "+tokens_taken);
354 System.out.println("tokens left = "+input_string_tokenizer.countTokens());
355 e.printStackTrace(System.out);
356 }
357 else
358 {
359 if(!error_in_update)
360 {
361 e.printStackTrace();
362 error_in_update = true;
363 }
364 }
365 }
366 }
367
368 public char update(char x)
369 {
370 try
371 {
372 if(error_in_update)
373 {
374 return x;
375 }
376 make_it_use_string();
377 if(decoding)
378 {
379 token = input_string_tokenizer.nextToken();
380 if(token != null)
381 {
382 char ch_array[] = new char[1];
383 token.getChars(0,1,ch_array,0);
384 x = ch_array[0];
385 }
386 }
387 else
388 {
389 output_string += x+",";
390 if(null != output_stream)
391 {
392 write_string_without_zero_end(x+",");
393 raw_data_size = output_string.length();
394 }
395 }
396 }
397 catch(Exception e)
398 {
399 if(debug_on)
400 {
401 System.out.println("token = "+token);
402 System.out.println("tokens_taken = "+tokens_taken);
403 System.out.println("tokens left = "+input_string_tokenizer.countTokens());
404 e.printStackTrace(System.out);
405 }
406 else
407 {
408 if(!error_in_update)
409 {
410 e.printStackTrace();
411 error_in_update = true;
412 }
413 }
414 }
415 return x;
416 }
417
418
419 public void update(char x[],int num_elements)
420 {
421 int i;
422 try
423 {
424 if(error_in_update)
425 {
426 return;
427 }
428 make_it_use_string();
429 if(decoding)
430 {
431 token = input_string_tokenizer.nextToken();
432 if(token != null)
433 {
434 int chars_to_get = token.length();
435 if(chars_to_get >= num_elements)
436 {
437 chars_to_get = num_elements;
438 }
439 token.getChars(0, chars_to_get,x,0);
440 }
441 }
442 else
443 {
444 for(i = 0; i < num_elements; i++)
445 {
446 if(x[i] == 0)
447 {
448 break;
449 }
450 }
451 token = new String(x,0,i);
452 output_string += token+",";
453 if(null != output_stream)
454 {
455 write_string_without_zero_end(token+",");
456 raw_data_size = output_string.length();
457 }
458 }
459 }
460 catch(Exception e)
461 {
462 if(debug_on)
463 {
464 System.out.println("token = "+token);
465 System.out.println("tokens_taken = "+tokens_taken);
466 System.out.println("tokens left = "+input_string_tokenizer.countTokens());
467 e.printStackTrace(System.out);
468 }
469 else
470 {
471 if(!error_in_update)
472 {
473 e.printStackTrace();
474 error_in_update = true;
475 }
476 }
477 }
478 }
479
480 public short update(short x)
481 {
482 try
483 {
484 if(error_in_update)
485 {
486 return x;
487 }
488 make_it_use_string();
489 if(decoding)
490 {
491 token = input_string_tokenizer.nextToken();
492 if(token != null)
493 {
494 x = (short) improved_parse_int(token);
495 }
496 }
497 else
498 {
499 output_string += x+",";
500 if(null != output_stream)
501 {
502 write_string_without_zero_end(x+",");
503 raw_data_size = output_string.length();
504 }
505 }
506 }
507 catch(Exception e)
508 {
509 if(debug_on)
510 {
511 System.out.println("token = "+token);
512 System.out.println("tokens_taken = "+tokens_taken);
513 System.out.println("tokens left = "+input_string_tokenizer.countTokens());
514 e.printStackTrace(System.out);
515 }
516 else
517 {
518 if(!error_in_update)
519 {
520 e.printStackTrace();
521 error_in_update = true;
522 }
523 }
524 }
525 return x;
526 }
527
528 public void update(short x[],int num_elements)
529 {
530 int i;
531 for(i = 0; i < num_elements; i++)
532 {
533 x[i] = update(x[i]);
534 }
535 }
536
537 public int update(int x)
538 {
539 try
540 {
541 if(error_in_update)
542 {
543 return x;
544 }
545 make_it_use_string();
546 if(decoding)
547 {
548 token = input_string_tokenizer.nextToken();
549 if(token != null)
550 {
551 x = improved_parse_int(token);
552 }
553 }
554 else
555 {
556 output_string += x+",";
557 if(null != output_stream)
558 {
559 write_string_without_zero_end(x+",");
560 raw_data_size = output_string.length();
561 }
562 }
563 }
564 catch(Exception e)
565 {
566 if(debug_on)
567 {
568 System.out.println("token = "+token);
569 System.out.println("tokens_taken = "+tokens_taken);
570 System.out.println("tokens left = "+input_string_tokenizer.countTokens());
571 e.printStackTrace(System.out);
572 }
573 else
574 {
575 if(!error_in_update)
576 {
577 e.printStackTrace();
578 error_in_update = true;
579 }
580 }
581 }
582 return x;
583 }
584
585 public void update(int x[],int num_elements)
586 {
587 int i;
588 for(i = 0; i < num_elements; i++)
589 {
590 /*if(debug_on)
591 {
592 System.out.println("tokens_taken = "+tokens_taken);
593 System.out.println("tokens left = "+input_string_tokenizer.countTokens());
594 } */
595 x[i] = update(x[i]);
596 }
597 }
598
599 public long update(long x)
600 {
601 if(error_in_update)
602 {
603 return x;
604 }
605 try
606 {
607 make_it_use_string();
608 if(decoding)
609 {
610 token = input_string_tokenizer.nextToken();
611 if(token != null)
612 {
613 x = improved_parse_long(token);
614 }
615 }
616 else
617 {
618 output_string += x+",";
619 if(null != output_stream)
620 {
621 write_string_without_zero_end(x+",");
622 raw_data_size = output_string.length();
623 }
624 }
625 }
626 catch(Exception e)
627 {
628 if(debug_on)
629 {
630 System.out.println("token = "+token);
631 System.out.println("tokens_taken = "+tokens_taken);
632 System.out.println("tokens left = "+input_string_tokenizer.countTokens());
633 e.printStackTrace(System.out);
634 }
635 else
636 {
637 if(!error_in_update)
638 {
639 e.printStackTrace();
640 error_in_update = true;
641 }
642 }
643 }
644 return x;
645 }
646
647 public void update(long x[],int num_elements)
648 {
649 int i;
650 for(i = 0; i < num_elements; i++)
651 {
652 x[i] = update(x[i]);
653 }
654 }
655
656 public float update(float x)
657 {
658 try
659 {
660 if(error_in_update)
661 {
662 return x;
663 }
664 make_it_use_string();
665 if(decoding)
666 {
667 token = input_string_tokenizer.nextToken();
668 if(token != null)
669 {
670 x = Float.valueOf(token).floatValue();
671 }
672 }
673 else
674 {
675 output_string += x+",";
676 if(null != output_stream)
677 {
678 write_string_without_zero_end(x+",");
679 raw_data_size = output_string.length();
680 }
681 }
682 }
683 catch(Exception e)
684 {
685 if(debug_on)
686 {
687 System.out.println("token = "+token);
688 System.out.println("tokens_taken = "+tokens_taken);
689 System.out.println("tokens left = "+input_string_tokenizer.countTokens());
690 e.printStackTrace(System.out);
691 }
692 else
693 {
694 if(!error_in_update)
695 {
696 e.printStackTrace();
697 error_in_update = true;
698 }
699 }
700 }
701 return x;
702 }
703
704 public void update(float x[],int num_elements)
705 {
706 int i;
707 for(i = 0; i < num_elements; i++)
708 {
709 x[i] = update(x[i]);
710 }
711 }
712
713
714 public double update(double x)
715 {
716 try
717 {
718 if(error_in_update)
719 {
720 return x;
721 }
722 make_it_use_string();
723 if(decoding)
724 {
725 token = input_string_tokenizer.nextToken();
726 if(token != null)
727 {
728 x = Double.valueOf(token).doubleValue();
729 }
730 }
731 else
732 {
733 output_string += x+",";
734 if(null != output_stream)
735 {
736 write_string_without_zero_end(x+",");
737 raw_data_size = output_string.length();
738 }
739 }
740 }
741 catch(Exception e)
742 {
743 if(debug_on)
744 {
745 System.out.println("token = "+token);
746 System.out.println("tokens_taken = "+tokens_taken);
747 System.out.println("tokens left = "+input_string_tokenizer.countTokens());
748 e.printStackTrace(System.out);
749 }
750 else
751 {
752 if(!error_in_update)
753 {
754 e.printStackTrace();
755 error_in_update = true;
756 }
757 }
758 }
759 return x;
760 }
761
762 public void update(double x[],int num_elements)
763 {
764 int i;
765 for(i = 0; i < num_elements; i++)
766 {
767 x[i] = update(x[i]);
768 }
769 }
770 }
771