View Javadoc
1   package org.andromda.andromdapp;
2   
3   import org.andromda.core.common.ClassUtils;
4   import org.andromda.core.common.Converter;
5   import org.apache.commons.lang.BooleanUtils;
6   import org.apache.commons.lang.ObjectUtils;
7   import org.apache.commons.lang.StringUtils;
8   
9   /**
10   * Some utilities for dealing with the AndroMDApp generator.
11   *
12   * @author Chad Brandon
13   */
14  public class AndroMDAppUtils
15  {
16      /**
17       * The delimiter for separating patterns.
18       */
19      private static final String COMMA = ",";
20  
21      /**
22       * Converts a comma delimited string to an array of Strings.
23       *
24       * @param string to convert.
25       * @return the resulting array or null if the string was null.
26       */
27      public static String[] stringToArray(final String string)
28      {
29          return string == null ? null : string.split(COMMA);
30      }
31  
32      /**
33       * Attempts to convert the given <code>value</code> to the given
34       * <code>type</code> (if the type is specified), otherwise does nothing and
35       * returns the value unchanged.
36       *
37       * @param value the value to convert.
38       * @param type the type to convert it to.
39       * @return the converted, or unconverted depending on whether it needed
40       *         to be converted.
41       */
42      public static Object convert(
43          final Object value,
44          final String type)
45      {
46          Object object = value;
47          if (StringUtils.isNotBlank(type))
48          {
49              try
50              {
51                  final Class typeClass = ClassUtils.getClassLoader().loadClass(type);
52  
53                  // - handle booleans differently, since we want to be able to
54                  // convert 'yes/no', 'on/off', etc
55                  // to boolean values
56                  if (typeClass == Boolean.class)
57                  {
58                      object = BooleanUtils.toBooleanObject(ObjectUtils.toString(value));
59                  }
60                  else
61                  {
62                      object = Converter.convert(
63                              value,
64                              typeClass);
65                  }
66              }
67              catch (final ClassNotFoundException exception)
68              {
69                  // - ignore
70              }
71          }
72          return object;
73      }
74  }