shz.spring.yaml.YamlHelp Maven / Gradle / Ivy
package shz.spring.yaml;
import org.springframework.core.io.Resource;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import shz.core.NullHelp;
import shz.core.io.IOHelp;
import shz.core.msg.ClientFailureMsg;
import java.io.*;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
@SuppressWarnings("unchecked")
public final class YamlHelp {
private YamlHelp() {
throw new IllegalStateException();
}
public static Map load(String yaml) {
return NullHelp.isBlank(yaml) ? Collections.emptyMap() : new Yaml().load(yaml);
}
public static Map load(InputStream is) {
return is == null ? Collections.emptyMap() : new Yaml().load(is);
}
public static Map load(Reader reader) {
return reader == null ? Collections.emptyMap() : new Yaml().load(reader);
}
public static Map load(Resource resource) {
if (resource != null)
try (BufferedReader br = IOHelp.newBufferedReader(resource.getInputStream())) {
return new Yaml().load(br);
} catch (IOException ignored) {
}
return Collections.emptyMap();
}
public static T load(Class cls, String yaml) {
return NullHelp.isBlank(yaml) ? NullHelp.empty(cls) : new Yaml().loadAs(yaml, cls);
}
public static T load(Class cls, InputStream is) {
return is == null ? NullHelp.empty(cls) : new Yaml().loadAs(is, cls);
}
public static T load(Class cls, Reader reader) {
return reader == null ? NullHelp.empty(cls) : new Yaml().loadAs(reader, cls);
}
public static T load(Class cls, Resource resource) {
if (resource != null)
try (BufferedReader br = IOHelp.newBufferedReader(resource.getInputStream())) {
return new Yaml().loadAs(br, cls);
} catch (IOException ignored) {
}
return NullHelp.empty(cls);
}
public static String dump(Object data) {
StringWriter sw = new StringWriter();
dump(data, sw);
return sw.toString();
}
public static void dump(Object data, Writer writer) {
if (NullHelp.isBlank(data)) return;
DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
Yaml yaml = new Yaml(options);
yaml.dump(data, writer);
}
public static T get(Class cls, Map map, String key) {
if (NullHelp.isEmpty(map)) return NullHelp.empty(cls);
if (NullHelp.isBlank(key)) return load(cls, dump(map));
ClientFailureMsg.requireNon(key.charAt(key.length() - 1) == '.', "yaml key错误");
return get0(cls, map, key);
}
private static T get0(Class cls, Map map, String key) {
if (NullHelp.isEmpty(map)) return NullHelp.empty(cls);
int idx = key.indexOf('.');
if (idx == -1) return load(cls, dump(map.get(key)));
String prefix = key.substring(0, idx);
return get0(cls, (Map) map.get(prefix), key.substring(idx + 1));
}
public static Map getMap(Map map, String key) {
return get(Map.class, map, key);
}
public static String getString(Map map, String key) {
return get(String.class, map, key);
}
public static Integer getInteger(Map map, String key) {
return get(Integer.class, map, key);
}
public static Long getLong(Map map, String key) {
return get(Long.class, map, key);
}
public static Boolean getBoolean(Map map, String key) {
return get(Boolean.class, map, key);
}
public static void set(Map map, String key, Object value) {
if (map == null) return;
ClientFailureMsg.requireNon(NullHelp.isEmpty(key) || key.charAt(key.length() - 1) == '.', "yaml key错误");
set0((Map) map, key, value);
}
private static void set0(Map map, String key, Object value) {
int idx = key.indexOf('.');
if (idx == -1) map.put(key, value);
else {
String prefix = key.substring(0, idx);
Map data = (Map) map.get(prefix);
if (data == null) {
data = new LinkedHashMap<>();
map.put(prefix, data);
}
set0(data, key.substring(idx + 1), value);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy