001package com.typesafe.config;
002
003import java.util.List;
004
005import com.typesafe.config.impl.ConfigImplUtil;
006
007/**
008 * Contains static utility methods.
009 * 
010 */
011public final class ConfigUtil {
012    private ConfigUtil() {
013
014    }
015
016    /**
017     * Quotes and escapes a string, as in the JSON specification.
018     *
019     * @param s
020     *            a string
021     * @return the string quoted and escaped
022     */
023    public static String quoteString(String s) {
024        return ConfigImplUtil.renderJsonString(s);
025    }
026
027    /**
028     * Converts a list of keys to a path expression, by quoting the path
029     * elements as needed and then joining them separated by a period. A path
030     * expression is usable with a {@link Config}, while individual path
031     * elements are usable with a {@link ConfigObject}.
032     * <p>
033     * See the overview documentation for {@link Config} for more detail on path
034     * expressions vs. keys.
035     * 
036     * @param elements
037     *            the keys in the path
038     * @return a path expression
039     * @throws ConfigException
040     *             if there are no elements
041     */
042    public static String joinPath(String... elements) {
043        return ConfigImplUtil.joinPath(elements);
044    }
045
046    /**
047     * Converts a list of strings to a path expression, by quoting the path
048     * elements as needed and then joining them separated by a period. A path
049     * expression is usable with a {@link Config}, while individual path
050     * elements are usable with a {@link ConfigObject}.
051     * <p>
052     * See the overview documentation for {@link Config} for more detail on path
053     * expressions vs. keys.
054     * 
055     * @param elements
056     *            the keys in the path
057     * @return a path expression
058     * @throws ConfigException
059     *             if the list is empty
060     */
061    public static String joinPath(List<String> elements) {
062        return ConfigImplUtil.joinPath(elements);
063    }
064
065    /**
066     * Converts a path expression into a list of keys, by splitting on period
067     * and unquoting the individual path elements. A path expression is usable
068     * with a {@link Config}, while individual path elements are usable with a
069     * {@link ConfigObject}.
070     * <p>
071     * See the overview documentation for {@link Config} for more detail on path
072     * expressions vs. keys.
073     * 
074     * @param path
075     *            a path expression
076     * @return the individual keys in the path
077     * @throws ConfigException
078     *             if the path expression is invalid
079     */
080    public static List<String> splitPath(String path) {
081        return ConfigImplUtil.splitPath(path);
082    }
083}