001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.util;
017:
018: import java.lang.reflect.Method;
019: import java.util.StringTokenizer;
020:
021: public class StringUtilities {
022:
023: public static final String CRLF = "\r\n";
024:
025: //we don't want anyone creating new instances
026: private StringUtilities() {
027: }
028:
029: public static String getLastToken(String tokenString,
030: String delimeter) {
031: StringTokenizer token = new StringTokenizer(tokenString,
032: delimeter);
033:
034: String returnValue = null;
035: while (token.hasMoreTokens()) {
036: returnValue = token.nextToken();
037: }
038:
039: return returnValue;
040: }
041:
042: public static String nullToBlankString(String stringToCheckForNull) {
043: return (stringToCheckForNull == null) ? ""
044: : stringToCheckForNull;
045: }
046:
047: public static boolean checkNullBlankString(String stringToCheck) {
048: return (stringToCheck == null || ""
049: .equals(stringToCheck.trim()));
050: }
051:
052: public static String blankToNullString(String stringToCheckForBlank) {
053: if (stringToCheckForBlank != null)
054: stringToCheckForBlank = stringToCheckForBlank.trim();
055: return ("".equals(stringToCheckForBlank)) ? null
056: : stringToCheckForBlank;
057: }
058:
059: public static String replaceNullOrBlankStringWithNonBreakingSpace(
060: String stringToCheckForNull) {
061: if ((stringToCheckForNull == null)
062: || (stringToCheckForNull.equals(""))) {
063: return " ";
064: } else {
065: return stringToCheckForNull;
066: }
067: }
068:
069: public static String createMethodString(Method method,
070: String lineBreak) {
071: Class[] parameterList = method.getParameterTypes();
072: Class[] exceptionList = method.getExceptionTypes();
073: StringBuffer methodString = new StringBuffer();
074:
075: methodString.append(method.getName()).append("(");
076:
077: for (int j = 0; j < parameterList.length; j++) {
078: methodString.append(StringUtilities.getLastToken(
079: parameterList[j].getName(), "."));
080:
081: if (j != (parameterList.length - 1)) {
082: methodString.append(", ");
083: }
084: }
085: methodString.append(") ");
086:
087: if (exceptionList.length > 0) {
088: methodString.append(lineBreak);
089: methodString.append("throws ");
090: }
091:
092: for (int j = 0; j < exceptionList.length; j++) {
093: methodString.append(StringUtilities.getLastToken(
094: exceptionList[j].getName(), "."));
095:
096: if (j != (exceptionList.length - 1)) {
097: methodString.append(", ");
098: }
099: }
100:
101: return methodString.toString();
102: }
103:
104: public static String stringArrayToCommaDelimitedStringList(
105: String[] stringArray) {
106: StringBuffer stringList = new StringBuffer();
107: for (int i = 0; i < stringArray.length; i++) {
108: stringList.append(stringArray[i]);
109: if (i != (stringArray.length - 1)) {
110: stringList.append(",");
111: }
112: }
113:
114: return stringList.toString();
115: }
116:
117: }
|