Search in sources :

Example 11 with Representer

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);
}
Also used : Representer(org.yaml.snakeyaml.representer.Representer) DumperOptions(org.yaml.snakeyaml.DumperOptions) SafeConstructor(org.yaml.snakeyaml.constructor.SafeConstructor) Yaml(org.yaml.snakeyaml.Yaml)

Example 12 with Representer

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;
}
Also used : FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Yaml(org.yaml.snakeyaml.Yaml) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Representer(org.yaml.snakeyaml.representer.Representer) BufferedReader(java.io.BufferedReader) SafeConstructor(org.yaml.snakeyaml.constructor.SafeConstructor) DumperOptions(org.yaml.snakeyaml.DumperOptions) FileReader(java.io.FileReader) Map(java.util.Map)

Example 13 with Representer

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);
}
Also used : Representer(org.yaml.snakeyaml.representer.Representer) DumperOptions(org.yaml.snakeyaml.DumperOptions) Yaml(org.yaml.snakeyaml.Yaml)

Example 14 with Representer

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);
}
Also used : Representer(org.yaml.snakeyaml.representer.Representer) YamlFormatter(org.rundeck.toolbelt.format.yaml.snakeyaml.YamlFormatter) DumperOptions(org.yaml.snakeyaml.DumperOptions) Yaml(org.yaml.snakeyaml.Yaml)

Example 15 with Representer

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);
}
Also used : LoaderOptions(org.yaml.snakeyaml.LoaderOptions) Representer(org.yaml.snakeyaml.representer.Representer) DumperOptions(org.yaml.snakeyaml.DumperOptions) Yaml(org.yaml.snakeyaml.Yaml)

Aggregations

Representer (org.yaml.snakeyaml.representer.Representer)82 Yaml (org.yaml.snakeyaml.Yaml)77 DumperOptions (org.yaml.snakeyaml.DumperOptions)54 Constructor (org.yaml.snakeyaml.constructor.Constructor)25 LoaderOptions (org.yaml.snakeyaml.LoaderOptions)21 FileInputStream (java.io.FileInputStream)12 SafeConstructor (org.yaml.snakeyaml.constructor.SafeConstructor)11 IOException (java.io.IOException)7 Tag (org.yaml.snakeyaml.nodes.Tag)7 File (java.io.File)6 TypeDescription (org.yaml.snakeyaml.TypeDescription)6 BaseConstructor (org.yaml.snakeyaml.constructor.BaseConstructor)5 FileNotFoundException (java.io.FileNotFoundException)4 InputStream (java.io.InputStream)4 PropertyUtils (org.yaml.snakeyaml.introspector.PropertyUtils)4 FileOutputStream (java.io.FileOutputStream)3 FileReader (java.io.FileReader)3 OutputStreamWriter (java.io.OutputStreamWriter)3 PrintWriter (java.io.PrintWriter)3 Map (java.util.Map)3