    001/**
    002 *   Copyright (C) 2011-2012 Typesafe Inc. <http://typesafe.com>
    003 */
    004package com.typesafe.config;
    005
    006/**
    007 * <p>
    008 * A set of options related to rendering a {@link ConfigValue}. Passed to
    009 * {@link ConfigValue#render(ConfigRenderOptions)}.
    010 *
    011 * <p>
    012 * Here is an example of creating a {@code ConfigRenderOptions}:
    013 *
    014 * <pre>
    015 *     ConfigRenderOptions options =
    016 *         ConfigRenderOptions.defaults().setComments(false)
    017 * </pre>
    018 */
    019public final class ConfigRenderOptions {
    020    private final boolean originComments;
    021    private final boolean comments;
    022    private final boolean formatted;
    023    private final boolean json;
    024
    025    private ConfigRenderOptions(boolean originComments, boolean comments, boolean formatted,
    026            boolean json) {
    027        this.originComments = originComments;
    028        this.comments = comments;
    029        this.formatted = formatted;
    030        this.json = json;
    031    }
    032
    033    /**
    034     * Returns the default render options which are verbose (commented and
    035     * formatted). See {@link ConfigRenderOptions#concise} for stripped-down
    036     * options. This rendering will not be valid JSON since it has comments.
    037     *
    038     * @return the default render options
    039     */
    040    public static ConfigRenderOptions defaults() {
    041        return new ConfigRenderOptions(true, true, true, true);
    042    }
    043
    044    /**
    045     * Returns concise render options (no whitespace or comments). For a
    046     * resolved {@link Config}, the concise rendering will be valid JSON.
    047     *
    048     * @return the concise render options
    049     */
    050    public static ConfigRenderOptions concise() {
    051        return new ConfigRenderOptions(false, false, false, true);
    052    }
    053
    054    /**
    055     * Returns options with comments toggled. This controls human-written
    056     * comments but not the autogenerated "origin of this setting" comments,
    057     * which are controlled by {@link ConfigRenderOptions#setOriginComments}.
    058     *
    059     * @param value
    060     *            true to include comments in the render
    061     * @return options with requested setting for comments
    062     */
    063    public ConfigRenderOptions setComments(boolean value) {
    064        if (value == comments)
    065            return this;
    066        else
    067            return new ConfigRenderOptions(originComments, value, formatted, json);
    068    }
    069
    070    /**
    071     * Returns whether the options enable comments. This method is mostly used
    072     * by the config lib internally, not by applications.
    073     *
    074     * @return true if comments should be rendered
    075     */
    076    public boolean getComments() {
    077        return comments;
    078    }
    079
    080    /**
    081     * Returns options with origin comments toggled. If this is enabled, the
    082     * library generates comments for each setting based on the
    083     * {@link ConfigValue#origin} of that setting's value. For example these
    084     * comments might tell you which file a setting comes from.
    085     *
    086     * <p>
    087     * {@code setOriginComments()} controls only these autogenerated
    088     * "origin of this setting" comments, to toggle regular comments use
    089     * {@link ConfigRenderOptions#setComments}.
    090     *
    091     * @param value
    092     *            true to include autogenerated setting-origin comments in the
    093     *            render
    094     * @return options with origin comments toggled
    095     */
    096    public ConfigRenderOptions setOriginComments(boolean value) {
    097        if (value == originComments)
    098            return this;
    099        else
    100            return new ConfigRenderOptions(value, comments, formatted, json);
    101    }
    102
    103    /**
    104     * Returns whether the options enable automated origin comments. This method
    105     * is mostly used by the config lib internally, not by applications.
    106     *
    107     * @return true if origin comments should be rendered
    108     */
    109    public boolean getOriginComments() {
    110        return originComments;
    111    }
    112
    113    /**
    114     * Returns options with formatting toggled. Formatting means indentation and
    115     * whitespace, enabling formatting makes things prettier but larger.
    116     *
    117     * @param value
    118     *            true to enable formatting
    119     * @return options with requested setting for formatting
    120     */
    121    public ConfigRenderOptions setFormatted(boolean value) {
    122        if (value == formatted)
    123            return this;
    124        else
    125            return new ConfigRenderOptions(originComments, comments, value, json);
    126    }
    127
    128    /**
    129     * Returns whether the options enable formatting. This method is mostly used
    130     * by the config lib internally, not by applications.
    131     *
    132     * @return true if the options enable formatting
    133     */
    134    public boolean getFormatted() {
    135        return formatted;
    136    }
    137
    138    /**
    139     * Returns options with JSON toggled. JSON means that HOCON extensions
    140     * (omitting commas, quotes for example) won't be used. However, whether to
    141     * use comments is controlled by the separate {@link #setComments(boolean)}
    142     * and {@link #setOriginComments(boolean)} options. So if you enable
    143     * comments you will get invalid JSON despite setting this to true.
    144     *
    145     * @param value
    146     *            true to include non-JSON extensions in the render
    147     * @return options with requested setting for JSON
    148     */
    149    public ConfigRenderOptions setJson(boolean value) {
    150        if (value == json)
    151            return this;
    152        else
    153            return new ConfigRenderOptions(originComments, comments, formatted, value);
    154    }
    155
    156    /**
    157     * Returns whether the options enable JSON. This method is mostly used by
    158     * the config lib internally, not by applications.
    159     *
    160     * @return true if only JSON should be rendered
    161     */
    162    public boolean getJson() {
    163        return json;
    164    }
    165
    166    @Override
    167    public String toString() {
    168        StringBuilder sb = new StringBuilder("ConfigRenderOptions(");
    169        if (originComments)
    170            sb.append("originComments,");
    171        if (comments)
    172            sb.append("comments,");
    173        if (formatted)
    174            sb.append("formatted,");
    175        if (json)
    176            sb.append("json,");
    177        if (sb.charAt(sb.length() - 1) == ',')
    178            sb.setLength(sb.length() - 1);
    179        sb.append(")");
    180        return sb.toString();
    181    }
    182}