From 07259c7e96ef98bae5f577c5fa49248b5cb38af8 Mon Sep 17 00:00:00 2001 From: aalleexxeeii Date: Sat, 2 Jul 2016 21:28:57 +0300 Subject: [PATCH] render option to configure indentation characters instead of hardcoded 4 spaces. --- .../typesafe/config/ConfigRenderOptions.java | 41 +++++++++++++++---- .../config/impl/AbstractConfigValue.java | 15 ++----- 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/config/src/main/java/com/typesafe/config/ConfigRenderOptions.java b/config/src/main/java/com/typesafe/config/ConfigRenderOptions.java index 7b07fabd..8e99d6de 100644 --- a/config/src/main/java/com/typesafe/config/ConfigRenderOptions.java +++ b/config/src/main/java/com/typesafe/config/ConfigRenderOptions.java @@ -17,17 +17,21 @@ package com.typesafe.config; * */ 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("); diff --git a/config/src/main/java/com/typesafe/config/impl/AbstractConfigValue.java b/config/src/main/java/com/typesafe/config/impl/AbstractConfigValue.java index 90db9242..f10c8e2b 100644 --- a/config/src/main/java/com/typesafe/config/impl/AbstractConfigValue.java +++ b/config/src/main/java/com/typesafe/config/impl/AbstractConfigValue.java @@ -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; } }