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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131 package com.ivata.mask.web.format;
132 import java.util.Iterator;
133 import java.util.List;
134 import java.util.Vector;
135 /***
136 * <p>
137 * This class defines a standard way of formatting HTML. It can be used as as a
138 * parameter to methods from other classes indicating that output should be
139 * truncated, spaces should be converted to non-breaking, or the string should
140 * be parsed for URLs.
141 * </p>
142 * <p>
143 * The actual conversions are achieved by add-in classes, with {@linkHTMLFormat
144 * HTMLFormat} as their superclass. This means the formatter can provided a rich
145 * selection of different formats without having knowledge of the processing of
146 * each one.
147 * <p>
148 * <p>
149 * To use this class, created an instance of the formats you wish to use, and an
150 * instance of this class too. Call any methods on the formats to set up the
151 * exact function of each one. Then call {@link #add add}for each format on the
152 * <code>HTMLFormatter</code> instance. Passing a text to
153 * {@link #format format}will ensure that each format is applied in the order
154 * in which they were added to the formatter.
155 * </p>
156 *
157 * @since ivata masks 0.2 (2001-10-03)
158 * @author Colin MacLeod
159 * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
160 * @version $Revision: 1.3 $
161 */
162 public class HTMLFormatter extends Object {
163 /***
164 * <p>
165 * Stores all of the formats which will be applied.
166 * </p>
167 */
168 private List formats = new Vector();
169 /***
170 * Used to note the position of any link format, so it does not interfere
171 * with character entity format.
172 */
173 private LinkFormat linkFormat = null;
174 /***
175 * Creates a new instance of HTMLFormatter with default settings.
176 */
177 public HTMLFormatter() {
178 super();
179 }
180 /***
181 * <p>
182 * If the object is not null, fill the attribute with this value otherwise
183 * return nothing.
184 * </p>
185 *
186 * <p>
187 * This is useful for HTML tags where you want to created an attribute value
188 * only if the object <code>objectParam</code> is non-null.
189 * </p>
190 *
191 * @param attribute
192 * the attribute name of the attribute to be set if
193 * <code>objectParam</code> is non-null
194 * @param objectParam
195 * an Object to assign attribute to if it is non-null
196 * @return if <code>objectParam</code> is not null, a string assigning the
197 * attribute sAttrubte to the value of
198 * <code>objectParam.toString()</code>, otherwise an empty
199 * string.
200 * @see #getBooleanAttribute(String attribute, boolean check)
201 * @see #getNotNull(Object o, String nullString)
202 */
203 public static final String getAttributeNotNull(final String attribute,
204 final Object objectParam) {
205 if (objectParam == null) {
206 return "";
207 } else {
208 return " " + attribute + "='" + objectParam.toString() + "'";
209 }
210 }
211 /***
212 * <p>
213 * If the boolean is true, fill the attribute with no value, otherwise
214 * return nothing.
215 * </p>
216 *
217 * <p>
218 * This is useful for HTML tags where you want to create an attribute only
219 * if the condition is <code>true</code>.
220 * </p>
221 *
222 * @param attribute
223 * the attribute name of the attribute to be set if check is
224 * <code>true</code>.
225 * @param check
226 * a boolean to evaluate.
227 * @return if check is <code>true</code>, the attribute string, otherwise
228 * an empty string.
229 * @see #getAttributeNotNull(String attribute, Object o)
230 * @see #getNotNull(Object o)
231 */
232 public static final String getBooleanAttribute(final String attribute,
233 final boolean check) {
234 if (check) {
235 return " " + attribute;
236 } else {
237 return "";
238 }
239 }
240 /***
241 * <p>
242 * This is the most important method of all.
243 * </p>
244 *
245 * <p>
246 * First call the methods setConvert.... to specify how to convert the
247 * string, then use this method to format the given string according to the
248 * set rules
249 * </p>.
250 *
251 * @param formatString
252 * String to format
253 * @return String containing formatted text, according to the current state
254 * of this HTMLFormatter instance.
255 * @see HTMLFormat#format
256 */
257 public String format(final String formatString) {
258
259 if (formatString == null) {
260 return null;
261 }
262 String returnString = formatString;
263
264 for (Iterator i = formats.iterator(); i.hasNext();) {
265 HTMLFormat format = (HTMLFormat) i.next();
266 returnString = format.format(returnString);
267 }
268 return returnString;
269 }
270 /***
271 * <p>
272 * Adds the format to the internal list of <code>HTMLFormat</code>
273 * instances this formatter holds.
274 * </p>
275 *
276 * @param format
277 * the format instance to add. This will be processed after all
278 * existing formats.
279 */
280 public final void add(final HTMLFormat format) {
281
282
283 if (linkFormat == null) {
284
285 if (LinkFormat.class.isInstance(format)) {
286 linkFormat = (LinkFormat) format;
287 }
288 } else if (CharacterEntityFormat.class.isInstance(format)) {
289
290 linkFormat.setConvertHTMLEntities(true);
291 }
292 formats.add(format);
293 }
294 /***
295 * <p>
296 * Locates a format in the object, identified by class.
297 * </p>
298 *
299 * @param findClass
300 * the <code>Class</code> of the format to locate in the
301 * formatter's internal list.
302 * @return first format found with the class specified, or <code>null</code>
303 * if no format with this class has been set in the formatter
304 */
305 public final HTMLFormat findFormat(final Class findClass) {
306 HTMLFormat formatReturn = null;
307 for (Iterator i = formats.iterator(); i.hasNext();) {
308 HTMLFormat format = (HTMLFormat) i.next();
309
310 if (findClass.isInstance(format)) {
311 formatReturn = format;
312 break;
313 }
314 }
315 return formatReturn;
316 }
317 /***
318 * <p>
319 * Get the internal list of all of the formats used by this formatter.
320 * </p>
321 *
322 * @return a <code>Vector</code> containing all of the formats from this
323 * formatter.
324 */
325 public final List getFormats() {
326 return formats;
327 }
328 /***
329 * <p>
330 * Set the internal list of all of the formats used by this formatter.
331 * </p>
332 *
333 * @param formatsParam
334 * a <code>Vector</code> containing all of the formats from
335 * this formatter.
336 */
337 protected final void setFormats(final List formatsParam) {
338 this.formats = formatsParam;
339 }
340 }
341