Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: rcs/nml/NMLFormatConverterBase.java


1   package rcs.nml;
2   
3   import java.io.*;
4   import java.util.*;
5   import rcs.utils.*;
6   
7   import rcs.nml.NMLmsg;
8   import rcs.nml.NMLMessageDictionary;
9   
10  import rcs.utils.CorrectedPipedInputStream;
11  import rcs.utils.CorrectedPipedOutputStream;
12  
13  
14  /**
15  * This class is the base class for all classes used by NML to convert
16  * local data types to some neutral format usable by many different types of 
17  * hosts.
18  * 
19  * Source Code:
20  * <A HREF="NMLFormatConverter.java">NMLFormatConverter.java</a>
21  * 
22  *
23  * @author  Will Shackleford -- <A HREF="mailto:shackle@cme.nist.gov">shackle@cme.nist.gov</a>
24  *
25  */
26  public abstract class NMLFormatConverterBase extends  NMLFormatConverter
27  {
28    /**
29     * True when the data from the network is being converted into a message in
30     * this hosts format, false when a message on this host is being converted to
31     * a neutral format.
32     */
33    public boolean decoding = false;
34    public boolean first_format_error = true;
35    protected boolean use_string = false;
36    public static boolean debug_on = false;
37    protected String output_string = null;
38    protected String input_string = null;
39    protected DataInputStream input_stream = null;
40    protected StringTokenizer input_string_tokenizer = null;
41    protected DataOutputStream output_stream = null;
42    
43    protected int msg_size = 0;
44    protected int raw_data_size = 0;
45    protected NMLMessageDictionary msg_dict = null;
46    protected int bytes_in_input_stream;
47    protected boolean bytes_in_input_stream_known = false;
48    protected void SetMessageDictionary(NMLMessageDictionary new_dict)
49    {
50      msg_dict = new_dict;
51    }
52  
53    /**
54     * Function that should be called before every message or string is parsed.
55     */
56    public void rewind()
57    {
58      msg_size = 0;
59      raw_data_size = 0;
60      bytes_in_input_stream = 0;
61      bytes_in_input_stream_known = false;
62      output_string = "";
63      input_string = "";
64      input_stream = null;
65      input_string_tokenizer = null;
66      output_stream = null;
67      stat_msg_updated = false;
68      cmd_msg_updated = false;
69      error_in_update = false;
70    
71    }
72  
73    
74      
75    protected NMLMessageDictionary GetMessageDictionary()
76    {
77      return msg_dict;
78    }
79    protected NMLmsg convertRawDataToMsg(byte b[], int offset, int size)
80    {
81      try
82        {
83    rewind();
84    if(b.length < size)
85      {
86        System.err.println("NMLFormatConverter.convertRawDataToMsg() , passed an invalid array of length "+b.length+" to hold "+size+" bytes");
87        return null;
88      }
89    decoding = true;
90    use_string = false;
91    raw_data_size = size;
92    CorrectedPipedInputStream pipe_in = new CorrectedPipedInputStream();
93    CorrectedPipedOutputStream pipe_out = new CorrectedPipedOutputStream(pipe_in);
94    input_stream = new DataInputStream(pipe_in);
95    if(debug_on)
96      {
97        System.out.println("convertRawDataToMsg(), Writing "+size+" bytes to pipe");
98        // System.out.println("b = "+new String(b,0,size));
99      }
100   pipe_out.write(b, offset, size );
101   bytes_in_input_stream = size;
102   raw_data_size = size;
103   bytes_in_input_stream_known = true;
104   msg_type = update(msg_type);
105   if(debug_on)
106     {
107       System.out.println("convertRawDataToMsg(), msg_type ="+msg_type);
108     }
109   msg_size = update(msg_size);
110   if(debug_on)
111     {
112       System.out.println("convertRawDataToMsg(), msg_size ="+msg_size);
113     }
114   if(null == msg_dict)
115     {
116       System.err.println("No Message Dictionary!!!");
117       return null;
118     }
119   if(msg_dict.formatMsg(this) < 0)
120     {
121       System.err.println("Format Message Error, msg_type = "+msg_type+", msg_dict = "+msg_dict+", formatter = "+this);
122       if(first_format_error)
123         {
124     Thread.dumpStack();
125     first_format_error = false;
126         }
127       return null;
128     }
129   return (NMLmsg) msg_to_update;
130       }
131     catch(Exception e)
132       {
133   e.printStackTrace();
134       }
135     return null;
136   }
137   
138   protected NMLmsg convertStringToMsg(String str)
139   {
140     try
141       {
142   rewind();
143   if(null == str)
144     {
145       return null;
146     }
147   decoding = true;
148   use_string = true;
149   bytes_in_input_stream = 0;
150   bytes_in_input_stream_known = false;
151   if(debug_on)
152     {
153       System.out.println("NMLFormatConverter.convertStringToMsg("+str+") called.");
154     }
155   input_string = str;
156   if(debug_on)
157     {
158       System.out.println("Checking for double commas");
159       System.out.println(input_string);
160     }
161   int dc_index = input_string.indexOf(",,");
162   while(dc_index > 0)
163     {
164       if(debug_on)
165         {
166     System.out.println("Breaking String");
167     System.out.println(input_string.substring(0,dc_index));
168     if(dc_index < input_string.length() -2)
169       {  
170         System.out.println(input_string.substring(dc_index+2));
171       }
172         }
173       if(dc_index < input_string.length() -2)
174         {
175     input_string = input_string.substring(0,dc_index)+",(null),"+input_string.substring(dc_index+2);
176         }
177       else
178         {
179     input_string = input_string.substring(0,dc_index)+",(null),";
180         }
181       if(debug_on)
182         {
183     System.out.println(input_string);
184         }
185       dc_index = input_string.indexOf(",,");
186     }
187   input_string_tokenizer = new StringTokenizer(input_string,",");
188   msg_type = update(msg_type);
189   msg_size = update(msg_size);
190   if(null == msg_dict)
191     {
192       System.err.println("No Message Dictionary!!!");
193       return null;
194     }
195   if(msg_dict.formatMsg(this) < 0)
196     {
197       System.err.println("Format Message Error, msg_type = "+msg_type+", msg_dict = "+msg_dict+", formatter = "+this);
198       if(first_format_error)
199         {
200     Thread.dumpStack();
201     first_format_error = false;
202         }
203       return null;
204     }
205   if(debug_on)
206     {
207       NMLmsg NMLmsg_to_update = (NMLmsg) msg_to_update;
208       System.out.println("NMLFormatConverter.convertStringToMsg: msg_type = "+NMLmsg_to_update.type+", msg_size = "+NMLmsg_to_update.size);
209     }
210   return (NMLmsg) msg_to_update;
211       }
212     catch(Exception e)
213       {
214   e.printStackTrace();
215       }
216     return null;
217   }
218 
219   protected int convertMsgToRawData(byte b[], int size, NMLmsg msg)
220   {
221     try
222       {
223   rewind();
224   decoding = false;
225   use_string = false;
226   if(debug_on)
227     {
228         
229     }
230   CorrectedPipedInputStream pipe_in = new CorrectedPipedInputStream();
231   CorrectedPipedOutputStream pipe_out = new CorrectedPipedOutputStream(pipe_in);
232   output_stream = new DataOutputStream(pipe_out);
233   msg_to_update = msg;
234   msg_type = msg.type;
235   msg_size = msg.size;
236   bytes_in_input_stream = 0;
237   raw_data_size = 0;
238   bytes_in_input_stream_known = false;
239   msg_type = update(msg_type);
240   msg_size = update(msg_size);
241   if(msg_type > 0 && msg.type == 0)
242     {
243       msg.type = msg_type;
244     }
245   msg.update(this);
246   if(raw_data_size > size)
247     {
248       return -1;
249     }
250   if(debug_on)
251     {
252       System.out.println("convertMsgToRawData(), Reading "+raw_data_size+" bytes from pipe");
253     }
254   pipe_in.read(b,0,raw_data_size);
255   if(debug_on)
256     {
257       //System.out.println("b = "+new String(b,0,raw_data_size));
258     }
259   return raw_data_size;
260       }
261     catch(Exception e)
262       {
263   e.printStackTrace();
264       }
265     return -1;
266   }
267 
268   protected String convertMsgToString(NMLmsg msg)
269   {
270     try
271       {
272   rewind();
273   if(null == msg)
274     {
275       return null;
276     }
277   decoding = false;
278   use_string = true;
279   output_string = "";
280   msg_to_update = msg;
281   msg_type = msg.type;
282   msg_size = msg.size;
283   bytes_in_input_stream = 0;
284   bytes_in_input_stream_known = false;
285   msg_type = update(msg_type);
286   msg_size = update(msg_size);
287   msg.update(this);
288   if(debug_on)
289     {
290       System.out.println("convertMsgToString, output_string =" + output_string);
291     }
292   if(null == output_string)
293     {
294       System.err.println(" convertMsgToString: output_string is null !!!");
295     }
296   return output_string;
297       }
298     catch(Exception e)
299       {
300   e.printStackTrace();
301       }
302     return null;
303   }
304 
305   protected NMLmsg getMessageFromInputStream(DataInputStream dis)
306   {
307     try
308       {
309   rewind();
310   input_stream = dis;
311   decoding = true;
312   use_string = false;
313   msg_type = update(msg_type);
314   msg_size = update(msg_size);
315   bytes_in_input_stream = -1;
316   if(null == msg_dict)
317     {
318       System.err.println("No Message Dictionary!!!");
319       return null;
320     }
321   if(msg_dict.formatMsg(this) < 0)
322     {
323       System.err.println("Format Message Error, msg_type = "+msg_type+", msg_dict = "+msg_dict+", formatter = "+this);
324       if(first_format_error)
325         {
326     Thread.dumpStack();
327     first_format_error = false;
328         }
329       return null;
330     }
331   return (NMLmsg) msg_to_update;
332       }
333     catch(Exception e)
334       {
335   e.printStackTrace();
336       }
337     return null;
338   }
339 
340   protected int sendMsgToOutputStream(DataOutputStream dos, NMLmsg msg)
341   {
342     try
343       {
344   rewind();
345   decoding = false;
346   use_string = false;
347   bytes_in_input_stream = 0;
348   output_stream = dos;
349   msg_to_update = msg;
350   msg.update(this);
351   return raw_data_size;
352       }
353     catch(Exception e)
354       {
355   e.printStackTrace();
356       }
357     return -1;
358   }
359   public abstract  byte update(byte x);
360   public abstract  void update(byte x[],int num_elements);
361   public abstract  char update(char x);
362   public abstract  void update(char x[],int num_elements);
363   public abstract  short update(short x);
364   public abstract  void update(short x[],int num_elements);
365   public abstract  int update(int x);
366   public abstract  void update(int x[],int num_elements);
367   public abstract  long update(long x);
368   public abstract  void update(long x[],int num_elements);
369   public abstract  float update(float x);
370   public abstract  void update(float x[],int num_elements);
371   public abstract  double update(double x);
372   public abstract  void update(double x[],int num_elements);
373 
374   // Don't implement the functions here.
375   /*  public  byte update(byte x) { return 0;} ;
376   public  void update(byte x[],int num_elements) {} ;
377   public  char update(char x) {return 0;} ;
378   public  void update(char x[],int num_elements) {} ;
379   public  short update(short x) {return 0;} ;
380   public  void update(short x[],int num_elements) {} ;
381   public  int update(int x) {return 0;} ;
382   public  void update(int x[],int num_elements) {} ;
383   public  long update(long x) {return 0;} ;
384   public  void update(long x[],int num_elements) {} ;
385   public  float update(float x) {return 0;} ;
386   public  void update(float x[],int num_elements) {} ;
387   public  double update(double x) {return 0;} ;
388   public  void update(double x[],int num_elements) {} ; */
389 }
390