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 java.io.IOException;
021import java.io.InputStream;
022import java.io.Reader;
023import java.io.Writer;
024import java.util.Map;
025
026import org.apache.commons.configuration2.ex.ConfigurationException;
027import org.apache.commons.configuration2.ex.ConfigurationRuntimeException;
028import org.apache.commons.configuration2.io.InputStreamSupport;
029import org.apache.commons.configuration2.tree.ImmutableNode;
030import org.yaml.snakeyaml.DumperOptions;
031import org.yaml.snakeyaml.LoaderOptions;
032import org.yaml.snakeyaml.Yaml;
033import org.yaml.snakeyaml.constructor.Constructor;
034import org.yaml.snakeyaml.representer.Representer;
035
036/**
037 * <p>
038 * A specialized hierarchical configuration class that is able to parse YAML documents.
039 * </p>
040 *
041 * @since 2.2
042 */
043public class YAMLConfiguration extends AbstractYAMLBasedConfiguration implements FileBasedConfiguration, InputStreamSupport {
044    /**
045     * Creates a new instance of {@code YAMLConfiguration}.
046     */
047    public YAMLConfiguration() {
048    }
049
050    /**
051     * Creates a new instance of {@code YAMLConfiguration} as a copy of the specified configuration.
052     *
053     * @param c the configuration to be copied
054     */
055    public YAMLConfiguration(final HierarchicalConfiguration<ImmutableNode> c) {
056        super(c);
057    }
058
059    @Override
060    public void read(final Reader in) throws ConfigurationException {
061        try {
062            final Yaml yaml = createYamlForReading(new LoaderOptions());
063            final Map<String, Object> map = yaml.load(in);
064            load(map);
065        } catch (final Exception e) {
066            rethrowException(e);
067        }
068    }
069
070    public void read(final Reader in, final LoaderOptions options) throws ConfigurationException {
071        try {
072            final Yaml yaml = createYamlForReading(options);
073            final Map<String, Object> map = yaml.load(in);
074            load(map);
075        } catch (final Exception e) {
076            rethrowException(e);
077        }
078    }
079
080    @Override
081    public void write(final Writer out) throws ConfigurationException, IOException {
082        final DumperOptions options = new DumperOptions();
083        options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
084        dump(out, options);
085    }
086
087    public void dump(final Writer out, final DumperOptions options)
088            throws ConfigurationException, IOException {
089        final Yaml yaml = new Yaml(options);
090        yaml.dump(constructMap(getNodeModel().getNodeHandler().getRootNode()), out);
091    }
092
093    /**
094     * Loads the configuration from the given input stream.
095     *
096     * @param in the input stream
097     * @throws ConfigurationException if an error occurs
098     */
099    @Override
100    public void read(final InputStream in) throws ConfigurationException {
101        try {
102            final Yaml yaml = createYamlForReading(new LoaderOptions());
103            final Map<String, Object> map = yaml.load(in);
104            load(map);
105        } catch (final Exception e) {
106            rethrowException(e);
107        }
108    }
109
110    public void read(final InputStream in, final LoaderOptions options) throws ConfigurationException {
111        try {
112            final Yaml yaml = createYamlForReading(options);
113            final Map<String, Object> map = yaml.load(in);
114            load(map);
115        } catch (final Exception e) {
116            rethrowException(e);
117        }
118    }
119
120    /**
121     * Creates a {@code Yaml} object for reading a Yaml file. The object is configured with some default settings.
122     *
123     * @param options options for loading the file
124     * @return the {@code Yaml} instance for loading a file
125     */
126    private static Yaml createYamlForReading(final LoaderOptions options) {
127        return new Yaml(createClassLoadingDisablingConstructor(), new Representer(), new DumperOptions(), options);
128    }
129
130    /**
131     * Returns a {@code Constructor} object for the YAML parser that prevents all classes from being loaded. This
132     * effectively disables the dynamic creation of Java objects that are declared in YAML files to be loaded.
133     *
134     * @return the {@code Constructor} preventing object creation
135     */
136    private static Constructor createClassLoadingDisablingConstructor() {
137        return new Constructor() {
138            @Override
139            protected Class<?> getClassForName(final String name) {
140                throw new ConfigurationRuntimeException("Class instantiation is disabled.");
141            }
142        };
143    }
144}