1 package org.apache.fulcrum.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.NoSuchElementException;
23 import java.util.StringTokenizer;
24
25 import org.apache.commons.lang.StringUtils;
26
27
28
29
30
31
32
33 public class WordWrapUtils {
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 public static String wrapText(String str, String newline, int wrapColumn) {
54 StringTokenizer lineTokenizer = new StringTokenizer(str, newline, true);
55 StringBuffer stringBuffer = new StringBuffer();
56
57 while (lineTokenizer.hasMoreTokens()) {
58 try {
59 String nextLine = lineTokenizer.nextToken();
60
61 if (nextLine.length() > wrapColumn) {
62
63 nextLine = wrapLine(nextLine, newline, wrapColumn);
64 }
65
66 stringBuffer.append(nextLine);
67
68 } catch (NoSuchElementException nsee) {
69
70 break;
71 }
72 }
73
74 return (stringBuffer.toString());
75 }
76
77
78
79
80
81
82
83
84
85
86 private static String wrapLine(String line, String newline, int wrapColumn) {
87 StringBuffer wrappedLine = new StringBuffer();
88
89 while (line.length() > wrapColumn) {
90 int spaceToWrapAt = line.lastIndexOf(' ', wrapColumn);
91
92 if (spaceToWrapAt >= 0) {
93 wrappedLine.append(line.substring(0, spaceToWrapAt));
94 wrappedLine.append(newline);
95 line = line.substring(spaceToWrapAt + 1);
96 }
97
98
99
100
101
102
103 else {
104 spaceToWrapAt = line.indexOf(' ', wrapColumn);
105
106 if (spaceToWrapAt >= 0) {
107 wrappedLine.append(line.substring(0, spaceToWrapAt));
108 wrappedLine.append(newline);
109 line = line.substring(spaceToWrapAt + 1);
110 } else {
111 wrappedLine.append(line);
112 line = "";
113 }
114 }
115 }
116
117
118 wrappedLine.append(line);
119
120 return (wrappedLine.toString());
121 }
122
123
124
125
126
127
128
129
130
131 public static String wordWrap(String str) {
132 return wordWrap(str, 80, "\n", "-");
133 }
134
135
136
137
138
139 public static String wordWrap(String str, int width) {
140 return wordWrap(str, width, "\n", "-");
141 }
142
143
144
145
146
147
148
149
150
151
152 public static String wordWrap(String str, int width, String delim, String split) {
153 int sz = str.length();
154
155
156 width++;
157
158
159 StringBuffer buffer = new StringBuffer(sz / width * delim.length() + sz);
160
161
162 width = width - delim.length();
163
164 int idx = -1;
165 String substr = null;
166
167
168 for (int i = 0; i < sz; i += width) {
169
170
171 if (i > sz - width) {
172 buffer.append(str.substring(i));
173 break;
174 }
175
176
177 substr = str.substring(i, i + width);
178
179
180 idx = substr.indexOf(delim);
181 if (idx != -1) {
182 buffer.append(substr.substring(0, idx));
183 buffer.append(delim);
184 i -= width - idx - delim.length();
185
186
187 if (substr.charAt(idx + 1) != '\n') {
188 if (Character.isWhitespace(substr.charAt(idx + 1))) {
189 i++;
190 }
191 }
192 continue;
193 }
194
195 idx = -1;
196
197
198 char[] chrs = substr.toCharArray();
199 for (int j = width; j > 0; j--) {
200 if (Character.isWhitespace(chrs[j - 1])) {
201 idx = j;
202 break;
203 }
204 }
205
206
207 if (idx == -1) {
208 for (int j = width; j > 0; j--) {
209 if (chrs[j - 1] == '-') {
210 idx = j;
211 break;
212 }
213 }
214 if (idx == -1) {
215 buffer.append(substr);
216 buffer.append(delim);
217 } else {
218 if (idx != width) {
219 idx++;
220 }
221 buffer.append(substr.substring(0, idx));
222 buffer.append(delim);
223 i -= width - idx;
224 }
225 } else {
226
227 buffer.append(substr.substring(0, idx));
228 buffer.append(StringUtils.repeat(" ", width - idx));
229 buffer.append(delim);
230 i -= width - idx;
231 }
232 }
233 return buffer.toString();
234 }
235
236 }