use of org.yaml.snakeyaml.representer.Representer in project WorldGuard by EngineHub.
the class SQLRegionDatabase method createYaml.
/**
* Create a YAML dumper / parser.
*
* @return a YAML dumper / parser
*/
static Yaml createYaml() {
DumperOptions options = new DumperOptions();
options.setIndent(2);
options.setDefaultFlowStyle(FlowStyle.FLOW);
Representer representer = new Representer();
representer.setDefaultFlowStyle(FlowStyle.FLOW);
// We have to use this in order to properly save non-string values
return new Yaml(new SafeConstructor(), new Representer(), options);
}
use of org.yaml.snakeyaml.representer.Representer in project icpctools by icpctools.
the class YamlParser method importContestInfo.
public static Info importContestInfo(File f, boolean oldFormat) throws IOException {
if (f == null || !f.exists())
throw new FileNotFoundException("Contest config file not found");
BufferedReader br = new BufferedReader(new FileReader(f));
Yaml yaml = new Yaml(new SafeConstructor(), new Representer(), new DumperOptions(), new CustomYamlResolver());
Object obj = yaml.load(br);
// the file should have a top-level map of problems, which contains a list of problems
if (!(obj instanceof Map<?, ?>))
throw new IOException("Contest config file not imported: invalid format");
Map<?, ?> map = (Map<?, ?>) obj;
Info info = new Info();
info.add("id", "1");
for (Object ob : map.keySet()) {
if (ob instanceof String) {
String key = (String) ob;
Object val = map.get(key);
String value = null;
if (val != null)
value = val.toString();
try {
if ("name".equals(key) && oldFormat)
info.add("formal_name", value);
else if ("short-name".equals(key))
info.add("name", value);
else if ("length".equals(key) || "duration".equals(key)) {
int length = RelativeTime.parse(value);
if (length >= 0)
info.add("duration", RelativeTime.format(length));
} else if ("scoreboard-freeze".equals(key) || "scoreboard_freeze_duration".equals(key)) {
int length = RelativeTime.parse(value);
int d = info.getDuration();
if (length >= 0 && d > 0)
info.add("scoreboard_freeze_duration", RelativeTime.format(d / 1000 - length));
} else if ("scoreboard-freeze-length".equals(key)) {
int length = RelativeTime.parse(value);
if (length >= 0)
info.add("scoreboard_freeze_duration", RelativeTime.format(length));
} else if ("penalty-time".equals(key)) {
info.add("penalty_time", value);
} else if ("start-time".equals(key)) {
info.add("start_time", value);
} else
info.add(key, value);
} catch (Exception ex) {
Trace.trace(Trace.ERROR, "Could not parse " + key + ": " + value);
}
}
}
br.close();
return info;
}
use of org.yaml.snakeyaml.representer.Representer in project ns4kafka by michelin.
the class DiffSubcommand method unifiedDiff.
private List<String> unifiedDiff(Resource live, Resource merged) {
// ignore status and timestamp for comparison
if (live != null) {
live.setStatus(null);
live.getMetadata().setCreationTimestamp(null);
}
merged.setStatus(null);
merged.getMetadata().setCreationTimestamp(null);
DumperOptions options = new DumperOptions();
options.setExplicitStart(true);
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
Representer representer = new Representer();
representer.addClassTag(Resource.class, Tag.MAP);
Yaml yaml = new Yaml(representer, options);
List<String> oldResourceStr = live != null ? yaml.dump(live).lines().collect(Collectors.toList()) : List.of();
List<String> newResourceStr = yaml.dump(merged).lines().collect(Collectors.toList());
Patch<String> diff = DiffUtils.diff(oldResourceStr, newResourceStr);
return UnifiedDiffUtils.generateUnifiedDiff(String.format("%s/%s-LIVE", merged.getKind(), merged.getMetadata().getName()), String.format("%s/%s-MERGED", merged.getKind(), merged.getMetadata().getName()), oldResourceStr, diff, 3);
}
use of org.yaml.snakeyaml.representer.Representer in project rundeck-cli by rundeck.
the class Main method configYamlFormat.
private static void configYamlFormat(final ToolBelt belt, final RdClientConfig config) {
DumperOptions dumperOptions = new DumperOptions();
dumperOptions.setDefaultFlowStyle("BLOCK".equalsIgnoreCase(config.getString("RD_YAML_FLOW", "BLOCK")) ? DumperOptions.FlowStyle.BLOCK : DumperOptions.FlowStyle.FLOW);
dumperOptions.setPrettyFlow(config.getBool("RD_YAML_PRETTY", true));
Representer representer = new Representer();
representer.addClassTag(JobItem.class, Tag.MAP);
representer.addClassTag(ScheduledJobItem.class, Tag.MAP);
representer.addClassTag(DateInfo.class, Tag.MAP);
representer.addClassTag(Execution.class, Tag.MAP);
belt.formatter(new YamlFormatter(DataOutputAsFormatable, new Yaml(representer, dumperOptions)));
belt.channels().infoEnabled(false);
belt.channels().warningEnabled(false);
belt.channels().errorEnabled(false);
}
use of org.yaml.snakeyaml.representer.Representer in project spring-framework by bluecrow1986.
the class YamlProcessor method createYaml.
/**
* Create the {@link Yaml} instance to use.
* <p>The default implementation sets the "allowDuplicateKeys" flag to {@code false},
* enabling built-in duplicate key handling in SnakeYAML 1.18+.
* <p>As of Spring Framework 5.1.16, if custom {@linkplain #setSupportedTypes
* supported types} have been configured, the default implementation creates
* a {@code Yaml} instance that filters out unsupported types encountered in
* YAML documents. If an unsupported type is encountered, an
* {@link IllegalStateException} will be thrown when the node is processed.
* @see LoaderOptions#setAllowDuplicateKeys(boolean)
*/
protected Yaml createYaml() {
LoaderOptions loaderOptions = new LoaderOptions();
loaderOptions.setAllowDuplicateKeys(false);
if (!this.supportedTypes.isEmpty()) {
return new Yaml(new FilteringConstructor(loaderOptions), new Representer(), new DumperOptions(), loaderOptions);
}
return new Yaml(loaderOptions);
}
Aggregations