1 /**************************************************************************
2 *
3 * Copyright (c) 2016-2020 Yawg project contributors.
4 *
5 **************************************************************************/
6
7 package com.varmateo.yawg.util;
8
9 import java.io.IOException;
10 import java.io.Reader;
11 import java.util.Collections;
12 import java.util.Map;
13
14 import com.esotericsoftware.yamlbeans.YamlReader;
15
16
17 /**
18 * A simple YAML parser.
19 */
20 public final class YamlParser {
21
22
23 /**
24 * Parses YAML contants from the given reader.
25 *
26 * @param reader Source of YAML contents to be read.
27 *
28 * @return A map with the results of parsing the given YAML
29 * content.
30 *
31 * @exception IOException Thrown if there were any problems
32 * reading from the reader, or if the YAML contents are invalid.
33 */
34 public static SimpleMap parse(final Reader reader)
35 throws IOException {
36
37 final Map<String, Object> resultMap;
38 final YamlReader yamlReader = new YamlReader(reader);
39 final Object yamlObj = yamlReader.read();
40
41 if ( (yamlObj != null) && (yamlObj instanceof Map) ) {
42 @SuppressWarnings("unchecked")
43 final Map<String, Object> map = (Map<String, Object>) yamlObj;
44 resultMap = map;
45 } else {
46 // The contents of the YAML file are invalid.
47 resultMap = Collections.emptyMap();
48 }
49
50 return new SimpleMap(resultMap);
51 }
52
53
54 }