MUTLUCELL-API

Uzun Mesajları Bölme

Uzun Mesajları Bölme

Önceki Konu Bu Son Konu  

Uzun Mesajları Bölme

Önceki Konu Bu Son Konu Yazdır için javascript aktif olması lazım!  

SMS Text Segmenter

 

import java.util.Vector;

 

public class SmsTextSegmenter

{

 private String message;

 private Charset charset;

 

 private static final String SWORD = "[]|{}^\\~€";

 private static final String TURKISH_WORD = "ĞğŞşİıç";

 

 public SmsTextSegmenter( String msg, Charset charset )

 {

         this.message = msg;

         this.charset = charset;

 }

 

 public Vector<String> getSegments()

 {

         Vector<String> msgSegments = new Vector<String>();

         

         String tempmsg = message;

         

         if ( isTurkish() ) {

                 tempmsg = toSmsWord( message );

         }

         

         int msglength = tempmsg.length();

         

         boolean needsConcat = isUnicode() ? msglength > 70 : ( isTurkish() ? msglength > 155 : msglength > 160 );

         

         if ( !needsConcat ) {

                 msgSegments.add( isUnicode() ? tempmsg : toNormalWord( tempmsg ) );

                 return msgSegments;

         }

         

         // needs concatenation

         int capacity = isUnicode() ? 67 : ( isTurkish() ? 149 : 153 );

         

         int index = 0;

         while ( msglength > 0 ) {

                 String submsg = tempmsg.substring( index * capacity, ( capacity * index )

                         + ( msglength > capacity ? capacity : msglength ) );

                 

                 msgSegments.add( isUnicode() ? submsg : toNormalWord( submsg ) );

                 if ( msgSegments.size() == 5 ) break;

                 msglength -= capacity;

                 index++;

         }

         

         return msgSegments;

 }

 

 private static boolean isSpecialChar( char c)

 {

         return SWORD.indexOf( c ) != -1;

 }

 

 private static boolean isTurkishWord( char c)

 {

         return TURKISH_WORD.indexOf( c ) != -1;

 }

 

 private String toSmsWord( String s)

 {

         char[] chars = s.toCharArray();

         StringBuffer sb = new StringBuffer();

         for ( int i = 0; i < chars.length; i++ ) {

                 if ( isSpecialChar( chars[i] ) ) {

                         sb.append( (char) 0 );

                 } else if ( !isLocked() && isTurkishWord( chars[i] ) ) {

                         sb.append( (char) 0 );

                 }

                 sb.append( chars[i] );

         }

         return sb.toString();

 }

 

 private String toNormalWord( String s)

 {

         char[] chars = s.toCharArray();

         StringBuffer sb = new StringBuffer();

         for ( int i = 0; i < chars.length; i++ ) {

                 if ( chars[i] == 0 ) {

                         continue;

                 }

                 sb.append( chars[i] );

         }

         return sb.toString();

 }

 

 private boolean isUnicode()

 {

         return charset == Charset.UNICODE;

 }

 

 private boolean isTurkish()

 {

         return charset == Charset.TURKISH || charset == Charset.TURKISHLOCK;

 }

 

 private boolean isLocked()

 {

         return charset == Charset.TURKISHLOCK;

 }

 

       enum Charset {

 DEFAULT, TURKISH, TURKISHLOCK, UNICODE;

 }

}