|
1 |
| -package dev.cdevents.generator; |
2 |
| - |
3 |
| -import com.fasterxml.jackson.databind.JsonNode; |
4 |
| -import com.fasterxml.jackson.databind.ObjectMapper; |
5 |
| -import com.fasterxml.jackson.databind.node.ObjectNode; |
6 |
| -import org.slf4j.Logger; |
7 |
| -import org.slf4j.LoggerFactory; |
8 |
| - |
9 |
| -import java.io.File; |
10 |
| -import java.io.IOException; |
11 |
| -import java.nio.file.Files; |
12 |
| -import java.nio.file.Path; |
13 |
| -import java.nio.file.Paths; |
14 |
| -import java.util.Iterator; |
15 |
| - |
16 |
| -public class PreprocessSchemas { |
17 |
| - |
18 |
| - private static final ObjectMapper objectMapper = new ObjectMapper(); |
19 |
| - |
20 |
| - private static final Logger log = LoggerFactory.getLogger(PreprocessSchemas.class); |
21 |
| - |
22 |
| - public static void main(String[] args) throws IOException { |
23 |
| - if (args == null || args.length != 1) { |
24 |
| - System.err.println("Usage: PreprocessSchemas <schema_directory_path>"); |
25 |
| - throw new IllegalArgumentException("Schema directory path arguments not passed to PreprocessSchemas"); |
26 |
| - } |
27 |
| - String parentBaseDir = args[0]; |
28 |
| - String specSchemaDir = parentBaseDir + File.separator + "spec" + File.separator + "schemas"; |
29 |
| - Files.walk(Paths.get(specSchemaDir)) |
30 |
| - .filter(Files::isRegularFile) |
31 |
| - .filter(path -> path.toString().endsWith(".json")) |
32 |
| - .forEach(PreprocessSchemas::processFile); |
33 |
| - } |
34 |
| - |
35 |
| - private static void processFile(Path filePath) { |
36 |
| - log.info("processing schema file {} to update $ref path with json extension.", filePath.getFileName()); |
37 |
| - try { |
38 |
| - JsonNode rootNode = objectMapper.readTree(filePath.toFile()); |
39 |
| - updateRefs(rootNode); |
40 |
| - objectMapper.writerWithDefaultPrettyPrinter().writeValue(filePath.toFile(), rootNode); |
41 |
| - } catch (IOException e) { |
42 |
| - log.error("Exception occurred while process schema file to update ref {}", e.getMessage()); |
43 |
| - throw new IllegalStateException("Exception occurred while process schema file to update ref ", e); |
44 |
| - } |
45 |
| - } |
46 |
| - |
47 |
| - private static void updateRefs(JsonNode node) { |
48 |
| - if (node.isObject()) { |
49 |
| - ObjectNode objectNode = (ObjectNode) node; |
50 |
| - Iterator<String> fieldNames = objectNode.fieldNames(); |
51 |
| - while (fieldNames.hasNext()) { |
52 |
| - String fieldName = fieldNames.next(); |
53 |
| - JsonNode childNode = objectNode.get(fieldName); |
54 |
| - if ("$ref".equals(fieldName) && !childNode.asText().endsWith(".json")) { |
55 |
| - objectNode.put(fieldName, childNode.asText() + ".json"); |
56 |
| - } else { |
57 |
| - updateRefs(childNode); |
58 |
| - } |
59 |
| - } |
60 |
| - } else if (node.isArray()) { |
61 |
| - for (JsonNode arrayItem : node) { |
62 |
| - updateRefs(arrayItem); |
63 |
| - } |
64 |
| - } |
65 |
| - } |
66 |
| -} |
| 1 | +package dev.cdevents.preprocessor; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.JsonNode; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 6 | +import org.slf4j.Logger; |
| 7 | +import org.slf4j.LoggerFactory; |
| 8 | + |
| 9 | +import java.io.File; |
| 10 | +import java.io.IOException; |
| 11 | +import java.nio.file.Files; |
| 12 | +import java.nio.file.Path; |
| 13 | +import java.nio.file.Paths; |
| 14 | +import java.util.Iterator; |
| 15 | + |
| 16 | +public class PreprocessSchemas { |
| 17 | + |
| 18 | + private PreprocessSchemas() { |
| 19 | + } |
| 20 | + |
| 21 | + private static ObjectMapper objectMapper = new ObjectMapper(); |
| 22 | + |
| 23 | + private static Logger log = LoggerFactory.getLogger(PreprocessSchemas.class); |
| 24 | + |
| 25 | + /** |
| 26 | + * |
| 27 | + * Main method to update schema files. |
| 28 | + * @param args [0] - parent directory for the cdevents-sdk-java |
| 29 | + */ |
| 30 | + public static void main(String[] args) { |
| 31 | + if (args == null || args.length != 1) { |
| 32 | + System.err.println("Usage: PreprocessSchemas <schema_directory_path>"); |
| 33 | + throw new IllegalArgumentException("Prent directory path argument not passed to PreprocessSchemas"); |
| 34 | + } |
| 35 | + String parentBaseDir = args[0]; |
| 36 | + String specSchemaDir = parentBaseDir + File.separator + "spec" + File.separator + "schemas"; |
| 37 | + try { |
| 38 | + Files.walk(Paths.get(specSchemaDir)) |
| 39 | + .filter(Files::isRegularFile) |
| 40 | + .filter(path -> path.toString().endsWith(".json")) |
| 41 | + .forEach(PreprocessSchemas::processFile); |
| 42 | + } catch (IOException e) { |
| 43 | + log.error("Exception occurred while processing schema directory {}", e.getMessage()); |
| 44 | + throw new IllegalStateException("Exception occurred while processing schema directory", e); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + private static void processFile(Path filePath) { |
| 49 | + log.info("processing schema file {} to update $ref path with json extension.", filePath.getFileName()); |
| 50 | + try { |
| 51 | + JsonNode rootNode = objectMapper.readTree(filePath.toFile()); |
| 52 | + updateRefs(rootNode); |
| 53 | + objectMapper.writerWithDefaultPrettyPrinter().writeValue(filePath.toFile(), rootNode); |
| 54 | + } catch (IOException e) { |
| 55 | + log.error("Exception occurred while process schema file to update ref {}", e.getMessage()); |
| 56 | + throw new IllegalStateException("Exception occurred while process schema file to update ref ", e); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private static void updateRefs(JsonNode node) { |
| 61 | + if (node.isObject()) { |
| 62 | + ObjectNode objectNode = (ObjectNode) node; |
| 63 | + Iterator<String> fieldNames = objectNode.fieldNames(); |
| 64 | + while (fieldNames.hasNext()) { |
| 65 | + String fieldName = fieldNames.next(); |
| 66 | + JsonNode childNode = objectNode.get(fieldName); |
| 67 | + if ("$ref".equals(fieldName) && !childNode.asText().endsWith(".json")) { |
| 68 | + objectNode.put(fieldName, childNode.asText() + ".json"); |
| 69 | + } else { |
| 70 | + updateRefs(childNode); |
| 71 | + } |
| 72 | + } |
| 73 | + } else if (node.isArray()) { |
| 74 | + for (JsonNode arrayItem : node) { |
| 75 | + updateRefs(arrayItem); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments