PlotSquared/Core/src/main/java/com/plotsquared/core/configuration/file/YamlConstructor.java

72 lines
2.6 KiB
Java
Raw Normal View History

/*
* PlotSquared, a land and world management plugin for Minecraft.
* Copyright (C) IntellectualSites <https://intellectualsites.com>
* Copyright (C) IntellectualSites team and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2020-04-15 21:26:54 +02:00
package com.plotsquared.core.configuration.file;
2015-07-05 12:51:34 +02:00
2020-04-15 21:26:54 +02:00
import com.plotsquared.core.configuration.serialization.ConfigurationSerialization;
import org.yaml.snakeyaml.LoaderOptions;
2015-07-05 12:51:34 +02:00
import org.yaml.snakeyaml.constructor.SafeConstructor;
import org.yaml.snakeyaml.error.YAMLException;
2015-07-05 17:44:10 +02:00
import org.yaml.snakeyaml.nodes.Node;
2015-07-05 12:51:34 +02:00
import org.yaml.snakeyaml.nodes.Tag;
2016-02-21 05:07:04 +01:00
import java.util.LinkedHashMap;
import java.util.Map;
2015-07-05 12:51:34 +02:00
2019-02-04 16:18:50 +01:00
public class YamlConstructor extends SafeConstructor {
2018-08-10 17:01:10 +02:00
2016-04-30 00:14:12 +02:00
YamlConstructor() {
super(new LoaderOptions());
2015-09-11 12:09:22 +02:00
yamlConstructors.put(Tag.MAP, new ConstructCustomObject());
2015-07-05 12:51:34 +02:00
}
2018-08-10 17:01:10 +02:00
2015-09-13 06:04:31 +02:00
private class ConstructCustomObject extends ConstructYamlMap {
@Override
public Object construct(final Node node) {
2015-09-13 06:04:31 +02:00
if (node.isTwoStepsConstruction()) {
throw new YAMLException("Unexpected referential mapping structure. Node: " + node);
}
2018-08-10 17:01:10 +02:00
2015-09-11 12:09:22 +02:00
final Map<?, ?> raw = (Map<?, ?>) super.construct(node);
2018-08-10 17:01:10 +02:00
2015-09-13 06:04:31 +02:00
if (raw.containsKey(ConfigurationSerialization.SERIALIZED_TYPE_KEY)) {
2016-02-21 05:07:04 +01:00
final Map<String, Object> typed = new LinkedHashMap<>(raw.size());
2015-09-13 06:04:31 +02:00
for (final Map.Entry<?, ?> entry : raw.entrySet()) {
2015-07-05 12:51:34 +02:00
typed.put(entry.getKey().toString(), entry.getValue());
}
2018-08-10 17:01:10 +02:00
2015-09-13 06:04:31 +02:00
try {
2015-07-05 12:51:34 +02:00
return ConfigurationSerialization.deserializeObject(typed);
2015-09-13 06:04:31 +02:00
} catch (final IllegalArgumentException ex) {
2015-07-05 12:51:34 +02:00
throw new YAMLException("Could not deserialize object", ex);
}
}
2018-08-10 17:01:10 +02:00
2015-07-05 12:51:34 +02:00
return raw;
}
2018-08-10 17:01:10 +02:00
@Override
public void construct2ndStep(final Node node, final Object object) {
2015-07-05 12:51:34 +02:00
throw new YAMLException("Unexpected referential mapping structure. Node: " + node);
}
2015-07-05 12:51:34 +02:00
}
2015-07-05 12:51:34 +02:00
}