mirror of
https://github.com/plexusorg/toml4j.git
synced 2025-02-11 11:40:27 +00:00
Created TomlWriter.Builder, so TomlWriter and IndentationPolicy can be
immutable
This commit is contained in:
parent
0f42d37f60
commit
56be048ab1
7 changed files with 80 additions and 93 deletions
|
@ -98,7 +98,6 @@ class DateConverter implements ValueConverter, ValueWriter {
|
||||||
dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:m:ss");
|
dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:m:ss");
|
||||||
}
|
}
|
||||||
dateFormat.setTimeZone(context.getTomlWriter().getTimeZone());
|
dateFormat.setTimeZone(context.getTomlWriter().getTimeZone());
|
||||||
|
|
||||||
context.write(dateFormat.format(value));
|
context.write(dateFormat.format(value));
|
||||||
|
|
||||||
if (customDateFormat == null) {
|
if (customDateFormat == null) {
|
||||||
|
|
|
@ -17,9 +17,7 @@ class PrimitiveArrayValueWriter extends ArrayValueWriter {
|
||||||
Collection values = normalize(value);
|
Collection values = normalize(value);
|
||||||
|
|
||||||
context.write('[');
|
context.write('[');
|
||||||
if (!context.getTomlWriter().wantTerseArrays()) {
|
context.writeArrayDelimiterPadding();
|
||||||
context.write(' ');
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean first = true;
|
boolean first = true;
|
||||||
ValueWriter firstWriter = null;
|
ValueWriter firstWriter = null;
|
||||||
|
@ -43,9 +41,7 @@ class PrimitiveArrayValueWriter extends ArrayValueWriter {
|
||||||
WRITERS.write(elem, context);
|
WRITERS.write(elem, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!context.getTomlWriter().wantTerseArrays()) {
|
context.writeArrayDelimiterPadding();
|
||||||
context.write(' ');
|
|
||||||
}
|
|
||||||
context.write(']');
|
context.write(']');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -33,16 +33,53 @@ import java.util.TimeZone;
|
||||||
* </code></pre>
|
* </code></pre>
|
||||||
*/
|
*/
|
||||||
public class TomlWriter {
|
public class TomlWriter {
|
||||||
|
|
||||||
|
public static class Builder {
|
||||||
|
private int keyIndentation;
|
||||||
|
private int tableIndentation;
|
||||||
|
private int arrayDelimiterPadding = 0;
|
||||||
|
|
||||||
|
public TomlWriter.Builder indentValuesBy(int spaces) {
|
||||||
|
this.keyIndentation = spaces;
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
private WriterIndentationPolicy indentationPolicy = new WriterIndentationPolicy();
|
public TomlWriter.Builder indentTablesBy(int spaces) {
|
||||||
private boolean wantTerseArraysValue = false;
|
this.tableIndentation = spaces;
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param spaces number of spaces to put between opening square bracket and first item and between closing square bracket and last item
|
||||||
|
* @return this TomlWriter.Builder instance
|
||||||
|
*/
|
||||||
|
public TomlWriter.Builder padArrayDelimitersBy(int spaces) {
|
||||||
|
this.arrayDelimiterPadding = spaces;
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TomlWriter build() {
|
||||||
|
return new TomlWriter(keyIndentation, tableIndentation, arrayDelimiterPadding);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private final WriterIndentationPolicy indentationPolicy;
|
||||||
private GregorianCalendar calendar = new GregorianCalendar();
|
private GregorianCalendar calendar = new GregorianCalendar();
|
||||||
private DateFormat customDateFormat = null;
|
private DateFormat customDateFormat = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a TomlWriter instance.
|
* Creates a TomlWriter instance.
|
||||||
*/
|
*/
|
||||||
public TomlWriter() {}
|
public TomlWriter() {
|
||||||
|
this(0, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
private TomlWriter(int keyIndentation, int tableIndentation, int arrayDelimiterPadding) {
|
||||||
|
this.indentationPolicy = new WriterIndentationPolicy(keyIndentation, tableIndentation, arrayDelimiterPadding);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write an Object into TOML String.
|
* Write an Object into TOML String.
|
||||||
|
@ -105,50 +142,6 @@ public class TomlWriter {
|
||||||
return indentationPolicy;
|
return indentationPolicy;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Set the {@link WriterIndentationPolicy} for this writer.
|
|
||||||
*
|
|
||||||
* If unset, the default policy (no indentation) is used.
|
|
||||||
*
|
|
||||||
* @param indentationPolicy the new policy
|
|
||||||
* @return this TomlWriter instance
|
|
||||||
*/
|
|
||||||
public TomlWriter setIndentationPolicy(WriterIndentationPolicy indentationPolicy) {
|
|
||||||
this.indentationPolicy = indentationPolicy;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <p>Control whitespace in arrays in the TOML output.</p>
|
|
||||||
*
|
|
||||||
* <p>Terse arrays = false (default):</p>
|
|
||||||
*
|
|
||||||
* <pre><code>
|
|
||||||
* a = [ 1, 2, 3 ]
|
|
||||||
* </code></pre>
|
|
||||||
*
|
|
||||||
* <p>Terse arrays = true:</p>
|
|
||||||
*
|
|
||||||
* <pre><code>
|
|
||||||
* a = [1,2,3]
|
|
||||||
* </code></pre>
|
|
||||||
*
|
|
||||||
* @param value terse arrays setting
|
|
||||||
* @return this TomlWriter instance
|
|
||||||
*/
|
|
||||||
public TomlWriter wantTerseArrays(boolean value) {
|
|
||||||
this.wantTerseArraysValue = value;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the current array whitespace policy
|
|
||||||
* @return the current policy
|
|
||||||
*/
|
|
||||||
public boolean wantTerseArrays() {
|
|
||||||
return wantTerseArraysValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the {@link TimeZone} used when formatting datetimes.
|
* Set the {@link TimeZone} used when formatting datetimes.
|
||||||
*
|
*
|
||||||
|
|
|
@ -93,6 +93,12 @@ class WriterContext {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void writeArrayDelimiterPadding() {
|
||||||
|
for (int i = 0; i < tomlWriter.getIndentationPolicy().getArrayDelimiterPadding(); i++) {
|
||||||
|
write(' ');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void indent() {
|
void indent() {
|
||||||
if (!key.isEmpty()) {
|
if (!key.isEmpty()) {
|
||||||
write(currentFieldIndent);
|
write(currentFieldIndent);
|
||||||
|
|
|
@ -5,37 +5,26 @@ package com.moandjiezana.toml;
|
||||||
*
|
*
|
||||||
* The default policy is to not indent.
|
* The default policy is to not indent.
|
||||||
*/
|
*/
|
||||||
public class WriterIndentationPolicy {
|
class WriterIndentationPolicy {
|
||||||
private int tableIndent = 0;
|
private final int tableIndent;
|
||||||
private int keyValueIndent = 0;
|
private final int keyValueIndent;
|
||||||
|
private final int arrayDelimiterPadding;
|
||||||
|
|
||||||
public int getTableIndent() {
|
WriterIndentationPolicy(int keyIndentation, int tableIndentation, int arrayDelimiterPadding) {
|
||||||
|
this.keyValueIndent = keyIndentation;
|
||||||
|
this.tableIndent = tableIndentation;
|
||||||
|
this.arrayDelimiterPadding = arrayDelimiterPadding;
|
||||||
|
}
|
||||||
|
|
||||||
|
int getTableIndent() {
|
||||||
return tableIndent;
|
return tableIndent;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
int getKeyValueIndent() {
|
||||||
* Sets the number of spaces a nested table name is indented.
|
|
||||||
*
|
|
||||||
* @param tableIndent number of spaces to indent
|
|
||||||
* @return this WriterIndentationPolicy instance
|
|
||||||
*/
|
|
||||||
public WriterIndentationPolicy setTableIndent(int tableIndent) {
|
|
||||||
this.tableIndent = tableIndent;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getKeyValueIndent() {
|
|
||||||
return keyValueIndent;
|
return keyValueIndent;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
int getArrayDelimiterPadding() {
|
||||||
* Sets the number of spaces key/value pairs within a table are indented.
|
return arrayDelimiterPadding;
|
||||||
*
|
|
||||||
* @param keyValueIndent number of spaces to indent
|
|
||||||
* @return this WriterIndentationPolicy instance
|
|
||||||
*/
|
|
||||||
public WriterIndentationPolicy setKeyValueIndent(int keyValueIndent) {
|
|
||||||
this.keyValueIndent = keyValueIndent;
|
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -114,7 +114,7 @@ public class BurntSushiValidEncoderTest {
|
||||||
public void table_array_nest_modified() throws Exception {
|
public void table_array_nest_modified() throws Exception {
|
||||||
// Modified to remove stray spaces in the expected TOML
|
// Modified to remove stray spaces in the expected TOML
|
||||||
runEncoder("table-array-nest-modified",
|
runEncoder("table-array-nest-modified",
|
||||||
new TomlWriter().setIndentationPolicy(new WriterIndentationPolicy().setTableIndent(2)));
|
new TomlWriter.Builder().indentTablesBy(2).build());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -126,8 +126,8 @@ public class BurntSushiValidEncoderTest {
|
||||||
|
|
||||||
private void runEncoder(String testName) {
|
private void runEncoder(String testName) {
|
||||||
runEncoder(testName,
|
runEncoder(testName,
|
||||||
new TomlWriter().
|
new TomlWriter.Builder().
|
||||||
wantTerseArrays(true).
|
build().
|
||||||
setTimeZone(TimeZone.getTimeZone("UTC")).
|
setTimeZone(TimeZone.getTimeZone("UTC")).
|
||||||
setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")));
|
setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")));
|
||||||
}
|
}
|
||||||
|
|
|
@ -123,9 +123,10 @@ public class TomlWriterTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void should_follow_indentation_policy_of_indented_values() {
|
public void should_follow_indentation_policy_of_indented_values() {
|
||||||
String output = new TomlWriter().
|
String output = new TomlWriter.Builder().
|
||||||
setIndentationPolicy(new WriterIndentationPolicy().setKeyValueIndent(2)).
|
indentValuesBy(2).
|
||||||
write(buildNestedMap());
|
build().
|
||||||
|
write(buildNestedMap());
|
||||||
String expected = "aBoolean = true\n\n" +
|
String expected = "aBoolean = true\n\n" +
|
||||||
"[aMap]\n" +
|
"[aMap]\n" +
|
||||||
" foo = 1\n" +
|
" foo = 1\n" +
|
||||||
|
@ -140,9 +141,10 @@ public class TomlWriterTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void should_follow_indentation_policy_of_indented_tables() {
|
public void should_follow_indentation_policy_of_indented_tables() {
|
||||||
String output = new TomlWriter().
|
String output = new TomlWriter.Builder().
|
||||||
setIndentationPolicy(new WriterIndentationPolicy().setTableIndent(2)).
|
indentTablesBy(2).
|
||||||
write(buildNestedMap());
|
build().
|
||||||
|
write(buildNestedMap());
|
||||||
String expected = "aBoolean = true\n\n" +
|
String expected = "aBoolean = true\n\n" +
|
||||||
"[aMap]\n" +
|
"[aMap]\n" +
|
||||||
"foo = 1\n" +
|
"foo = 1\n" +
|
||||||
|
@ -157,9 +159,11 @@ public class TomlWriterTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void should_follow_indentation_policy_of_indented_tables_and_values() {
|
public void should_follow_indentation_policy_of_indented_tables_and_values() {
|
||||||
String output = new TomlWriter().
|
String output = new TomlWriter.Builder().
|
||||||
setIndentationPolicy(new WriterIndentationPolicy().setTableIndent(2).setKeyValueIndent(2)).
|
indentValuesBy(2).
|
||||||
write(buildNestedMap());
|
indentTablesBy(2).
|
||||||
|
build().
|
||||||
|
write(buildNestedMap());
|
||||||
String expected = "aBoolean = true\n\n" +
|
String expected = "aBoolean = true\n\n" +
|
||||||
"[aMap]\n" +
|
"[aMap]\n" +
|
||||||
" foo = 1\n" +
|
" foo = 1\n" +
|
||||||
|
@ -202,7 +206,7 @@ public class TomlWriterTest {
|
||||||
}
|
}
|
||||||
ArrayTest arrayTest = new ArrayTest();
|
ArrayTest arrayTest = new ArrayTest();
|
||||||
|
|
||||||
String output = new TomlWriter().write(arrayTest);
|
String output = new TomlWriter.Builder().padArrayDelimitersBy(1).build().write(arrayTest);
|
||||||
String expected = "array = [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]\n";
|
String expected = "array = [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]\n";
|
||||||
assertEquals(expected, output);
|
assertEquals(expected, output);
|
||||||
}
|
}
|
||||||
|
@ -216,7 +220,7 @@ public class TomlWriterTest {
|
||||||
o.aList.add(1);
|
o.aList.add(1);
|
||||||
o.aList.add(2);
|
o.aList.add(2);
|
||||||
|
|
||||||
assertEquals("aList = [ 1, 2 ]\n", new TomlWriter().write(o));
|
assertEquals("aList = [ 1, 2 ]\n", new TomlWriter.Builder().padArrayDelimitersBy(1).build().write(o));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
Loading…
Reference in a new issue