Added split lines option to DumperOptions to allow line splitting to be disabled.
diff --git a/src/main/java/org/yaml/snakeyaml/DumperOptions.java b/src/main/java/org/yaml/snakeyaml/DumperOptions.java
index 71bae7f..bc8a19a 100644
--- a/src/main/java/org/yaml/snakeyaml/DumperOptions.java
+++ b/src/main/java/org/yaml/snakeyaml/DumperOptions.java
@@ -165,6 +165,7 @@
private boolean allowReadOnlyProperties = false;
private int indent = 2;
private int bestWidth = 80;
+ private boolean splitLines = true;
private LineBreak lineBreak = LineBreak.UNIX;
private boolean explicitStart = false;
private boolean explicitEnd = false;
@@ -265,7 +266,7 @@
* split into a few lines. The default is 80.
*
* @param bestWidth
- * the preferred with for scalars.
+ * the preferred width for scalars.
*/
public void setWidth(int bestWidth) {
this.bestWidth = bestWidth;
@@ -275,6 +276,21 @@
return this.bestWidth;
}
+ /**
+ * Specify whether to split lines exceeding preferred width for
+ * scalars. The default is true.
+ *
+ * @param splitLines
+ * whether to split lines exceeding preferred width for scalars.
+ */
+ public void setSplitLines(boolean splitLines) {
+ this.splitLines = splitLines;
+ }
+
+ public boolean getSplitLines() {
+ return this.splitLines;
+ }
+
public LineBreak getLineBreak() {
return lineBreak;
}
diff --git a/src/main/java/org/yaml/snakeyaml/emitter/Emitter.java b/src/main/java/org/yaml/snakeyaml/emitter/Emitter.java
index c7de823..1d47653 100644
--- a/src/main/java/org/yaml/snakeyaml/emitter/Emitter.java
+++ b/src/main/java/org/yaml/snakeyaml/emitter/Emitter.java
@@ -138,6 +138,7 @@
private int bestIndent;
private int bestWidth;
private char[] bestLineBreak;
+ private boolean splitLines;
// Tag prefixes.
private Map<String, String> tagPrefixes;
@@ -196,6 +197,7 @@
this.bestWidth = opts.getWidth();
}
this.bestLineBreak = opts.getLineBreak().getString().toCharArray();
+ this.splitLines = opts.getSplitLines();
// Tag prefixes.
this.tagPrefixes = new LinkedHashMap<String, String>();
@@ -445,7 +447,7 @@
writeIndicator("]", false, false, false);
state = states.pop();
} else {
- if (canonical || column > bestWidth || prettyFlow) {
+ if (canonical || (column > bestWidth && splitLines) || prettyFlow) {
writeIndent();
}
states.push(new ExpectFlowSequenceItem());
@@ -499,7 +501,7 @@
writeIndicator("}", false, false, false);
state = states.pop();
} else {
- if (canonical || column > bestWidth || prettyFlow) {
+ if (canonical || (column > bestWidth && splitLines) || prettyFlow) {
writeIndent();
}
if (!canonical && checkSimpleKey()) {
@@ -530,7 +532,7 @@
state = states.pop();
} else {
writeIndicator(",", false, false, false);
- if (canonical || column > bestWidth || prettyFlow) {
+ if (canonical || (column > bestWidth && splitLines) || prettyFlow) {
writeIndent();
}
if (!canonical && checkSimpleKey()) {
@@ -555,7 +557,7 @@
private class ExpectFlowMappingValue implements EmitterState {
public void expect() throws IOException {
- if (canonical || column > bestWidth || prettyFlow) {
+ if (canonical || (column > bestWidth && splitLines) || prettyFlow) {
writeIndent();
}
writeIndicator(":", true, false, false);
@@ -792,7 +794,7 @@
if (style == null) {
style = chooseScalarStyle();
}
- boolean split = !simpleKeyContext;
+ boolean split = !simpleKeyContext && splitLines;
if (style == null) {
writePlain(analysis.scalar, split);
} else {
@@ -804,7 +806,7 @@
writeSingleQuoted(analysis.scalar, split);
break;
case '>':
- writeFolded(analysis.scalar);
+ writeFolded(analysis.scalar, split);
break;
case '|':
writeLiteral(analysis.scalar);
@@ -1293,7 +1295,7 @@
return hints.toString();
}
- void writeFolded(String text) throws IOException {
+ void writeFolded(String text, boolean split) throws IOException {
String hints = determineBlockHints(text);
writeIndicator(">" + hints, true, false, false);
if (hints.length() > 0 && (hints.charAt(hints.length() - 1) == '+')) {
@@ -1330,7 +1332,7 @@
}
} else if (spaces) {
if (ch != ' ') {
- if (start + 1 == end && this.column > this.bestWidth) {
+ if (start + 1 == end && this.column > this.bestWidth && split) {
writeIndent();
} else {
int len = end - start;
diff --git a/src/test/java/org/yaml/snakeyaml/DumperOptionsTest.java b/src/test/java/org/yaml/snakeyaml/DumperOptionsTest.java
index 6b83aae..c8feefd 100644
--- a/src/test/java/org/yaml/snakeyaml/DumperOptionsTest.java
+++ b/src/test/java/org/yaml/snakeyaml/DumperOptionsTest.java
@@ -347,4 +347,90 @@
map.put("c", list);
assertEquals("a: b\nc:\n- 1\n- 2\n- 3\n", yaml.dump(map));
}
+
+ public void testSplitLinesDoubleQuoted() {
+ DumperOptions options = new DumperOptions();
+ options.setDefaultScalarStyle(DumperOptions.ScalarStyle.DOUBLE_QUOTED);
+ Yaml yaml;
+ String output;
+
+ // Split lines enabled (default)
+ assertTrue(options.getSplitLines());
+ yaml = new Yaml(options);
+ output = yaml.dump("1111111111 2222222222 3333333333 4444444444 5555555555 6666666666 7777777777 8888888888 9999999999 0000000000");
+ assertEquals("\"1111111111 2222222222 3333333333 4444444444 5555555555 6666666666 7777777777 8888888888\\\n \\ 9999999999 0000000000\"\n", output);
+
+ // Split lines disabled
+ options.setSplitLines(false);
+ assertFalse(options.getSplitLines());
+ yaml = new Yaml(options);
+ output = yaml.dump("1111111111 2222222222 3333333333 4444444444 5555555555 6666666666 7777777777 8888888888 9999999999 0000000000");
+ assertEquals("\"1111111111 2222222222 3333333333 4444444444 5555555555 6666666666 7777777777 8888888888 9999999999 0000000000\"\n", output);
+ }
+
+ public void testSplitLinesSingleQuoted() {
+ DumperOptions options = new DumperOptions();
+ options.setDefaultScalarStyle(DumperOptions.ScalarStyle.SINGLE_QUOTED);
+ Yaml yaml;
+ String output;
+
+ // Split lines enabled (default)
+ assertTrue(options.getSplitLines());
+ yaml = new Yaml(options);
+ output = yaml.dump("1111111111 2222222222 3333333333 4444444444 5555555555 6666666666 7777777777 8888888888 9999999999 0000000000");
+ assertEquals("'1111111111 2222222222 3333333333 4444444444 5555555555 6666666666 7777777777 8888888888\n 9999999999 0000000000'\n", output);
+
+ // Split lines disabled
+ options.setSplitLines(false);
+ assertFalse(options.getSplitLines());
+ yaml = new Yaml(options);
+ output = yaml.dump("1111111111 2222222222 3333333333 4444444444 5555555555 6666666666 7777777777 8888888888 9999999999 0000000000");
+ assertEquals("'1111111111 2222222222 3333333333 4444444444 5555555555 6666666666 7777777777 8888888888 9999999999 0000000000'\n", output);
+ }
+
+ public void testSplitLinesFolded() {
+ DumperOptions options = new DumperOptions();
+ options.setDefaultScalarStyle(DumperOptions.ScalarStyle.FOLDED);
+ Yaml yaml;
+ String output;
+
+ // Split lines enabled (default)
+ assertTrue(options.getSplitLines());
+ yaml = new Yaml(options);
+ output = yaml.dump("1111111111 2222222222 3333333333 4444444444 5555555555 6666666666 7777777777 8888888888 9999999999 0000000000");
+ assertEquals(">-\n 1111111111 2222222222 3333333333 4444444444 5555555555 6666666666 7777777777 8888888888\n 9999999999 0000000000\n", output);
+
+ // Split lines disabled
+ options.setSplitLines(false);
+ assertFalse(options.getSplitLines());
+ yaml = new Yaml(options);
+ output = yaml.dump("1111111111 2222222222 3333333333 4444444444 5555555555 6666666666 7777777777 8888888888 9999999999 0000000000");
+ assertEquals(">-\n 1111111111 2222222222 3333333333 4444444444 5555555555 6666666666 7777777777 8888888888 9999999999 0000000000\n", output);
+ }
+
+ public void testSplitLinesLiteral() {
+ DumperOptions options = new DumperOptions();
+ options.setDefaultScalarStyle(DumperOptions.ScalarStyle.LITERAL);
+ Yaml yaml;
+ String output;
+
+ // Split lines enabled (default) -- split lines does not apply to literal style
+ assertTrue(options.getSplitLines());
+ yaml = new Yaml(options);
+ output = yaml.dump("1111111111 2222222222 3333333333 4444444444 5555555555 6666666666 7777777777 8888888888 9999999999 0000000000");
+ assertEquals("|-\n 1111111111 2222222222 3333333333 4444444444 5555555555 6666666666 7777777777 8888888888 9999999999 0000000000\n", output);
+ }
+
+ public void testSplitLinesPlain() {
+ DumperOptions options = new DumperOptions();
+ options.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN);
+ Yaml yaml;
+ String output;
+
+ // Split lines enabled (default) -- split lines does not apply to plain style
+ assertTrue(options.getSplitLines());
+ yaml = new Yaml(options);
+ output = yaml.dump("1111111111 2222222222 3333333333 4444444444 5555555555 6666666666 7777777777 8888888888 9999999999 0000000000");
+ assertEquals("1111111111 2222222222 3333333333 4444444444 5555555555 6666666666 7777777777 8888888888 9999999999 0000000000\n", output);
+ }
}
diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue29/FlexibleScalarStyleTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue29/FlexibleScalarStyleTest.java
index 4e079a5..8565673 100644
--- a/src/test/java/org/yaml/snakeyaml/issues/issue29/FlexibleScalarStyleTest.java
+++ b/src/test/java/org/yaml/snakeyaml/issues/issue29/FlexibleScalarStyleTest.java
@@ -68,6 +68,17 @@
assertEquals(etalon, output);
}
+ public void testCustomScalarStyleNoSplitLines() {
+ DumperOptions options = new DumperOptions();
+ options.setWidth(30);
+ options.setSplitLines(false);
+ Yaml yaml = new Yaml(new MyRepresenter(), options);
+ String output = yaml.dump(getData());
+ // System.out.println(output);
+ String etalon = Util.getLocalResource("representer/scalar-style3.yaml");
+ assertEquals(etalon, output);
+ }
+
private Map<String, String> getData() {
Map<String, String> map = new LinkedHashMap<String, String>();
map.put("name", "Steve Jobs");
diff --git a/src/test/java/org/yaml/snakeyaml/issues/issue52/LineBreakDooubleQuotedTest.java b/src/test/java/org/yaml/snakeyaml/issues/issue52/LineBreakDooubleQuotedTest.java
index 29a8e78..2616777 100644
--- a/src/test/java/org/yaml/snakeyaml/issues/issue52/LineBreakDooubleQuotedTest.java
+++ b/src/test/java/org/yaml/snakeyaml/issues/issue52/LineBreakDooubleQuotedTest.java
@@ -39,4 +39,19 @@
String parsed = (String) yaml.load(output);
assertEquals(etalon, parsed);
}
+
+ public void testDoubleQuotedStyleNoLineSplit() {
+ DumperOptions options = new DumperOptions();
+ options.setDefaultScalarStyle(ScalarStyle.DOUBLE_QUOTED);
+ options.setWidth(20);
+ options.setSplitLines(false);
+ options.setIndent(4);
+ Yaml yaml = new Yaml(options);
+ String etalon = "12345678901234567890\n\n123 456";
+ String output = yaml.dump(etalon);
+ // System.out.println(output);
+ assertEquals("\"12345678901234567890\\n\\n123 456\"\n", output);
+ String parsed = (String) yaml.load(output);
+ assertEquals(etalon, parsed);
+ }
}
diff --git a/src/test/resources/representer/scalar-style3.yaml b/src/test/resources/representer/scalar-style3.yaml
new file mode 100644
index 0000000..8402790
--- /dev/null
+++ b/src/test/resources/representer/scalar-style3.yaml
@@ -0,0 +1,7 @@
+name: Steve Jobs
+address: |-
+ Name
+ Street Number
+ Country
+description: >-
+ 1111111111 2222222222 3333333333 4444444444 5555555555 6666666666 7777777777 8888888888 9999999999 0000000000