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: WordWrapFormat.java,v $
31 * Revision 1.3 2005/04/11 14:45:40 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.portal.jsp into separate sub-categories:
68 * format, JavaScript, theme and tree.
69 * -----------------------------------------------------------------------------
70 */
71 package com.ivata.mask.web.format;
72 /***
73 * <p>
74 * This format word-wraps each line of text to a user-specified column.
75 * </p>
76 *
77 * <p>
78 * <b>Note: </b> by default, no wrapping will take place. You must call {@link
79 * #setWordWrapColumn setWordWrapColumn}.
80 * </p>
81 *
82 * @since ivata masks 0.4 (2002-06-19)
83 * @author Colin MacLeod
84 * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
85 * @version $Revision: 1.3 $
86 */
87 public class WordWrapFormat implements HTMLFormat {
88 /***
89 * <p>
90 * Private member used to store the value set in setWordWrapColumn.
91 * </p>
92 *
93 * @see #setWordWrapColumn(int wordWrapColumn)
94 */
95 private int wordWrapColumn = 0;
96 /***
97 * <p>
98 * Format the string given in <code>hTMLText</code>, wrapped to the
99 * column provided by calling <code>setWordWrapColumn</code>.
100 * </p>
101 *
102 * <p>
103 * <b>Note: </b> by default, no wrapping will take place.
104 * </p>
105 *
106 * @param hTMLTextParam
107 * the text to truncate.
108 * @return text wrapped at the column specified.
109 */
110 public final String format(final String hTMLTextParam) {
111 String hTMLText = hTMLTextParam;
112 // prerequisites: should we word wrap at all?
113 if (wordWrapColumn <= 0) {
114 return hTMLText;
115 }
116 String outString = "";
117 int actualWordWrapColumn = this.wordWrapColumn;
118 int wordWrapColumnCut = 0;
119 while (!hTMLText.equals("")) {
120 // if the remaining text is smaller than the column at which we
121 // should wrap, then output the whole text
122 if (hTMLText.length() <= actualWordWrapColumn) {
123 outString += hTMLText;
124 hTMLText = "";
125 } else {
126 // see if there is a line-break anywhere
127 for (wordWrapColumnCut = 0;
128 (wordWrapColumnCut < actualWordWrapColumn)
129 && (hTMLText.charAt(wordWrapColumnCut) != '\n');) {
130 ++wordWrapColumnCut;
131 }
132 if (wordWrapColumnCut == actualWordWrapColumn) {
133 // now go back from the column and find the first space
134 for (wordWrapColumnCut = actualWordWrapColumn;
135 (wordWrapColumnCut >= 0)
136 && !(hTMLText.charAt(wordWrapColumnCut) == ' ');) {
137 --wordWrapColumnCut;
138 }
139 // if this is the first time, reduce the column for all
140 // subsequent rows by the length of the newline string
141 if (outString.equals("")) {
142 --actualWordWrapColumn;
143 }
144 // did we find a space?
145 if (wordWrapColumnCut > 0) {
146 outString += hTMLText.substring(0, wordWrapColumnCut)
147 + "\n";
148 hTMLText = hTMLText.substring(wordWrapColumnCut + 1);
149 // in this case, there is no space in the whole lot; we
150 // break at the column as there are no words
151 } else {
152 outString += hTMLText
153 .substring(0, actualWordWrapColumn)
154 + "\n";
155 hTMLText = hTMLText.substring(actualWordWrapColumn + 1);
156 }
157 } else {
158 outString += hTMLText.substring(0, wordWrapColumnCut)
159 + "\n";
160 hTMLText = hTMLText.substring(wordWrapColumnCut + 1);
161 }
162 }
163 }
164 return outString;
165 }
166 /***
167 * <p>
168 * Private member used to store the value set in setWordWrapColumn.
169 * </p>
170 *
171 * @see #setWordWrapColumn(int wordWrapColumn)
172 *
173 *
174 * @return the current value of wordWrapColumn.
175 */
176 public final int getWordWrapColumn() {
177 return wordWrapColumn;
178 }
179 /***
180 * <p>
181 * Private member used to store the value set in setWordWrapColumn.
182 * </p>
183 *
184 * @see #setWordWrapColumn(int wordWrapColumn)
185 * @param wordWrapColumnParam
186 * the new value of wordWrapColumn.
187 */
188 public final void setWordWrapColumn(final int wordWrapColumnParam) {
189 this.wordWrapColumn = wordWrapColumnParam;
190 }
191 }
192