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 fLineSeparator = "\n"; 9 10 15 public static String formatMsgBody(String msgBody, int lineWidth){ 16 17 String arrow = "> "; 18 String line = ""; 19 Vector lines1 = new Vector(); 20 Vector lines2 = new Vector(); 21 Vector returnLines = new Vector(); 22 StringBuffer replyString = new StringBuffer (); 23 24 int startIndex = 0; 25 int endIndex ; 26 int lineCapacity = lineWidth - arrow.length(); 27 28 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 for(int i=0; i<lines1.size(); i++){ 49 lines2 = breakLine((String )lines1.elementAt(i), lineCapacity, " ") ; 50 for(int j=0 ; j<lines2.size(); j++){ 51 returnLines.addElement((String )lines2.elementAt(j)); 52 } 53 } 54 55 for (int k=0; k<returnLines.size(); k++){ 56 replyString.append(arrow).append((String )returnLines.elementAt(k)).append(fLineSeparator); 57 } 58 59 return replyString.toString(); 60 61 } 62 63 64 public static Vector breakLine(String inStr, int lineWidth, String delimiter){ 65 int startIndex = 0; 66 int endIndex; 67 String leftPart; 68 String rightPart; 69 Vector lines = new Vector(); 70 Vector rightStrings = new Vector(); 71 Vector leftStrings = new Vector(); 72 73 if (inStr.length() > lineWidth){ 75 76 endIndex = inStr.lastIndexOf(delimiter); 77 if(endIndex != -1){ 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 if (leftPart.length()<lineWidth){ 87 leftStrings.addElement(leftPart); 88 } 89 else{ 90 leftStrings = breakLine(leftPart, lineWidth, delimiter); 91 } 92 if (rightPart.length()<lineWidth){ 94 rightStrings.addElement(rightPart); 95 } 96 else{ 97 rightStrings = breakLine(rightPart, lineWidth); 98 } 99 } 100 else{ 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 )leftStrings.elementAt(i)); 120 } 121 for (j=i; j<rightStrings.size(); j++){ 122 lines.addElement((String )rightStrings.elementAt(j)); 123 } 124 } 125 else{ 126 lines.addElement(inStr); 127 } 128 return lines; 129 } 130 131 132 135 public static Vector breakLine(String 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 154 public String formatBodyText(String str) { 155 if ((str == null) || (str.length() == 0)) { 156 return " "; 157 } else { 158 return str; 159 } 160 } 161 162 public String formatSubject(String str) { 163 if ((str == null) || (str.length() == 0)) { 164 return "[none]"; 165 } else { 166 return str; 167 } 168 } 169 170 } | Popular Tags |