All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.varmateo.yawg.util.YamlParser Maven / Gradle / Ivy

/**************************************************************************
 *
 * Copyright (c) 2016-2020 Yawg project contributors.
 *
 **************************************************************************/

package com.varmateo.yawg.util;

import java.io.IOException;
import java.io.Reader;
import java.util.Collections;
import java.util.Map;

import com.esotericsoftware.yamlbeans.YamlReader;


/**
 * A simple YAML parser.
 */
public final class YamlParser {


    /**
     * Parses YAML contants from the given reader.
     *
     * @param reader Source of YAML contents to be read.
     *
     * @return A map with the results of parsing the given YAML
     * content.
     *
     * @exception IOException Thrown if there were any problems
     * reading from the reader, or if the YAML contents are invalid.
     */
    public static SimpleMap parse(final Reader reader)
            throws IOException {

        final Map resultMap;
        final YamlReader yamlReader = new YamlReader(reader);
        final Object yamlObj = yamlReader.read();

        if ( (yamlObj != null) && (yamlObj instanceof Map) ) {
            @SuppressWarnings("unchecked")
            final Map map = (Map) yamlObj;
            resultMap = map;
        } else {
            // The contents of the YAML file are invalid.
            resultMap = Collections.emptyMap();
        }

        return new SimpleMap(resultMap);
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy