KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > util > StringUtilities


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "OpenEJB" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of The OpenEJB Group. For written permission,
18  * please contact dev@openejb.org.
19  *
20  * 4. Products derived from this Software may not be called "OpenEJB"
21  * nor may "OpenEJB" appear in their names without prior written
22  * permission of The OpenEJB Group. OpenEJB is a registered
23  * trademark of The OpenEJB Group.
24  *
25  * 5. Due credit should be given to the OpenEJB Project
26  * (http://www.openejb.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2001 (C) The OpenEJB Group. All Rights Reserved.
42  *
43  * $Id: StringUtilities.java 1921 2005-06-19 22:40:34Z jlaskowski $
44  */

45 package org.openejb.util;
46
47 import java.lang.reflect.Method JavaDoc;
48 import java.util.StringTokenizer JavaDoc;
49
50 /**
51  * A simple string utilities class.
52  *
53  * @author <a HREF="mailto:tim_urberg@yahoo.com">Tim Urberg</a>
54  */

55 public class StringUtilities {
56     /** the CRLF for use in String manipulation */
57     public static final String JavaDoc CRLF = "\r\n";
58
59     //we don't want anyone creating new instances
60
private StringUtilities() {}
61
62     /**
63      * Gets the last token in a StringTokenizer.
64      * @param tokenString - the string to get the last token from
65      * @param delimeter - the delimeter of the string
66      * @return the last token or null if there are none
67      */

68     public static String JavaDoc getLastToken(String JavaDoc tokenString, String JavaDoc delimeter) {
69         StringTokenizer JavaDoc token = new StringTokenizer JavaDoc(tokenString, delimeter);
70
71         String JavaDoc returnValue = null;
72         while (token.hasMoreTokens()) {
73             returnValue = token.nextToken();
74         }
75
76         return returnValue;
77     }
78
79     /**
80      * Checks a String to see if it's value is null,
81      * and if so returns a blank string.
82      * @param stringToCheckForNull - the string to check for null
83      * @return the checked string
84      */

85     public static String JavaDoc nullToBlankString(String JavaDoc stringToCheckForNull) {
86         return (stringToCheckForNull == null) ? "" : stringToCheckForNull;
87     }
88
89     /**
90      * Checks a String to see if it's value is null or blank
91      * @param stringToCheck - the string to check for blank or null
92      * @return whether blank or null
93      */

94     public static boolean checkNullBlankString(String JavaDoc stringToCheck) {
95         return (stringToCheck == null || "".equals(stringToCheck.trim()));
96     }
97
98     /**
99      * Checks a String to see if it's blank,
100      * and if so returns null (the opposite of <code>nullToBlankString</code>.
101      * @param stringToCheckForBlank the string to check for blank
102      * @return the checked string or null
103      */

104     public static String JavaDoc blankToNullString(String JavaDoc stringToCheckForBlank) {
105         if(stringToCheckForBlank != null) stringToCheckForBlank = stringToCheckForBlank.trim();
106         return ("".equals(stringToCheckForBlank)) ? null : stringToCheckForBlank;
107     }
108
109     /**
110      * Checks a String to see if it's value is null or blank,
111      * and if so returns a non-breaking space.
112      * @param stringToCheckForNull - the string to check for null or blank
113      * @return the checked string
114      */

115     public static String JavaDoc replaceNullOrBlankStringWithNonBreakingSpace(String JavaDoc stringToCheckForNull) {
116         if ((stringToCheckForNull == null) || (stringToCheckForNull.equals(""))) {
117             return "&nbsp;";
118         } else {
119             return stringToCheckForNull;
120         }
121     }
122
123     /**
124      * Creates a string representation of a reflection method for example
125      * <br>
126      * <code>
127      * myMethod(String, String) throws Exception
128      * </code>
129      * <br>
130      * @param method - the reflection method
131      * @param lineBreak - the type of line break usually \n or &lt;br&gt;
132      * @return the string representation of the method
133      */

134     public static String JavaDoc createMethodString(Method JavaDoc method, String JavaDoc lineBreak) {
135         Class JavaDoc[] parameterList = method.getParameterTypes();
136         Class JavaDoc[] exceptionList = method.getExceptionTypes();
137         StringBuffer JavaDoc methodString = new StringBuffer JavaDoc();
138
139         methodString.append(method.getName()).append("(");
140
141         for (int j = 0; j < parameterList.length; j++) {
142             methodString.append(StringUtilities.getLastToken(parameterList[j].getName(), "."));
143
144             if (j != (parameterList.length - 1)) {
145                 methodString.append(", ");
146             }
147         }
148         methodString.append(") ");
149
150         if (exceptionList.length > 0) {
151             methodString.append(lineBreak);
152             methodString.append("throws ");
153         }
154
155         for (int j = 0; j < exceptionList.length; j++) {
156             methodString.append(StringUtilities.getLastToken(exceptionList[j].getName(), "."));
157
158             if (j != (exceptionList.length - 1)) {
159                 methodString.append(", ");
160             }
161         }
162
163         return methodString.toString();
164     }
165
166     /**
167      * Changes a string array into a comma delimted list
168      * @param stringArray - The string array to be converted
169      * @return the comma delimted list
170      */

171     public static String JavaDoc stringArrayToCommaDelimitedStringList(String JavaDoc[] stringArray) {
172         StringBuffer JavaDoc stringList = new StringBuffer JavaDoc();
173         for (int i = 0; i < stringArray.length; i++) {
174             stringList.append(stringArray[i]);
175             if (i != (stringArray.length - 1)) {
176                 stringList.append(",");
177             }
178         }
179
180         return stringList.toString();
181     }
182
183 }
Popular Tags