render option to configure indentation characters instead of hardcoded 4 spaces.

This commit is contained in:
aalleexxeeii 2016-07-02 21:28:57 +03:00
parent a8d691ad74
commit 07259c7e96
2 changed files with 37 additions and 19 deletions

View File

@ -17,17 +17,21 @@ package com.typesafe.config;
* </pre>
*/
public final class ConfigRenderOptions {
private final static String DEFAULT_INDENTATION = " ";
private final boolean originComments;
private final boolean comments;
private final boolean formatted;
private final boolean json;
private final String indentation;
private ConfigRenderOptions(boolean originComments, boolean comments, boolean formatted,
boolean json) {
boolean json, String indentation) {
this.originComments = originComments;
this.comments = comments;
this.formatted = formatted;
this.json = json;
this.indentation = indentation;
}
/**
@ -38,7 +42,7 @@ public final class ConfigRenderOptions {
* @return the default render options
*/
public static ConfigRenderOptions defaults() {
return new ConfigRenderOptions(true, true, true, true);
return new ConfigRenderOptions(true, true, true, true, DEFAULT_INDENTATION);
}
/**
@ -48,7 +52,7 @@ public final class ConfigRenderOptions {
* @return the concise render options
*/
public static ConfigRenderOptions concise() {
return new ConfigRenderOptions(false, false, false, true);
return new ConfigRenderOptions(false, false, false, true, DEFAULT_INDENTATION);
}
/**
@ -64,7 +68,7 @@ public final class ConfigRenderOptions {
if (value == comments)
return this;
else
return new ConfigRenderOptions(originComments, value, formatted, json);
return new ConfigRenderOptions(originComments, value, formatted, json, indentation);
}
/**
@ -97,7 +101,7 @@ public final class ConfigRenderOptions {
if (value == originComments)
return this;
else
return new ConfigRenderOptions(value, comments, formatted, json);
return new ConfigRenderOptions(value, comments, formatted, json, indentation);
}
/**
@ -122,7 +126,7 @@ public final class ConfigRenderOptions {
if (value == formatted)
return this;
else
return new ConfigRenderOptions(originComments, comments, value, json);
return new ConfigRenderOptions(originComments, comments, value, json, indentation);
}
/**
@ -150,7 +154,7 @@ public final class ConfigRenderOptions {
if (value == json)
return this;
else
return new ConfigRenderOptions(originComments, comments, formatted, value);
return new ConfigRenderOptions(originComments, comments, formatted, value, indentation);
}
/**
@ -163,6 +167,29 @@ public final class ConfigRenderOptions {
return json;
}
/**
* Returns options with indentation changed. The default value is 4 spaces.
*
* @param value
* characters to use for one level of indentation
* @return options with requested indentation setting
*/
public ConfigRenderOptions setIndentation(String value) {
if (value.equals(indentation))
return this;
else
return new ConfigRenderOptions(originComments, comments, formatted, json, value);
}
/**
* Returns indentation characters.
*
* @return characters used for one level of indentation
*/
public String getIndentation() {
return indentation;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder("ConfigRenderOptions(");

View File

@ -3,18 +3,9 @@
*/
package com.typesafe.config.impl;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import com.typesafe.config.*;
import com.typesafe.config.ConfigException;
import com.typesafe.config.ConfigMergeable;
import com.typesafe.config.ConfigObject;
import com.typesafe.config.ConfigOrigin;
import com.typesafe.config.ConfigRenderOptions;
import com.typesafe.config.ConfigValue;
import java.util.*;
/**
*
@ -322,7 +313,7 @@ abstract class AbstractConfigValue implements ConfigValue, MergeableValue {
if (options.getFormatted()) {
int remaining = indent;
while (remaining > 0) {
sb.append(" ");
sb.append(options.getIndentation());
--remaining;
}
}