View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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  import java.util.Map;
25  
26  import org.apache.commons.configuration2.ex.ConfigurationException;
27  import org.apache.commons.configuration2.io.InputStreamSupport;
28  import org.apache.commons.configuration2.tree.ImmutableNode;
29  import org.yaml.snakeyaml.DumperOptions;
30  import org.yaml.snakeyaml.LoaderOptions;
31  import org.yaml.snakeyaml.Yaml;
32  import org.yaml.snakeyaml.constructor.SafeConstructor;
33  import org.yaml.snakeyaml.representer.Representer;
34  
35  /**
36   * <p>
37   * A specialized hierarchical configuration class that is able to parse YAML documents.
38   * </p>
39   *
40   * @since 2.2
41   */
42  public class YAMLConfiguration extends AbstractYAMLBasedConfiguration implements FileBasedConfiguration, InputStreamSupport {
43      /**
44       * Creates a {@code Yaml} object for reading a Yaml file. The object is configured with some default settings.
45       *
46       * @param options options for loading the file
47       * @return the {@code Yaml} instance for loading a file
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       * Creates a new instance of {@code YAMLConfiguration}.
55       */
56      public YAMLConfiguration() {
57      }
58  
59      /**
60       * Creates a new instance of {@code YAMLConfiguration} as a copy of the specified configuration.
61       *
62       * @param c the configuration to be copied
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          final Yaml yaml = new Yaml(options);
71          yaml.dump(constructMap(getNodeModel().getNodeHandler().getRootNode()), out);
72      }
73  
74      /**
75       * Loads the configuration from the given input stream.
76       *
77       * @param in the input stream
78       * @throws ConfigurationException if an error occurs
79       */
80      @Override
81      public void read(final InputStream in) throws ConfigurationException {
82          try {
83              final Yaml yaml = createYamlForReading(new LoaderOptions());
84              final Map<String, Object> map = yaml.load(in);
85              load(map);
86          } catch (final Exception e) {
87              rethrowException(e);
88          }
89      }
90  
91      public void read(final InputStream in, final LoaderOptions options) throws ConfigurationException {
92          try {
93              final Yaml yaml = createYamlForReading(options);
94              final Map<String, Object> map = yaml.load(in);
95              load(map);
96          } catch (final Exception e) {
97              rethrowException(e);
98          }
99      }
100 
101     @Override
102     public void read(final Reader in) throws ConfigurationException {
103         try {
104             final Yaml yaml = createYamlForReading(new LoaderOptions());
105             final Map<String, Object> map = yaml.load(in);
106             load(map);
107         } catch (final Exception e) {
108             rethrowException(e);
109         }
110     }
111 
112     public void read(final Reader in, final LoaderOptions options) throws ConfigurationException {
113         try {
114             final Yaml yaml = createYamlForReading(options);
115             final Map<String, Object> map = yaml.load(in);
116             load(map);
117         } catch (final Exception e) {
118             rethrowException(e);
119         }
120     }
121 
122     @Override
123     public void write(final Writer out) throws ConfigurationException, IOException {
124         final DumperOptions options = new DumperOptions();
125         options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
126         dump(out, options);
127     }
128 
129 }