1 /*
2 * Portions of this software was developed by employees of the National Institute
3 * of Standards and Technology (NIST), an agency of the Federal Government and is
4 * being made available as a public service. Pursuant to title 17 United States
5 * Code Section 105, works of NIST employees are not subject to copyright
6 * protection in the United States. This software may be subject to foreign
7 * copyright. Permission in the United States and in foreign countries, to the
8 * extent that NIST may hold copyright, to use, copy, modify, create derivative
9 * works, and distribute this software and its documentation without fee is hereby
10 * granted on a non-exclusive basis, provided that this notice and disclaimer
11 * of warranty appears in all copies.
12 *
13 * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
14 * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
15 * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
17 * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
18 * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT
19 * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
20 * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
21 * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
22 * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
23 * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
24 * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
25 */
26
27 package gov.nist.secauto.metaschema.databind.io.yaml;
28
29 import org.json.JSONException;
30 import org.json.JSONObject;
31 import org.yaml.snakeyaml.DumperOptions;
32 import org.yaml.snakeyaml.LoaderOptions;
33 import org.yaml.snakeyaml.Yaml;
34 import org.yaml.snakeyaml.constructor.Constructor;
35 import org.yaml.snakeyaml.nodes.Tag;
36 import org.yaml.snakeyaml.representer.Representer;
37 import org.yaml.snakeyaml.resolver.Resolver;
38
39 import java.io.BufferedReader;
40 import java.io.IOException;
41 import java.nio.charset.StandardCharsets;
42 import java.nio.file.Files;
43 import java.nio.file.Path;
44 import java.util.Map;
45
46 import edu.umd.cs.findbugs.annotations.NonNull;
47
48 public final class YamlOperations {
49 private static final Yaml YAML_PARSER;
50
51 static {
52 LoaderOptions loaderOptions = new LoaderOptions();
53 loaderOptions.setCodePointLimit(Integer.MAX_VALUE - 1); // 2GB
54 Constructor constructor = new Constructor(loaderOptions);
55 DumperOptions dumperOptions = new DumperOptions();
56 Representer representer = new Representer(dumperOptions);
57 YAML_PARSER = new Yaml(constructor, representer, dumperOptions, loaderOptions, new Resolver() {
58 @Override
59 protected void addImplicitResolvers() {
60 addImplicitResolver(Tag.BOOL, BOOL, "yYnNtTfFoO");
61 addImplicitResolver(Tag.INT, INT, "-+0123456789");
62 addImplicitResolver(Tag.FLOAT, FLOAT, "-+0123456789.");
63 addImplicitResolver(Tag.MERGE, MERGE, "<");
64 addImplicitResolver(Tag.NULL, NULL, "~nN\0");
65 addImplicitResolver(Tag.NULL, EMPTY, null);
66 // addImplicitResolver(Tag.TIMESTAMP, TIMESTAMP, "0123456789");
67 }
68 });
69 }
70
71 private YamlOperations() {
72 // disable construction
73 }
74
75 /**
76 * Parse the data represented in YAML in the provided {@code target}, producing
77 * an mapping of field names to Java object values.
78 *
79 * @param target
80 * the YAML file to parse
81 * @return the mapping of field names to Java object values
82 * @throws IOException
83 * if an error occurred while parsing the YAML content
84 */
85 @SuppressWarnings({ "unchecked", "null" })
86 @NonNull
87 public static Map<String, Object> parseYaml(Path target) throws IOException {
88 try (BufferedReader reader = Files.newBufferedReader(target.toAbsolutePath(), StandardCharsets.UTF_8)) {
89 return (Map<String, Object>) YAML_PARSER.load(reader);
90 }
91 }
92
93 /**
94 * Converts the provided YAML {@code map} into JSON.
95 *
96 * @param map
97 * the YAML map
98 * @return the JSON object
99 * @throws JSONException
100 * if an error occurred while building the JSON tree
101 */
102 public static JSONObject yamlToJson(@NonNull Map<String, Object> map) {
103 return new JSONObject(map);
104 }
105 }