replace yaml mapping with own implementation to fix pluginVerifierTask
This commit is contained in:
parent
d5295fa111
commit
d7d2880c44
@ -7,6 +7,7 @@
|
|||||||
- Dropped support for IDE versions older than 2022.2
|
- Dropped support for IDE versions older than 2022.2
|
||||||
- Updated dependencies
|
- Updated dependencies
|
||||||
- Removed deprecated API access (TranslatorToolWindowFactory)
|
- Removed deprecated API access (TranslatorToolWindowFactory)
|
||||||
|
- Replaced YAML mapping with own implementation
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
- Support for all 2023.1 builds (231.*)
|
- Support for all 2023.1 builds (231.*)
|
||||||
|
@ -2,19 +2,16 @@ package de.marhali.easyi18n.io.parser.yaml;
|
|||||||
|
|
||||||
import de.marhali.easyi18n.io.parser.ArrayMapper;
|
import de.marhali.easyi18n.io.parser.ArrayMapper;
|
||||||
|
|
||||||
import thito.nodeflow.config.ListSection;
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
|
||||||
* Map for yaml array values.
|
|
||||||
* @author marhali
|
|
||||||
*/
|
|
||||||
public class YamlArrayMapper extends ArrayMapper {
|
public class YamlArrayMapper extends ArrayMapper {
|
||||||
public static String read(ListSection list) {
|
public static String read(List<Object> list) {
|
||||||
return read(list.iterator(), Object::toString);
|
return read(list.iterator(), Object::toString);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ListSection write(String concat) {
|
public static List<Object> write(String concat) {
|
||||||
ListSection list = new ListSection();
|
List<Object> list = new ArrayList<>();
|
||||||
write(concat, list::add);
|
write(concat, list::add);
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
@ -7,31 +7,25 @@ import de.marhali.easyi18n.util.StringUtil;
|
|||||||
import org.apache.commons.lang.StringEscapeUtils;
|
import org.apache.commons.lang.StringEscapeUtils;
|
||||||
import org.apache.commons.lang.math.NumberUtils;
|
import org.apache.commons.lang.math.NumberUtils;
|
||||||
|
|
||||||
import thito.nodeflow.config.ListSection;
|
import java.util.HashMap;
|
||||||
import thito.nodeflow.config.MapSection;
|
import java.util.List;
|
||||||
import thito.nodeflow.config.Section;
|
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
|
||||||
* Mapper for mapping yaml files into translation nodes and backwards.
|
|
||||||
* @author marhali
|
|
||||||
*/
|
|
||||||
public class YamlMapper {
|
public class YamlMapper {
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
public static void read(String locale, Section section, TranslationNode node) {
|
public static void read(String locale, Map<String, Object> section, TranslationNode node) {
|
||||||
for(String key : section.getKeys()) {
|
for(String key : section.keySet()) {
|
||||||
Object value = section.getInScope(key).get();
|
Object value = section.get(key);
|
||||||
TranslationNode childNode = node.getOrCreateChildren(key);
|
TranslationNode childNode = node.getOrCreateChildren(key);
|
||||||
|
|
||||||
if(value instanceof MapSection) {
|
if(value instanceof Map) {
|
||||||
// Nested element - run recursively
|
// Nested element run recursively
|
||||||
read(locale, (MapSection) value, childNode);
|
read(locale, (Map<String, Object>) value, childNode);
|
||||||
} else {
|
} else {
|
||||||
TranslationValue translation = childNode.getValue();
|
TranslationValue translation = childNode.getValue();
|
||||||
|
|
||||||
String content = value instanceof ListSection
|
String content = value instanceof List
|
||||||
? YamlArrayMapper.read((ListSection) value)
|
? YamlArrayMapper.read((List<Object>) value)
|
||||||
: StringUtil.escapeControls(String.valueOf(value), true);
|
: StringUtil.escapeControls(String.valueOf(value), true);
|
||||||
|
|
||||||
translation.put(locale, content);
|
translation.put(locale, content);
|
||||||
@ -40,17 +34,17 @@ public class YamlMapper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void write(String locale, Section section, TranslationNode node) {
|
public static void write(String locale, Map<String, Object> section, TranslationNode node) {
|
||||||
for(Map.Entry<String, TranslationNode> entry : node.getChildren().entrySet()) {
|
for(Map.Entry<String, TranslationNode> entry : node.getChildren().entrySet()) {
|
||||||
String key = entry.getKey();
|
String key = entry.getKey();
|
||||||
TranslationNode childNode = entry.getValue();
|
TranslationNode childNode = entry.getValue();
|
||||||
|
|
||||||
if(!childNode.isLeaf()) {
|
if(!childNode.isLeaf()) {
|
||||||
// Nested node - run recursively
|
// Nested node - run recursively
|
||||||
MapSection childSection = new MapSection();
|
Map<String, Object> childSection = new HashMap<>();
|
||||||
write(locale, childSection, childNode);
|
write(locale, childSection, childNode);
|
||||||
if(childSection.size() > 0) {
|
if(childSection.size() > 0) {
|
||||||
section.setInScope(key, childSection);
|
section.put(key, childSection);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
TranslationValue translation = childNode.getValue();
|
TranslationValue translation = childNode.getValue();
|
||||||
@ -58,11 +52,11 @@ public class YamlMapper {
|
|||||||
|
|
||||||
if(content != null) {
|
if(content != null) {
|
||||||
if(YamlArrayMapper.isArray(content)) {
|
if(YamlArrayMapper.isArray(content)) {
|
||||||
section.setInScope(key, YamlArrayMapper.write(content));
|
section.put(key, YamlArrayMapper.write(content));
|
||||||
} else if(NumberUtils.isNumber(content)) {
|
} else if(NumberUtils.isNumber(content)) {
|
||||||
section.setInScope(key, NumberUtils.createNumber(content));
|
section.put(key, NumberUtils.createNumber(content));
|
||||||
} else {
|
} else {
|
||||||
section.setInScope(key, StringEscapeUtils.unescapeJava(content));
|
section.put(key, StringEscapeUtils.unescapeJava(content));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,20 +10,32 @@ import de.marhali.easyi18n.model.TranslationNode;
|
|||||||
import de.marhali.easyi18n.settings.ProjectSettings;
|
import de.marhali.easyi18n.settings.ProjectSettings;
|
||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import org.yaml.snakeyaml.DumperOptions;
|
||||||
|
import org.yaml.snakeyaml.Yaml;
|
||||||
import org.yaml.snakeyaml.error.YAMLException;
|
import org.yaml.snakeyaml.error.YAMLException;
|
||||||
import thito.nodeflow.config.MapSection;
|
|
||||||
import thito.nodeflow.config.Section;
|
|
||||||
|
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.io.Reader;
|
import java.io.Reader;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
|
||||||
* Yaml / YML file format parser strategy.
|
|
||||||
* @author marhali
|
|
||||||
*/
|
|
||||||
public class YamlParserStrategy extends ParserStrategy {
|
public class YamlParserStrategy extends ParserStrategy {
|
||||||
|
|
||||||
|
private static DumperOptions dumperOptions() {
|
||||||
|
DumperOptions options = new DumperOptions();
|
||||||
|
|
||||||
|
options.setIndent(2);
|
||||||
|
options.setAllowUnicode(true);
|
||||||
|
options.setPrettyFlow(true);
|
||||||
|
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
||||||
|
|
||||||
|
return options;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final Yaml YAML = new Yaml(dumperOptions());
|
||||||
|
|
||||||
public YamlParserStrategy(@NotNull ProjectSettings settings) {
|
public YamlParserStrategy(@NotNull ProjectSettings settings) {
|
||||||
super(settings);
|
super(settings);
|
||||||
}
|
}
|
||||||
@ -36,11 +48,11 @@ public class YamlParserStrategy extends ParserStrategy {
|
|||||||
TranslationNode targetNode = super.getOrCreateTargetNode(file, data);
|
TranslationNode targetNode = super.getOrCreateTargetNode(file, data);
|
||||||
|
|
||||||
try(Reader reader = new InputStreamReader(vf.getInputStream(), vf.getCharset())) {
|
try(Reader reader = new InputStreamReader(vf.getInputStream(), vf.getCharset())) {
|
||||||
Section input;
|
Map<String, Object> input;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
input = Section.parseToMap(reader);
|
input = YAML.load(reader);
|
||||||
} catch (YAMLException ex) {
|
} catch(YAMLException ex) {
|
||||||
throw new SyntaxException(ex.getMessage(), file);
|
throw new SyntaxException(ex.getMessage(), file);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,11 +61,12 @@ public class YamlParserStrategy extends ParserStrategy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull String write(@NotNull TranslationData data, @NotNull TranslationFile file) throws Exception {
|
public @Nullable String write(@NotNull TranslationData data, @NotNull TranslationFile file) throws Exception {
|
||||||
TranslationNode targetNode = super.getTargetNode(data, file);
|
TranslationNode targetNode = super.getTargetNode(data, file);
|
||||||
|
|
||||||
Section output = new MapSection();
|
Map<String, Object> output = new HashMap<>();
|
||||||
YamlMapper.write(file.getLocale(), output, targetNode);
|
YamlMapper.write(file.getLocale(), output, targetNode);
|
||||||
return Section.toString(output);
|
|
||||||
|
return YAML.dumpAsMap(output);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,227 +0,0 @@
|
|||||||
package thito.nodeflow.config;
|
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
import java.util.stream.*;
|
|
||||||
|
|
||||||
public class ListSection extends ArrayList<Object> implements Section {
|
|
||||||
private Section parent;
|
|
||||||
private String name;
|
|
||||||
|
|
||||||
public ListSection() {
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
|
|
||||||
public ListSection(Collection<?> c) {
|
|
||||||
super();
|
|
||||||
addAll(c);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setParent(Section parent, String name) {
|
|
||||||
this.parent = parent;
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getName() {
|
|
||||||
if (name == null && parent != null) {
|
|
||||||
if (parent instanceof ListSection) {
|
|
||||||
return String.valueOf(((ListSection) parent).indexOf(this));
|
|
||||||
}
|
|
||||||
if (parent instanceof MapSection) {
|
|
||||||
return ((MapSection) parent).entrySet().stream().filter(e -> Objects.equals(e.getValue(), this))
|
|
||||||
.findAny().map(Map.Entry::getKey).orElse(null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Optional<Object> getObject(int index) {
|
|
||||||
return index >= 0 && index < size() ? Optional.ofNullable(get(index)) : Optional.empty();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Section getParent() {
|
|
||||||
return parent;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Set<String> getKeys() {
|
|
||||||
return IntStream.range(0, size()).mapToObj(String::valueOf).collect(Collectors.toSet());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Optional<?> getInScope(String key) {
|
|
||||||
try {
|
|
||||||
return Optional.ofNullable(get(Integer.parseInt(key)));
|
|
||||||
} catch (Throwable t) {
|
|
||||||
}
|
|
||||||
return Optional.empty();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setInScope(String key, Object value) {
|
|
||||||
try {
|
|
||||||
set(Integer.parseInt(key), value);
|
|
||||||
} catch (Throwable t) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object set(int index, Object element) {
|
|
||||||
element = Section.wrap(element);
|
|
||||||
if (element instanceof Section) element = Section.wrapParent(this, null, (Section) element);
|
|
||||||
return super.set(index, element);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean add(Object o) {
|
|
||||||
o = Section.wrap(o);
|
|
||||||
if (o instanceof Section) o = Section.wrapParent(this, null, (Section) o);
|
|
||||||
return super.add(o);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void add(int index, Object element) {
|
|
||||||
element = Section.wrap(element);
|
|
||||||
if (element instanceof Section) element = Section.wrapParent(this, null, (Section) element);
|
|
||||||
super.add(index, element);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean addAll(Collection<?> c) {
|
|
||||||
c.forEach(o -> add(o));
|
|
||||||
return !c.isEmpty();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean addAll(int index, Collection<?> c) {
|
|
||||||
List<Object> wrapped = new ArrayList<>();
|
|
||||||
c.forEach(obj -> {
|
|
||||||
Object o = Section.wrap(obj);
|
|
||||||
if (o instanceof Section) o = Section.wrapParent(this, null, (Section) o);
|
|
||||||
wrapped.add(o);
|
|
||||||
});
|
|
||||||
return super.addAll(index, wrapped);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return Section.toString(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public <T extends Enum<T>> Optional<T> getEnum(int index, Class<T> clz) {
|
|
||||||
return getObject(index).map(o -> {
|
|
||||||
try {
|
|
||||||
return Enum.valueOf(clz, String.valueOf(o));
|
|
||||||
} catch (Throwable t) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public Optional<String> getString(int index) {
|
|
||||||
return getObject(index).map(String::valueOf);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Optional<Integer> getInteger(int index) {
|
|
||||||
return getObject(index).map(o -> {
|
|
||||||
try {
|
|
||||||
return Integer.valueOf(String.valueOf(o));
|
|
||||||
} catch (Throwable t) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public Optional<Double> getDouble(int index) {
|
|
||||||
return getObject(index).map(o -> {
|
|
||||||
try {
|
|
||||||
return Double.valueOf(String.valueOf(o));
|
|
||||||
} catch (Throwable t) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public Optional<Long> getLong(int index) {
|
|
||||||
return getObject(index).map(o -> {
|
|
||||||
try {
|
|
||||||
return Long.valueOf(String.valueOf(o));
|
|
||||||
} catch (Throwable t) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public Optional<Float> getFloat(int index) {
|
|
||||||
return getObject(index).map(o -> {
|
|
||||||
try {
|
|
||||||
return Float.valueOf(String.valueOf(o));
|
|
||||||
} catch (Throwable t) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public Optional<Short> getShort(int index) {
|
|
||||||
return getObject(index).map(o -> {
|
|
||||||
try {
|
|
||||||
return Short.valueOf(String.valueOf(o));
|
|
||||||
} catch (Throwable t) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public Optional<Byte> getByte(int index) {
|
|
||||||
return getObject(index).map(o -> {
|
|
||||||
try {
|
|
||||||
return Byte.valueOf(String.valueOf(o));
|
|
||||||
} catch (Throwable t) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public Optional<Character> getCharacter(int index) {
|
|
||||||
return getObject(index).map(o -> {
|
|
||||||
String text = String.valueOf(o);
|
|
||||||
return text.isEmpty() ? null : text.charAt(0);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public Optional<Boolean> getBoolean(int index) {
|
|
||||||
return getObject(index).map(o -> {
|
|
||||||
String text = String.valueOf(o);
|
|
||||||
return text.equals("true") ? Boolean.TRUE : text.equals("false") ? Boolean.FALSE : null;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public Optional<MapSection> getMap(int index) {
|
|
||||||
return getObject(index).map(o -> {
|
|
||||||
if (o instanceof Map) {
|
|
||||||
if (o instanceof MapSection) return (MapSection) o;
|
|
||||||
MapSection mapSection = new MapSection((Map<?, ?>) o);
|
|
||||||
mapSection.setParent(this, null);
|
|
||||||
return mapSection;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public Optional<ListSection> getList(int index) {
|
|
||||||
return getObject(index).map(o -> {
|
|
||||||
if (o instanceof List) {
|
|
||||||
if (o instanceof ListSection) {
|
|
||||||
return (ListSection) o;
|
|
||||||
}
|
|
||||||
ListSection list = new ListSection((List<?>) o);
|
|
||||||
list.setParent(this, null);
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
ListSection list = new ListSection(Collections.singleton(o));
|
|
||||||
list.setParent(this, null);
|
|
||||||
return list;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,78 +0,0 @@
|
|||||||
package thito.nodeflow.config;
|
|
||||||
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
public class MapSection extends HashMap<String, Object> implements Section {
|
|
||||||
private Section parent;
|
|
||||||
private String name;
|
|
||||||
|
|
||||||
public MapSection() {
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
|
|
||||||
public MapSection(@Nullable Map<?, ?> m) {
|
|
||||||
super();
|
|
||||||
|
|
||||||
if(m != null) {
|
|
||||||
m.forEach((key, value) -> put(String.valueOf(key), value));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setParent(Section parent, String name) {
|
|
||||||
this.parent = parent;
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getName() {
|
|
||||||
if (name == null && parent != null) {
|
|
||||||
if (parent instanceof ListSection) {
|
|
||||||
return String.valueOf(((ListSection) parent).indexOf(this));
|
|
||||||
}
|
|
||||||
if (parent instanceof MapSection) {
|
|
||||||
return ((MapSection) parent).entrySet().stream().filter(e -> Objects.equals(e.getValue(), this))
|
|
||||||
.findAny().map(Entry::getKey).orElse(null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Section getParent() {
|
|
||||||
return parent;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setInScope(String key, Object value) {
|
|
||||||
put(key, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object put(String key, Object value) {
|
|
||||||
value = Section.wrap(value);
|
|
||||||
if (value instanceof Section) value = Section.wrapParent(this, key, (Section) value);
|
|
||||||
return super.put(key, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void putAll(Map<? extends String, ?> m) {
|
|
||||||
m.forEach(this::put);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Set<String> getKeys() {
|
|
||||||
return keySet();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Optional<?> getInScope(String key) {
|
|
||||||
return Optional.ofNullable(get(key));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return Section.toString(this);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,221 +0,0 @@
|
|||||||
package thito.nodeflow.config;
|
|
||||||
|
|
||||||
import org.yaml.snakeyaml.*;
|
|
||||||
|
|
||||||
import java.io.*;
|
|
||||||
import java.util.*;
|
|
||||||
import java.util.regex.*;
|
|
||||||
|
|
||||||
public interface Section {
|
|
||||||
String SEPARATOR = ".";
|
|
||||||
static Object wrap(Object o) {
|
|
||||||
if (o instanceof Section) return o;
|
|
||||||
if (o instanceof List) {
|
|
||||||
return new ListSection((List<?>) o);
|
|
||||||
}
|
|
||||||
if (o instanceof Map) {
|
|
||||||
return new MapSection((Map<?, ?>) o);
|
|
||||||
}
|
|
||||||
return o;
|
|
||||||
}
|
|
||||||
static String getName(String path) {
|
|
||||||
String[] split = path.split(Pattern.quote(SEPARATOR));
|
|
||||||
return split[split.length - 1];
|
|
||||||
}
|
|
||||||
static Section wrapParent(Section parent, String name, Section current) {
|
|
||||||
if (current.getParent() != null && current.getParent() != parent) {
|
|
||||||
if (current instanceof MapSection) {
|
|
||||||
MapSection mapSection = new MapSection((MapSection) current);
|
|
||||||
mapSection.setParent(parent, name);
|
|
||||||
return mapSection;
|
|
||||||
}
|
|
||||||
if (current instanceof ListSection) {
|
|
||||||
ListSection objects = new ListSection((ListSection) current);
|
|
||||||
objects.setParent(parent, name);
|
|
||||||
return objects;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (current instanceof MapSection) {
|
|
||||||
((MapSection) current).setParent(parent, name);
|
|
||||||
}
|
|
||||||
if (current instanceof ListSection) {
|
|
||||||
((ListSection) current).setParent(parent, name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return current;
|
|
||||||
}
|
|
||||||
static String toString(Section section) {
|
|
||||||
DumperOptions options = new DumperOptions();
|
|
||||||
options.setIndent(2);
|
|
||||||
options.setAllowUnicode(true);
|
|
||||||
options.setPrettyFlow(true);
|
|
||||||
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
|
||||||
Yaml yaml = new Yaml(options);
|
|
||||||
return yaml.dumpAsMap(section);
|
|
||||||
}
|
|
||||||
static MapSection parseToMap(Reader reader) {
|
|
||||||
Yaml yaml = new Yaml();
|
|
||||||
return new MapSection(yaml.loadAs(reader, Map.class));
|
|
||||||
}
|
|
||||||
Set<String> getKeys();
|
|
||||||
default Set<String> getPaths() {
|
|
||||||
Set<String> paths = new HashSet<>();
|
|
||||||
for (String k : getKeys()) {
|
|
||||||
Object lookup = getInScope(k).orElse(null);
|
|
||||||
if (lookup instanceof Section) {
|
|
||||||
for (String p : ((Section) lookup).getPaths()) {
|
|
||||||
paths.add(k + "." + p);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return paths;
|
|
||||||
}
|
|
||||||
String getName();
|
|
||||||
default String getPath() {
|
|
||||||
StringBuilder path = new StringBuilder(getName());
|
|
||||||
Section parent;
|
|
||||||
while ((parent = getParent()) != null) {
|
|
||||||
path.insert(0, parent.getName() + SEPARATOR);
|
|
||||||
}
|
|
||||||
return path.toString();
|
|
||||||
}
|
|
||||||
Section getParent();
|
|
||||||
Optional<?> getInScope(String key);
|
|
||||||
void setInScope(String key, Object value);
|
|
||||||
default void set(String path, Object value) {
|
|
||||||
String[] paths = path.split(Pattern.quote(SEPARATOR));
|
|
||||||
Object lookup = this;
|
|
||||||
for (int i = 0; i < paths.length - 1; i++) {
|
|
||||||
Section oldLookup = (Section) lookup;
|
|
||||||
lookup = oldLookup.getInScope(paths[i]).orElse(null);
|
|
||||||
if (!(lookup instanceof Section)) {
|
|
||||||
oldLookup.setInScope(paths[i], lookup = new MapSection());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (paths.length > 0) {
|
|
||||||
((Section) lookup).setInScope(paths[paths.length - 1], value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
default Optional<?> getObject(String path) {
|
|
||||||
String[] paths = path.split(Pattern.quote(SEPARATOR));
|
|
||||||
Object lookup = this;
|
|
||||||
for (String s : paths) {
|
|
||||||
if (lookup instanceof Section) {
|
|
||||||
lookup = ((Section) lookup).getInScope(s).orElse(null);
|
|
||||||
} else {
|
|
||||||
return Optional.empty();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return Optional.ofNullable(lookup);
|
|
||||||
}
|
|
||||||
default <T extends Enum<T>> Optional<T> getEnum(String path, Class<T> clz) {
|
|
||||||
return getObject(path).map(o -> {
|
|
||||||
try {
|
|
||||||
return Enum.valueOf(clz, String.valueOf(o));
|
|
||||||
} catch (Throwable t) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
default Optional<String> getString(String path) {
|
|
||||||
return getObject(path).map(String::valueOf);
|
|
||||||
}
|
|
||||||
default Optional<Integer> getInteger(String path) {
|
|
||||||
return getObject(path).map(o -> {
|
|
||||||
try {
|
|
||||||
return Integer.valueOf(String.valueOf(o));
|
|
||||||
} catch (Throwable t) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
default Optional<Double> getDouble(String path) {
|
|
||||||
return getObject(path).map(o -> {
|
|
||||||
try {
|
|
||||||
return Double.valueOf(String.valueOf(o));
|
|
||||||
} catch (Throwable t) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
default Optional<Long> getLong(String path) {
|
|
||||||
return getObject(path).map(o -> {
|
|
||||||
try {
|
|
||||||
return Long.valueOf(String.valueOf(o));
|
|
||||||
} catch (Throwable t) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
default Optional<Float> getFloat(String path) {
|
|
||||||
return getObject(path).map(o -> {
|
|
||||||
try {
|
|
||||||
return Float.valueOf(String.valueOf(o));
|
|
||||||
} catch (Throwable t) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
default Optional<Short> getShort(String path) {
|
|
||||||
return getObject(path).map(o -> {
|
|
||||||
try {
|
|
||||||
return Short.valueOf(String.valueOf(o));
|
|
||||||
} catch (Throwable t) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
default Optional<Byte> getByte(String path) {
|
|
||||||
return getObject(path).map(o -> {
|
|
||||||
try {
|
|
||||||
return Byte.valueOf(String.valueOf(o));
|
|
||||||
} catch (Throwable t) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
default Optional<Character> getCharacter(String path) {
|
|
||||||
return getObject(path).map(o -> {
|
|
||||||
String text = String.valueOf(o);
|
|
||||||
return text.isEmpty() ? null : text.charAt(0);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
default Optional<Boolean> getBoolean(String path) {
|
|
||||||
return getObject(path).map(o -> {
|
|
||||||
String text = String.valueOf(o);
|
|
||||||
return text.equals("true") ? Boolean.TRUE : text.equals("false") ? Boolean.FALSE : null;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
default Optional<MapSection> getMap(String path) {
|
|
||||||
return getObject(path).map(o -> {
|
|
||||||
if (o instanceof Map) {
|
|
||||||
if (o instanceof MapSection) return (MapSection) o;
|
|
||||||
MapSection mapSection = new MapSection((Map<?, ?>) o);
|
|
||||||
mapSection.setParent(getParent(), Section.getName(path));
|
|
||||||
return mapSection;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
default boolean isList(String path) {
|
|
||||||
Optional<?> o = getObject(path);
|
|
||||||
return o.isPresent() && o.get() instanceof List;
|
|
||||||
}
|
|
||||||
|
|
||||||
default Optional<ListSection> getList(String path) {
|
|
||||||
return getObject(path).map(o -> {
|
|
||||||
if (o instanceof List) {
|
|
||||||
if (o instanceof ListSection) {
|
|
||||||
return (ListSection) o;
|
|
||||||
}
|
|
||||||
ListSection list = new ListSection((List<?>) o);
|
|
||||||
list.setParent(this, Section.getName(path));
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
ListSection list = new ListSection(Collections.singleton(o));
|
|
||||||
list.setParent(getParent(), Section.getName(path));
|
|
||||||
return list;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
@ -8,51 +8,47 @@ import org.apache.commons.lang.StringEscapeUtils;
|
|||||||
|
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
|
|
||||||
import thito.nodeflow.config.MapSection;
|
import java.util.*;
|
||||||
import thito.nodeflow.config.Section;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.LinkedHashSet;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unit tests for {@link YamlMapper}.
|
* Unit tests for {@link YamlMapper}.
|
||||||
* @author marhali
|
* @author marhali
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
public class YamlMapperTest extends AbstractMapperTest {
|
public class YamlMapperTest extends AbstractMapperTest {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void testNonSorting() {
|
public void testNonSorting() {
|
||||||
Section input = new MapSection();
|
Map<String, Object> input = new HashMap<>();
|
||||||
input.set("zulu", "test");
|
input.put("zulu", "test");
|
||||||
input.set("alpha", "test");
|
input.put("alpha", "test");
|
||||||
input.set("bravo", "test");
|
input.put("bravo", "test");
|
||||||
|
|
||||||
TranslationData data = new TranslationData(false);
|
TranslationData data = new TranslationData(false);
|
||||||
YamlMapper.read("en", input, data.getRootNode());
|
YamlMapper.read("en", input, data.getRootNode());
|
||||||
|
|
||||||
Section output = new MapSection();
|
Map<String, Object> output = new HashMap<>();
|
||||||
YamlMapper.write("en", output, data.getRootNode());
|
YamlMapper.write("en", output, data.getRootNode());
|
||||||
|
|
||||||
Set<String> expect = new LinkedHashSet<>(Arrays.asList("zulu", "alpha", "bravo"));
|
Set<String> expect = new LinkedHashSet<>(Arrays.asList("zulu", "alpha", "bravo"));
|
||||||
Assert.assertEquals(expect, output.getKeys());
|
Assert.assertEquals(expect, output.keySet());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void testSorting() {
|
public void testSorting() {
|
||||||
Section input = new MapSection();
|
Map<String, Object> input = new HashMap<>();
|
||||||
input.set("zulu", "test");
|
input.put("zulu", "test");
|
||||||
input.set("alpha", "test");
|
input.put("alpha", "test");
|
||||||
input.set("bravo", "test");
|
input.put("bravo", "test");
|
||||||
|
|
||||||
TranslationData data = new TranslationData(true);
|
TranslationData data = new TranslationData(true);
|
||||||
YamlMapper.read("en", input, data.getRootNode());
|
YamlMapper.read("en", input, data.getRootNode());
|
||||||
|
|
||||||
Section output = new MapSection();
|
Map<String, Object> output = new HashMap<>();
|
||||||
YamlMapper.write("en", output, data.getRootNode());
|
YamlMapper.write("en", output, data.getRootNode());
|
||||||
|
|
||||||
Set<String> expect = new LinkedHashSet<>(Arrays.asList("alpha", "bravo", "zulu"));
|
Set<String> expect = new LinkedHashSet<>(Arrays.asList("alpha", "bravo", "zulu"));
|
||||||
Assert.assertEquals(expect, output.getKeys());
|
Assert.assertEquals(expect, output.keySet());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -61,13 +57,13 @@ public class YamlMapperTest extends AbstractMapperTest {
|
|||||||
data.setTranslation(new KeyPath("simple"), create(arraySimple));
|
data.setTranslation(new KeyPath("simple"), create(arraySimple));
|
||||||
data.setTranslation(new KeyPath("escaped"), create(arrayEscaped));
|
data.setTranslation(new KeyPath("escaped"), create(arrayEscaped));
|
||||||
|
|
||||||
Section output = new MapSection();
|
Map<String, Object> output = new HashMap<>();
|
||||||
YamlMapper.write("en", output, data.getRootNode());
|
YamlMapper.write("en", output, data.getRootNode());
|
||||||
|
|
||||||
Assert.assertTrue(output.isList("simple"));
|
Assert.assertTrue(output.get("simple") instanceof List);
|
||||||
Assert.assertEquals(arraySimple, YamlArrayMapper.read(output.getList("simple").get()));
|
Assert.assertEquals(arraySimple, YamlArrayMapper.read((List<Object>) output.get("simple")));
|
||||||
Assert.assertTrue(output.isList("escaped"));
|
Assert.assertTrue(output.get("escaped") instanceof List);
|
||||||
Assert.assertEquals(arrayEscaped, StringEscapeUtils.unescapeJava(YamlArrayMapper.read(output.getList("escaped").get())));
|
Assert.assertEquals(arrayEscaped, StringEscapeUtils.unescapeJava(YamlArrayMapper.read((List<Object>) output.get("escaped"))));
|
||||||
|
|
||||||
TranslationData input = new TranslationData(true);
|
TranslationData input = new TranslationData(true);
|
||||||
YamlMapper.read("en", output, input.getRootNode());
|
YamlMapper.read("en", output, input.getRootNode());
|
||||||
@ -81,10 +77,10 @@ public class YamlMapperTest extends AbstractMapperTest {
|
|||||||
TranslationData data = new TranslationData(true);
|
TranslationData data = new TranslationData(true);
|
||||||
data.setTranslation(new KeyPath("chars"), create(specialCharacters));
|
data.setTranslation(new KeyPath("chars"), create(specialCharacters));
|
||||||
|
|
||||||
Section output = new MapSection();
|
Map<String, Object> output = new HashMap<>();
|
||||||
YamlMapper.write("en", output, data.getRootNode());
|
YamlMapper.write("en", output, data.getRootNode());
|
||||||
|
|
||||||
Assert.assertEquals(specialCharacters, output.getString("chars").get());
|
Assert.assertEquals(specialCharacters, output.get("chars"));
|
||||||
|
|
||||||
TranslationData input = new TranslationData(true);
|
TranslationData input = new TranslationData(true);
|
||||||
YamlMapper.read("en", output, input.getRootNode());
|
YamlMapper.read("en", output, input.getRootNode());
|
||||||
@ -98,10 +94,13 @@ public class YamlMapperTest extends AbstractMapperTest {
|
|||||||
TranslationData data = new TranslationData(true);
|
TranslationData data = new TranslationData(true);
|
||||||
data.setTranslation(new KeyPath("nested", "key", "section"), create("test"));
|
data.setTranslation(new KeyPath("nested", "key", "section"), create("test"));
|
||||||
|
|
||||||
Section output = new MapSection();
|
Map<String, Object> output = new HashMap<>();
|
||||||
YamlMapper.write("en", output, data.getRootNode());
|
YamlMapper.write("en", output, data.getRootNode());
|
||||||
|
|
||||||
Assert.assertEquals("test", output.getString("nested.key.section").get());
|
Assert.assertTrue(output.containsKey("nested"));
|
||||||
|
Assert.assertTrue(((Map<String, Object>) output.get("nested")).containsKey("key"));
|
||||||
|
|
||||||
|
Assert.assertEquals("test", ((Map)((Map)output.get("nested")).get("key")).get("section"));
|
||||||
|
|
||||||
TranslationData input = new TranslationData(true);
|
TranslationData input = new TranslationData(true);
|
||||||
YamlMapper.read("en", output, input.getRootNode());
|
YamlMapper.read("en", output, input.getRootNode());
|
||||||
@ -114,10 +113,10 @@ public class YamlMapperTest extends AbstractMapperTest {
|
|||||||
TranslationData data = new TranslationData(true);
|
TranslationData data = new TranslationData(true);
|
||||||
data.setTranslation(new KeyPath("long.key.with.many.sections"), create("test"));
|
data.setTranslation(new KeyPath("long.key.with.many.sections"), create("test"));
|
||||||
|
|
||||||
Section output = new MapSection();
|
Map<String, Object> output = new HashMap<>();
|
||||||
YamlMapper.write("en", output, data.getRootNode());
|
YamlMapper.write("en", output, data.getRootNode());
|
||||||
|
|
||||||
Assert.assertTrue(output.getKeys().contains("long.key.with.many.sections"));
|
Assert.assertTrue(output.containsKey("long.key.with.many.sections"));
|
||||||
|
|
||||||
TranslationData input = new TranslationData(true);
|
TranslationData input = new TranslationData(true);
|
||||||
YamlMapper.read("en", output, input.getRootNode());
|
YamlMapper.read("en", output, input.getRootNode());
|
||||||
@ -130,10 +129,10 @@ public class YamlMapperTest extends AbstractMapperTest {
|
|||||||
TranslationData data = new TranslationData(true);
|
TranslationData data = new TranslationData(true);
|
||||||
data.setTranslation(new KeyPath("space"), create(leadingSpace));
|
data.setTranslation(new KeyPath("space"), create(leadingSpace));
|
||||||
|
|
||||||
Section output = new MapSection();
|
Map<String, Object> output = new HashMap<>();
|
||||||
YamlMapper.write("en", output, data.getRootNode());
|
YamlMapper.write("en", output, data.getRootNode());
|
||||||
|
|
||||||
Assert.assertEquals(leadingSpace, output.getString("space").get());
|
Assert.assertEquals(leadingSpace, output.get("space"));
|
||||||
|
|
||||||
TranslationData input = new TranslationData(true);
|
TranslationData input = new TranslationData(true);
|
||||||
YamlMapper.read("en", output, input.getRootNode());
|
YamlMapper.read("en", output, input.getRootNode());
|
||||||
@ -146,13 +145,13 @@ public class YamlMapperTest extends AbstractMapperTest {
|
|||||||
TranslationData data = new TranslationData(true);
|
TranslationData data = new TranslationData(true);
|
||||||
data.setTranslation(new KeyPath("numbered"), create("15000"));
|
data.setTranslation(new KeyPath("numbered"), create("15000"));
|
||||||
|
|
||||||
Section output = new MapSection();
|
Map<String, Object> output = new HashMap<>();
|
||||||
YamlMapper.write("en", output, data.getRootNode());
|
YamlMapper.write("en", output, data.getRootNode());
|
||||||
|
|
||||||
Assert.assertEquals(15000, output.getInteger("numbered").get().intValue());
|
Assert.assertEquals(15000, output.get("numbered"));
|
||||||
|
|
||||||
Section input = new MapSection();
|
Map<String, Object> input = new HashMap<>();
|
||||||
input.set("numbered", 143.23);
|
input.put("numbered", 143.23);
|
||||||
YamlMapper.read("en", input, data.getRootNode());
|
YamlMapper.read("en", input, data.getRootNode());
|
||||||
|
|
||||||
Assert.assertEquals("143.23", data.getTranslation(new KeyPath("numbered")).get("en"));
|
Assert.assertEquals("143.23", data.getTranslation(new KeyPath("numbered")).get("en"));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user