1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.configuration2;
19
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.Reader;
23 import java.io.Writer;
24
25 import org.apache.commons.configuration2.ex.ConfigurationException;
26 import org.apache.commons.configuration2.io.InputStreamSupport;
27 import org.apache.commons.configuration2.tree.ImmutableNode;
28 import org.yaml.snakeyaml.DumperOptions;
29 import org.yaml.snakeyaml.LoaderOptions;
30 import org.yaml.snakeyaml.Yaml;
31 import org.yaml.snakeyaml.constructor.SafeConstructor;
32 import org.yaml.snakeyaml.representer.Representer;
33
34
35
36
37
38
39
40
41 public class YAMLConfiguration extends AbstractYAMLBasedConfiguration implements FileBasedConfiguration, InputStreamSupport {
42
43
44
45
46
47
48
49 private static Yaml createYamlForReading(final LoaderOptions options) {
50 return new Yaml(new SafeConstructor(options), new Representer(new DumperOptions()), new DumperOptions(), options);
51 }
52
53
54
55
56 public YAMLConfiguration() {
57 }
58
59
60
61
62
63
64 public YAMLConfiguration(final HierarchicalConfiguration<ImmutableNode> c) {
65 super(c);
66 }
67
68 public void dump(final Writer out, final DumperOptions options)
69 throws ConfigurationException, IOException {
70 new Yaml(options).dump(constructMap(getNodeModel().getNodeHandler().getRootNode()), out);
71 }
72
73
74
75
76
77
78
79 @Override
80 public void read(final InputStream in) throws ConfigurationException {
81 try {
82 load(createYamlForReading(new LoaderOptions()).load(in));
83 } catch (final Exception e) {
84 rethrowException(e);
85 }
86 }
87
88 public void read(final InputStream in, final LoaderOptions options) throws ConfigurationException {
89 try {
90 load(createYamlForReading(options).load(in));
91 } catch (final Exception e) {
92 rethrowException(e);
93 }
94 }
95
96 @Override
97 public void read(final Reader in) throws ConfigurationException {
98 try {
99 load(createYamlForReading(new LoaderOptions()).load(in));
100 } catch (final Exception e) {
101 rethrowException(e);
102 }
103 }
104
105 public void read(final Reader in, final LoaderOptions options) throws ConfigurationException {
106 try {
107 load(createYamlForReading(options).load(in));
108 } catch (final Exception e) {
109 rethrowException(e);
110 }
111 }
112
113 @Override
114 public void write(final Writer out) throws ConfigurationException, IOException {
115 final DumperOptions options = new DumperOptions();
116 options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
117 dump(out, options);
118 }
119
120 }