Coverage Report - org.kuali.rice.core.util.jaxb.EnumStringAdapter
 
Classes in this File Line Coverage Branch Coverage Complexity
EnumStringAdapter
57%
12/21
40%
4/10
3.75
 
 1  
 /*
 2  
  * Copyright 2007-2009 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.core.util.jaxb;
 17  
 
 18  
 import javax.xml.bind.annotation.adapters.XmlAdapter;
 19  
 
 20  
 import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
 21  
 import org.kuali.rice.core.api.mo.common.Coded;
 22  
 
 23  
 /**
 24  
  * An abstract base class for use when marshaling enumeration values to and from XML.
 25  
  * This allows these values to be handled as plain strings (as opposed to xs:enumeration)
 26  
  * in the generated schema definitions.  This improves compatibility by allows for new
 27  
  * enumeration values to be added without breaking the schema.
 28  
  * 
 29  
  * <p>Subclasses need to indicate the concrete type of the Enum being adapted using the
 30  
  * {@link #getEnumClass()} method.
 31  
  * 
 32  
  * <p>If the enum implements the {@link Coded} interface, then the actual string value
 33  
  * that is produced by the adapter will be the result of the {@link Coded#getCode()}
 34  
  * method.  Otherwise the {@link Enum#name()} value is used.
 35  
  * 
 36  
  * <p>In situations where a string value is being requested to be unmarshalled to an
 37  
  * enum and the enum does not understand the value, then this adapter will unmarshal
 38  
  * the value to {@code null}.  This could happen in situations where a newer version
 39  
  * of an endpoint (which has added additional items to the enumeration) sends a message
 40  
  * to an older version client of the service.  In these cases, that older client will
 41  
  * not be aware of the new enumeration values and thus cannot effectively translate them.
 42  
  * 
 43  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 44  
  *
 45  
  */
 46  5
 public abstract class EnumStringAdapter<T extends Enum<T>> extends XmlAdapter<String, String> {        
 47  
                 
 48  1
         private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(EnumStringAdapter.class);
 49  
         
 50  
         @Override
 51  
         public String marshal(String value) throws Exception {
 52  1
                 return processValue(value, false);
 53  
         }
 54  
 
 55  
         @Override
 56  
         public String unmarshal(String value) throws Exception {
 57  2
                 return processValue(value, true);
 58  
         }
 59  
         
 60  
         private String processValue(String value, boolean unmarshal) throws Exception {
 61  3
                 if (value == null) {
 62  0
                         return null;
 63  
                 }
 64  3
                 Class<T> enumClass = getEnumClass();
 65  3
                 if (Coded.class.isAssignableFrom(enumClass)) {
 66  3
                         T[] enumConstants = enumClass.getEnumConstants();
 67  3
                         for (T enumConstant : enumConstants) {
 68  3
                                 Coded codedEnumConstant = (Coded)enumConstant;
 69  3
                                 if (codedEnumConstant.getCode().equals(value)) {
 70  
                                         // value is good, return value
 71  3
                                         return value;
 72  
                                 }
 73  
                         }
 74  0
                 } else {
 75  
                         try {
 76  0
                                 Enum.valueOf(enumClass, value);
 77  0
                                 return value;
 78  0
                         } catch (IllegalArgumentException e) {
 79  
                                 // failed to unmarshal, will fall through to null return below...
 80  
                         }
 81  
                 }
 82  0
                 if (unmarshal) {
 83  0
                         LOG.warn("Failed to unmarshal enumeration value '" + value + "' for enum type: " + enumClass.getName());
 84  0
                         return null;
 85  
                 } else {
 86  0
                         throw new RiceIllegalArgumentException("Failed to marshal enumeration value '" + value + "' for enum type: " + getEnumClass().getName());
 87  
                 }
 88  
         }
 89  
         
 90  
         protected abstract Class<T> getEnumClass();
 91  
         
 92  
 }