001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)StringHelper.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.management.util;
030:
031: import java.util.List;
032:
033: /**
034: * Helper class for string operations
035: *
036: * @author Sun Microsystems, Inc.
037: */
038: public class StringHelper {
039: /**
040: * Trim the whitespaces from all the string elements in the list. If the string
041: * is empty then it is removed from the list.
042: */
043: public static List<String> trim(List<String> list) {
044: List<String> trimmedList = new java.util.ArrayList<String>(list
045: .size());
046: for (String str : list) {
047: String trimmedStr = trim(str);
048: if (!"".equals(trimmedStr)) {
049: trimmedList.add(trim(str));
050: }
051: }
052: return trimmedList;
053: }
054:
055: /**
056: * Trim the white spaces from the source string. If the string is a null
057: * value, an empty string is returned.
058: */
059: public static String trim(String srcStr) {
060: if (srcStr == null) {
061: return "";
062: }
063:
064: return srcStr.trim();
065: }
066:
067: /**
068: * Convert a String Value to an Object of a Specific Type.
069: *
070: * @param type - the fully qualified name of the desired type
071: * @param arg - String value to be converted
072: * @return Object Instance of the type specified, constructed with the String value
073: */
074: public static Object convertStringToType(String type, String arg)
075: throws Exception {
076: Class objClass = null;
077:
078: // -- If it's a primitive type
079: if (type.equals("boolean")) {
080: objClass = Boolean.class;
081: } else if (type.equals("byte")) {
082: objClass = Byte.class;
083: } else if (type.equals("char")) {
084: objClass = Character.class;
085: } else if (type.equals("short")) {
086: objClass = Short.class;
087: } else if (type.equals("int")) {
088: objClass = Integer.class;
089: } else if (type.equals("long")) {
090: objClass = Long.class;
091: } else if (type.equals("float")) {
092: objClass = Float.class;
093: } else if (type.equals("double")) {
094: objClass = Double.class;
095: } else if (type.equals("void")) {
096: objClass = Void.class;
097: } else {
098: objClass = Class.forName(type);
099: }
100:
101: Class[] argClass = new Class[] { String.class };
102: java.lang.reflect.Constructor objConstructor = objClass
103: .getConstructor(argClass);
104: String[] argValue = { arg };
105: return objConstructor.newInstance(argValue);
106: }
107: }
|