Skip to content

[Core] Delegate encoding and BOM handling to gherkin #2624

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed
- [Core] Update dependency io.cucumber:gherkin to v24.1
- [Core] Delegate encoding and BOM handling to gherkin ([2624](https://github.com/cucumber/cucumber-jvm/issues/2624) M.P. Korstanje)

## [7.8.1] - 2022-10-03
### Fixed
- [Core] Remove Jackson services from `META-INF/services` ([#2621](https://github.com/cucumber/cucumber-jvm/issues/2621) M.P. Korstanje)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package io.cucumber.core.feature;

import io.cucumber.core.gherkin.Feature;
import io.cucumber.core.gherkin.FeatureParserException;
import io.cucumber.core.resource.Resource;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -19,8 +22,6 @@

public final class FeatureParser {

private final EncodingParser encodingParser = new EncodingParser();

private final Supplier<UUID> idGenerator;

public FeatureParser(Supplier<UUID> idGenerator) {
Expand All @@ -31,8 +32,6 @@ public Optional<Feature> parseResource(Resource resource) {
requireNonNull(resource);
URI uri = resource.getUri();

String source = encodingParser.parse(resource);

ServiceLoader<io.cucumber.core.gherkin.FeatureParser> services = ServiceLoader
.load(io.cucumber.core.gherkin.FeatureParser.class);
Iterator<io.cucumber.core.gherkin.FeatureParser> iterator = services.iterator();
Expand All @@ -42,7 +41,13 @@ public Optional<Feature> parseResource(Resource resource) {
}
Comparator<io.cucumber.core.gherkin.FeatureParser> version = comparing(
io.cucumber.core.gherkin.FeatureParser::version);
return Collections.max(parser, version).parse(uri, source, idGenerator);

try (InputStream source = resource.getInputStream()) {
return Collections.max(parser, version).parse(uri, source, idGenerator);

} catch (IOException e) {
throw new FeatureParserException("Failed to parse resource at: " + uri, e);
}
}

}

This file was deleted.

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion cucumber-gherkin-messages/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,36 @@
import io.cucumber.messages.types.ParseError;
import io.cucumber.messages.types.Source;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Supplier;

import static io.cucumber.messages.types.SourceMediaType.TEXT_X_CUCUMBER_GHERKIN_PLAIN;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.stream.Collectors.toList;

public final class GherkinMessagesFeatureParser implements FeatureParser {

@Deprecated
@Override
public Optional<Feature> parse(URI path, String source, Supplier<UUID> idGenerator) {
try (InputStream is = new ByteArrayInputStream(source.getBytes(UTF_8))) {
return parse(path, is, idGenerator);
} catch (IOException e) {
throw new FeatureParserException("Failed to parse resource at: " + path, e);
}
}

@Override
public Optional<Feature> parse(URI path, InputStream source, Supplier<UUID> idGenerator) throws IOException {
List<Envelope> envelopes = GherkinParser.builder()
.idGenerator(() -> idGenerator.get().toString())
.build()
.parse(Envelope.of(new Source(path.toString(), source, TEXT_X_CUCUMBER_GHERKIN_PLAIN)))
.parse(path.toString(), source)
.collect(toList());

List<String> errors = envelopes.stream()
Expand Down Expand Up @@ -70,10 +83,17 @@ public Optional<Feature> parse(URI path, String source, Supplier<UUID> idGenerat
.map(pickle -> new GherkinMessagesPickle(pickle, path, dialect, cucumberQuery))
.collect(toList());

Source sourceMessage = envelopes.stream()
.map(Envelope::getSource)
.filter(Optional::isPresent)
.map(Optional::get)
.findFirst()
.orElseThrow(() -> new IllegalStateException("source message was not emitted by parser"));

return new GherkinMessagesFeature(
feature,
path,
source,
sourceMessage.getData(),
pickles,
envelopes);
});
Expand Down
Loading