/
TSFBuilder.java
300 lines (241 loc) · 8.87 KB
/
TSFBuilder.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
package com.fasterxml.jackson.core;
import com.fasterxml.jackson.core.io.InputDecorator;
import com.fasterxml.jackson.core.io.OutputDecorator;
import com.fasterxml.jackson.core.json.JsonReadFeature;
import com.fasterxml.jackson.core.json.JsonWriteFeature;
/**
* Since 2.10, Builder class is offered for creating token stream factories
* with difference configurations: with 3.x they will be fully immutable.
*
* @since 2.10
*/
public abstract class TSFBuilder<F extends JsonFactory,
B extends TSFBuilder<F,B>>
{
/*
/**********************************************************************
/* Constants
/**********************************************************************
*/
/**
* Bitfield (set of flags) of all factory features that are enabled by default.
*/
protected final static int DEFAULT_FACTORY_FEATURE_FLAGS = JsonFactory.Feature.collectDefaults();
/**
* Bitfield (set of flags) of all parser features that are enabled
* by default.
*/
protected final static int DEFAULT_PARSER_FEATURE_FLAGS = JsonParser.Feature.collectDefaults();
/**
* Bitfield (set of flags) of all generator features that are enabled
* by default.
*/
protected final static int DEFAULT_GENERATOR_FEATURE_FLAGS = JsonGenerator.Feature.collectDefaults();
/*
/**********************************************************************
/* Configured features
/**********************************************************************
*/
/**
* Set of {@link TokenStreamFactory.Feature}s enabled, as bitmask.
*/
protected int _factoryFeatures;
/**
* Set of {@link JsonParser.Feature}s enabled, as bitmask.
*/
protected int _streamReadFeatures;
/**
* Set of {@link JsonGenerator.Feature}s enabled, as bitmask.
*/
protected int _streamWriteFeatures;
/*
/**********************************************************************
/* Other configuration
/**********************************************************************
*/
/**
* Optional helper object that may decorate input sources, to do
* additional processing on input during parsing.
*/
protected InputDecorator _inputDecorator;
/**
* Optional helper object that may decorate output object, to do
* additional processing on output during content generation.
*/
protected OutputDecorator _outputDecorator;
/*
/**********************************************************************
/* Construction
/**********************************************************************
*/
protected TSFBuilder() {
_factoryFeatures = DEFAULT_FACTORY_FEATURE_FLAGS;
_streamReadFeatures = DEFAULT_PARSER_FEATURE_FLAGS;
_streamWriteFeatures = DEFAULT_GENERATOR_FEATURE_FLAGS;
_inputDecorator = null;
_outputDecorator = null;
}
protected TSFBuilder(JsonFactory base)
{
this(base._factoryFeatures,
base._parserFeatures, base._generatorFeatures);
}
protected TSFBuilder(int factoryFeatures,
int parserFeatures, int generatorFeatures)
{
_factoryFeatures = factoryFeatures;
_streamReadFeatures = parserFeatures;
_streamWriteFeatures = generatorFeatures;
}
// // // Accessors
public int factoryFeaturesMask() { return _factoryFeatures; }
public int streamReadFeatures() { return _streamReadFeatures; }
public int streamWriteFeatures() { return _streamWriteFeatures; }
public InputDecorator inputDecorator() { return _inputDecorator; }
public OutputDecorator outputDecorator() { return _outputDecorator; }
// // // Factory features
public B enable(JsonFactory.Feature f) {
_factoryFeatures |= f.getMask();
return _this();
}
public B disable(JsonFactory.Feature f) {
_factoryFeatures &= ~f.getMask();
return _this();
}
public B configure(JsonFactory.Feature f, boolean state) {
return state ? enable(f) : disable(f);
}
// // // StreamReadFeatures (replacement of non-json-specific parser features)
public B enable(StreamReadFeature f) {
_streamReadFeatures |= f.mappedFeature().getMask();
return _this();
}
public B enable(StreamReadFeature first, StreamReadFeature... other) {
_streamReadFeatures |= first.mappedFeature().getMask();
for (StreamReadFeature f : other) {
_streamReadFeatures |= f.mappedFeature().getMask();
}
return _this();
}
public B disable(StreamReadFeature f) {
_streamReadFeatures &= ~f.mappedFeature().getMask();
return _this();
}
public B disable(StreamReadFeature first, StreamReadFeature... other) {
_streamReadFeatures &= ~first.mappedFeature().getMask();
for (StreamReadFeature f : other) {
_streamReadFeatures &= ~f.mappedFeature().getMask();
}
return _this();
}
public B configure(StreamReadFeature f, boolean state) {
return state ? enable(f) : disable(f);
}
// // // StreamWriteFeatures (replacement of non-json-specific generator features)
public B enable(StreamWriteFeature f) {
_streamWriteFeatures |= f.mappedFeature().getMask();
return _this();
}
public B enable(StreamWriteFeature first, StreamWriteFeature... other) {
_streamWriteFeatures |= first.mappedFeature().getMask();
for (StreamWriteFeature f : other) {
_streamWriteFeatures |= f.mappedFeature().getMask();
}
return _this();
}
public B disable(StreamWriteFeature f) {
_streamWriteFeatures &= ~f.mappedFeature().getMask();
return _this();
}
public B disable(StreamWriteFeature first, StreamWriteFeature... other) {
_streamWriteFeatures &= ~first.mappedFeature().getMask();
for (StreamWriteFeature f : other) {
_streamWriteFeatures &= ~f.mappedFeature().getMask();
}
return _this();
}
public B configure(StreamWriteFeature f, boolean state) {
return state ? enable(f) : disable(f);
}
/* 26-Jun-2018, tatu: This should not be needed here, but due to 2.x limitations,
* we do need to include it or require casting.
* Specifically: since `JsonFactory` (and not `TokenStreamFactory`) is base class
* for all backends, it can not expose JSON-specific builder, but this.
* So let's select lesser evil(s).
*/
// // // JSON-specific, reads
public B enable(JsonReadFeature f) {
return _failNonJSON(f);
}
public B enable(JsonReadFeature first, JsonReadFeature... other) {
return _failNonJSON(first);
}
public B disable(JsonReadFeature f) {
return _failNonJSON(f);
}
public B disable(JsonReadFeature first, JsonReadFeature... other) {
return _failNonJSON(first);
}
public B configure(JsonReadFeature f, boolean state) {
return _failNonJSON(f);
}
private B _failNonJSON(Object feature) {
throw new IllegalArgumentException("Feature "+feature.getClass().getName()
+"#"+feature.toString()+" not supported for non-JSON backend");
}
// // // JSON-specific, writes
public B enable(JsonWriteFeature f) {
return _failNonJSON(f);
}
public B enable(JsonWriteFeature first, JsonWriteFeature... other) {
return _failNonJSON(first);
}
public B disable(JsonWriteFeature f) {
return _failNonJSON(f);
}
public B disable(JsonWriteFeature first, JsonWriteFeature... other) {
return _failNonJSON(first);
}
public B configure(JsonWriteFeature f, boolean state) {
return _failNonJSON(f);
}
// // // Other configuration
public B inputDecorator(InputDecorator dec) {
_inputDecorator = dec;
return _this();
}
public B outputDecorator(OutputDecorator dec) {
_outputDecorator = dec;
return _this();
}
// // // Other methods
/**
* Method for constructing actual {@link TokenStreamFactory} instance, given
* configuration.
*/
public abstract F build();
// silly convenience cast method we need
@SuppressWarnings("unchecked")
protected final B _this() { return (B) this; }
// // // Support for subtypes
protected void _legacyEnable(JsonParser.Feature f) {
if (f != null) {
_streamReadFeatures |= f.getMask();
}
}
protected void _legacyDisable(JsonParser.Feature f) {
if (f != null) {
_streamReadFeatures &= ~f.getMask();
}
}
protected void _legacyEnable(JsonGenerator.Feature f) {
if (f != null) {
_streamWriteFeatures |= f.getMask();
}
}
protected void _legacyDisable(JsonGenerator.Feature f) {
if (f != null) {
_streamWriteFeatures &= ~f.getMask();
}
}
}