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: MaximumLengthFormat.java,v $
31   * Revision 1.3  2005/04/11 14:45:39  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.4  2004/11/03 17:10:02  colinmacleod
49   * Changed appended string to hellip entity.
50   *
51   * Revision 1.3  2004/03/21 21:16:37  colinmacleod
52   * Shortened name to ivata op.
53   *
54   * Revision 1.2  2004/02/01 22:07:32  colinmacleod
55   * Added full names to author tags
56   *
57   * Revision 1.1.1.1  2004/01/27 20:59:48  colinmacleod
58   * Moved ivata op to SourceForge.
59   *
60   * Revision 1.2  2003/10/15 14:13:39  colin
61   * Fixes for XDoclet.
62   *
63   * Revision 1.1  2003/02/24 19:33:33  colin
64   * Moved to new subproject.
65   *
66   * Revision 1.3  2003/02/04 17:43:46  colin
67   * copyright notice
68   *
69   * Revision 1.2  2003/02/04 16:38:59  peter
70   * the dots character found
71   *
72   * Revision 1.1  2002/06/21 11:58:37  colin
73   * restructured com.ivata.mask.jsp into separate sub-categories:
74   * format, JavaScript, theme and tree.
75   * -----------------------------------------------------------------------------
76   */
77  package com.ivata.mask.web.format;
78  /***
79   * <p>
80   * If the text supplied is longer than the maximum line length, the text is
81   * truncated and the dots character is appended to it.
82   * </p>
83   *
84   * @since ivata masks 0.4 (2002-06-19)
85   * @author Colin MacLeod
86   * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
87   * @version $Revision: 1.3 $
88   */
89  public class MaximumLengthFormat implements HTMLFormat {
90      /***
91       * <p>
92       * This string is appended to truncated strings.
93       * </p>
94       */
95      private static final String DOTS_STRING = "&hellip;";
96      /***
97       * <p>
98       * Stores the effective length of the dots string.
99       * </p>
100      */
101     private static int dotsLength;
102     /***
103      * <p>
104      * Work out the effective length of the string; treat character entities as
105      * a single character.
106      * </p>
107      */
108     static {
109         CharacterEntityFormat entityFormat = new CharacterEntityFormat();
110         entityFormat.setReverse(true);
111         String effectiveString = entityFormat.format(DOTS_STRING);
112         dotsLength = effectiveString.length();
113     }
114     /***
115      * <p>
116      * Stores the maximum string length to output when format is called.
117      * </p>
118      */
119     private Integer maximumLength = null;
120     /***
121      * <p>
122      * Format the string given in <code>hTMLText</code> to the maximum length
123      * provided by calling <code>setMaxLength</code>.
124      * </p>
125      *
126      * @param hTMLTextParam
127      *            the text to truncate
128      * @return Refer to {@link HTMLFormat#format}.
129      */
130     public final String format(final String hTMLTextParam) {
131         String hTMLText = hTMLTextParam;
132         if (this.maximumLength != null) {
133             int maximumLengthInt = this.maximumLength.intValue();
134             if ((hTMLText.length() > maximumLengthInt)) {
135                 // if the max length is greater than 2, use the dots
136                 if (maximumLengthInt > (1 + dotsLength)) {
137                     hTMLText = hTMLText.substring(0, maximumLengthInt
138                             - dotsLength)
139                             + DOTS_STRING;
140                 } else {
141                     // otherwise, just shrink the string
142                     hTMLText = hTMLText.substring(0, maximumLengthInt);
143                 }
144             }
145         }
146         return hTMLText;
147     }
148     /***
149      * <p>
150      * Get the maximum string length to output when format is called.
151      * </p>
152      *
153      * @return the current maximum length of the string which is output, or
154      *         <code>null</code> if no maximum has been set yet.
155      */
156     public final Integer getMaximumLength() {
157         return maximumLength;
158     }
159     /***
160      * <p>
161      * Set the maximum string length to output when <code>format</code> is
162      * called.
163      * </p>
164      *
165      * @param maximumLengthParam
166      *            the new value of the maximum length to output.
167      */
168     public final void setMaximumLength(final Integer maximumLengthParam) {
169         this.maximumLength = maximumLengthParam;
170     }
171 }
172