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: StringHandling.java,v $
031: * Revision 1.5 2005/10/11 18:55:58 colinmacleod
032: * Fixed some checkstyle and javadoc issues.
033: *
034: * Revision 1.4 2005/10/02 14:06:32 colinmacleod
035: * Added/improved log4j logging.
036: *
037: * Revision 1.3 2005/04/09 18:04:17 colinmacleod
038: * Changed copyright text to GPL v2 explicitly.
039: *
040: * Revision 1.2 2005/01/06 22:21:45 colinmacleod
041: * Moved up a version number.
042: * Changed copyright notices to 2005.
043: * Updated the documentation:
044: * - started working on multiproject:site docu.
045: * - changed the logo.
046: * Added checkstyle and fixed LOADS of style issues.
047: * Added separate thirdparty subproject.
048: * Added struts (in web), util and webgui (in webtheme) from ivata op.
049: *
050: * Revision 1.3 2004/03/21 21:16:36 colinmacleod
051: * Shortened name to ivata op.
052: *
053: * Revision 1.2 2004/02/01 22:07:32 colinmacleod
054: * Added full names to author tags
055: *
056: * Revision 1.1.1.1 2004/01/27 20:59:47 colinmacleod
057: * Moved ivata op to SourceForge.
058: *
059: * Revision 1.2 2003/10/15 14:13:53 colin
060: * fixing for XDoclet
061: *
062: * Revision 1.9 2003/02/24 19:27:31 colin
063: * restructured file paths
064: *
065: * Revision 1.8 2003/02/04 17:43:52 colin
066: * copyright notice
067: *
068: * Revision 1.7 2002/09/16 14:18:52 colin
069: * added booleanValue
070: *
071: * Revision 1.6 2002/09/09 13:57:35 peter
072: * fixed naming convention
073: *
074: * Revision 1.4 2002/08/15 08:27:04 peter
075: * modified
076: *
077: * Revision 1.1 2002/04/27 17:38:21 colin
078: * first compiling version in EJB/JBuilder project
079: *
080: * Revision 1.1.1.1 2002/04/22 13:51:47 colin
081: * EJB version of the intranet project
082: *
083: * Revision 1.2 2002/02/03 13:09:45 colin
084: * made docbook documentation;
085: * changed name of method valueOf to integerValue
086: *
087: * Revision 1.1 2002/01/20 19:28:25 colin
088: * added tab and tree tags
089: * implemented address book functionality
090: * -----------------------------------------------------------------------------
091: */
092: package com.ivata.mask.util;
093:
094: import org.apache.log4j.Logger;
095:
096: /**
097: * <p>
098: * StringHandling is a helper class for handling Strings. It contains methods
099: * for converting to and from different data types, and for handling
100: * <code>null</code> conditions.
101: * </p>
102: *
103: * <p>
104: * Don't create an instance of this class; use the static final methods.
105: * </p>
106: *
107: * @since ivata masks 0.4 (2001-12-27)
108: * @author Colin MacLeod
109: * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
110: * @version $Revision: 1.5 $
111: */
112: public final class StringHandling {
113: /**
114: * Logger for this class.
115: */
116: private static final Logger logger = Logger
117: .getLogger(StringHandling.class);
118:
119: /**
120: * <p>
121: * This method works like <code>Boolean.valueOf</code>, but allows
122: * <code>null</code> values without an exception, and also translates the
123: * string <code>"on"</code> as <code>true</code> which is useful for
124: * converting request values.
125: * </p>
126: *
127: * @param convertToBoolean
128: * a String to be converted to a boolean value. <code>null</code>
129: * Strings return in a <code>null
130: * Boolean</code> returned.
131: * @return <code>null</code> if <code>convertToBoolean</code> is
132: * <code>null</code> or if <code>convertToBoolean</code> equals
133: * the string <code>"null"</code>. It returns a
134: * <code>Boolean true</code> value if the string is equal to
135: * <code>"true"</code> or <code>"on"</code> (ignoring case),
136: * otherwise a <code>Boolean
137: * false</code> value is returned.
138: */
139: public static Boolean booleanValue(final String convertToBoolean) {
140: if (logger.isDebugEnabled()) {
141: logger.debug("booleanValue(String convertToBoolean = "
142: + convertToBoolean + ") - start");
143: }
144:
145: if ((convertToBoolean == null)
146: || (convertToBoolean.equals("null"))) {
147: if (logger.isDebugEnabled()) {
148: logger
149: .debug("booleanValue(String) - end - return value = "
150: + null);
151: }
152: return null;
153: }
154: Boolean returnBoolean = new Boolean(convertToBoolean
155: .equalsIgnoreCase("true")
156: || convertToBoolean.equalsIgnoreCase("on"));
157: if (logger.isDebugEnabled()) {
158: logger.debug("booleanValue(String) - end - return value = "
159: + returnBoolean);
160: }
161: return returnBoolean;
162: }
163:
164: /**
165: * <p>
166: * Generates a random string of specified length, when the length 0 or less,
167: * defaults to 8 The string only consists of letters and numbers and the
168: * first character is always a letter.
169: * </p>
170: *
171: * @param length
172: * the length of the string to generate.
173: * @return a random string of the specified length.
174: */
175: public static String generateRandomString(final int length) {
176: if (logger.isDebugEnabled()) {
177: logger.debug("generateRandomString(int length = " + length
178: + ") - start");
179: }
180:
181: // check we got some length
182: assert (length > 0);
183: String returnString = "";
184: Character ch;
185: java.util.Random randomGenerator = new java.util.Random();
186: int i;
187: byte randomByte;
188: for (i = 0; i < length; i++) {
189: // the first character must be a letter
190: if (i == 0) {
191: do {
192: randomByte = (byte) randomGenerator.nextInt('z');
193: } while (randomByte < 'A'
194: || (randomByte > 'Z' && randomByte < 'a'));
195: // the others can also be numbers
196: } else {
197: do {
198: randomByte = (byte) randomGenerator.nextInt('z');
199: } while (randomByte < '0'
200: || (randomByte > '9' && randomByte < 'A')
201: || (randomByte > 'Z' && randomByte < 'a'));
202: }
203: ch = new Character((char) randomByte);
204: returnString += ch.toString();
205: }
206:
207: if (logger.isDebugEnabled()) {
208: logger
209: .debug("generateRandomString(int) - end - return value = "
210: + returnString);
211: }
212: return returnString;
213: }
214:
215: /**
216: * <p>
217: * If the string supplied is not <code>null</code>, return the string,
218: * otherwise return the empty string.
219: * </p>
220: *
221: * @param check
222: * an object to compare against null
223: * @return an empty string, if <code>checkString</code> is
224: * <code>null</code>, otherwise the value of
225: * <code>checkString</code> unaltered.
226: * @see #getNotNull(Object o, String nullString)
227: */
228: public static String getNotNull(final Object check) {
229: if (logger.isDebugEnabled()) {
230: logger.debug("getNotNull(Object check = " + check
231: + ") - start");
232: }
233:
234: if (check == null) {
235: if (logger.isDebugEnabled()) {
236: logger
237: .debug("getNotNull(Object) - end - return value = ");
238: }
239: return "";
240: } else {
241: String returnString = check.toString();
242: if (logger.isDebugEnabled()) {
243: logger
244: .debug("getNotNull(Object) - end - return value = "
245: + returnString);
246: }
247: return returnString;
248: }
249: }
250:
251: /**
252: * <p>
253: * Handle null objects in a standard way. If the object you pass is null,
254: * then the string <code>nullString</code> will be returned.
255: * </p>
256: *
257: * @param check
258: * an object to compare against null
259: * @param nullString
260: * the string to return if o is null
261: * @return the parameter nullString if o is null, otherwise o.toString()
262: * @see #getNotNull(Object check)
263: */
264: public static String getNotNull(final Object check,
265: final String nullString) {
266: if (logger.isDebugEnabled()) {
267: logger.debug("getNotNull(Object check = " + check
268: + ", String nullString = " + nullString
269: + ") - start");
270: }
271:
272: if (check != null) {
273: String returnString = check.toString();
274: if (logger.isDebugEnabled()) {
275: logger.debug("getNotNull - end - return value = "
276: + returnString);
277: }
278: return returnString;
279: } else {
280: if (logger.isDebugEnabled()) {
281: logger.debug("getNotNull - end - return value = "
282: + nullString);
283: }
284: return nullString;
285: }
286: }
287:
288: /**
289: * <p>
290: * This method works like <code>Integer.valueOf</code>, but allows
291: * <code>null</code> values without an exception.
292: * </p>
293: *
294: * @param convertToInteger
295: * a String to be converted to an integer number.
296: * <code>null</code> Strings return in a <code>null
297: * Integer</code>
298: * returned.
299: * @return <code>null</code> if <code>convertToInteger</code> is
300: * <code>null</code> or if <code>convertToInteger</code> equals
301: * the string value "null", otherwise an integer representing the
302: * base 10 value of <code>convertToInteger</code>.
303: * @see java.lang.Integer#valueOf(String convertToInteger)
304: */
305: public static Integer integerValue(final String convertToInteger) {
306: if (logger.isDebugEnabled()) {
307: logger.debug("integerValue(String convertToInteger = "
308: + convertToInteger + ") - start");
309: }
310:
311: if (isNullOrEmpty(convertToInteger)
312: || (convertToInteger.equals("null"))) {
313: if (logger.isDebugEnabled()) {
314: logger
315: .debug("integerValue(String) - end - return value = "
316: + null);
317: }
318: return null;
319: }
320: Integer returnInteger = Integer.valueOf(convertToInteger);
321: if (logger.isDebugEnabled()) {
322: logger.debug("integerValue(String) - end - return value = "
323: + returnInteger);
324: }
325: return returnInteger;
326: }
327:
328: /**
329: * <p>
330: * This method does just what it says: evaluates the String you give it and
331: * returns <code>true</code> if it is <code>null</code> or an empty
332: * string.
333: * </p>
334: *
335: * @param checkString
336: * the string to check for being <code>null</code> or empty.
337: * @return <code>true</code> if <code>checkString</code> is
338: * <code>null</code> or an empty string
339: */
340: public static boolean isNullOrEmpty(final String checkString) {
341: if (logger.isDebugEnabled()) {
342: logger.debug("isNullOrEmpty(String checkString = "
343: + checkString + ") - start");
344: }
345:
346: boolean returnboolean = ((checkString == null) || (checkString
347: .trim().equals("")));
348: if (logger.isDebugEnabled()) {
349: logger
350: .debug("isNullOrEmpty(String) - end - return value = "
351: + returnboolean);
352: }
353: return returnboolean;
354: }
355:
356: /**
357: * <p>
358: * Works like the standard <code>Integer.toString()</code> except that it
359: * allows <code>null</code> values, returning a <code>null</code> string
360: * in this case.
361: * </p>
362: *
363: * @param convert
364: * the <code>Integer</code> you want to convert to a string.
365: * @return <code>null</code> if the integer <code>convert</code>. is
366: * <code>null</code>, otherwise the string equivalent of
367: * <code>convert</code>.
368: */
369: public static String toString(final Integer convert) {
370: if (logger.isDebugEnabled()) {
371: logger.debug("toString(Integer convert = " + convert
372: + ") - start");
373: }
374:
375: if (convert == null) {
376: if (logger.isDebugEnabled()) {
377: logger
378: .debug("toString(Integer) - end - return value = "
379: + null);
380: }
381: return null;
382: } else {
383: String returnString = convert.toString();
384: if (logger.isDebugEnabled()) {
385: logger
386: .debug("toString(Integer) - end - return value = "
387: + returnString);
388: }
389: return returnString;
390: }
391: }
392:
393: /**
394: * <p>
395: * Private default constructor ensures utility class functionality.
396: * </p>
397: */
398: private StringHandling() {
399: }
400: }
|