forked from hazelcast/hazelcast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
YamlLoader.java
157 lines (144 loc) · 5.44 KB
/
YamlLoader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* Copyright (c) 2008-2020, Hazelcast, Inc. All Rights Reserved.
*
* 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 com.hazelcast.internal.yaml;
import org.snakeyaml.engine.v2.api.Load;
import org.snakeyaml.engine.v2.api.LoadSettings;
import java.io.InputStream;
import java.io.Reader;
/**
* YAML loader that can load, parse YAML documents and can build a tree
* of {@link YamlNode} instances.
* <p>
* The possible sources of the YAML documents are:
* <ul>
* <li>{@link InputStream}</li>
* <li>{@link Reader}</li>
* <li>{@link String}</li>
* </ul>
*/
public final class YamlLoader {
private YamlLoader() {
}
/**
* Loads a YAML document from an {@link InputStream} and builds a
* {@link YamlNode} tree that is under the provided top-level
* {@code rootName} key. This loading mode requires the topmost level
* of the YAML document to be a mapping.
*
* @param inputStream The input stream to load the YAML from
* @param rootName The name of the root's key
* @return the tree built from the YAML document
*/
public static YamlNode load(InputStream inputStream, String rootName) {
try {
Object document = getLoad().loadFromInputStream(inputStream);
return buildDom(rootName, document);
} catch (Exception ex) {
throw new YamlException("An error occurred while loading and parsing the YAML stream", ex);
}
}
/**
* Loads a YAML document from an {@link InputStream} and builds a
* {@link YamlNode} tree.
*
* @param inputStream The input stream to load the YAML from
* @return the tree built from the YAML document
*/
public static YamlNode load(InputStream inputStream) {
try {
Object document = getLoad().loadFromInputStream(inputStream);
return buildDom(document);
} catch (Exception ex) {
throw new YamlException("An error occurred while loading and parsing the YAML stream", ex);
}
}
/**
* Loads a YAML document from an {@link Reader} and builds a
* {@link YamlNode} tree that is under the provided top-level
* {@code rootName} key. This loading mode requires the topmost level
* of the YAML document to be a mapping.
*
* @param reader The reader to load the YAML from
* @param rootName The name of the root's key
* @return the tree built from the YAML document
*/
public static YamlNode load(Reader reader, String rootName) {
try {
Object document = getLoad().loadFromReader(reader);
return buildDom(rootName, document);
} catch (Exception ex) {
throw new YamlException("An error occurred while loading and parsing the YAML stream", ex);
}
}
/**
* Loads a YAML document from an {@link Reader} and builds a
* {@link YamlNode} tree.
*
* @param reader The reader to load the YAML from
* @return the tree built from the YAML document
*/
public static YamlNode load(Reader reader) {
try {
Object document = getLoad().loadFromReader(reader);
return buildDom(document);
} catch (Exception ex) {
throw new YamlException("An error occurred while loading and parsing the YAML stream", ex);
}
}
/**
* Loads a YAML document from an {@link String} and builds a
* {@link YamlNode} tree that is under the provided top-level
* {@code rootName} key. This loading mode requires the topmost level
* of the YAML document to be a mapping.
*
* @param yaml The string to load the YAML from
* @param rootName The name of the root's key
* @return the tree built from the YAML document
*/
public static YamlNode load(String yaml, String rootName) {
try {
Object document = getLoad().loadFromString(yaml);
return buildDom(rootName, document);
} catch (Exception ex) {
throw new YamlException("An error occurred while loading and parsing the YAML string", ex);
}
}
/**
* Loads a YAML document from an {@link String} and builds a
* {@link YamlNode} tree.
*
* @param yaml The string to load the YAML from
* @return the tree built from the YAML string
*/
public static YamlNode load(String yaml) {
try {
Object document = getLoad().loadFromString(yaml);
return buildDom(document);
} catch (Exception ex) {
throw new YamlException("An error occurred while loading and parsing the YAML string", ex);
}
}
private static Load getLoad() {
LoadSettings settings = LoadSettings.builder().build();
return new Load(settings);
}
private static YamlNode buildDom(String rootName, Object document) {
return YamlDomBuilder.build(document, rootName);
}
private static YamlNode buildDom(Object document) {
return YamlDomBuilder.build(document);
}
}