View Javadoc

1   /*
2    * Copyright (c) 2001 - 2005 ivata limited.
3    * All rights reserved.
4    * -----------------------------------------------------------------------------
5    * ivata masks may be redistributed under the GNU General Public
6    * License as published by the Free Software Foundation;
7    * version 2 of the License.
8    *
9    * These programs are free software; you can redistribute them and/or
10   * modify them under the terms of the GNU General Public License
11   * as published by the Free Software Foundation; version 2 of the License.
12   *
13   * These programs are distributed in the hope that they will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16   *
17   * See the GNU General Public License in the file LICENSE.txt for more
18   * details.
19   *
20   * If you would like a copy of the GNU General Public License write to
21   *
22   * Free Software Foundation, Inc.
23   * 59 Temple Place - Suite 330
24   * Boston, MA 02111-1307, USA.
25   *
26   *
27   * To arrange commercial support and licensing, contact ivata at
28   *                  http://www.ivata.com/contact.jsp
29   * -----------------------------------------------------------------------------
30   * $Log: HTMLFormatter.java,v $
31   * Revision 1.3  2005/04/09 18:04:18  colinmacleod
32   * Changed copyright text to GPL v2 explicitly.
33   *
34   * Revision 1.2  2005/01/06 22:41:01  colinmacleod
35   * Moved up a version number.
36   * Changed copyright notices to 2005.
37   * Updated the documentation:
38   *   - started working on multiproject:site docu.
39   *   - changed the logo.
40   * Added checkstyle and fixed LOADS of style issues.
41   * Added separate thirdparty subproject.
42   * Added struts (in web), util and webgui (in webtheme) from ivata op.
43   *
44   * Revision 1.3  2004/03/21 21:16:37  colinmacleod
45   * Shortened name to ivata op.
46   *
47   * Revision 1.2  2004/02/01 22:07:32  colinmacleod
48   * Added full names to author tags
49   *
50   * Revision 1.1.1.1  2004/01/27 20:59:47  colinmacleod
51   * Moved ivata op to SourceForge.
52   *
53   * Revision 1.2  2003/10/15 14:13:39  colin
54   * Fixes for XDoclet.
55   *
56   * Revision 1.1  2003/02/24 19:33:33  colin
57   * Moved to new subproject.
58   *
59   * Revision 1.4  2003/02/04 17:43:46  colin
60   * copyright notice
61   *
62   * Revision 1.3  2002/09/06 15:06:31  colin
63   * added null checking on input format text
64   *
65   * Revision 1.2  2002/06/21 16:00:30  colin
66   * added protected accessors (get/set) to the Vector of formats.
67   *
68   * Revision 1.1  2002/04/26 13:52:44  colin
69   * move to EJB/JBuilder project
70   *
71   * Revision 1.12  2002/02/02 19:10:52  colin
72   * changed formatEmail methods to just return null when you pass null
73   * to them
74   *
75   * Revision 1.11  2002/01/25 18:04:06  colin
76   * Formatted for JavaDoc.
77   *
78   * Revision 1.10  2002/01/20 19:40:24  colin
79   * added tab and tree tags
80   * implemented address book functionality
81   *
82   * Revision 1.9  2002/01/20 19:28:25  colin
83   * added tab and tree tags
84   * implemented address book functionality
85   *
86   * Revision 1.8  2001/12/20 17:11:44  colin
87   * tidied up the HTMLFormatter code, and improved InternetAddress
88   * handling
89   * truncated email addresses which are too long in index.jsp
90   * changed non-alphanumeric symbols (such as apostrophe ') to their
91   * HTML equivalents in compose and display
92   * made error messages in the compose window limited in width
93   * (word-wrap)
94   * converted URLs into links
95   *
96   * Revision 1.7  2001/12/12 20:24:57  colin
97   * fixed the following bugs:
98   * made display window same width as index
99   * made leading spaces non-breaking
100  * put a little header with from/to/subject information before
101  * forwarded and replied to mails
102  * stopped append re: to messages which are already re: (same applies
103  * to forward)
104  * convert people's email addresses into links (mailto)
105  * word wrap the messages (replies) in compose
106  * make the subject line spread right over the whole space in display
107  *
108  * Revision 1.6  2001/11/24 19:19:53  colin
109  * display, compose, reply and forward features of webmail implemented
110  *
111  * Revision 1.5  2001/11/08 01:38:56  colin
112  * implemented new Solero theme in shop
113  *
114  * Revision 1.4  2001/10/22 19:50:51  colin
115  * commit everything before move from monifieth1 to lucenec1
116  *
117  * Revision 1.3  2001/10/16 17:52:02  colin
118  * updated mail classes to set formatterName="nothing" instead of null
119  * several rebuilds to avoid problem of classes not recompiling
120  *
121  * Revision 1.2  2001/10/14 18:01:02  colin
122  * added '..' to shortened lines
123  *
124  * Revision 1.1  2001/10/07 00:30:16  colin
125  * no message
126  *
127  * Revision 1.1.1.1  2001/10/06 15:40:20  colin
128  * initial import into CVS
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         // rubbish in, rubbish out...
259         if (formatString == null) {
260             return null;
261         }
262         String returnString = formatString;
263         // go through all of the format objects provided and call each one
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         // see if there is a convert character entities after the link format,
282         // and warn it if there is
283         if (linkFormat == null) {
284             // see if this is a link format
285             if (LinkFormat.class.isInstance(format)) {
286                 linkFormat = (LinkFormat) format;
287             }
288         } else if (CharacterEntityFormat.class.isInstance(format)) {
289             // warn the link format
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             // see if this instance matches the class we're looking for
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