YAMLConfiguration.java

  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. package org.apache.commons.configuration2;

  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.io.Reader;
  21. import java.io.Writer;
  22. import java.util.Map;

  23. import org.apache.commons.configuration2.ex.ConfigurationException;
  24. import org.apache.commons.configuration2.io.InputStreamSupport;
  25. import org.apache.commons.configuration2.tree.ImmutableNode;
  26. import org.yaml.snakeyaml.DumperOptions;
  27. import org.yaml.snakeyaml.LoaderOptions;
  28. import org.yaml.snakeyaml.Yaml;
  29. import org.yaml.snakeyaml.constructor.SafeConstructor;
  30. import org.yaml.snakeyaml.representer.Representer;

  31. /**
  32.  * <p>
  33.  * A specialized hierarchical configuration class that is able to parse YAML documents.
  34.  * </p>
  35.  *
  36.  * @since 2.2
  37.  */
  38. public class YAMLConfiguration extends AbstractYAMLBasedConfiguration implements FileBasedConfiguration, InputStreamSupport {
  39.     /**
  40.      * Creates a {@code Yaml} object for reading a Yaml file. The object is configured with some default settings.
  41.      *
  42.      * @param options options for loading the file
  43.      * @return the {@code Yaml} instance for loading a file
  44.      */
  45.     private static Yaml createYamlForReading(final LoaderOptions options) {
  46.         return new Yaml(new SafeConstructor(options), new Representer(new DumperOptions()), new DumperOptions(), options);
  47.     }

  48.     /**
  49.      * Creates a new instance of {@code YAMLConfiguration}.
  50.      */
  51.     public YAMLConfiguration() {
  52.     }

  53.     /**
  54.      * Creates a new instance of {@code YAMLConfiguration} as a copy of the specified configuration.
  55.      *
  56.      * @param c the configuration to be copied
  57.      */
  58.     public YAMLConfiguration(final HierarchicalConfiguration<ImmutableNode> c) {
  59.         super(c);
  60.     }

  61.     public void dump(final Writer out, final DumperOptions options)
  62.             throws ConfigurationException, IOException {
  63.         final Yaml yaml = new Yaml(options);
  64.         yaml.dump(constructMap(getNodeModel().getNodeHandler().getRootNode()), out);
  65.     }

  66.     /**
  67.      * Loads the configuration from the given input stream.
  68.      *
  69.      * @param in the input stream
  70.      * @throws ConfigurationException if an error occurs
  71.      */
  72.     @Override
  73.     public void read(final InputStream in) throws ConfigurationException {
  74.         try {
  75.             final Yaml yaml = createYamlForReading(new LoaderOptions());
  76.             final Map<String, Object> map = yaml.load(in);
  77.             load(map);
  78.         } catch (final Exception e) {
  79.             rethrowException(e);
  80.         }
  81.     }

  82.     public void read(final InputStream in, final LoaderOptions options) throws ConfigurationException {
  83.         try {
  84.             final Yaml yaml = createYamlForReading(options);
  85.             final Map<String, Object> map = yaml.load(in);
  86.             load(map);
  87.         } catch (final Exception e) {
  88.             rethrowException(e);
  89.         }
  90.     }

  91.     @Override
  92.     public void read(final Reader in) throws ConfigurationException {
  93.         try {
  94.             final Yaml yaml = createYamlForReading(new LoaderOptions());
  95.             final Map<String, Object> map = yaml.load(in);
  96.             load(map);
  97.         } catch (final Exception e) {
  98.             rethrowException(e);
  99.         }
  100.     }

  101.     public void read(final Reader in, final LoaderOptions options) throws ConfigurationException {
  102.         try {
  103.             final Yaml yaml = createYamlForReading(options);
  104.             final Map<String, Object> map = yaml.load(in);
  105.             load(map);
  106.         } catch (final Exception e) {
  107.             rethrowException(e);
  108.         }
  109.     }

  110.     @Override
  111.     public void write(final Writer out) throws ConfigurationException, IOException {
  112.         final DumperOptions options = new DumperOptions();
  113.         options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
  114.         dump(out, options);
  115.     }

  116. }