KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scioworks > imap > business > util > MessagingUtilImpl


1 package scioworks.imap.business.util;
2
3 import scioworks.imap.spec.util.MessagingUtil;
4 import java.util.*;
5
6 public class MessagingUtilImpl implements MessagingUtil{
7
8   static final String JavaDoc fLineSeparator = "\n";
9
10   /**
11    * This method format a message body and append a "> " character
12    * in front of a new line. Length of a new line can be specified
13    * in the parameter lineWidth
14    */

15   public static String JavaDoc formatMsgBody(String JavaDoc msgBody, int lineWidth){
16
17     String JavaDoc arrow = "> ";
18     String JavaDoc line = "";
19     Vector lines1 = new Vector();
20     Vector lines2 = new Vector();
21     Vector returnLines = new Vector();
22     StringBuffer JavaDoc replyString = new StringBuffer JavaDoc();
23
24     int startIndex = 0;
25     int endIndex ;
26     int lineCapacity = lineWidth - arrow.length();
27
28     //first split msgBody into lines delimited by "\n"
29
//first we check the msgBody has at least one "\n" character
30
//note the lines themselves do not contain a "\n" at the end of the line
31
endIndex = msgBody.indexOf(fLineSeparator);
32     if (endIndex != -1){
33       while (endIndex != -1){
34         line = msgBody.substring(startIndex, endIndex);
35         lines1.addElement(line);
36         startIndex = endIndex + 1;
37         endIndex = msgBody.indexOf(fLineSeparator, startIndex);
38       }
39       if (startIndex < msgBody.length()){
40         lines1.addElement(msgBody.substring(startIndex, msgBody.length() -1));
41       }
42     }
43     else{
44       lines1.addElement(msgBody);
45     }
46
47     // next split the lines by " "
48
for(int i=0; i<lines1.size(); i++){
49       lines2 = breakLine((String JavaDoc)lines1.elementAt(i), lineCapacity, " ") ;
50       for(int j=0 ; j<lines2.size(); j++){
51         returnLines.addElement((String JavaDoc)lines2.elementAt(j));
52       }
53     }
54
55     for (int k=0; k<returnLines.size(); k++){
56       replyString.append(arrow).append((String JavaDoc)returnLines.elementAt(k)).append(fLineSeparator);
57     }
58
59     return replyString.toString();
60
61   }
62
63
64   public static Vector breakLine(String JavaDoc inStr, int lineWidth, String JavaDoc delimiter){
65     int startIndex = 0;
66     int endIndex;
67     String JavaDoc leftPart;
68     String JavaDoc rightPart;
69     Vector lines = new Vector();
70     Vector rightStrings = new Vector();
71     Vector leftStrings = new Vector();
72
73     //check if inStr is > than lineWidth
74
if (inStr.length() > lineWidth){
75
76       endIndex = inStr.lastIndexOf(delimiter);
77       if(endIndex != -1){ //there is at least one delimiter in the line
78
leftPart = inStr.substring(startIndex, endIndex);
79         if (inStr.length()-1 > endIndex){
80           rightPart = inStr.substring(endIndex+1, inStr.length()-1);
81         }
82         else{
83           rightPart = "";
84         }
85         //break the left part further if necessary
86
if (leftPart.length()<lineWidth){
87           leftStrings.addElement(leftPart);
88         }
89         else{
90           leftStrings = breakLine(leftPart, lineWidth, delimiter);
91         }
92         //break the right part further if necessary
93
if (rightPart.length()<lineWidth){
94           rightStrings.addElement(rightPart);
95         }
96         else{
97           rightStrings = breakLine(rightPart, lineWidth);
98         }
99       }
100       else{ //there is no delimiter in the line
101
if(inStr.length()-1 > startIndex){
102           rightPart = inStr.substring(startIndex, inStr.length()-1);
103         }
104         else{
105           rightPart = "";
106         }
107
108         if (rightPart.length() < lineWidth){
109           rightStrings.addElement(rightPart);
110         }
111         else{
112           rightStrings = breakLine(rightPart, lineWidth);
113         }
114       }
115
116       int i = 0;
117       int j;
118       for (i=0; i<leftStrings.size(); i++){
119         lines.addElement((String JavaDoc)leftStrings.elementAt(i));
120       }
121       for (j=i; j<rightStrings.size(); j++){
122         lines.addElement((String JavaDoc)rightStrings.elementAt(j));
123       }
124     }
125     else{
126       lines.addElement(inStr);
127     }
128     return lines;
129   }
130
131
132   /**
133    * This procedure will break a string into equal string of length lineWidth
134    */

135   public static Vector breakLine(String JavaDoc inStr, int lineWidth){
136     Vector lines = new Vector();
137     int startIndex = 0;
138     int endIndex = lineWidth ;
139
140     while (endIndex < inStr.length()){
141       lines.addElement(inStr.substring(startIndex, endIndex));
142       startIndex = endIndex + 1;
143       endIndex += lineWidth;
144     }
145
146     lines.addElement(inStr.substring(startIndex, inStr.length()-1));
147
148     return(lines);
149   }
150
151   /**
152    * Turns a null into " " so that DOM won't break
153    */

154   public String JavaDoc formatBodyText(String JavaDoc str) {
155     if ((str == null) || (str.length() == 0)) {
156       return " ";
157     } else {
158       return str;
159     }
160   }
161
162   public String JavaDoc formatSubject(String JavaDoc str) {
163     if ((str == null) || (str.length() == 0)) {
164       return "[none]";
165     } else {
166       return str;
167     }
168   }
169
170 }
Popular Tags