View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of Java-Common.
5    *
6    * Java-Common is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * Java-Common is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with Java-Common.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.util;
21  
22  import java.lang.reflect.Array;
23  import java.lang.reflect.InvocationTargetException;
24  import java.lang.reflect.Method;
25  
26  import com.cosylab.introspection.ClassIntrospector;
27  
28  /**
29   * 
30   * @author <a href="mailto:miha.kadunc@cosylab.com">Miha Kadunc</a>
31   * @version $id$
32   * 
33   * @deprecated renamed to StringUtilities
34   */
35  
36  public final class StringHelper {
37  
38      /**
39       * Constructor for StringHelper.
40       */
41      private StringHelper() {
42  
43      }
44      public static Object fromString(String sValue, Class targetClass) throws CommonException {
45          try {
46              if ((sValue == null) || (sValue.equals("")))
47                  return targetClass.newInstance();
48              if (targetClass.isArray())
49                  return arrayFromString(sValue, targetClass);
50              if (targetClass.equals(String.class))
51                  return sValue;
52              else if (
53                  Number.class.isAssignableFrom(targetClass)
54                      || Boolean.class.isAssignableFrom(targetClass)) {
55                  return targetClass.getMethod("valueOf", new Class[] { String.class }).invoke(
56                      null,
57                      new Object[] { sValue });
58              } else if (targetClass.isPrimitive()) {
59                  // we are not able to return Object of a primitive type so use its class implementation
60                  if (Boolean.TYPE.equals(targetClass))
61                      return Boolean.valueOf(sValue);
62                  if (Short.TYPE.equals(targetClass))
63                      return Short.valueOf(sValue);
64                  if (Byte.TYPE.equals(targetClass))
65                      return Byte.valueOf(sValue);
66                  if (Character.TYPE.equals(targetClass))
67                      return new Character(sValue.charAt(0));
68                  if (Integer.TYPE.equals(targetClass))
69                      return Integer.valueOf(sValue);
70                  if (Long.TYPE.equals(targetClass))
71                      return Long.valueOf(sValue);
72                  if (Float.TYPE.equals(targetClass))
73                      return Float.valueOf(sValue);
74                  if (Double.TYPE.equals(targetClass))
75                      return Double.valueOf(sValue);
76                  return null;
77              } else if (Class.class == targetClass) {
78                  // in case of primitive types return its class
79                  return classFromString(sValue);
80              }
81              // check if the class has the valueOf(String) method and call it with given parameter if so
82              // the example is in case of DataProducer which is instantiated by the parameters from the xml file
83              else {
84                  Method met =
85                      ClassIntrospector.getMethod(
86                          targetClass,
87                          "valueOf",
88                          new Class[] { String.class });
89                  if (met != null) {
90                      return met.invoke(null, new String[] { sValue });
91                  }
92                  return targetClass.newInstance();
93              }
94          } catch (NoSuchMethodException e) {
95              throw new CommonException(
96                  ArrayHelper.class,
97                  "Objects could not be instantiated from strings",
98                  e);
99          } catch (IllegalAccessException e) {
100             throw new CommonException(
101                 ArrayHelper.class,
102                 "Objects could not be instantiated from strings",
103                 e);
104         } catch (InvocationTargetException e) {
105             throw new CommonException(
106                 ArrayHelper.class,
107                 "Objects could not be instantiated from strings",
108                 e);
109         } catch (InstantiationException e) {
110             throw new CommonException(
111                 ArrayHelper.class,
112                 "Objects could not be instantiated from strings",
113                 e);
114         }
115 
116     }
117 
118     public static Class classFromString(String sValue) throws CommonException {
119         try {
120             if (sValue.equals(Boolean.TYPE.getName()))
121                 return Boolean.TYPE;
122             if (sValue.equals(Short.TYPE.getName()))
123                 return Short.TYPE;
124             if (sValue.equals(Byte.TYPE.getName()))
125                 return Byte.TYPE;
126             if (sValue.equals(Character.TYPE.getName()))
127                 return Character.TYPE;
128             if (sValue.equals(Integer.TYPE.getName()))
129                 return Integer.TYPE;
130             if (sValue.equals(Long.TYPE.getName()))
131                 return Long.TYPE;
132             if (sValue.equals(Float.TYPE.getName()))
133                 return Float.TYPE;
134             if (sValue.equals(Double.TYPE.getName()))
135                 return Double.TYPE;
136             if (sValue.equals(Void.TYPE.getName()))
137                 return Void.TYPE;
138             return Class.forName(sValue);
139         } catch (ClassNotFoundException e) {
140             throw new CommonException(
141                 ArrayHelper.class,
142                 "Class could not be instantiated from a String",
143                 e);
144         }
145     }
146     public static Object arrayFromString(String arrayString, Class arrayClass) throws CommonException {
147             return arrayFromString(arrayString, arrayClass, ",", true);
148         }
149 
150         public static Object arrayFromString(String arrayString, Class arrayClass,String separator,boolean parentheses) throws CommonException {
151             String[] pStrings = arrayString.split(separator);
152             if (parentheses) {
153                 pStrings[0] = pStrings[0].substring(1);
154                 pStrings[pStrings.length-1] = pStrings[pStrings.length-1].substring(0, pStrings[pStrings.length-1].length() - 1);
155             }
156 
157             Object retArray = Array.newInstance(arrayClass.getComponentType(), pStrings.length);
158             for (int i = 0; i < pStrings.length; i++) {
159                 Array.set(retArray,i,StringHelper.fromString(pStrings[i], arrayClass.getComponentType()));
160             }
161             return retArray;
162         }
163     
164 
165 }