001: package scioworks.imap.business.util;
002:
003: import scioworks.imap.spec.util.MessagingUtil;
004: import java.util.*;
005:
006: public class MessagingUtilImpl implements MessagingUtil {
007:
008: static final String fLineSeparator = "\n";
009:
010: /**
011: * This method format a message body and append a "> " character
012: * in front of a new line. Length of a new line can be specified
013: * in the parameter lineWidth
014: */
015: public static String formatMsgBody(String msgBody, int lineWidth) {
016:
017: String arrow = "> ";
018: String line = "";
019: Vector lines1 = new Vector();
020: Vector lines2 = new Vector();
021: Vector returnLines = new Vector();
022: StringBuffer replyString = new StringBuffer();
023:
024: int startIndex = 0;
025: int endIndex;
026: int lineCapacity = lineWidth - arrow.length();
027:
028: //first split msgBody into lines delimited by "\n"
029: //first we check the msgBody has at least one "\n" character
030: //note the lines themselves do not contain a "\n" at the end of the line
031: endIndex = msgBody.indexOf(fLineSeparator);
032: if (endIndex != -1) {
033: while (endIndex != -1) {
034: line = msgBody.substring(startIndex, endIndex);
035: lines1.addElement(line);
036: startIndex = endIndex + 1;
037: endIndex = msgBody.indexOf(fLineSeparator, startIndex);
038: }
039: if (startIndex < msgBody.length()) {
040: lines1.addElement(msgBody.substring(startIndex, msgBody
041: .length() - 1));
042: }
043: } else {
044: lines1.addElement(msgBody);
045: }
046:
047: // next split the lines by " "
048: for (int i = 0; i < lines1.size(); i++) {
049: lines2 = breakLine((String) lines1.elementAt(i),
050: lineCapacity, " ");
051: for (int j = 0; j < lines2.size(); j++) {
052: returnLines.addElement((String) lines2.elementAt(j));
053: }
054: }
055:
056: for (int k = 0; k < returnLines.size(); k++) {
057: replyString.append(arrow).append(
058: (String) returnLines.elementAt(k)).append(
059: fLineSeparator);
060: }
061:
062: return replyString.toString();
063:
064: }
065:
066: public static Vector breakLine(String inStr, int lineWidth,
067: String delimiter) {
068: int startIndex = 0;
069: int endIndex;
070: String leftPart;
071: String rightPart;
072: Vector lines = new Vector();
073: Vector rightStrings = new Vector();
074: Vector leftStrings = new Vector();
075:
076: //check if inStr is > than lineWidth
077: if (inStr.length() > lineWidth) {
078:
079: endIndex = inStr.lastIndexOf(delimiter);
080: if (endIndex != -1) { //there is at least one delimiter in the line
081: leftPart = inStr.substring(startIndex, endIndex);
082: if (inStr.length() - 1 > endIndex) {
083: rightPart = inStr.substring(endIndex + 1, inStr
084: .length() - 1);
085: } else {
086: rightPart = "";
087: }
088: //break the left part further if necessary
089: if (leftPart.length() < lineWidth) {
090: leftStrings.addElement(leftPart);
091: } else {
092: leftStrings = breakLine(leftPart, lineWidth,
093: delimiter);
094: }
095: //break the right part further if necessary
096: if (rightPart.length() < lineWidth) {
097: rightStrings.addElement(rightPart);
098: } else {
099: rightStrings = breakLine(rightPart, lineWidth);
100: }
101: } else { //there is no delimiter in the line
102: if (inStr.length() - 1 > startIndex) {
103: rightPart = inStr.substring(startIndex, inStr
104: .length() - 1);
105: } else {
106: rightPart = "";
107: }
108:
109: if (rightPart.length() < lineWidth) {
110: rightStrings.addElement(rightPart);
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: } else {
125: lines.addElement(inStr);
126: }
127: return lines;
128: }
129:
130: /**
131: * This procedure will break a string into equal string of length lineWidth
132: */
133: public static Vector breakLine(String inStr, int lineWidth) {
134: Vector lines = new Vector();
135: int startIndex = 0;
136: int endIndex = lineWidth;
137:
138: while (endIndex < inStr.length()) {
139: lines.addElement(inStr.substring(startIndex, endIndex));
140: startIndex = endIndex + 1;
141: endIndex += lineWidth;
142: }
143:
144: lines.addElement(inStr
145: .substring(startIndex, inStr.length() - 1));
146:
147: return (lines);
148: }
149:
150: /**
151: * Turns a null into " " so that DOM won't break
152: */
153: public String formatBodyText(String str) {
154: if ((str == null) || (str.length() == 0)) {
155: return " ";
156: } else {
157: return str;
158: }
159: }
160:
161: public String formatSubject(String str) {
162: if ((str == null) || (str.length() == 0)) {
163: return "[none]";
164: } else {
165: return str;
166: }
167: }
168:
169: }
|