001: /*
002: * Copyright (c) 2001 - 2005 ivata limited.
003: * All rights reserved.
004: * -----------------------------------------------------------------------------
005: * ivata masks may be redistributed under the GNU General Public
006: * License as published by the Free Software Foundation;
007: * version 2 of the License.
008: *
009: * These programs are free software; you can redistribute them and/or
010: * modify them under the terms of the GNU General Public License
011: * as published by the Free Software Foundation; version 2 of the License.
012: *
013: * These programs are distributed in the hope that they will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: *
017: * See the GNU General Public License in the file LICENSE.txt for more
018: * details.
019: *
020: * If you would like a copy of the GNU General Public License write to
021: *
022: * Free Software Foundation, Inc.
023: * 59 Temple Place - Suite 330
024: * Boston, MA 02111-1307, USA.
025: *
026: *
027: * To arrange commercial support and licensing, contact ivata at
028: * http://www.ivata.com/contact.jsp
029: * -----------------------------------------------------------------------------
030: * $Log: WordWrapFormat.java,v $
031: * Revision 1.4 2005/10/02 14:06:33 colinmacleod
032: * Added/improved log4j logging.
033: *
034: * Revision 1.3 2005/04/11 14:45:40 colinmacleod
035: * Changed HTMLFormat from an abstract class
036: * into an interface.
037: *
038: * Revision 1.2 2005/04/09 18:04:18 colinmacleod
039: * Changed copyright text to GPL v2 explicitly.
040: *
041: * Revision 1.1 2005/01/06 22:41:01 colinmacleod
042: * Moved up a version number.
043: * Changed copyright notices to 2005.
044: * Updated the documentation:
045: * - started working on multiproject:site docu.
046: * - changed the logo.
047: * Added checkstyle and fixed LOADS of style issues.
048: * Added separate thirdparty subproject.
049: * Added struts (in web), util and webgui (in webtheme) from ivata op.
050: *
051: * Revision 1.3 2004/03/21 21:16:37 colinmacleod
052: * Shortened name to ivata op.
053: *
054: * Revision 1.2 2004/02/01 22:07:32 colinmacleod
055: * Added full names to author tags
056: *
057: * Revision 1.1.1.1 2004/01/27 20:59:48 colinmacleod
058: * Moved ivata op to SourceForge.
059: *
060: * Revision 1.2 2003/10/15 14:13:39 colin
061: * Fixes for XDoclet.
062: *
063: * Revision 1.1 2003/02/24 19:33:33 colin
064: * Moved to new subproject.
065: *
066: * Revision 1.2 2003/02/04 17:43:46 colin
067: * copyright notice
068: *
069: * Revision 1.1 2002/06/21 11:58:37 colin
070: * restructured com.ivata.portal.jsp into separate sub-categories:
071: * format, JavaScript, theme and tree.
072: * -----------------------------------------------------------------------------
073: */
074: package com.ivata.mask.web.format;
075:
076: import org.apache.log4j.Logger;
077:
078: /**
079: * <p>
080: * This format word-wraps each line of text to a user-specified column.
081: * </p>
082: *
083: * <p>
084: * <b>Note: </b> by default, no wrapping will take place. You must call {@link
085: * #setWordWrapColumn setWordWrapColumn}.
086: * </p>
087: *
088: * @since ivata masks 0.4 (2002-06-19)
089: * @author Colin MacLeod
090: * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
091: * @version $Revision: 1.4 $
092: */
093: public class WordWrapFormat implements HTMLFormat {
094: /**
095: * Logger for this class.
096: */
097: private static final Logger logger = Logger
098: .getLogger(WordWrapFormat.class);
099:
100: /**
101: * <p>
102: * Private member used to store the value set in setWordWrapColumn.
103: * </p>
104: *
105: * @see #setWordWrapColumn(int wordWrapColumn)
106: */
107: private int wordWrapColumn = 0;
108:
109: /**
110: * <p>
111: * Format the string given in <code>hTMLText</code>, wrapped to the
112: * column provided by calling <code>setWordWrapColumn</code>.
113: * </p>
114: *
115: * <p>
116: * <b>Note: </b> by default, no wrapping will take place.
117: * </p>
118: *
119: * @param hTMLTextParam
120: * the text to truncate.
121: * @return text wrapped at the column specified.
122: */
123: public final String format(final String hTMLTextParam) {
124: if (logger.isDebugEnabled()) {
125: logger.debug("format(String hTMLTextParam = "
126: + hTMLTextParam + ") - start");
127: }
128:
129: String hTMLText = hTMLTextParam;
130: // prerequisites: should we word wrap at all?
131: if (wordWrapColumn <= 0) {
132: if (logger.isDebugEnabled()) {
133: logger.debug("format(String) - end - return value = "
134: + hTMLText);
135: }
136: return hTMLText;
137: }
138: String outString = "";
139: int actualWordWrapColumn = this .wordWrapColumn;
140: int wordWrapColumnCut = 0;
141: while (!hTMLText.equals("")) {
142: // if the remaining text is smaller than the column at which we
143: // should wrap, then output the whole text
144: if (hTMLText.length() <= actualWordWrapColumn) {
145: outString += hTMLText;
146: hTMLText = "";
147: } else {
148: // see if there is a line-break anywhere
149: for (wordWrapColumnCut = 0; (wordWrapColumnCut < actualWordWrapColumn)
150: && (hTMLText.charAt(wordWrapColumnCut) != '\n');) {
151: ++wordWrapColumnCut;
152: }
153: if (wordWrapColumnCut == actualWordWrapColumn) {
154: // now go back from the column and find the first space
155: for (wordWrapColumnCut = actualWordWrapColumn; (wordWrapColumnCut >= 0)
156: && !(hTMLText.charAt(wordWrapColumnCut) == ' ');) {
157: --wordWrapColumnCut;
158: }
159: // if this is the first time, reduce the column for all
160: // subsequent rows by the length of the newline string
161: if (outString.equals("")) {
162: --actualWordWrapColumn;
163: }
164: // did we find a space?
165: if (wordWrapColumnCut > 0) {
166: outString += hTMLText.substring(0,
167: wordWrapColumnCut)
168: + "\n";
169: hTMLText = hTMLText
170: .substring(wordWrapColumnCut + 1);
171: // in this case, there is no space in the whole lot; we
172: // break at the column as there are no words
173: } else {
174: outString += hTMLText.substring(0,
175: actualWordWrapColumn)
176: + "\n";
177: hTMLText = hTMLText
178: .substring(actualWordWrapColumn + 1);
179: }
180: } else {
181: outString += hTMLText.substring(0,
182: wordWrapColumnCut)
183: + "\n";
184: hTMLText = hTMLText
185: .substring(wordWrapColumnCut + 1);
186: }
187: }
188: }
189:
190: if (logger.isDebugEnabled()) {
191: logger.debug("format(String) - end - return value = "
192: + outString);
193: }
194: return outString;
195: }
196:
197: /**
198: * <p>
199: * Private member used to store the value set in setWordWrapColumn.
200: * </p>
201: *
202: * @see #setWordWrapColumn(int wordWrapColumn)
203: *
204: *
205: * @return the current value of wordWrapColumn.
206: */
207: public final int getWordWrapColumn() {
208: if (logger.isDebugEnabled()) {
209: logger.debug("getWordWrapColumn() - start");
210: }
211:
212: if (logger.isDebugEnabled()) {
213: logger.debug("getWordWrapColumn() - end - return value = "
214: + wordWrapColumn);
215: }
216: return wordWrapColumn;
217: }
218:
219: /**
220: * <p>
221: * Private member used to store the value set in setWordWrapColumn.
222: * </p>
223: *
224: * @see #setWordWrapColumn(int wordWrapColumn)
225: * @param wordWrapColumnParam
226: * the new value of wordWrapColumn.
227: */
228: public final void setWordWrapColumn(final int wordWrapColumnParam) {
229: if (logger.isDebugEnabled()) {
230: logger.debug("setWordWrapColumn(int wordWrapColumnParam = "
231: + wordWrapColumnParam + ") - start");
232: }
233:
234: this .wordWrapColumn = wordWrapColumnParam;
235:
236: if (logger.isDebugEnabled()) {
237: logger.debug("setWordWrapColumn(int) - end");
238: }
239: }
240: }
|