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: LeadingCharacterFormat.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/10/24 14:22:22  colin
67   * first version used to format numbers for alphanumeric comparison
68   * -----------------------------------------------------------------------------
69   */
70  package com.ivata.mask.web.format;
71  /***
72   * <p>
73   * Format a string by appending a leading character. You can specify either an
74   * absolute length of the final string, or a number of characters to append.
75   * </p>
76   *
77   * @since ivata masks 0.4 (2002-10-24)
78   * @author Colin MacLeod
79   * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
80   * @version $Revision: 1.3 $
81   */
82  public class LeadingCharacterFormat implements HTMLFormat {
83      /***
84       * <p>
85       * If <code>false</code>, the <code>count</code> from {@link #setCount
86       * setCount} represents a total number of characters to prepend. Otherwise (
87       * <code>true</code>- default setting) it represents a maximum string
88       * length for the whole string returned in {@link #format format}.
89       * </p>
90       */
91      private boolean countIsMaximum = true;
92      /***
93       * <p>
94       * This character will be prepended to the string.
95       * </p>
96       */
97      private char character = ' ';
98      /***
99       * <p>
100      * The number of times the character will be prepended.
101      * </p>
102      */
103     private int count = 1;
104     /***
105      * <p>
106      * Takes the text provided and prepends the character defined in
107      * {@link #setCharacter setCharacter}the number of times defined in
108      * {@link #setCount setCount}. If you specified this is a maximum (
109      * {@link #setMaximum setMaximum(true)}), then only the rightmost count of
110      * characters are returned.
111      * </p>
112      *
113      * @param hTMLText Refer to {@link HTMLFormat#format}.
114      * @return Refer to {@link HTMLFormat#format}.
115      */
116     public final String format(final String hTMLText) {
117         // if the count is a maximum and you supply a string larger, just return
118         // the rightmost count characters
119         int length = hTMLText.length();
120         if (countIsMaximum && (length > count)) {
121             return hTMLText.substring(length - count - 1);
122         }
123         // reverse the string so we can append
124         StringBuffer buffer = new StringBuffer(hTMLText).reverse();
125         for (int i = 0; (i < count)
126                 && (!countIsMaximum || (buffer.length() < count)); ++i) {
127             buffer.append(character);
128         }
129         return buffer.reverse().toString();
130     }
131     /***
132      * <p>
133      * Get whether the number specified in {@link #setCount setCount}should be
134      * a maximum string length, or the number of characters to prepend.
135      *
136      * @return <code>false</code> if the <code>count</code> from {@link
137      *         #setCount setCount} represents a total number of characters to
138      *         prepend, or <code>true</code>- default setting - represents a
139      *         maximum string length for the whole string returned in
140      *         {@link #format}.
141      *         </p>
142      */
143     public final boolean getCountIsMaximum() {
144         return countIsMaximum;
145     }
146     /***
147      * <p>
148      * Set whether the number specified in {@link #setCount setCount}should be
149      * a maximum string length, or the number of characters to prepend.
150      *
151      * @param countIsMaximumParam
152      *            set to <code>false</code> if the <code>count</code> from
153      *            {@link #setCount setCount}represents a total number of
154      *            characters to prepend, or <code>true</code>- default
155      *            setting - represents a maximum string length for the whole
156      *            string returned in {@link #format format}.
157      *            </p>
158      */
159     public final void setCountIsMaximum(final boolean countIsMaximumParam) {
160         this.countIsMaximum = countIsMaximumParam;
161     }
162     /***
163      * <p>
164      * This character will be prepended to the string.
165      * </p>
166      *
167      * @return the current value of the character which will prepended.
168      */
169     public final char getCharacter() {
170         return character;
171     }
172     /***
173      * <p>
174      * This character will be prepended to the string.
175      * </p>
176      *
177      * @param characterParam
178      *            the new value of the character which will be prepended.
179      */
180     public final void setCharacter(final char characterParam) {
181         this.character = characterParam;
182     }
183     /***
184      * <p>
185      * The number of times the character will be prepended.
186      * </p>
187      *
188      * @return the current value of count.
189      */
190     public final int getCount() {
191         return count;
192     }
193     /***
194      * <p>
195      * The number of times the character will be prepended.
196      * </p>
197      *
198      * @param countParam
199      *            the new value of count.
200      */
201     public final void setCount(final int countParam) {
202         this.count = countParam;
203     }
204 }
205