|
| 1 | +/* |
| 2 | + * Copyright 2021-2023 DiffPlug |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.diffplug.spotless.glue.yaml; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 22 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 23 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 24 | +import com.fasterxml.jackson.databind.SerializationFeature; |
| 25 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 26 | +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; |
| 27 | + |
| 28 | +import com.diffplug.spotless.FormatterFunc; |
| 29 | + |
| 30 | +public class YamlJacksonFormatterFunc implements FormatterFunc { |
| 31 | + private List<String> enabledFeatures; |
| 32 | + private List<String> disabledFeatures; |
| 33 | + |
| 34 | + public YamlJacksonFormatterFunc(List<String> enabledFeatures, List<String> disabledFeatures) { |
| 35 | + this.enabledFeatures = enabledFeatures; |
| 36 | + this.disabledFeatures = disabledFeatures; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public String apply(String input) throws Exception { |
| 41 | + ObjectMapper objectMapper = makeObjectMapper(); |
| 42 | + |
| 43 | + return format(objectMapper, input); |
| 44 | + } |
| 45 | + |
| 46 | + protected ObjectMapper makeObjectMapper() { |
| 47 | + YAMLFactory yamlFactory = new YAMLFactory(); |
| 48 | + ObjectMapper objectMapper = new ObjectMapper(yamlFactory); |
| 49 | + |
| 50 | + // Configure the ObjectMapper |
| 51 | + // https://github.com/FasterXML/jackson-databind#commonly-used-features |
| 52 | + for (String rawFeature : enabledFeatures) { |
| 53 | + // https://stackoverflow.com/questions/3735927/java-instantiating-an-enum-using-reflection |
| 54 | + SerializationFeature feature = SerializationFeature.valueOf(rawFeature); |
| 55 | + |
| 56 | + objectMapper.enable(feature); |
| 57 | + } |
| 58 | + |
| 59 | + for (String rawFeature : disabledFeatures) { |
| 60 | + // https://stackoverflow.com/questions/3735927/java-instantiating-an-enum-using-reflection |
| 61 | + SerializationFeature feature = SerializationFeature.valueOf(rawFeature); |
| 62 | + |
| 63 | + objectMapper.disable(feature); |
| 64 | + } |
| 65 | + return objectMapper; |
| 66 | + } |
| 67 | + |
| 68 | + protected String format(ObjectMapper objectMapper, String input) throws IllegalArgumentException, IOException { |
| 69 | + // We may consider adding manually an initial '---' prefix to help management of multiple documents |
| 70 | + // if (!input.trim().startsWith("---")) { |
| 71 | + // input = "---" + "\n" + input; |
| 72 | + // } |
| 73 | + |
| 74 | + try { |
| 75 | + // https://stackoverflow.com/questions/25222327/deserialize-pojos-from-multiple-yaml-documents-in-a-single-file-in-jackson |
| 76 | + // https://github.com/FasterXML/jackson-dataformats-text/issues/66#issuecomment-375328648 |
| 77 | + // 2023-01: For now, we get 'Cannot deserialize value of type `com.fasterxml.jackson.databind.node.ObjectNode` from Array value' |
| 78 | + // JsonParser yamlParser = objectMapper.getFactory().createParser(input); |
| 79 | + // List<ObjectNode> docs = objectMapper.readValues(yamlParser, ObjectNode.class).readAll(); |
| 80 | + // return objectMapper.writeValueAsString(docs); |
| 81 | + |
| 82 | + // 2023-01: This returns JSON instead of YAML |
| 83 | + // This will transit with a JsonNode |
| 84 | + // A JsonNode may keep the comments from the input node |
| 85 | + // JsonNode jsonNode = objectMapper.readTree(input); |
| 86 | + //Not 'toPrettyString' as one could require no INDENT_OUTPUT |
| 87 | + // return jsonNode.toPrettyString(); |
| 88 | + ObjectNode objectNode = objectMapper.readValue(input, ObjectNode.class); |
| 89 | + return objectMapper.writeValueAsString(objectNode); |
| 90 | + } catch (JsonProcessingException e) { |
| 91 | + throw new AssertionError("Unable to format YAML. input='" + input + "'", e); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // Spotbugs |
| 96 | + private static class ObjectNodeTypeReference extends TypeReference<ObjectNode> {} |
| 97 | +} |
0 commit comments