01: package org.andromda.andromdapp;
02:
03: import org.andromda.core.common.ClassUtils;
04: import org.andromda.core.common.Converter;
05: import org.apache.commons.lang.BooleanUtils;
06: import org.apache.commons.lang.ObjectUtils;
07:
08: /**
09: * Some utlities for dealing with the AndroMDApp generator.
10: *
11: * @author Chad Brandon
12: */
13: public class AndroMDAppUtils {
14: /**
15: * The delimiter for seperating patterns.
16: */
17: private static final String COMMA = ",";
18:
19: /**
20: * Converts a comma delimated string to an array of Strings.
21: *
22: * @param string to convert.
23: * @return the resulting array or null if the string was null.
24: */
25: public static String[] stringToArray(final String string) {
26: return string != null ? string.split(COMMA) : null;
27: }
28:
29: /**
30: * Attempts to convert the given <code>value</code> to the given
31: * <code>type</code> (if the type is specifed), otherwise does nothing and
32: * returns the value unchanged.
33: *
34: * @param value the value to convert.
35: * @param type the type to convert it to.
36: * @return the converted, or unconverted dependending on whether ir needed
37: * to be converted.
38: */
39: public static Object convert(final Object value, final String type) {
40: Object object = value;
41: if (type != null && type.trim().length() > 0) {
42: try {
43: final Class typeClass = ClassUtils.getClassLoader()
44: .loadClass(type);
45:
46: // - handle booleans differently, since we want to be able to
47: // convert 'yes/no', 'on/off', etc
48: // to boolean values
49: if (typeClass == Boolean.class) {
50: object = BooleanUtils.toBooleanObject(ObjectUtils
51: .toString(value));
52: } else {
53: object = Converter.convert(value, typeClass);
54: }
55: } catch (final ClassNotFoundException exception) {
56: // - ignore
57: }
58: }
59: return object;
60: }
61: }
|