Coverage Report - org.kuali.rice.core.api.util.jaxb.EnumStringAdapter
 
Classes in this File Line Coverage Branch Coverage Complexity
EnumStringAdapter
0%
0/21
0%
0/10
3.75
 
 1  
 /**
 2  
  * Copyright 2005-2011 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.api.util.jaxb;
 17  
 
 18  
 import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
 19  
 import org.kuali.rice.core.api.mo.common.Coded;
 20  
 
 21  
 import javax.xml.bind.annotation.adapters.XmlAdapter;
 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  0
 public abstract class EnumStringAdapter<T extends Enum<T>> extends XmlAdapter<String, String> {        
 47  
                 
 48  0
         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  0
                 return processValue(value, false);
 53  
         }
 54  
 
 55  
         @Override
 56  
         public String unmarshal(String value) throws Exception {
 57  0
                 return processValue(value, true);
 58  
         }
 59  
         
 60  
         private String processValue(String value, boolean unmarshal) throws Exception {
 61  0
                 if (value == null) {
 62  0
                         return null;
 63  
                 }
 64  0
                 Class<T> enumClass = getEnumClass();
 65  0
                 if (Coded.class.isAssignableFrom(enumClass)) {
 66  0
                         T[] enumConstants = enumClass.getEnumConstants();
 67  0
                         for (T enumConstant : enumConstants) {
 68  0
                                 Coded codedEnumConstant = (Coded)enumConstant;
 69  0
                                 if (codedEnumConstant.getCode().equals(value)) {
 70  
                                         // value is good, return value
 71  0
                                         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  
 }