-
-
Notifications
You must be signed in to change notification settings - Fork 220
/
XmlMapper.java
355 lines (308 loc) · 11.1 KB
/
XmlMapper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package com.fasterxml.jackson.dataformat.xml;
import java.io.IOException;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.ObjectMapper.DefaultTypeResolverBuilder;
import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping;
import com.fasterxml.jackson.databind.cfg.MapperBuilder;
import com.fasterxml.jackson.databind.jsontype.TypeResolverBuilder;
import com.fasterxml.jackson.dataformat.xml.deser.FromXmlParser;
import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;
import com.fasterxml.jackson.dataformat.xml.ser.XmlSerializerProvider;
import com.fasterxml.jackson.dataformat.xml.util.DefaultXmlPrettyPrinter;
import com.fasterxml.jackson.dataformat.xml.util.XmlRootNameLookup;
/**
* Customized {@link ObjectMapper} that will read and write XML instead of JSON,
* using XML-backed {@link com.fasterxml.jackson.core.JsonFactory}
* implementation ({@link XmlFactory}).
*<p>
* Mapper itself overrides some aspects of functionality to try to handle
* data binding aspects as similar to JAXB as possible.
*<p>
* Note that most of configuration should be done by pre-constructing
* {@link JacksonXmlModule} explicitly, instead of relying on default settings.
*/
public class XmlMapper extends ObjectMapper
{
// as of 2.6
private static final long serialVersionUID = 1L;
/**
* Builder implementation for constructing {@link XmlMapper} instances.
*
* @since 2.10
*/
public static class Builder extends MapperBuilder<XmlMapper, Builder>
{
public Builder(XmlMapper m) {
super(m);
}
public Builder enable(FromXmlParser.Feature... features) {
for (FromXmlParser.Feature f : features) {
_mapper.enable(f);
}
return this;
}
public Builder disable(FromXmlParser.Feature... features) {
for (FromXmlParser.Feature f : features) {
_mapper.disable(f);
}
return this;
}
public Builder configure(FromXmlParser.Feature feature, boolean state)
{
if (state) {
_mapper.enable(feature);
} else {
_mapper.disable(feature);
}
return this;
}
public Builder enable(ToXmlGenerator.Feature... features) {
for (ToXmlGenerator.Feature f : features) {
_mapper.enable(f);
}
return this;
}
public Builder disable(ToXmlGenerator.Feature... features) {
for (ToXmlGenerator.Feature f : features) {
_mapper.disable(f);
}
return this;
}
public Builder configure(ToXmlGenerator.Feature feature, boolean state)
{
if (state) {
_mapper.enable(feature);
} else {
_mapper.disable(feature);
}
return this;
}
public Builder nameForTextElement(String name) {
_mapper.setXMLTextElementName(name);
return this;
}
public Builder defaultUseWrapper(boolean state) {
_mapper.setDefaultUseWrapper(state);
return this;
}
}
protected final static JacksonXmlModule DEFAULT_XML_MODULE = new JacksonXmlModule();
protected final static DefaultXmlPrettyPrinter DEFAULT_XML_PRETTY_PRINTER = new DefaultXmlPrettyPrinter();
// need to hold on to module instance just in case copy() is used
protected final JacksonXmlModule _xmlModule;
/*
/**********************************************************
/* Life-cycle: construction, configuration
/**********************************************************
*/
public XmlMapper() {
this(new XmlFactory());
}
/**
* @since 2.4
*/
public XmlMapper(XMLInputFactory inputF, XMLOutputFactory outF) {
this(new XmlFactory(inputF, outF));
}
/**
* @since 2.4
*/
public XmlMapper(XMLInputFactory inputF) {
this(new XmlFactory(inputF));
}
public XmlMapper(XmlFactory xmlFactory) {
this(xmlFactory, DEFAULT_XML_MODULE);
}
public XmlMapper(JacksonXmlModule module) {
this(new XmlFactory(), module);
}
public XmlMapper(XmlFactory xmlFactory, JacksonXmlModule module)
{
/* Need to override serializer provider (due to root name handling);
* deserializer provider fine as is
*/
super(xmlFactory, new XmlSerializerProvider(new XmlRootNameLookup()), null);
_xmlModule = module;
// but all the rest is done via Module interface!
if (module != null) {
registerModule(module);
}
// 19-May-2015, tatu: Must ensure we use XML-specific indenter
_serializationConfig = _serializationConfig.withDefaultPrettyPrinter(DEFAULT_XML_PRETTY_PRINTER);
// 21-Jun-2017, tatu: Seems like there are many cases in XML where ability to coerce empty
// String into `null` (where it otherwise is an error) is very useful.
enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
}
/**
* @since 2.8.9
*/
protected XmlMapper(XmlMapper src) {
super(src);
_xmlModule = src._xmlModule;
}
@Override
public XmlMapper copy()
{
_checkInvalidCopy(XmlMapper.class);
return new XmlMapper(this);
}
/**
* @since 2.10
*/
public static XmlMapper.Builder xmlBuilder() {
return new XmlMapper.Builder(new XmlMapper());
}
/**
* @since 2.10
*/
public static XmlMapper.Builder builder() {
return new XmlMapper.Builder(new XmlMapper());
}
/**
* @since 2.10
*/
public static XmlMapper.Builder builder(XmlFactory streamFactory) {
return new XmlMapper.Builder(new XmlMapper(streamFactory));
}
@Override
public Version version() {
return PackageVersion.VERSION;
}
/*
/**********************************************************
/* Factory method overrides
/**********************************************************
*/
@Override
protected TypeResolverBuilder<?> _constructDefaultTypeResolverBuilder(DefaultTyping applicability) {
// return new DefaultTypeResolverBuilder(applicability);
// just a stub for now
return super._constructDefaultTypeResolverBuilder(applicability);
}
/*
/**********************************************************
/* Additional XML-specific configurations
/**********************************************************
*/
/**
* Method called by {@link JacksonXmlModule} to pass configuration
* information to {@link XmlFactory}, during registration; NOT
* exposed as public method since configuration should be done
* via {@link JacksonXmlModule}.
*
* @since 2.1
*
* @deprecated Since 2.10 use {@link Builder#nameForTextElement(String)} instead
*/
@Deprecated
protected void setXMLTextElementName(String name) {
((XmlFactory) _jsonFactory).setXMLTextElementName(name);
}
/**
* Since 2.7
*
* @deprecated Since 2.10 use {@link Builder#defaultUseWrapper(boolean)} instead
*/
@Deprecated
public XmlMapper setDefaultUseWrapper(boolean state) {
// ser and deser configs should usually have the same introspector, so:
AnnotationIntrospector ai0 = getDeserializationConfig().getAnnotationIntrospector();
for (AnnotationIntrospector ai : ai0.allIntrospectors()) {
if (ai instanceof XmlAnnotationIntrospector) {
((XmlAnnotationIntrospector) ai).setDefaultUseWrapper(state);
}
}
return this;
}
/*
/**********************************************************
/* Access to configuration settings
/**********************************************************
*/
@Override
public XmlFactory getFactory() {
return (XmlFactory) _jsonFactory;
}
public ObjectMapper configure(ToXmlGenerator.Feature f, boolean state) {
((XmlFactory)_jsonFactory).configure(f, state);
return this;
}
public ObjectMapper configure(FromXmlParser.Feature f, boolean state) {
((XmlFactory)_jsonFactory).configure(f, state);
return this;
}
public ObjectMapper enable(ToXmlGenerator.Feature f) {
((XmlFactory)_jsonFactory).enable(f);
return this;
}
public ObjectMapper enable(FromXmlParser.Feature f) {
((XmlFactory)_jsonFactory).enable(f);
return this;
}
public ObjectMapper disable(ToXmlGenerator.Feature f) {
((XmlFactory)_jsonFactory).disable(f);
return this;
}
public ObjectMapper disable(FromXmlParser.Feature f) {
((XmlFactory)_jsonFactory).disable(f);
return this;
}
/*
/**********************************************************
/* XML-specific access
/**********************************************************
*/
/**
* Method for reading a single XML value from given XML-specific input
* source; useful for incremental data-binding, combining traversal using
* basic Stax {@link XMLStreamReader} with data-binding by Jackson.
*
* @since 2.4
*/
public <T> T readValue(XMLStreamReader r, Class<T> valueType) throws IOException {
return readValue(r, _typeFactory.constructType(valueType));
}
/**
* Method for reading a single XML value from given XML-specific input
* source; useful for incremental data-binding, combining traversal using
* basic Stax {@link XMLStreamReader} with data-binding by Jackson.
*
* @since 2.4
*/
public <T> T readValue(XMLStreamReader r, TypeReference<T> valueTypeRef) throws IOException {
return readValue(r, _typeFactory.constructType(valueTypeRef));
}
/**
* Method for reading a single XML value from given XML-specific input
* source; useful for incremental data-binding, combining traversal using
* basic Stax {@link XMLStreamReader} with data-binding by Jackson.
*
* @since 2.4
*/
@SuppressWarnings("resource")
public <T> T readValue(XMLStreamReader r, JavaType valueType) throws IOException
{
FromXmlParser p = getFactory().createParser(r);
return super.readValue(p, valueType);
}
/**
* Method for serializing given value using specific {@link XMLStreamReader}:
* useful when building large XML files by binding individual items, one at
* a time.
*
* @since 2.4
*/
public void writeValue(XMLStreamWriter w0, Object value) throws IOException {
@SuppressWarnings("resource")
ToXmlGenerator g = getFactory().createGenerator(w0);
super.writeValue(g, value);
// NOTE: above call should do flush(); and we should NOT close here.
// Finally, 'g' has no buffers to release.
}
}