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

tech.ferus.util.config.JsonConfigFile 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.

The 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.json.JSONConfigurationLoader;
import ninja.leaping.configurate.loader.ConfigurationLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

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

    /**
     * The JsonConfigFile logger.
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(JsonConfigFile.class);

    /**
     * Constructs a new {@link JsonConfigFile}.
     *
     * @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 JsonConfigFile(@Nonnull final Path file,
                          @Nonnull final ConfigurationLoader loader,
                          @Nonnull final ConfigurationNode root) {
        super(file, loader, root);
    }

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

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

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 JsonConfigFile} * @throws IOException if files cannot be properly deleted/created */ public static JsonConfigFile load(@Nonnull final Path file, @Nonnull final String resource) throws IOException { return load(file, resource, false); } /** * Creates a {@link JsonConfigFile}. * *

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 JsonConfigFile} * @throws IOException if files cannot be properly deleted/created */ public static JsonConfigFile load(@Nonnull final Path file, @Nullable final String resource, final boolean overwrite) throws IOException { if (overwrite) { try { Files.deleteIfExists(file); } catch (final IOException e) { LOGGER.error("Failed to delete file: {}", file.toString(), e); } } if (!Files.exists(file)) { try { Files.createDirectories(file.getParent()); } catch (final IOException e) { LOGGER.error("Failed to create parent directories of: {}", file.toString(), e); } if (resource != null && !resource.isEmpty()) { try { Files.copy(JsonConfigFile.class.getResourceAsStream(resource), file); } catch (final IOException e) { LOGGER.error("Failed to copy resource to: {}", file.toString(), e); } } } final JSONConfigurationLoader loader = JSONConfigurationLoader.builder().setPath(file).build(); try { return new JsonConfigFile(file, loader, loader.load()); } catch (final IOException e) { LOGGER.error("Failed to load configuration file at: {}", file.toString(), e); } return null; } /** * Creates a {@link JsonConfigFile}. * *

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 JsonConfigFile} * @throws IOException if files cannot be properly deleted/created */ @Deprecated public static JsonConfigFile 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 JSONConfigurationLoader fileLoader = JSONConfigurationLoader.builder().setPath(file).build(); ConfigurationNode root; root = fileLoader.load(); if (merge) { final JSONConfigurationLoader resourceLoader = JSONConfigurationLoader.builder().setURL(ConfigFile.class.getResource(resource)).build(); root.mergeValuesFrom(resourceLoader.load()); fileLoader.save(root); } return new JsonConfigFile(file, fileLoader, root); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy