1 package com.fasterxml.jackson.dataformat.xml.deser;
2
3 import java.util.*;
4
5 import com.fasterxml.jackson.databind.*;
6 import com.fasterxml.jackson.databind.deser.*;
7 import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
8 import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
9 import com.fasterxml.jackson.dataformat.xml.util.AnnotationUtil;
10
11
15 public class XmlBeanDeserializerModifier
16 extends BeanDeserializerModifier
17 implements java.io.Serializable
18 {
19 private static final long serialVersionUID = 1L;
20
21
24 protected String _cfgNameForTextValue = "";
25
26 public XmlBeanDeserializerModifier(String nameForTextValue)
27 {
28 _cfgNameForTextValue = nameForTextValue;
29 }
30
31 @Override
32 public List<BeanPropertyDefinition> updateProperties(DeserializationConfig config,
33 BeanDescription beanDesc, List<BeanPropertyDefinition> propDefs)
34 {
35 final AnnotationIntrospector intr = config.getAnnotationIntrospector();
36 int changed = 0;
37
38 for (int i = 0, propCount = propDefs.size(); i < propCount; ++i) {
39 BeanPropertyDefinition prop = propDefs.get(i);
40 AnnotatedMember acc = prop.getPrimaryMember();
41
42 if (acc == null) {
43 continue;
44 }
45
50 Boolean b = AnnotationUtil.findIsTextAnnotation(intr, acc);
51 if (b != null && b.booleanValue()) {
52
53 BeanPropertyDefinition newProp = prop.withSimpleName(_cfgNameForTextValue);
54 if (newProp != prop) {
55 propDefs.set(i, newProp);
56 }
57 continue;
58 }
59
60 PropertyName wrapperName = prop.getWrapperName();
61
62 if (wrapperName != null && wrapperName != PropertyName.NO_NAME) {
63 String localName = wrapperName.getSimpleName();
64 if ((localName != null && localName.length() > 0)
65 && !localName.equals(prop.getName())) {
66
67 if (changed == 0) {
68 propDefs = new ArrayList<BeanPropertyDefinition>(propDefs);
69 }
70 ++changed;
71 propDefs.set(i, prop.withSimpleName(localName));
72 continue;
73 }
74
75 }
76 }
77 return propDefs;
78 }
79
80 @Override
81 public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config,
82 BeanDescription beanDesc, JsonDeserializer<?> deser0)
83 {
84 if (!(deser0 instanceof BeanDeserializerBase)) {
85 return deser0;
86 }
87
92 BeanDeserializerBase deser = (BeanDeserializerBase) deser0;
93
94
95
96 ValueInstantiator inst = deser.getValueInstantiator();
97
98
99
100
101 if (!inst.canCreateFromString()) {
102 SettableBeanProperty textProp = _findSoleTextProp(config, deser.properties());
103 if (textProp != null) {
104 return new XmlTextDeserializer(deser, textProp);
105 }
106 }
107 return new WrapperHandlingDeserializer(deser);
108 }
109
110 private SettableBeanProperty _findSoleTextProp(DeserializationConfig config,
111 Iterator<SettableBeanProperty> propIt)
112 {
113 final AnnotationIntrospector ai = config.getAnnotationIntrospector();
114 SettableBeanProperty textProp = null;
115 while (propIt.hasNext()) {
116 SettableBeanProperty prop = propIt.next();
117 AnnotatedMember m = prop.getMember();
118 if (m != null) {
119
120 PropertyName n = prop.getFullName();
121 if (_cfgNameForTextValue.equals(n.getSimpleName())) {
122
123 textProp = prop;
124 continue;
125 }
126
127 Boolean b = AnnotationUtil.findIsAttributeAnnotation(ai, m);
128 if (b != null && b.booleanValue()) {
129 continue;
130 }
131 }
132
133 return null;
134 }
135 return textProp;
136 }
137 }
138