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: MaximumLengthFormat.java,v $
031: * Revision 1.5 2005/10/03 10:17:25 colinmacleod
032: * Fixed some style and javadoc issues.
033: *
034: * Revision 1.4 2005/10/02 14:06:33 colinmacleod
035: * Added/improved log4j logging.
036: *
037: * Revision 1.3 2005/04/11 14:45:39 colinmacleod
038: * Changed HTMLFormat from an abstract class
039: * into an interface.
040: *
041: * Revision 1.2 2005/04/09 18:04:18 colinmacleod
042: * Changed copyright text to GPL v2 explicitly.
043: *
044: * Revision 1.1 2005/01/06 22:41:01 colinmacleod
045: * Moved up a version number.
046: * Changed copyright notices to 2005.
047: * Updated the documentation:
048: * - started working on multiproject:site docu.
049: * - changed the logo.
050: * Added checkstyle and fixed LOADS of style issues.
051: * Added separate thirdparty subproject.
052: * Added struts (in web), util and webgui (in webtheme) from ivata op.
053: *
054: * Revision 1.4 2004/11/03 17:10:02 colinmacleod
055: * Changed appended string to hellip entity.
056: *
057: * Revision 1.3 2004/03/21 21:16:37 colinmacleod
058: * Shortened name to ivata op.
059: *
060: * Revision 1.2 2004/02/01 22:07:32 colinmacleod
061: * Added full names to author tags
062: *
063: * Revision 1.1.1.1 2004/01/27 20:59:48 colinmacleod
064: * Moved ivata op to SourceForge.
065: *
066: * Revision 1.2 2003/10/15 14:13:39 colin
067: * Fixes for XDoclet.
068: *
069: * Revision 1.1 2003/02/24 19:33:33 colin
070: * Moved to new subproject.
071: *
072: * Revision 1.3 2003/02/04 17:43:46 colin
073: * copyright notice
074: *
075: * Revision 1.2 2003/02/04 16:38:59 peter
076: * the dots character found
077: *
078: * Revision 1.1 2002/06/21 11:58:37 colin
079: * restructured com.ivata.mask.jsp into separate sub-categories:
080: * format, JavaScript, theme and tree.
081: * -----------------------------------------------------------------------------
082: */
083: package com.ivata.mask.web.format;
084:
085: import org.apache.log4j.Logger;
086:
087: /**
088: * <p>
089: * If the text supplied is longer than the maximum line length, the text is
090: * truncated and the dots character is appended to it.
091: * </p>
092: *
093: * @since ivata masks 0.4 (2002-06-19)
094: * @author Colin MacLeod
095: * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
096: * @version $Revision: 1.5 $
097: */
098: public class MaximumLengthFormat implements HTMLFormat {
099: /**
100: * Logger for this class.
101: */
102: private static final Logger logger = Logger
103: .getLogger(MaximumLengthFormat.class);
104:
105: /**
106: * <p>
107: * This string is appended to truncated strings.
108: * </p>
109: */
110: private static final String DOTS_STRING = "…";
111: /**
112: * <p>
113: * Stores the effective length of the dots string.
114: * </p>
115: */
116: private static int dotsLength;
117: /**
118: * <p>
119: * Work out the effective length of the string; treat character entities as
120: * a single character.
121: * </p>
122: */
123: static {
124: CharacterEntityFormat entityFormat = new CharacterEntityFormat();
125: entityFormat.setReverse(true);
126: String effectiveString = entityFormat.format(DOTS_STRING);
127: dotsLength = effectiveString.length();
128: }
129: /**
130: * <p>
131: * Stores the maximum string length to output when format is called.
132: * </p>
133: */
134: private Integer maximumLength = null;
135:
136: /**
137: * <p>
138: * Format the string given in <code>hTMLText</code> to the maximum length
139: * provided by calling <code>setMaxLength</code>.
140: * </p>
141: *
142: * @param hTMLTextParam
143: * the text to truncate
144: * @return <copyDoc>Refer to {@link HTMLFormat#format}.</copyDoc>
145: */
146: public final String format(final String hTMLTextParam) {
147: if (logger.isDebugEnabled()) {
148: logger.debug("format(String hTMLTextParam = "
149: + hTMLTextParam + ") - start");
150: }
151:
152: String hTMLText = hTMLTextParam;
153: if (this .maximumLength != null) {
154: int maximumLengthInt = this .maximumLength.intValue();
155: if ((hTMLText.length() > maximumLengthInt)) {
156: // if the max length is greater than 2, use the dots
157: if (maximumLengthInt > (1 + dotsLength)) {
158: hTMLText = hTMLText.substring(0, maximumLengthInt
159: - dotsLength)
160: + DOTS_STRING;
161: } else {
162: // otherwise, just shrink the string
163: hTMLText = hTMLText.substring(0, maximumLengthInt);
164: }
165: }
166: }
167:
168: if (logger.isDebugEnabled()) {
169: logger.debug("format(String) - end - return value = "
170: + hTMLText);
171: }
172: return hTMLText;
173: }
174:
175: /**
176: * <p>
177: * Get the maximum string length to output when format is called.
178: * </p>
179: *
180: * @return the current maximum length of the string which is output, or
181: * <code>null</code> if no maximum has been set yet.
182: */
183: public final Integer getMaximumLength() {
184: if (logger.isDebugEnabled()) {
185: logger.debug("getMaximumLength() - start");
186: }
187:
188: if (logger.isDebugEnabled()) {
189: logger.debug("getMaximumLength() - end - return value = "
190: + maximumLength);
191: }
192: return maximumLength;
193: }
194:
195: /**
196: * <p>
197: * Set the maximum string length to output when <code>format</code> is
198: * called.
199: * </p>
200: *
201: * @param maximumLengthParam
202: * the new value of the maximum length to output.
203: */
204: public final void setMaximumLength(final Integer maximumLengthParam) {
205: if (logger.isDebugEnabled()) {
206: logger
207: .debug("setMaximumLength(Integer maximumLengthParam = "
208: + maximumLengthParam + ") - start");
209: }
210:
211: this .maximumLength = maximumLengthParam;
212:
213: if (logger.isDebugEnabled()) {
214: logger.debug("setMaximumLength(Integer) - end");
215: }
216: }
217: }
|