| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package javax.faces.internal; |
| 17 | |
|
| 18 | |
import java.util.HashMap; |
| 19 | |
import java.util.Iterator; |
| 20 | |
import java.util.Map; |
| 21 | |
import java.util.Map.Entry; |
| 22 | |
|
| 23 | |
import org.seasar.framework.util.ClassUtil; |
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | 0 | public class EnumUtil { |
| 29 | |
|
| 30 | |
private static final String ENUM_SUPPORT_CLASS_NAME = "javax.faces.internal.EnumSupportImpl"; |
| 31 | |
|
| 32 | 0 | private static EnumSupport enumSupport = null; |
| 33 | |
static { |
| 34 | |
try { |
| 35 | 0 | enumSupport = (EnumSupport) ClassUtil |
| 36 | |
.newInstance(ENUM_SUPPORT_CLASS_NAME); |
| 37 | 0 | } catch (final Throwable ignore) { |
| 38 | 0 | } |
| 39 | 0 | } |
| 40 | |
|
| 41 | |
public static Map convertEnumToName(final Map src) { |
| 42 | 0 | if (src == null || src.isEmpty()) { |
| 43 | 0 | return src; |
| 44 | |
} |
| 45 | 0 | Map dest = new HashMap(src.size()); |
| 46 | 0 | for (final Iterator it = src.entrySet().iterator(); it.hasNext();) { |
| 47 | 0 | final Entry entry = (Entry) it.next(); |
| 48 | 0 | final String key = (String) entry.getKey(); |
| 49 | 0 | final Object value = entry.getValue(); |
| 50 | 0 | final Class valueType = value.getClass(); |
| 51 | 0 | if (isEnum(valueType)) { |
| 52 | 0 | final EnumHolder holder = new EnumHolder(valueType.getName(), |
| 53 | |
toName(value)); |
| 54 | 0 | dest.put(key, holder); |
| 55 | |
} else { |
| 56 | 0 | dest.put(key, value); |
| 57 | |
} |
| 58 | |
} |
| 59 | 0 | return dest; |
| 60 | |
} |
| 61 | |
|
| 62 | |
public static Map convertNameToEnum(final Map src) { |
| 63 | 0 | if (src == null || src.isEmpty()) { |
| 64 | 0 | return src; |
| 65 | |
} |
| 66 | 0 | Map dest = new HashMap(src.size()); |
| 67 | 0 | for (final Iterator it = src.entrySet().iterator(); it.hasNext();) { |
| 68 | 0 | final Entry entry = (Entry) it.next(); |
| 69 | 0 | final String key = (String) entry.getKey(); |
| 70 | 0 | final Object value = entry.getValue(); |
| 71 | 0 | if (value instanceof EnumHolder) { |
| 72 | 0 | final Object enumValue = toEnum((EnumHolder) value); |
| 73 | 0 | dest.put(key, enumValue); |
| 74 | |
} else { |
| 75 | 0 | dest.put(key, value); |
| 76 | |
} |
| 77 | |
} |
| 78 | 0 | return dest; |
| 79 | |
} |
| 80 | |
|
| 81 | |
public static boolean isEnum(final Class clazz) { |
| 82 | 0 | return enumSupport != null && enumSupport.isEnum(clazz); |
| 83 | |
} |
| 84 | |
|
| 85 | |
public static String toName(final Object enumObject) { |
| 86 | 0 | return enumSupport.toName(enumObject); |
| 87 | |
} |
| 88 | |
|
| 89 | |
public static Object toEnum(final EnumHolder enumHolder) { |
| 90 | 0 | return enumSupport.toEnum(ClassUtil.forName(enumHolder.enumType), |
| 91 | |
enumHolder.name); |
| 92 | |
} |
| 93 | |
|
| 94 | |
public static Object toEnum(final Class enumType, final String name) { |
| 95 | 0 | return enumSupport.toEnum(enumType, name); |
| 96 | |
} |
| 97 | |
|
| 98 | |
public interface EnumSupport { |
| 99 | |
|
| 100 | |
boolean isEnum(Class clazz); |
| 101 | |
|
| 102 | |
String toName(Object enumObject); |
| 103 | |
|
| 104 | |
Object toEnum(Class enumType, String name); |
| 105 | |
|
| 106 | |
} |
| 107 | |
|
| 108 | 0 | public static class EnumHolder { |
| 109 | |
|
| 110 | |
public String enumType; |
| 111 | |
|
| 112 | |
public String name; |
| 113 | |
|
| 114 | 0 | public EnumHolder(final String enumType, final String name) { |
| 115 | 0 | this.enumType = enumType; |
| 116 | 0 | this.name = name; |
| 117 | 0 | } |
| 118 | |
|
| 119 | |
} |
| 120 | |
|
| 121 | |
} |