1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package javax.faces.convert;
21
22 import javax.faces.component.StateHolder;
23 import javax.faces.component.UIComponent;
24 import javax.faces.context.FacesContext;
25
26 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFConverter;
27
28
29
30
31
32
33 @JSFConverter
34 public class EnumConverter implements Converter, StateHolder {
35
36 public static final String CONVERTER_ID = "javax.faces.Enum";
37 public static final String ENUM_ID = "javax.faces.converter.EnumConverter.ENUM";
38 public static final String ENUM_NO_CLASS_ID = "javax.faces.converter.EnumConverter.ENUM_NO_CLASS";
39
40 private Class targetClass;
41
42 private boolean isTransient = false;
43
44
45 public EnumConverter() {
46 }
47
48 public EnumConverter(Class targetClass) {
49 if (!targetClass.isEnum()) throw new IllegalArgumentException("targetClass for EnumConverter must be an Enum");
50 this.targetClass = targetClass;
51 }
52
53 public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value) throws ConverterException {
54 if (facesContext == null) throw new NullPointerException("facesContext can not be null");
55 if (uiComponent == null) throw new NullPointerException("uiComponent can not be null");
56 if (value == null) return "";
57 checkTargetClass(facesContext, uiComponent, value);
58
59 for (Object enumConstant : targetClass.getEnumConstants()) {
60 if (enumConstant == value) return enumConstant.toString();
61 }
62
63 return value.toString();
64 }
65
66 public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value) throws ConverterException {
67 if (facesContext == null) throw new NullPointerException("facesContext");
68 if (uiComponent == null) throw new NullPointerException("uiComponent");
69 if (value == null) return null;
70 value = value.trim();
71 if (value.length() == 0) return null;
72 checkTargetClass(facesContext, uiComponent, value);
73
74
75
76 try {
77 return Enum.valueOf(targetClass, value);
78 } catch (IllegalArgumentException e) {
79 Object[] params = new Object[]{value,
80 firstConstantOfEnum(),
81 _MessageUtils.getLabel(facesContext, uiComponent)};
82
83 throw new ConverterException(_MessageUtils.getErrorMessage(facesContext,
84 ENUM_ID,
85 params));
86 }
87 }
88
89 private void checkTargetClass(FacesContext facesContext, UIComponent uiComponent, Object value) {
90 if (targetClass == null) {
91 Object[] params = new Object[]{value, _MessageUtils.getLabel(facesContext, uiComponent)};
92 throw new ConverterException(_MessageUtils.getErrorMessage(facesContext,
93 ENUM_NO_CLASS_ID,
94 params));
95 }
96 }
97
98
99 private String firstConstantOfEnum() {
100 Object[] enumConstants= targetClass.getEnumConstants();
101
102 if (enumConstants.length != 0 ) return enumConstants[0].toString();
103
104 return "";
105 }
106
107 public void restoreState(FacesContext context, Object state) {
108 targetClass = (Class)state;
109 }
110
111 public Object saveState(FacesContext context) {
112 return targetClass;
113 }
114
115 public void setTransient(boolean newTransientValue) {
116 isTransient = newTransientValue;
117 }
118
119 public boolean isTransient() {
120 return isTransient;
121 }
122
123 }