package com.fasterxml.jackson.dataformat.xml.deser;
import java.util.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.deser.*;
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
import com.fasterxml.jackson.dataformat.xml.util.AnnotationUtil;
public class XmlBeanDeserializerModifier
extends BeanDeserializerModifier
implements java.io.Serializable
{
private static final long serialVersionUID = 1L;
protected String _cfgNameForTextValue = "";
public XmlBeanDeserializerModifier(String nameForTextValue)
{
_cfgNameForTextValue = nameForTextValue;
}
@Override
public List<BeanPropertyDefinition> updateProperties(DeserializationConfig config,
BeanDescription beanDesc, List<BeanPropertyDefinition> propDefs)
{
final AnnotationIntrospector intr = config.getAnnotationIntrospector();
int changed = 0;
for (int i = 0, propCount = propDefs.size(); i < propCount; ++i) {
BeanPropertyDefinition prop = propDefs.get(i);
AnnotatedMember acc = prop.getPrimaryMember();
if (acc == null) {
continue;
}
Boolean b = AnnotationUtil.findIsTextAnnotation(intr, acc);
if (b != null && b.booleanValue()) {
BeanPropertyDefinition newProp = prop.withSimpleName(_cfgNameForTextValue);
if (newProp != prop) {
propDefs.set(i, newProp);
}
continue;
}
PropertyName wrapperName = prop.getWrapperName();
if (wrapperName != null && wrapperName != PropertyName.NO_NAME) {
String localName = wrapperName.getSimpleName();
if ((localName != null && localName.length() > 0)
&& !localName.equals(prop.getName())) {
if (changed == 0) {
propDefs = new ArrayList<BeanPropertyDefinition>(propDefs);
}
++changed;
propDefs.set(i, prop.withSimpleName(localName));
continue;
}
}
}
return propDefs;
}
@Override
public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config,
BeanDescription beanDesc, JsonDeserializer<?> deser0)
{
if (!(deser0 instanceof BeanDeserializerBase)) {
return deser0;
}
BeanDeserializerBase deser = (BeanDeserializerBase) deser0;
ValueInstantiator inst = deser.getValueInstantiator();
if (!inst.canCreateFromString()) {
SettableBeanProperty textProp = _findSoleTextProp(config, deser.properties());
if (textProp != null) {
return new XmlTextDeserializer(deser, textProp);
}
}
return new WrapperHandlingDeserializer(deser);
}
private SettableBeanProperty _findSoleTextProp(DeserializationConfig config,
Iterator<SettableBeanProperty> propIt)
{
final AnnotationIntrospector ai = config.getAnnotationIntrospector();
SettableBeanProperty textProp = null;
while (propIt.hasNext()) {
SettableBeanProperty prop = propIt.next();
AnnotatedMember m = prop.getMember();
if (m != null) {
PropertyName n = prop.getFullName();
if (_cfgNameForTextValue.equals(n.getSimpleName())) {
textProp = prop;
continue;
}
Boolean b = AnnotationUtil.findIsAttributeAnnotation(ai, m);
if (b != null && b.booleanValue()) {
continue;
}
}
return null;
}
return textProp;
}
}