Coverage Report - javax.faces.convert.EnumConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
EnumConverter
0%
0/37
0%
0/24
0
 
 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  0
     private boolean isTransient = false;
 43  
     
 44  
     /** Creates a new instance of EnumConverter */
 45  0
     public EnumConverter() {
 46  0
     }
 47  
     
 48  0
     public EnumConverter(Class targetClass) {
 49  0
         if (!targetClass.isEnum()) throw new IllegalArgumentException("targetClass for EnumConverter must be an Enum");
 50  0
         this.targetClass = targetClass;
 51  0
     }
 52  
 
 53  
     public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value) throws ConverterException {
 54  0
         if (facesContext == null) throw new NullPointerException("facesContext can not be null");
 55  0
         if (uiComponent == null) throw new NullPointerException("uiComponent can not be null");
 56  0
         if (value == null) return "";
 57  0
         checkTargetClass(facesContext, uiComponent, value);
 58  
         
 59  0
         for (Object enumConstant : targetClass.getEnumConstants()) {
 60  0
             if (enumConstant == value) return enumConstant.toString();
 61  
         }
 62  
 
 63  0
         return value.toString();
 64  
     }
 65  
 
 66  
     public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value) throws ConverterException {
 67  0
         if (facesContext == null) throw new NullPointerException("facesContext");
 68  0
         if (uiComponent == null) throw new NullPointerException("uiComponent");
 69  0
         if (value == null)  return null;
 70  0
         value = value.trim();
 71  0
         if (value.length() == 0)  return null;
 72  0
         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  0
            return Enum.valueOf(targetClass, value);    
 78  0
         } catch (IllegalArgumentException e) {
 79  0
             Object[] params = new Object[]{value, 
 80  
                                            firstConstantOfEnum(), 
 81  
                                            _MessageUtils.getLabel(facesContext, uiComponent)};
 82  
             
 83  0
             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  0
         if (targetClass == null) {
 91  0
             Object[] params = new Object[]{value, _MessageUtils.getLabel(facesContext, uiComponent)};
 92  0
             throw new ConverterException(_MessageUtils.getErrorMessage(facesContext, 
 93  
                                                                        ENUM_NO_CLASS_ID, 
 94  
                                                                        params));
 95  
         }
 96  0
     }
 97  
 
 98  
     // find the first constant value of the targetClass and return as a String
 99  
     private String firstConstantOfEnum() {
 100  0
         Object[] enumConstants= targetClass.getEnumConstants();
 101  
 
 102  0
         if (enumConstants.length != 0 ) return enumConstants[0].toString();
 103  
         
 104  0
         return ""; // if empty Enum
 105  
     }
 106  
 
 107  
     public void restoreState(FacesContext context, Object state) {
 108  0
         targetClass = (Class)state;
 109  0
     }
 110  
 
 111  
     public Object saveState(FacesContext context) {
 112  0
         return targetClass;
 113  
     }
 114  
 
 115  
     public void setTransient(boolean newTransientValue) {
 116  0
         isTransient = newTransientValue;
 117  0
     }
 118  
 
 119  
     public boolean isTransient() {
 120  0
         return isTransient;
 121  
     }
 122  
     
 123  
 }