Coverage Report - javax.faces.convert.EnumConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
EnumConverter
0%
0/62
0%
0/32
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.PartialStateHolder;
 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  
 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFWebConfigParam;
 28  
 
 29  
 /**
 30  
  * see Javadoc of <a href="http://java.sun.com/j2ee/javaserverfaces/1.2/docs/api/index.html">JSF Specification</a>
 31  
  * 
 32  
  * @author Stan Silvert
 33  
  */
 34  
 @JSFConverter
 35  
 public class EnumConverter implements Converter, PartialStateHolder
 36  
 {
 37  
 
 38  
     public static final String CONVERTER_ID = "javax.faces.Enum";
 39  
     public static final String ENUM_ID = "javax.faces.converter.EnumConverter.ENUM";
 40  
     public static final String ENUM_NO_CLASS_ID = "javax.faces.converter.EnumConverter.ENUM_NO_CLASS";
 41  
 
 42  
     /**
 43  
      * If value is a String instance and this param is true, pass it directly without try any change.
 44  
      */
 45  
     @JSFWebConfigParam(name="org.apache.myfaces.ENUM_CONVERTER_ALLOW_STRING_PASSTROUGH", since="2.0.1", expectedValues="true,false",defaultValue="false")
 46  
     private static final String ALLOW_STRING_PASSTROUGH = "org.apache.myfaces.ENUM_CONVERTER_ALLOW_STRING_PASSTROUGH";
 47  
     
 48  
     // TODO: Find a valid generic usage -= Simon Lessard =-
 49  
     private Class targetClass;
 50  
 
 51  0
     private boolean isTransient = false;
 52  
 
 53  
     /** Creates a new instance of EnumConverter */
 54  
     public EnumConverter()
 55  0
     {
 56  0
     }
 57  
 
 58  
     public EnumConverter(Class targetClass)
 59  0
     {
 60  0
         if (!targetClass.isEnum())
 61  0
             throw new IllegalArgumentException("targetClass for EnumConverter must be an Enum");
 62  0
         this.targetClass = targetClass;
 63  0
     }
 64  
 
 65  
     public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value)
 66  
         throws ConverterException
 67  
     {
 68  0
         if (facesContext == null)
 69  0
             throw new NullPointerException("facesContext can not be null");
 70  0
         if (uiComponent == null)
 71  0
             throw new NullPointerException("uiComponent can not be null");
 72  
 
 73  0
         checkTargetClass(facesContext, uiComponent, value);
 74  
 
 75  0
         if (value == null)
 76  0
             return null;
 77  
         
 78  0
         if (value instanceof String
 79  
                 && _isPassThroughStringValues(facesContext))
 80  
         {
 81  
             // pass through the String value
 82  0
             return (String) value;
 83  
         }
 84  
 
 85  
         // check if the value is an instance of the enum class
 86  0
         if (targetClass.isInstance(value))
 87  
         {
 88  0
             return ((Enum<?>) value).name();
 89  
         }
 90  
         
 91  0
         Object[] params =
 92  
             new Object[] { value, firstConstantOfEnum(), _MessageUtils.getLabel(facesContext, uiComponent) };
 93  
 
 94  0
         throw new ConverterException(_MessageUtils.getErrorMessage(facesContext, ENUM_ID, params));
 95  
     }
 96  
 
 97  
     public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value)
 98  
         throws ConverterException
 99  
     {
 100  0
         if (facesContext == null)
 101  0
             throw new NullPointerException("facesContext");
 102  0
         if (uiComponent == null)
 103  0
             throw new NullPointerException("uiComponent");
 104  0
         if (value == null)
 105  0
             return null;
 106  0
         value = value.trim();
 107  0
         if (value.length() == 0)
 108  0
             return null;
 109  0
         checkTargetClass(facesContext, uiComponent, value);
 110  
 
 111  
         // we know targetClass and value can't be null, so we can use Enum.valueOf
 112  
         // instead of the hokey looping called for in the javadoc
 113  
         try
 114  
         {
 115  0
             return Enum.valueOf(targetClass, value);
 116  
         }
 117  0
         catch (IllegalArgumentException e)
 118  
         {
 119  0
             Object[] params =
 120  
                     new Object[] { value, firstConstantOfEnum(), _MessageUtils.getLabel(facesContext, uiComponent) };
 121  
 
 122  0
             throw new ConverterException(_MessageUtils.getErrorMessage(facesContext, ENUM_ID, params));
 123  
         }
 124  
     }
 125  
 
 126  
     private void checkTargetClass(FacesContext facesContext, UIComponent uiComponent, Object value)
 127  
     {
 128  0
         if (targetClass == null)
 129  
         {
 130  0
             Object[] params = new Object[] { value, _MessageUtils.getLabel(facesContext, uiComponent) };
 131  0
             throw new ConverterException(_MessageUtils.getErrorMessage(facesContext, ENUM_NO_CLASS_ID, params));
 132  
         }
 133  0
     }
 134  
 
 135  
     // find the first constant value of the targetClass and return as a String
 136  
     private String firstConstantOfEnum()
 137  
     {
 138  0
         Object[] enumConstants = targetClass.getEnumConstants();
 139  
 
 140  0
         if (enumConstants.length != 0)
 141  0
             return enumConstants[0].toString();
 142  
 
 143  0
         return ""; // if empty Enum
 144  
     }
 145  
 
 146  
     public void restoreState(FacesContext context, Object state)
 147  
     {
 148  0
         if (state != null)
 149  
         {
 150  0
             targetClass = (Class<?>)state;
 151  
         }
 152  0
     }
 153  
 
 154  
     public Object saveState(FacesContext context)
 155  
     {
 156  0
         if (!initialStateMarked())
 157  
         {
 158  0
             return targetClass;
 159  
         }
 160  0
         return null;
 161  
     }
 162  
 
 163  
     public void setTransient(boolean newTransientValue)
 164  
     {
 165  0
         isTransient = newTransientValue;
 166  0
     }
 167  
 
 168  
     public boolean isTransient()
 169  
     {
 170  0
         return isTransient;
 171  
     }
 172  
     
 173  0
     private boolean _initialStateMarked = false;
 174  
 
 175  
     public void clearInitialState()
 176  
     {
 177  0
         _initialStateMarked = false;
 178  0
     }
 179  
 
 180  
     public boolean initialStateMarked()
 181  
     {
 182  0
         return _initialStateMarked;
 183  
     }
 184  
 
 185  
     public void markInitialState()
 186  
     {
 187  0
         _initialStateMarked = true;
 188  0
     }
 189  
     
 190  
     private boolean _isPassThroughStringValues(FacesContext facesContext)
 191  
     {
 192  0
         String param = facesContext.getExternalContext().getInitParameter(ALLOW_STRING_PASSTROUGH);
 193  0
         if (param != null)
 194  
         {
 195  0
             return param.trim().equalsIgnoreCase("true");
 196  
         }
 197  
         // default: false
 198  0
         return false;
 199  
     }
 200  
 
 201  
 }