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    *     https://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  
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   * <p>
36   * A specialized hierarchical configuration class that is able to parse YAML documents.
37   * </p>
38   *
39   * @since 2.2
40   */
41  public class YAMLConfiguration extends AbstractYAMLBasedConfiguration implements FileBasedConfiguration, InputStreamSupport {
42  
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          new Yaml(options).dump(constructMap(getNodeModel().getNodeHandler().getRootNode()), out);
71      }
72  
73      /**
74       * Loads the configuration from the given input stream.
75       *
76       * @param in the input stream
77       * @throws ConfigurationException if an error occurs
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 }