17
17
package org .springframework .beans .factory .config ;
18
18
19
19
import java .io .IOException ;
20
- import java .io .Reader ;
20
+ import java .io .InputStream ;
21
21
import java .util .Arrays ;
22
22
import java .util .Collection ;
23
23
import java .util .Collections ;
24
24
import java .util .LinkedHashMap ;
25
25
import java .util .List ;
26
26
import java .util .Map ;
27
27
import java .util .Properties ;
28
- import java .util .Set ;
29
- import java .util .stream .Collectors ;
30
-
31
28
import org .apache .commons .logging .Log ;
32
29
import org .apache .commons .logging .LogFactory ;
33
- import org .yaml .snakeyaml .DumperOptions ;
34
- import org .yaml .snakeyaml .LoaderOptions ;
35
- import org .yaml .snakeyaml .Yaml ;
36
- import org .yaml .snakeyaml .constructor .Constructor ;
37
- import org .yaml .snakeyaml .reader .UnicodeReader ;
38
- import org .yaml .snakeyaml .representer .Representer ;
30
+ import org .snakeyaml .engine .v2 .api .Load ;
31
+ import org .snakeyaml .engine .v2 .api .LoadSettings ;
39
32
40
33
import org .springframework .core .CollectionFactory ;
41
34
import org .springframework .core .io .Resource ;
42
35
import org .springframework .lang .Nullable ;
43
36
import org .springframework .util .Assert ;
44
- import org .springframework .util .ObjectUtils ;
45
37
import org .springframework .util .StringUtils ;
46
38
47
39
/**
48
40
* Base class for YAML factories.
49
41
*
50
- * <p>Requires SnakeYAML 1.18 or higher, as of Spring Framework 5.0.6.
42
+ * <p>Requires SnakeYAML engine 2.3 or higher
51
43
*
52
44
* @author Dave Syer
53
45
* @author Juergen Hoeller
@@ -67,9 +59,6 @@ public abstract class YamlProcessor {
67
59
68
60
private boolean matchDefault = true ;
69
61
70
- private Set <String > supportedTypes = Collections .emptySet ();
71
-
72
-
73
62
/**
74
63
* A map of document matchers allowing callers to selectively use only
75
64
* some of the documents in a YAML resource. In YAML documents are
@@ -127,29 +116,6 @@ public void setResources(Resource... resources) {
127
116
this .resources = resources ;
128
117
}
129
118
130
- /**
131
- * Set the supported types that can be loaded from YAML documents.
132
- * <p>If no supported types are configured, only Java standard classes
133
- * (as defined in {@link org.yaml.snakeyaml.constructor.SafeConstructor})
134
- * encountered in YAML documents will be supported.
135
- * If an unsupported type is encountered, an {@link IllegalStateException}
136
- * will be thrown when the corresponding YAML node is processed.
137
- * @param supportedTypes the supported types, or an empty array to clear the
138
- * supported types
139
- * @since 5.1.16
140
- * @see #createYaml()
141
- */
142
- public void setSupportedTypes (Class <?>... supportedTypes ) {
143
- if (ObjectUtils .isEmpty (supportedTypes )) {
144
- this .supportedTypes = Collections .emptySet ();
145
- }
146
- else {
147
- Assert .noNullElements (supportedTypes , "'supportedTypes' must not contain null elements" );
148
- this .supportedTypes = Arrays .stream (supportedTypes ).map (Class ::getName )
149
- .collect (Collectors .toUnmodifiableSet ());
150
- }
151
- }
152
-
153
119
/**
154
120
* Provide an opportunity for subclasses to process the Yaml parsed from the supplied
155
121
* resources. Each resource is parsed in turn and the documents inside checked against
@@ -161,7 +127,7 @@ public void setSupportedTypes(Class<?>... supportedTypes) {
161
127
* @see #createYaml()
162
128
*/
163
129
protected void process (MatchCallback callback ) {
164
- Yaml yaml = createYaml ();
130
+ Load yaml = createYaml ();
165
131
for (Resource resource : this .resources ) {
166
132
boolean found = process (callback , yaml , resource );
167
133
if (this .resolutionMethod == ResolutionMethod .FIRST_FOUND && found ) {
@@ -171,31 +137,21 @@ protected void process(MatchCallback callback) {
171
137
}
172
138
173
139
/**
174
- * Create the {@link Yaml} instance to use.
175
- * <p>The default implementation sets the "allowDuplicateKeys" flag to {@code false},
176
- * enabling built-in duplicate key handling in SnakeYAML 1.18+.
177
- * <p>As of Spring Framework 5.1.16, if custom {@linkplain #setSupportedTypes
178
- * supported types} have been configured, the default implementation creates
179
- * a {@code Yaml} instance that filters out unsupported types encountered in
180
- * YAML documents. If an unsupported type is encountered, an
181
- * {@link IllegalStateException} will be thrown when the node is processed.
182
- * @see LoaderOptions#setAllowDuplicateKeys(boolean)
140
+ * Create the {@link Load} instance to use.
183
141
*/
184
- protected Yaml createYaml () {
185
- LoaderOptions loaderOptions = new LoaderOptions ();
186
- loaderOptions .setAllowDuplicateKeys (false );
187
- return new Yaml (new FilteringConstructor (loaderOptions ), new Representer (),
188
- new DumperOptions (), loaderOptions );
142
+ protected Load createYaml () {
143
+ LoadSettings settings = LoadSettings .builder ().build ();
144
+ return new Load (settings );
189
145
}
190
146
191
- private boolean process (MatchCallback callback , Yaml yaml , Resource resource ) {
147
+ private boolean process (MatchCallback callback , Load yaml , Resource resource ) {
192
148
int count = 0 ;
193
149
try {
194
150
if (logger .isDebugEnabled ()) {
195
151
logger .debug ("Loading from YAML: " + resource );
196
152
}
197
- try (Reader reader = new UnicodeReader ( resource .getInputStream () )) {
198
- for (Object object : yaml .loadAll ( reader )) {
153
+ try (InputStream is = resource .getInputStream ()) {
154
+ for (Object object : yaml .loadAllFromInputStream ( is )) {
199
155
if (object != null && process (asMap (object ), callback )) {
200
156
count ++;
201
157
if (this .resolutionMethod == ResolutionMethod .FIRST_FOUND ) {
@@ -428,24 +384,4 @@ public enum ResolutionMethod {
428
384
FIRST_FOUND
429
385
}
430
386
431
-
432
- /**
433
- * {@link Constructor} that supports filtering of unsupported types.
434
- * <p>If an unsupported type is encountered in a YAML document, an
435
- * {@link IllegalStateException} will be thrown from {@link #getClassForName}.
436
- */
437
- private class FilteringConstructor extends Constructor {
438
-
439
- FilteringConstructor (LoaderOptions loaderOptions ) {
440
- super (loaderOptions );
441
- }
442
-
443
- @ Override
444
- protected Class <?> getClassForName (String name ) throws ClassNotFoundException {
445
- Assert .state (YamlProcessor .this .supportedTypes .contains (name ),
446
- () -> "Unsupported type encountered in YAML document: " + name );
447
- return super .getClassForName (name );
448
- }
449
- }
450
-
451
387
}
0 commit comments