1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 package com.ivata.mask.web.format;
72 import java.util.StringTokenizer;
73 /***
74 * <p>
75 * Convert line breaks into HTML break tags.
76 * </p>
77 *
78 * @since ivata masks 0.4 (2002-06-19)
79 * @author Colin MacLeod
80 * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
81 * @version $Revision: 1.3 $
82 */
83 public class LineBreakFormat implements HTMLFormat {
84 /***
85 * <p>
86 * Stores the string which is prepended to each new line.
87 * </p>
88 */
89 private String prepend = "";
90 /***
91 * <p>
92 * If set to <code>true</code>, then all line break characters in the
93 * <code>htmText</code> (see {@link #format format}) are converted into
94 * HTML line-breaks (<br/>).
95 * </p>
96 */
97 private boolean convertLineBreaks = false;
98 /***
99 * <p>
100 * Convert all line breaks in the text provided to <br/> tags, and
101 * prepend a string to new each line. One example where this is required is
102 * in'quoted' return emails, where each line is traditionally preceded by
103 * the 'greater than' symbol >
104 * </p>
105 *
106 * @param hTMLTextParam
107 * HTML text to convert line breaks in.
108 * @return formatted text, with all of the line breaks converted to HTML
109 * tags
110 */
111 public final String format(final String hTMLTextParam) {
112 String hTMLText = hTMLTextParam;
113 int index = 0;
114
115 if (convertLineBreaks) {
116 while ((index = hTMLText.indexOf('\n')) != -1) {
117 if (index > 1) {
118 hTMLText = hTMLText.substring(0, index) + "<br/>" + prepend
119 + hTMLText.substring(index + 1);
120 } else {
121 hTMLText = "<br/>" + prepend
122 + hTMLText.substring(index + 1);
123 }
124 }
125
126 } else if (!prepend.equals("")) {
127 String sNew = "";
128 StringTokenizer st = new StringTokenizer(hTMLText, "\n");
129 while (st.hasMoreTokens()) {
130
131 if (!sNew.equals("")) {
132 sNew += "\n";
133 }
134 sNew += prepend + st.nextToken();
135 }
136 hTMLText = sNew;
137 }
138 return hTMLText;
139 }
140 /***
141 * <p>
142 * Get the string which is prepended to each new line.
143 * </p>
144 *
145 * @return the current value of the string to prepend to each line.
146 */
147 public final String getPrepend() {
148 return prepend;
149 }
150 /***
151 * <p>
152 * Set the string which is prepended to each new line.
153 * </p>
154 *
155 * @param prependParam
156 * the new value of the string to prepend to each line.
157 */
158 public final void setPrepend(final String prependParam) {
159 this.prepend = prependParam;
160 }
161 /***
162 * <p>
163 * Get whether or not we should convert line breaks. If set to
164 * <code>true</code>, then all line break characters in the
165 * <code>htmText</code> (see {@link #format format}) are converted into
166 * HTML line-breaks (<br/>).
167 * </p>
168 *
169 * @return <code>true</code> if line breaks are converted, otherwise
170 * <code>false</code>.
171 */
172 public final boolean getConvertLineBreaks() {
173 return convertLineBreaks;
174 }
175 /***
176 * <p>
177 * Set whether or not we should convert line breaks. If set to
178 * <code>true</code>, then all line break characters in the
179 * <code>hTMLText</code> (see {@link #format format}) are converted into
180 * HTML line-breaks (<br/>).
181 * </p>
182 *
183 * @param convertLineBreaksParam
184 * set to <code>true</code> if line breaks should be converted,
185 * otherwise <code>false</code>.
186 */
187 public final void setConvertLineBreaks(
188 final boolean convertLineBreaksParam) {
189 this.convertLineBreaks = convertLineBreaksParam;
190 }
191 }
192