-
Notifications
You must be signed in to change notification settings - Fork 431
/
YamlUtil.java
109 lines (95 loc) · 3.56 KB
/
YamlUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* Copyright (c) 2019 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at:
*
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.jkube.kit.common.util;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.SortedMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import org.apache.commons.lang3.StringUtils;
import static org.eclipse.jkube.kit.common.util.MapUtil.getFlattenedMap;
public class YamlUtil {
private static final YAMLFactory YAML_FACTORY = new YAMLFactory();
private static final ObjectMapper YAML_MAPPER = new ObjectMapper(YAML_FACTORY);
private static final String EMPTY_YAML = "---\n";
private YamlUtil() {
}
public static Properties getPropertiesFromYamlResource(URL resource) {
return getPropertiesFromYamlResource(null, resource);
}
protected static Properties getPropertiesFromYamlResource(String activeProfile, URL resource) {
if (resource != null) {
try {
Properties properties = new Properties();
List<String> profiles = splitYamlResource(resource);
if (!profiles.isEmpty()) {
properties.putAll(getPropertiesFromYamlString(getYamlFromYamlList(activeProfile, profiles)));
}
return properties;
} catch (IOException e) {
throw new IllegalStateException("Error while reading Yaml resource from URL " + resource, e);
}
}
return new Properties();
}
public static Properties getPropertiesFromYamlString(String yamlString) throws IOException {
final Properties properties = new Properties();
final SortedMap<String, ?> source = YAML_MAPPER.readValue(
Optional.ofNullable(yamlString).filter(StringUtils::isNotBlank).orElse(EMPTY_YAML),
new TypeReference<SortedMap<String, ?>>() {
});
if (source != null) {
properties.putAll(getFlattenedMap(source));
}
return properties;
}
static List<String> splitYamlResource(URL resource) throws IOException {
final List<String> serializedYamlList = new ArrayList<>();
final List<Map<String, ?>> parsedList = YAML_MAPPER.readValues(
YAML_FACTORY.createParser(resource), new TypeReference<Map<String, ?>>() {
}).readAll();
for (Map<String, ?> listEntry : parsedList) {
serializedYamlList.add(YAML_MAPPER.writeValueAsString(listEntry));
}
return serializedYamlList;
}
static String getYamlFromYamlList(String pattern, List<String> yamlAsStringList) {
if (pattern != null) {
for (String yamlStr : yamlAsStringList) {
if (yamlStr.contains(pattern)) {
return yamlStr;
}
}
}
return yamlAsStringList.iterator().next();
}
public static boolean isYaml(File file) {
return file.getName().toLowerCase().matches(".*?\\.ya?ml$");
}
public static List<File> listYamls(File directory) {
return Stream.of(Optional.ofNullable(directory).map(File::listFiles).orElse(new File[0]))
.filter(File::isFile)
.filter(YamlUtil::isYaml)
.collect(Collectors.toList());
}
}