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: LineBreakFormat.java,v $
31   * Revision 1.3  2005/04/11 14:45:38  colinmacleod
32   * Changed HTMLFormat from an abstract class
33   * into an interface.
34   *
35   * Revision 1.2  2005/04/09 18:04:18  colinmacleod
36   * Changed copyright text to GPL v2 explicitly.
37   *
38   * Revision 1.1  2005/01/06 22:41:01  colinmacleod
39   * Moved up a version number.
40   * Changed copyright notices to 2005.
41   * Updated the documentation:
42   *   - started working on multiproject:site docu.
43   *   - changed the logo.
44   * Added checkstyle and fixed LOADS of style issues.
45   * Added separate thirdparty subproject.
46   * Added struts (in web), util and webgui (in webtheme) from ivata op.
47   *
48   * Revision 1.3  2004/03/21 21:16:37  colinmacleod
49   * Shortened name to ivata op.
50   *
51   * Revision 1.2  2004/02/01 22:07:32  colinmacleod
52   * Added full names to author tags
53   *
54   * Revision 1.1.1.1  2004/01/27 20:59:48  colinmacleod
55   * Moved ivata op to SourceForge.
56   *
57   * Revision 1.2  2003/10/15 14:13:39  colin
58   * Fixes for XDoclet.
59   *
60   * Revision 1.1  2003/02/24 19:33:33  colin
61   * Moved to new subproject.
62   *
63   * Revision 1.2  2003/02/04 17:43:46  colin
64   * copyright notice
65   *
66   * Revision 1.1  2002/06/21 11:58:37  colin
67   * restructured com.ivata.mask.jsp into separate sub-categories:
68   * format, JavaScript, theme and tree.
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 (&lt;br/&gt;).
95       * </p>
96       */
97      private boolean convertLineBreaks = false;
98      /***
99       * <p>
100      * Convert all line breaks in the text provided to &lt;br/&gt; 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 &gt;
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         // should we convert all line breaks to <br/>
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             // otherwise, if a prepend string was specified, use that
126         } else if (!prepend.equals("")) {
127             String sNew = "";
128             StringTokenizer st = new StringTokenizer(hTMLText, "\n");
129             while (st.hasMoreTokens()) {
130                 // if this isn't the first one, prepend as you must...
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 (&lt;br/&gt;).
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 (&lt;br/&gt;).
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