io.coala.config.YamlUtil.java Source code

Java tutorial

Introduction

Here is the source code for io.coala.config.YamlUtil.java

Source

/* $Id: a8ea371fc4c649dd7bbdcf0dc43a7997849e7171 $
 * 
 * @license
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy
 * of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package io.coala.config;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringWriter;
import java.util.Map;
import java.util.Properties;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;

import io.coala.exception.Thrower;
import io.coala.util.FileUtil;
import io.coala.util.Util;

/**
 * {@link YamlUtil}
 */
public class YamlUtil implements Util {

    /** {@link YamlUtil} singleton constructor */
    private YamlUtil() {
        // empty
    }

    private static ObjectMapper mapper = null;

    /**
     * @return a {@link ObjectMapper} singleton for YAML file formats
     */
    public synchronized static ObjectMapper getYamlMapper() {
        if (mapper == null)
            mapper = new ObjectMapper(new YAMLFactory());
        return mapper;
    }

    /**
     * @param yamlContent the YAML-formatted tree
     * @param baseKeys the base keys for all imported property keys
     * @return a flat {@link Properties} mapping
     * @throws IOException
     */
    public static Properties flattenYaml(final CharSequence yamlContent, final CharSequence... baseKeys)
            throws IOException {
        return ConfigUtil.flatten(getYamlMapper().readTree(yamlContent.toString()), baseKeys);
    }

    /**
     * @param yamlPath the (relative, absolute, or class-path) YAML location
     * @param baseKeys the base keys for all imported property keys
     * @return a flat {@link Properties} mapping
     * @throws IOException
     */
    public static Properties flattenYaml(final File yamlPath, final CharSequence... baseKeys) throws IOException {
        return flattenYaml(FileUtil.toInputStream(yamlPath), baseKeys);
    }

    /**
     * @param yamlPath the (relative, absolute, or class-path) YAML location
     * @param baseKeys the base keys for all imported property keys
     * @return a flat {@link Properties} mapping
     * @throws IOException
     */
    public static Properties flattenYaml(final InputStream is, final CharSequence... baseKeys) throws IOException {
        return ConfigUtil.flatten(getYamlMapper().readTree(is), baseKeys);
    }

    private static final String nl = "\r\n", hash = "# ";

    public static String toComment(final String comment) {
        return hash + comment.replace(nl, nl + hash) + nl;
    }

    /**
     * @param props the flat {@link Map}, e.g. {@link Properties} to convert
     * @param baseKeys the {@link String} key prefixes to filter on
     * @return the YAML {@link String}
     * @throws IOException
     */
    public static String toYAML(final String comment, final Map<?, ?> props, final String... baseKeys) {
        return toYAML(comment, ConfigUtil.expand(props, baseKeys));
    }

    /**
     * @param tree the root {@link JsonNode} of the value tree
     * @return the YAML {@link String}
     * @throws IOException
     */
    public static String toYAML(final String comment, final JsonNode tree) {
        final StringWriter writer = new StringWriter();
        if (comment != null && !comment.isEmpty())
            writer.append(toComment(comment));
        try {
            getYamlMapper().writer().writeValue(writer, tree);
        } catch (final IOException e) {
            Thrower.rethrowUnchecked(e);
        }
        return writer.toString();
    }

    /**
     * @param props the flat {@link Map}, e.g. {@link Properties} to convert
     * @param baseKeys the {@link String} key prefixes to filter on
     * @param out the {@link OutputStream} to write the YAML to
     * @throws IOException
     */
    public static void toYAML(final String comment, final OutputStream out, final Map<?, ?> props,
            final String... baseKeys) throws IOException {
        toYAML(comment, out, ConfigUtil.expand(props, baseKeys));
    }

    /**
     * @param tree the root {@link JsonNode} of the value tree
     * @return the YAML {@link String}
     * @throws IOException
     */
    public static void toYAML(final String comment, final OutputStream out, final JsonNode tree)
            throws IOException {
        getYamlMapper().writer().writeValue(out,
                comment == null || comment.isEmpty() ? tree : toComment(comment) + tree);
    }
}