Source code: cxtable/core_comm/xMessageConverter.java
1 package cxtable.core_comm;
2
3 import cxtable.xLineSplit;
4
5 /*This class handles two actions. pack(String) returns a String where
6 each line that ends with a linebreak is placed between <LN> </LN> tags.
7 The unpack(String) returns a String containing it switched back.
8 There is a potential weakness currently, in that any text potentially
9 "outside" of tags would be lost...however... the pack() method
10 defaults to automatically placing all text between those tags..so it should
11 be ok.. xMessageConverter has a counter on it to measure how many
12 are created... 2 per xClientConn (and therefore 4 per 'peer-to-peer' connection)
13 are created..*/
14
15
16
17 public class xMessageConverter{
18
19 private xLineSplit xls;
20 static int wawa=1;
21
22 public xMessageConverter()
23 {System.out.println("xMessageConverter("+wawa+") created");
24 wawa++;
25 xls=new xLineSplit();
26 }
27
28 public String unpack(String source)
29 {
30 String ret="";
31 String[] spl = xls.split("LN",source);
32 for(int i=0; i<spl.length; i++)
33 {
34 ret=ret+spl[i]+"\n";
35 }
36 return ret;
37 }
38
39 public String pack(String source)
40 {
41 String ret="";
42 boolean eline=true;
43 for (int i=0; i<source.length(); i++)
44 {
45 if (eline==true) {eline=false; ret=ret+"<LN>";}
46 if (source.charAt(i) == '\n')
47 {ret=ret+"</LN>"; eline=true;}
48 else{ret=ret+source.charAt(i);}
49 }
50 if (eline==false){
51 ret=ret+"</LN>";}
52
53 return ret;
54 }
55
56 public static void main(String[] args)
57 {
58 xMessageConverter xmc = new xMessageConverter();
59 String mssg="Hello, I am\n the walrus\n\n Who Who Cachoo\n";
60 System.out.println("Original:\n"+mssg+"\n");
61 String s=xmc.pack(mssg);
62 System.out.println("Formatted for transfer:\n"+s);
63 String t=xmc.unpack(s);
64 System.out.println("Converted back for output:\n"+t+"\n\nEND");
65 }
66
67 /*end*/
68 }