| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.dozer.propertydescriptor; |
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 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 | |
|
| 64 | |
|
| 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 | |
|
| 76 | 120 | int lastDotIndex = fieldName.lastIndexOf(DOT); |
| 77 | 120 | if ((lastDotIndex < 0) || (lastDotIndex == (fieldName.length() - 1))) { |
| 78 | |
|
| 79 | 84 | return IS_SET_PREFIX + fieldName; |
| 80 | |
} |
| 81 | |
|
| 82 | |
|
| 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 | |
} |