From 17cdbcac12585b433f7752fbcacacd7ff8c7aeb5 Mon Sep 17 00:00:00 2001 From: zml2008 Date: Tue, 10 Jan 2012 15:10:17 -0800 Subject: [PATCH] Improved formatting of YAMLProcessor output --- .../com/sk89q/util/yaml/YAMLProcessor.java | 32 ++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/sk89q/util/yaml/YAMLProcessor.java b/src/main/java/com/sk89q/util/yaml/YAMLProcessor.java index eec16c575..e146d8025 100644 --- a/src/main/java/com/sk89q/util/yaml/YAMLProcessor.java +++ b/src/main/java/com/sk89q/util/yaml/YAMLProcessor.java @@ -22,7 +22,11 @@ package com.sk89q.util.yaml; import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.constructor.SafeConstructor; +import org.yaml.snakeyaml.emitter.ScalarAnalysis; +import org.yaml.snakeyaml.nodes.Node; +import org.yaml.snakeyaml.nodes.Tag; import org.yaml.snakeyaml.reader.UnicodeReader; +import org.yaml.snakeyaml.representer.Represent; import org.yaml.snakeyaml.representer.Representer; import java.io.*; @@ -58,8 +62,6 @@ import java.util.Map; * getBoolean("sturmeh.eats.babies", false). For lists, there are * methods such as getStringList that will return a type safe list. * - *

This class is currently incomplete. It is not yet possible to get a node. - *

* * @author sk89q */ @@ -71,10 +73,10 @@ public class YAMLProcessor extends YAMLNode { public YAMLProcessor(File file, boolean writeDefaults, YAMLFormat format) { super(new HashMap(), writeDefaults); - DumperOptions options = new DumperOptions(); + DumperOptions options = new FancyDumperOptions(); options.setIndent(4); options.setDefaultFlowStyle(format.getStyle()); - Representer representer = new Representer(); + Representer representer = new FancyRepresenter(); representer.setDefaultFlowStyle(format.getStyle()); yaml = new Yaml(new SafeConstructor(), representer, options); @@ -215,4 +217,26 @@ public class YAMLProcessor extends YAMLNode { public static YAMLNode getEmptyNode(boolean writeDefaults) { return new YAMLNode(new HashMap(), writeDefaults); } + + private static class FancyDumperOptions extends DumperOptions { + @Override + public DumperOptions.ScalarStyle calculateScalarStyle(ScalarAnalysis analysis, + DumperOptions.ScalarStyle style) { + if (analysis.scalar.contains("\n") || analysis.scalar.contains("\r")) { + return ScalarStyle.LITERAL; + } else { + return super.calculateScalarStyle(analysis, style); + } + } + } + + private static class FancyRepresenter extends Representer { + public FancyRepresenter() { + this.nullRepresenter = new Represent() { + public Node representData(Object o) { + return representScalar(Tag.NULL, ""); + } + }; + } + } }