|
18 | 18 |
|
19 | 19 | import com.fasterxml.jackson.databind.JsonNode;
|
20 | 20 |
|
| 21 | +import java.util.Optional; |
| 22 | + |
21 | 23 | /**
|
22 | 24 | * This class is used to detect schema version
|
23 | 25 | *
|
24 | 26 | * @author Subhajitdas298
|
25 | 27 | * @since 25/06/20
|
26 | 28 | */
|
27 |
| -public class SpecVersionDetector { |
| 29 | +public final class SpecVersionDetector { |
28 | 30 |
|
29 | 31 | private static final String SCHEMA_TAG = "$schema";
|
30 | 32 |
|
| 33 | + private SpecVersionDetector() { |
| 34 | + // Prevent instantiation of this utility class |
| 35 | + } |
| 36 | + |
31 | 37 | /**
|
32 |
| - * Detects schema version based on the schema tag |
| 38 | + * Detects schema version based on the schema tag: if the schema tag is not present, throws |
| 39 | + * {@link JsonSchemaException} with the corresponding message, otherwise - returns the detected spec version. |
33 | 40 | *
|
34 |
| - * @param jsonNode Json Node to read from |
35 |
| - * @return Spec version |
| 41 | + * @param jsonNode JSON Node to read from |
| 42 | + * @return Spec version if present, otherwise throws an exception |
36 | 43 | */
|
37 | 44 | public static SpecVersion.VersionFlag detect(JsonNode jsonNode) {
|
38 |
| - JsonNode schemaTag = jsonNode.get(SCHEMA_TAG); |
39 |
| - if (schemaTag == null) { |
40 |
| - throw new JsonSchemaException("'" + SCHEMA_TAG + "' tag is not present"); |
41 |
| - } |
| 45 | + return detectOptionalVersion(jsonNode).orElseThrow( |
| 46 | + () -> new JsonSchemaException("'" + SCHEMA_TAG + "' tag is not present") |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Detects schema version based on the schema tag: if the schema tag is not present, returns an empty {@link |
| 52 | + * Optional} value, otherwise - returns the detected spec version wrapped into {@link Optional}. |
| 53 | + * |
| 54 | + * @param jsonNode JSON Node to read from |
| 55 | + * @return Spec version if present, otherwise empty |
| 56 | + */ |
| 57 | + public static Optional<SpecVersion.VersionFlag> detectOptionalVersion(JsonNode jsonNode) { |
| 58 | + return Optional.ofNullable(jsonNode.get(SCHEMA_TAG)).map(schemaTag -> { |
| 59 | + |
| 60 | + final boolean forceHttps = true; |
| 61 | + final boolean removeEmptyFragmentSuffix = true; |
42 | 62 |
|
43 |
| - final boolean forceHttps = true; |
44 |
| - final boolean removeEmptyFragmentSuffix = true; |
| 63 | + String schemaTagValue = schemaTag.asText(); |
| 64 | + String schemaUri = JsonSchemaFactory.normalizeMetaSchemaUri(schemaTagValue, forceHttps, |
| 65 | + removeEmptyFragmentSuffix); |
45 | 66 |
|
46 |
| - String schemaTagValue = schemaTag.asText(); |
47 |
| - String schemaUri = JsonSchemaFactory.normalizeMetaSchemaUri(schemaTagValue, forceHttps, removeEmptyFragmentSuffix); |
48 |
| - if (schemaUri.equals(JsonMetaSchema.getV4().getUri())) { |
49 |
| - return SpecVersion.VersionFlag.V4; |
50 |
| - } |
51 |
| - if (schemaUri.equals(JsonMetaSchema.getV6().getUri())) { |
52 |
| - return SpecVersion.VersionFlag.V6; |
53 |
| - } |
54 |
| - if (schemaUri.equals(JsonMetaSchema.getV7().getUri())) { |
55 |
| - return SpecVersion.VersionFlag.V7; |
56 |
| - } |
57 |
| - if (schemaUri.equals(JsonMetaSchema.getV201909().getUri())) { |
58 |
| - return SpecVersion.VersionFlag.V201909; |
59 |
| - } |
60 |
| - if (schemaUri.equals(JsonMetaSchema.getV202012().getUri())) { |
61 |
| - return SpecVersion.VersionFlag.V202012; |
62 |
| - } |
63 |
| - throw new JsonSchemaException("'" + schemaTagValue + "' is unrecognizable schema"); |
| 67 | + if (schemaUri.equals(JsonMetaSchema.getV4().getUri())) { |
| 68 | + return SpecVersion.VersionFlag.V4; |
| 69 | + } |
| 70 | + if (schemaUri.equals(JsonMetaSchema.getV6().getUri())) { |
| 71 | + return SpecVersion.VersionFlag.V6; |
| 72 | + } |
| 73 | + if (schemaUri.equals(JsonMetaSchema.getV7().getUri())) { |
| 74 | + return SpecVersion.VersionFlag.V7; |
| 75 | + } |
| 76 | + if (schemaUri.equals(JsonMetaSchema.getV201909().getUri())) { |
| 77 | + return SpecVersion.VersionFlag.V201909; |
| 78 | + } |
| 79 | + if (schemaUri.equals(JsonMetaSchema.getV202012().getUri())) { |
| 80 | + return SpecVersion.VersionFlag.V202012; |
| 81 | + } |
| 82 | + throw new JsonSchemaException("'" + schemaTagValue + "' is unrecognizable schema"); |
| 83 | + }); |
64 | 84 | }
|
65 | 85 |
|
66 | 86 | }
|
0 commit comments