All Downloads are FREE. Search and download functionalities are using the official Maven repository.

tech.ferus.util.config.YamlConfigFile Maven / Gradle / Ivy

Go to download

ConfigKeys is a simple wrapper for zml's Configurate, providing a more stream-lined way to access configuration on-the-fly.

There is a newer version: 3.0.0
Show newest version
/*
 * Copyright 2017 FerusTech LLC
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
 * and associated documentation files (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package tech.ferus.util.config;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.loader.ConfigurationLoader;
import ninja.leaping.configurate.yaml.YAMLConfigurationLoader;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
 * An implementation of {@link ConfigFile} for the YAML specification.
 */
public class YamlConfigFile extends ConfigFile {

    /**
     * Constructs a new {@link YamlConfigFile}.
     *
     * @param file the location of the configuration file on disk
     * @param loader the active loaded for the configuration file on disk
     * @param root the loaded {@link ConfigurationNode} at the root level
     */
    public YamlConfigFile(@Nonnull final Path file,
                          @Nonnull final ConfigurationLoader loader,
                          @Nonnull final ConfigurationNode root) {
        super(file, loader, root);
    }

    /**
     * Creates a {@link YamlConfigFile}.
     *
     * @param file the location of the config file on disk
     * @return a new {@link YamlConfigFile}
     * @throws IOException if files cannot be properly deleted/created
     */
    public static YamlConfigFile load(final Path file) throws IOException {
        return load(file, null, false, false);
    }

    /**
     * Creates a {@link YamlConfigFile}.
     *
     * 

Also merges a configuration file from classpath into the file on disk.

* * @param file the location of the config file on disk * @param resource the location of the resource in the classpath * @return a new {@link YamlConfigFile} * @throws IOException if files cannot be properly deleted/created */ public static YamlConfigFile load(@Nonnull final Path file, @Nonnull final String resource) throws IOException { return load(file, resource, false, true); } /** * Creates a {@link YamlConfigFile}. * *

Also merges a configuration file from the classpath into the file on disk.

* *

May delete file on disk before copying resource.

* * @param file the location of the config file on disk * @param resource the location of the resource in the classpath * @param overwrite whether or not to delete the file on disk before copying resource * @return a new {@link YamlConfigFile} * @throws IOException if files cannot be properly deleted/created */ public static YamlConfigFile load(@Nonnull final Path file, @Nonnull final String resource, final boolean overwrite) throws IOException { return load(file, resource, overwrite, true); } /** * Creates a {@link YamlConfigFile}. * *

May merge a configuration file from the classpath into the file on disk.

* *

May delete the file on disk before copying resource.

* *

If accessing this method directly, keep in mind that passing true * for overwrite but false for merge is going * to result in a non-existent file on disk.

* * @param file the location of the config file on disk * @param resource the location of the resource in the classpath * @param overwrite whether or not to delete the file on disk before copying the resource * @param merge whether or not to merge the resource into the file on disk * @return a new {@link YamlConfigFile} * @throws IOException if files cannot be properly deleted/created */ public static YamlConfigFile load(@Nonnull final Path file, @Nullable final String resource, final boolean overwrite, final boolean merge) throws IOException { if (overwrite) { Files.deleteIfExists(file); } if (!Files.exists(file)) { Files.createDirectories(file.getParent()); Files.createFile(file); } final YAMLConfigurationLoader fileLoader = YAMLConfigurationLoader.builder().setPath(file).build(); ConfigurationNode root; root = fileLoader.load(); if (merge) { final YAMLConfigurationLoader resourceLoader = YAMLConfigurationLoader.builder().setURL(ConfigFile.class.getResource(resource)).build(); root.mergeValuesFrom(resourceLoader.load()); fileLoader.save(root); } return new YamlConfigFile(file, fileLoader, root); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy