Skip to content

Commit 4df60a4

Browse files
committed
2.13 compatibility
1 parent d984c0e commit 4df60a4

File tree

15 files changed

+355
-380
lines changed

15 files changed

+355
-380
lines changed

toml/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<parent>
1010
<groupId>com.fasterxml.jackson.dataformat</groupId>
1111
<artifactId>jackson-dataformats-text</artifactId>
12-
<version>3.0.0-SNAPSHOT</version>
12+
<version>2.13.0-SNAPSHOT</version>
1313
</parent>
1414
<artifactId>jackson-dataformat-toml</artifactId>
1515
<packaging>bundle</packaging>

toml/src/main/java/com/fasterxml/jackson/dataformat/toml/JacksonTomlParseException.java

+13
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.fasterxml.jackson.core.JsonParser;
55
import com.fasterxml.jackson.core.exc.StreamReadException;
66
import com.fasterxml.jackson.core.io.ContentReference;
7+
import com.fasterxml.jackson.core.util.RequestPayload;
78

89
public class JacksonTomlParseException extends StreamReadException {
910
private JacksonTomlParseException(JsonParser p, String msg, JsonLocation loc) {
@@ -14,6 +15,18 @@ private JacksonTomlParseException(String msg, JsonLocation loc, Throwable rootCa
1415
super(msg, loc, rootCause);
1516
}
1617

18+
@Override
19+
public JacksonTomlParseException withParser(JsonParser p) {
20+
this._processor = p;
21+
return this;
22+
}
23+
24+
@Override
25+
public JacksonTomlParseException withRequestPayload(RequestPayload p) {
26+
this._requestPayload = p;
27+
return this;
28+
}
29+
1730
static class ErrorContext {
1831
private final ContentReference ContentReference;
1932
private final JsonParser parser;

toml/src/main/java/com/fasterxml/jackson/dataformat/toml/Parser.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static ObjectNode parse(JacksonTomlParseException.ErrorContext errorConte
3737
return new Parser(errorContext, options, reader).parse();
3838
}
3939

40-
private TomlToken peek() {
40+
private TomlToken peek() throws JacksonTomlParseException {
4141
TomlToken here = this.next;
4242
if (here == null) throw errorContext.atPosition(lexer).generic("Premature end of file");
4343
return here;
@@ -382,7 +382,7 @@ private void parseKeyVal(TomlObjectNode target, int nextState) throws IOExceptio
382382
fieldRef.object.set(fieldRef.key, value);
383383
}
384384

385-
private TomlObjectNode getOrCreateObject(ObjectNode node, String field) {
385+
private TomlObjectNode getOrCreateObject(ObjectNode node, String field) throws JacksonTomlParseException {
386386
JsonNode existing = node.get(field);
387387
if (existing == null) {
388388
return (TomlObjectNode) node.putObject(field);
@@ -393,7 +393,7 @@ private TomlObjectNode getOrCreateObject(ObjectNode node, String field) {
393393
}
394394
}
395395

396-
private TomlArrayNode getOrCreateArray(ObjectNode node, String field) {
396+
private TomlArrayNode getOrCreateArray(ObjectNode node, String field) throws JacksonTomlParseException {
397397
JsonNode existing = node.get(field);
398398
if (existing == null) {
399399
return (TomlArrayNode) node.putArray(field);

toml/src/main/java/com/fasterxml/jackson/dataformat/toml/TomlFactory.java

+111-57
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,32 @@
11
package com.fasterxml.jackson.dataformat.toml;
22

3-
import com.fasterxml.jackson.core.*;
4-
import com.fasterxml.jackson.core.base.TextualTSFactory;
3+
import com.fasterxml.jackson.core.FormatFeature;
4+
import com.fasterxml.jackson.core.FormatSchema;
5+
import com.fasterxml.jackson.core.JacksonException;
6+
import com.fasterxml.jackson.core.JsonFactory;
7+
import com.fasterxml.jackson.core.JsonGenerator;
8+
import com.fasterxml.jackson.core.JsonParseException;
9+
import com.fasterxml.jackson.core.JsonParser;
10+
import com.fasterxml.jackson.core.ObjectCodec;
11+
import com.fasterxml.jackson.core.StreamReadFeature;
12+
import com.fasterxml.jackson.core.Version;
13+
import com.fasterxml.jackson.core.format.InputAccessor;
14+
import com.fasterxml.jackson.core.format.MatchStrength;
515
import com.fasterxml.jackson.core.io.IOContext;
616
import com.fasterxml.jackson.core.io.UTF8Writer;
717
import com.fasterxml.jackson.databind.node.ObjectNode;
818
import com.fasterxml.jackson.databind.node.TreeTraversingParser;
9-
10-
import java.io.*;
19+
import java.io.ByteArrayInputStream;
20+
import java.io.CharArrayReader;
21+
import java.io.IOException;
22+
import java.io.InputStream;
23+
import java.io.InputStreamReader;
24+
import java.io.OutputStream;
25+
import java.io.Reader;
26+
import java.io.Writer;
1127
import java.nio.charset.StandardCharsets;
1228

13-
public final class TomlFactory extends TextualTSFactory {
29+
public final class TomlFactory extends JsonFactory {
1430

1531
public final static String FORMAT_NAME_TOML = "toml";
1632

@@ -26,18 +42,28 @@ public final class TomlFactory extends TextualTSFactory {
2642
*/
2743
final static int DEFAULT_TOML_GENERATOR_FEATURE_FLAGS = 0;
2844

45+
/*
46+
/**********************************************************************
47+
/* Configuration
48+
/**********************************************************************
49+
*/
50+
51+
protected int _tomlParserFeatures = DEFAULT_TOML_PARSER_FEATURE_FLAGS;
52+
protected int _tomlGeneratorFeatures = DEFAULT_TOML_GENERATOR_FEATURE_FLAGS;
53+
2954
/*
3055
/**********************************************************************
3156
/* Factory construction, configuration
3257
/**********************************************************************
3358
*/
3459

3560
public TomlFactory() {
36-
super(DEFAULT_TOML_PARSER_FEATURE_FLAGS, DEFAULT_TOML_GENERATOR_FEATURE_FLAGS);
3761
}
3862

39-
TomlFactory(TomlFactory src) {
40-
super(src);
63+
TomlFactory(TomlFactory src, ObjectCodec oc) {
64+
super(src, oc);
65+
_tomlGeneratorFeatures = src._tomlGeneratorFeatures;
66+
_tomlParserFeatures = src._tomlParserFeatures;
4167
}
4268

4369
/**
@@ -46,7 +72,9 @@ public TomlFactory() {
4672
* @since 3.0
4773
*/
4874
TomlFactory(TomlFactoryBuilder b) {
49-
super(b);
75+
super(b, false);
76+
_tomlGeneratorFeatures = b._formatGeneratorFeatures;
77+
_tomlParserFeatures = b._formatParserFeatures;
5078
}
5179

5280
@Override
@@ -64,15 +92,8 @@ public static TomlFactoryBuilder builder() {
6492

6593
@Override
6694
public TomlFactory copy() {
67-
return new TomlFactory(this);
68-
}
69-
70-
/**
71-
* Instances are immutable so just return `this`
72-
*/
73-
@Override
74-
public TokenStreamFactory snapshot() {
75-
return this;
95+
_checkInvalidCopy(TomlFactory.class);
96+
return new TomlFactory(this, null);
7697
}
7798

7899
/*
@@ -128,13 +149,60 @@ public Class<? extends FormatFeature> getFormatWriteFeatureType() {
128149
}
129150

130151
@Override
131-
public int getFormatReadFeatures() {
132-
return _formatReadFeatures;
152+
public MatchStrength hasFormat(InputAccessor acc) throws IOException {
153+
return MatchStrength.INCONCLUSIVE;
154+
}
155+
156+
157+
158+
/*
159+
/**********************************************************
160+
/* Configuration, parser settings
161+
/**********************************************************
162+
*/
163+
164+
/**
165+
* Method for enabling or disabling specified parser feature
166+
* (check {@link TomlReadFeature} for list of features)
167+
*/
168+
public final TomlFactory configure(TomlReadFeature f, boolean state)
169+
{
170+
if (state) {
171+
enable(f);
172+
} else {
173+
disable(f);
174+
}
175+
return this;
176+
}
177+
178+
/**
179+
* Method for enabling specified parser feature
180+
* (check {@link TomlReadFeature} for list of features)
181+
*/
182+
public TomlFactory enable(TomlReadFeature f) {
183+
_tomlParserFeatures |= f.getMask();
184+
return this;
185+
}
186+
187+
/**
188+
* Method for disabling specified parser features
189+
* (check {@link TomlReadFeature} for list of features)
190+
*/
191+
public TomlFactory disable(TomlReadFeature f) {
192+
_tomlParserFeatures &= ~f.getMask();
193+
return this;
194+
}
195+
196+
/**
197+
* Checked whether specified parser feature is enabled.
198+
*/
199+
public final boolean isEnabled(TomlReadFeature f) {
200+
return (_tomlParserFeatures & f.getMask()) != 0;
133201
}
134202

135203
@Override
136-
public int getFormatWriteFeatures() {
137-
return _formatWriteFeatures;
204+
public int getFormatParserFeatures() {
205+
return _tomlParserFeatures;
138206
}
139207

140208
/*
@@ -144,30 +212,25 @@ public int getFormatWriteFeatures() {
144212
*/
145213

146214
@Override
147-
protected JsonParser _createParser(ObjectReadContext readCtxt, IOContext ctxt, InputStream in) throws JacksonException {
215+
public JsonParser _createParser(InputStream in, IOContext ctxt) throws IOException {
148216
// "A TOML file must be a valid UTF-8 encoded Unicode document."
149-
return _createParser(readCtxt, ctxt, new InputStreamReader(in, StandardCharsets.UTF_8));
217+
return _createParser(new InputStreamReader(in, StandardCharsets.UTF_8), ctxt);
150218
}
151219

152220
@Override
153-
protected JsonParser _createParser(ObjectReadContext readCtxt, IOContext ctxt, Reader r) throws JacksonException {
154-
ObjectNode node = parse(readCtxt, ctxt, r);
155-
return new TreeTraversingParser(node, readCtxt);
221+
public JsonParser _createParser(Reader r, IOContext ctxt) throws IOException {
222+
ObjectNode node = parse(ctxt, r);
223+
return new TreeTraversingParser(node); // don't pass our _objectCodec, this part shouldn't be customized
156224
}
157225

158226
@Override
159-
protected JsonParser _createParser(ObjectReadContext readCtxt, IOContext ctxt, byte[] data, int offset, int len) throws JacksonException {
160-
return _createParser(readCtxt, ctxt, new ByteArrayInputStream(data, offset, len));
227+
public JsonParser _createParser(byte[] data, int offset, int len, IOContext ctxt) throws IOException {
228+
return _createParser(new ByteArrayInputStream(data, offset, len), ctxt);
161229
}
162230

163231
@Override
164-
protected JsonParser _createParser(ObjectReadContext readCtxt, IOContext ctxt, char[] data, int offset, int len, boolean recyclable) throws JacksonException {
165-
return _createParser(readCtxt, ctxt, new CharArrayReader(data, offset, len));
166-
}
167-
168-
@Override
169-
protected JsonParser _createParser(ObjectReadContext readCtxt, IOContext ctxt, DataInput input) throws JacksonException {
170-
return _unsupported();
232+
protected JsonParser _createParser(char[] data, int offset, int len, IOContext ctxt, boolean recyclable) throws IOException {
233+
return _createParser(new CharArrayReader(data, offset, len), ctxt);
171234
}
172235

173236
/*
@@ -177,19 +240,15 @@ protected JsonParser _createParser(ObjectReadContext readCtxt, IOContext ctxt, D
177240
*/
178241

179242
@Override
180-
protected JsonGenerator _createGenerator(ObjectWriteContext writeCtxt, IOContext ioCtxt, Writer out) throws JacksonException {
181-
return new TomlGenerator(writeCtxt, ioCtxt, writeCtxt.getStreamWriteFeatures(_streamWriteFeatures), out);
243+
public JsonGenerator createGenerator(Writer out) throws JacksonException {
244+
IOContext ctxt = _createContext(_createContentReference(out), false);
245+
return new TomlGenerator(ctxt, _tomlGeneratorFeatures, _objectCodec, out);
182246
}
183247

184248
@Override
185-
protected JsonGenerator _createUTF8Generator(ObjectWriteContext writeCtxt, IOContext ioCtxt, OutputStream out) throws JacksonException {
186-
return _createGenerator(writeCtxt, ioCtxt, new UTF8Writer(ioCtxt, out));
187-
}
188-
189-
@Override
190-
protected Writer _createWriter(IOContext ioCtxt, OutputStream out, JsonEncoding enc) throws JacksonException {
191-
// "A TOML file must be a valid UTF-8 encoded Unicode document."
192-
return new UTF8Writer(ioCtxt, out);
249+
public JsonGenerator createGenerator(OutputStream out) throws JacksonException {
250+
IOContext ctxt = _createContext(_createContentReference(out), false);
251+
return new TomlGenerator(ctxt, _tomlGeneratorFeatures, _objectCodec, new UTF8Writer(ctxt, out));
193252
}
194253

195254
/*
@@ -198,19 +257,14 @@ protected Writer _createWriter(IOContext ioCtxt, OutputStream out, JsonEncoding
198257
/**********************************************************************
199258
*/
200259

201-
private ObjectNode parse(ObjectReadContext readCtxt, IOContext ctxt, Reader r0) {
260+
private ObjectNode parse(IOContext ctxt, Reader r0) throws IOException {
202261
JacksonTomlParseException.ErrorContext errorContext = new JacksonTomlParseException.ErrorContext(ctxt.contentReference(), null);
203-
int readFeatures = readCtxt.getFormatReadFeatures(DEFAULT_TOML_PARSER_FEATURE_FLAGS);
204-
try {
205-
if (ctxt.isResourceManaged() || isEnabled(StreamReadFeature.AUTO_CLOSE_SOURCE)) {
206-
try (Reader r = r0) {
207-
return Parser.parse(errorContext, readFeatures, r);
208-
}
209-
} else {
210-
return Parser.parse(errorContext, readFeatures, r0);
262+
if (ctxt.isResourceManaged() || isEnabled(StreamReadFeature.AUTO_CLOSE_SOURCE)) {
263+
try (Reader r = r0) {
264+
return Parser.parse(errorContext, _tomlParserFeatures, r);
211265
}
212-
} catch (IOException e) {
213-
throw _wrapIOFailure(e);
266+
} else {
267+
return Parser.parse(errorContext, _tomlParserFeatures, r0);
214268
}
215269
}
216270
}

toml/src/main/java/com/fasterxml/jackson/dataformat/toml/TomlFactoryBuilder.java

+24-10
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
11
package com.fasterxml.jackson.dataformat.toml;
22

3-
import com.fasterxml.jackson.core.base.DecorableTSFactory;
3+
import com.fasterxml.jackson.core.TSFBuilder;
44

55
/**
6-
* {@link com.fasterxml.jackson.core.TokenStreamFactory.TSFBuilder}
6+
* {@link com.fasterxml.jackson.core.TSFBuilder}
77
* implementation for constructing {@link TomlFactory}
88
* instances.
99
*
1010
* @since 3.0
1111
*/
12-
public class TomlFactoryBuilder extends DecorableTSFactory.DecorableTSFBuilder<TomlFactory, TomlFactoryBuilder> {
12+
public class TomlFactoryBuilder extends TSFBuilder<TomlFactory, TomlFactoryBuilder> {/*
13+
/**********************************************************
14+
/* Configuration
15+
/**********************************************************
16+
*/
17+
18+
protected int _formatParserFeatures = TomlFactory.DEFAULT_TOML_PARSER_FEATURE_FLAGS;
19+
protected int _formatGeneratorFeatures = TomlFactory.DEFAULT_TOML_GENERATOR_FEATURE_FLAGS;
20+
21+
/*
22+
/**********************************************************
23+
/* Life cycle
24+
/**********************************************************
25+
*/
26+
1327
TomlFactoryBuilder() {
14-
super(TomlFactory.DEFAULT_TOML_PARSER_FEATURE_FLAGS, TomlFactory.DEFAULT_TOML_GENERATOR_FEATURE_FLAGS);
28+
super();
1529
}
1630

1731
TomlFactoryBuilder(TomlFactory base) {
@@ -30,27 +44,27 @@ public TomlFactory build() {
3044
*/
3145

3246
public TomlFactoryBuilder enable(TomlReadFeature f) {
33-
_formatReadFeatures |= f.getMask();
47+
_formatParserFeatures |= f.getMask();
3448
return this;
3549
}
3650

3751
public TomlFactoryBuilder enable(TomlReadFeature first, TomlReadFeature... other) {
38-
_formatReadFeatures |= first.getMask();
52+
_formatParserFeatures |= first.getMask();
3953
for (TomlReadFeature f : other) {
40-
_formatReadFeatures |= f.getMask();
54+
_formatParserFeatures |= f.getMask();
4155
}
4256
return this;
4357
}
4458

4559
public TomlFactoryBuilder disable(TomlReadFeature f) {
46-
_formatReadFeatures &= ~f.getMask();
60+
_formatParserFeatures &= ~f.getMask();
4761
return this;
4862
}
4963

5064
public TomlFactoryBuilder disable(TomlReadFeature first, TomlReadFeature... other) {
51-
_formatReadFeatures &= ~first.getMask();
65+
_formatParserFeatures &= ~first.getMask();
5266
for (TomlReadFeature f : other) {
53-
_formatReadFeatures &= ~f.getMask();
67+
_formatParserFeatures &= ~f.getMask();
5468
}
5569
return this;
5670
}

0 commit comments

Comments
 (0)