Coverage Report - org.dozer.propertydescriptor.XmlBeanPropertyDescriptor
 
Classes in this File Line Coverage Branch Coverage Complexity
XmlBeanPropertyDescriptor
94%
18/19
75%
6/8
2.143
 
 1  
 /**
 2  
  * Copyright 2005-2013 Dozer Project
 3  
  *
 4  
  * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
 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.dozer.propertydescriptor;
 17  
 
 18  
 /**
 19  
  * @author Stuntmunkee
 20  
  */
 21  
 import org.dozer.fieldmap.FieldMap;
 22  
 import org.dozer.fieldmap.HintContainer;
 23  
 
 24  
 public class XmlBeanPropertyDescriptor implements DozerPropertyDescriptor {
 25  
 
 26  
   private static final String IS_SET_PREFIX = "set";
 27  
 
 28  
   private static final String DOT = ".";
 29  
 
 30  
   private final JavaBeanPropertyDescriptor fieldPropertyDescriptor;
 31  
 
 32  
   private final JavaBeanPropertyDescriptor isSetFieldPropertyDescriptor;
 33  
 
 34  
   public XmlBeanPropertyDescriptor(Class<?> clazz, String fieldName, boolean isIndexed, int index,
 35  120
       HintContainer srcDeepIndexHintContainer, HintContainer destDeepIndexHintContainer) {
 36  
 
 37  120
     fieldPropertyDescriptor = new JavaBeanPropertyDescriptor(clazz, fieldName, isIndexed, index, srcDeepIndexHintContainer,
 38  
         destDeepIndexHintContainer);
 39  
 
 40  120
     isSetFieldPropertyDescriptor = new JavaBeanPropertyDescriptor(clazz, getIsSetFieldName(fieldName), isIndexed, index,
 41  
         srcDeepIndexHintContainer, destDeepIndexHintContainer);
 42  120
   }
 43  
 
 44  
   public Class<?> getPropertyType() {
 45  146
     return fieldPropertyDescriptor.getPropertyType();
 46  
   }
 47  
 
 48  
   public Object getPropertyValue(Object bean) {
 49  38
     return isFieldSet(bean) ? fieldPropertyDescriptor.getPropertyValue(bean) : null;
 50  
   }
 51  
 
 52  
   public void setPropertyValue(Object bean, Object value, FieldMap fieldMap) {
 53  18
     fieldPropertyDescriptor.setPropertyValue(bean, value, fieldMap);
 54  18
   }
 55  
 
 56  
   private boolean isFieldSet(Object bean) {
 57  
 
 58  
     try {
 59  38
       final Boolean isSetField = (Boolean) isSetFieldPropertyDescriptor.getPropertyValue(bean);
 60  18
       return isSetField.booleanValue();
 61  
 
 62  20
     } catch (Throwable e) {
 63  
       // The isSetField will not be present for all fields eg. Class.name -
 64  
       // that's ok.
 65  20
       return true;
 66  
     }
 67  
   }
 68  
 
 69  
   private String getIsSetFieldName(final String fieldName) {
 70  
 
 71  120
     if (fieldName == null) {
 72  0
       return null;
 73  
     }
 74  
 
 75  
     // Does the field use dot notation eg. parent.child.field
 76  120
     int lastDotIndex = fieldName.lastIndexOf(DOT);
 77  120
     if ((lastDotIndex < 0) || (lastDotIndex == (fieldName.length() - 1))) {
 78  
       // No just return the setfield field name
 79  84
       return IS_SET_PREFIX + fieldName;
 80  
     }
 81  
 
 82  
     // yes - return the parent.child.setfield field name
 83  36
     return fieldName.substring(0, lastDotIndex + 1) + IS_SET_PREFIX + fieldName.substring(lastDotIndex + 1);
 84  
   }
 85  
 
 86  
   public Class<?> genericType() {
 87  4
     return null;
 88  
   }
 89  
   
 90  
 }