parse message algorithms..
hi all,
we are involving in sms project. due to the limitation of sms..
we need parse a exceed length message into smaller one.
we have implement a method to split the message to an array of vector.
but i got the the method seem very burte force. here is the example code. I am seeking a better approach to handle the same thing. any suggestion. there is some constraint. we need add "cont..." for first message if exceed length, "related to .." and "cont." between the message if it still span in next message. if the added message excced the length. the whole line will be added to next message vector.
import java.util.*;
publicclass SplitMessage2
{
publicstatic String msg2 ="Testing messageessage, @@this is a message @@this is a message edfd@@for testing split @@message function, @@it will split exceed @@length message into multiple";
publicstatic String con_msg ="Msg Cont..";
publicstatic String related_label ="Related..";
publicstaticvoid main(String args[])
{
StringTokenizer st =new StringTokenizer(msg2,"@@");
System.out.println("num of tokens:"+st.countTokens());
Vector v2 = split_message2(msg2,140);//the length must larger than length cont..+length of a line content
for (int i =0;i<v2.size();i++)
{
System.out.println("Line"+i+":"+ (String)v2.get(i));
}
}
publicstatic Vector split_message2(String msg,int len)
{
StringTokenizer st =new StringTokenizer(msg,"@@");
String loc_i_smsContinue ="Cont...";
String loc_i_smsRelate ="Related..";
StringBuffer loc_cls_sbSMSContent =new StringBuffer();
String loc_str_smsString ="";
Vector loc_vec_smsMsg =new Vector(2,2);
int i = 0;
int j =0;
int index = 0;
int totaltokens = st.countTokens();
j=0;
while (i==0)
{
while (true)
{
for(;j><totaltokens;)
{
System.out.println("Start of for-do");
if (j == 0){
//main header
System.out.println("in main header j:"+j);
loc_str_smsString = st.nextToken();
}
elseif(index ==j)//create a new page
{
//append related header
loc_cls_sbSMSContent.delete(0, loc_cls_sbSMSContent.length());
System.out.println("elseif will append:"+loc_str_smsString);
loc_cls_sbSMSContent.append(loc_i_smsRelate);//append previous one
loc_cls_sbSMSContent.append(loc_str_smsString);//append previous one
loc_str_smsString ="";
System.out.println("elseif num of tokens:"+st.countTokens());
if (st.hasMoreTokens())
{
loc_str_smsString = st.nextToken();
}else
{
j=totaltokens;
}
}else//next token
{
System.out.println("else num of tokens:"+st.countTokens());
if (st.hasMoreTokens())
{
loc_str_smsString = st.nextToken();
}else
{
j=totaltokens;
}
}
if (!(loc_str_smsString.length()==0) && (loc_cls_sbSMSContent.length() + loc_str_smsString.length() + loc_i_smsContinue.length()) ><= len)
{
System.out.println("in core function j:"+j);
loc_cls_sbSMSContent.append(loc_str_smsString);
j++;
}
else
{
if (j!=totaltokens)//not last one
loc_cls_sbSMSContent.append(loc_i_smsContinue);
//loc_vec_smsMsg.addElement(loc_cls_sbSMSContent.toString());
System.out.println("First Break");
index = j;
break;
}
}
if (j>=totaltokens)
{
System.out.println("All finish");
i=-1;
}
System.out.println("Last Break");
break;
}
System.out.println("Append sb to vector:"+i+loc_cls_sbSMSContent);
loc_vec_smsMsg.addElement(loc_cls_sbSMSContent.toString());
}
return loc_vec_smsMsg;
}
}
Message was edited by:
kyho

