View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * see Javadoc of <a href="http://java.sun.com/j2ee/javaserverfaces/1.2/docs/api/index.html">JSF Specification</a>
30   *
31   * @author Stan Silvert
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      /** Creates a new instance of EnumConverter */
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          // we know targetClass and value can't be null, so we can use Enum.valueOf
75          // instead of the hokey looping called for in the javadoc
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      // find the first constant value of the targetClass and return as a String
99      private String firstConstantOfEnum() {
100         Object[] enumConstants= targetClass.getEnumConstants();
101 
102         if (enumConstants.length != 0 ) return enumConstants[0].toString();
103         
104         return ""; // if empty Enum
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 }