001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.configuration2;
019
020import org.apache.commons.configuration2.ex.ConfigurationException;
021import org.apache.commons.configuration2.io.InputStreamSupport;
022import org.apache.commons.configuration2.tree.ImmutableNode;
023import org.yaml.snakeyaml.DumperOptions;
024import org.yaml.snakeyaml.LoaderOptions;
025import org.yaml.snakeyaml.Yaml;
026
027import java.io.IOException;
028import java.io.InputStream;
029import java.io.Reader;
030import java.io.Writer;
031import java.util.Map;
032
033/**
034 * <p>
035 * A specialized hierarchical configuration class that is able to parse YAML
036 * documents.
037 * </p>
038 *
039 * @since 2.2
040 */
041public class YAMLConfiguration extends AbstractYAMLBasedConfiguration
042        implements FileBasedConfiguration, InputStreamSupport
043{
044    /**
045     * Creates a new instance of {@code YAMLConfiguration}.
046     */
047    public YAMLConfiguration()
048    {
049        super();
050    }
051
052    /**
053     * Creates a new instance of {@code YAMLConfiguration} as a copy of the
054     * specified configuration.
055     *
056     * @param c the configuration to be copied
057     */
058    public YAMLConfiguration(final HierarchicalConfiguration<ImmutableNode> c)
059    {
060        super(c);
061    }
062
063    @Override
064    public void read(final Reader in) throws ConfigurationException
065    {
066        try
067        {
068            final Yaml yaml = new Yaml();
069            final Map<String, Object> map = (Map) yaml.load(in);
070            load(map);
071        }
072        catch (final Exception e)
073        {
074            rethrowException(e);
075        }
076    }
077
078    public void read(final Reader in, final LoaderOptions options)
079            throws ConfigurationException
080    {
081        try
082        {
083            final Yaml yaml = new Yaml(options);
084            final Map<String, Object> map = (Map) yaml.load(in);
085            load(map);
086        }
087        catch (final Exception e)
088        {
089            rethrowException(e);
090        }
091    }
092
093    @Override
094    public void write(final Writer out) throws ConfigurationException, IOException
095    {
096        final DumperOptions options = new DumperOptions();
097        options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
098        dump(out, options);
099    }
100
101    public void dump(final Writer out, final DumperOptions options)
102            throws ConfigurationException, IOException
103    {
104        final Yaml yaml = new Yaml(options);
105        yaml.dump(constructMap(getNodeModel().getNodeHandler().getRootNode()),
106                out);
107    }
108
109    /**
110     * Loads the configuration from the given input stream.
111     *
112     * @param in the input stream
113     * @throws ConfigurationException if an error occurs
114     */
115    @Override
116    public void read(final InputStream in) throws ConfigurationException
117    {
118        try
119        {
120            final Yaml yaml = new Yaml();
121            final Map<String, Object> map = (Map) yaml.load(in);
122            load(map);
123        }
124        catch (final Exception e)
125        {
126            rethrowException(e);
127        }
128    }
129
130    public void read(final InputStream in, final LoaderOptions options)
131            throws ConfigurationException
132    {
133        try
134        {
135            final Yaml yaml = new Yaml(options);
136            final Map<String, Object> map = (Map) yaml.load(in);
137            load(map);
138        }
139        catch (final Exception e)
140        {
141            rethrowException(e);
142        }
143    }
144
145}