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: LeadingCharacterFormat.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:38 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.3 2004/03/21 21:16:37 colinmacleod
055: * Shortened name to ivata op.
056: *
057: * Revision 1.2 2004/02/01 22:07:32 colinmacleod
058: * Added full names to author tags
059: *
060: * Revision 1.1.1.1 2004/01/27 20:59:48 colinmacleod
061: * Moved ivata op to SourceForge.
062: *
063: * Revision 1.2 2003/10/15 14:13:39 colin
064: * Fixes for XDoclet.
065: *
066: * Revision 1.1 2003/02/24 19:33:33 colin
067: * Moved to new subproject.
068: *
069: * Revision 1.2 2003/02/04 17:43:46 colin
070: * copyright notice
071: *
072: * Revision 1.1 2002/10/24 14:22:22 colin
073: * first version used to format numbers for alphanumeric comparison
074: * -----------------------------------------------------------------------------
075: */
076: package com.ivata.mask.web.format;
077:
078: import org.apache.log4j.Logger;
079:
080: /**
081: * <p>
082: * Format a string by appending a leading character. You can specify either an
083: * absolute length of the final string, or a number of characters to append.
084: * </p>
085: *
086: * @since ivata masks 0.4 (2002-10-24)
087: * @author Colin MacLeod
088: * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
089: * @version $Revision: 1.5 $
090: */
091: public class LeadingCharacterFormat implements HTMLFormat {
092: /**
093: * Logger for this class.
094: */
095: private static final Logger logger = Logger
096: .getLogger(LeadingCharacterFormat.class);
097:
098: /**
099: * <p>
100: * If <code>false</code>, the <code>count</code> from {@link #setCount
101: * setCount} represents a total number of characters to prepend. Otherwise (
102: * <code>true</code>- default setting) it represents a maximum string
103: * length for the whole string returned in {@link #format format}.
104: * </p>
105: */
106: private boolean countIsMaximum = true;
107: /**
108: * <p>
109: * This character will be prepended to the string.
110: * </p>
111: */
112: private char character = ' ';
113: /**
114: * <p>
115: * The number of times the character will be prepended.
116: * </p>
117: */
118: private int count = 1;
119:
120: /**
121: * <p>
122: * Takes the text provided and prepends the character defined in
123: * {@link #setCharacter setCharacter}the number of times defined in
124: * {@link #setCount setCount}. If you specified this is a maximum (
125: * {@link #setMaximum setMaximum(true)}), then only the rightmost count of
126: * characters are returned.
127: * </p>
128: *
129: * @param hTMLText <copyDoc>Refer to {@link HTMLFormat#format}.</copyDoc>
130: * @return <copyDoc>Refer to {@link HTMLFormat#format}.</copyDoc>
131: */
132: public final String format(final String hTMLText) {
133: if (logger.isDebugEnabled()) {
134: logger.debug("format(String hTMLText = " + hTMLText
135: + ") - start");
136: }
137:
138: // if the count is a maximum and you supply a string larger, just return
139: // the rightmost count characters
140: int length = hTMLText.length();
141: if (countIsMaximum && (length > count)) {
142: String returnString = hTMLText
143: .substring(length - count - 1);
144: if (logger.isDebugEnabled()) {
145: logger.debug("format(String) - end - return value = "
146: + returnString);
147: }
148: return returnString;
149: }
150: // reverse the string so we can append
151: StringBuffer buffer = new StringBuffer(hTMLText).reverse();
152: for (int i = 0; (i < count)
153: && (!countIsMaximum || (buffer.length() < count)); ++i) {
154: buffer.append(character);
155: }
156: String returnString = buffer.reverse().toString();
157: if (logger.isDebugEnabled()) {
158: logger.debug("format(String) - end - return value = "
159: + returnString);
160: }
161: return returnString;
162: }
163:
164: /**
165: * <p>
166: * Get whether the number specified in {@link #setCount setCount}should be
167: * a maximum string length, or the number of characters to prepend.
168: *
169: * @return <code>false</code> if the <code>count</code> from {@link
170: * #setCount setCount} represents a total number of characters to
171: * prepend, or <code>true</code>- default setting - represents a
172: * maximum string length for the whole string returned in
173: * {@link #format}.
174: * </p>
175: */
176: public final boolean getCountIsMaximum() {
177: if (logger.isDebugEnabled()) {
178: logger.debug("getCountIsMaximum() - start");
179: }
180:
181: if (logger.isDebugEnabled()) {
182: logger.debug("getCountIsMaximum() - end - return value = "
183: + countIsMaximum);
184: }
185: return countIsMaximum;
186: }
187:
188: /**
189: * <p>
190: * Set whether the number specified in {@link #setCount setCount}should be
191: * a maximum string length, or the number of characters to prepend.
192: *
193: * @param countIsMaximumParam
194: * set to <code>false</code> if the <code>count</code> from
195: * {@link #setCount setCount}represents a total number of
196: * characters to prepend, or <code>true</code>- default
197: * setting - represents a maximum string length for the whole
198: * string returned in {@link #format format}.
199: * </p>
200: */
201: public final void setCountIsMaximum(
202: final boolean countIsMaximumParam) {
203: if (logger.isDebugEnabled()) {
204: logger
205: .debug("setCountIsMaximum(boolean countIsMaximumParam = "
206: + countIsMaximumParam + ") - start");
207: }
208:
209: this .countIsMaximum = countIsMaximumParam;
210:
211: if (logger.isDebugEnabled()) {
212: logger.debug("setCountIsMaximum(boolean) - end");
213: }
214: }
215:
216: /**
217: * <p>
218: * This character will be prepended to the string.
219: * </p>
220: *
221: * @return the current value of the character which will prepended.
222: */
223: public final char getCharacter() {
224: if (logger.isDebugEnabled()) {
225: logger.debug("getCharacter() - start");
226: }
227:
228: if (logger.isDebugEnabled()) {
229: logger.debug("getCharacter() - end - return value = "
230: + character);
231: }
232: return character;
233: }
234:
235: /**
236: * <p>
237: * This character will be prepended to the string.
238: * </p>
239: *
240: * @param characterParam
241: * the new value of the character which will be prepended.
242: */
243: public final void setCharacter(final char characterParam) {
244: if (logger.isDebugEnabled()) {
245: logger.debug("setCharacter(char characterParam = "
246: + characterParam + ") - start");
247: }
248:
249: this .character = characterParam;
250:
251: if (logger.isDebugEnabled()) {
252: logger.debug("setCharacter(char) - end");
253: }
254: }
255:
256: /**
257: * <p>
258: * The number of times the character will be prepended.
259: * </p>
260: *
261: * @return the current value of count.
262: */
263: public final int getCount() {
264: if (logger.isDebugEnabled()) {
265: logger.debug("getCount() - start");
266: }
267:
268: if (logger.isDebugEnabled()) {
269: logger.debug("getCount() - end - return value = " + count);
270: }
271: return count;
272: }
273:
274: /**
275: * <p>
276: * The number of times the character will be prepended.
277: * </p>
278: *
279: * @param countParam
280: * the new value of count.
281: */
282: public final void setCount(final int countParam) {
283: if (logger.isDebugEnabled()) {
284: logger.debug("setCount(int countParam = " + countParam
285: + ") - start");
286: }
287:
288: this .count = countParam;
289:
290: if (logger.isDebugEnabled()) {
291: logger.debug("setCount(int) - end");
292: }
293: }
294: }
|