com.electronwill.nightconfig.core.file.FileConfig Java Examples
The following examples show how to use
com.electronwill.nightconfig.core.file.FileConfig.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AutoreloadExample.java From night-config with GNU Lesser General Public License v3.0 | 6 votes |
public static void main(String[] args) throws IOException, InterruptedException { // Creates an autoreloaded config: File configFile = new File("autoreload.json"); FileConfig config = FileConfig.builder(configFile).autoreload().build(); System.out.println("Config: " + config.valueMap()); // Modifies the file: try (FileWriter writer = new FileWriter(configFile)) { writer.write("{ \"value\": 123 }"); } Thread.sleep(1000);// The modifications take some (short) time to be detected System.out.println("Config: " + config);// You should see "Config: {value=123}" /* Don't forget to close the config! In that case the program terminates so it's not a big deal, but otherwise it's important to close the FileConfig in order to release the associated resources. For an autoreloaded config, the close() method tells the OS that it doesn't need to watch this file anymore. */ config.close(); }
Example #2
Source File: JsonConfigTest.java From night-config with GNU Lesser General Public License v3.0 | 6 votes |
@Test public void testAuto() throws InterruptedException { FileConfig config = FileConfig.builder(file).autoreload().autosave().build(); config.load(); System.out.println(config); double d; for (int i = 0; i < 20; i++) { d = Math.random(); config.set("double", d); Thread.sleep(20); //System.out.println(i + ":" + d); assertEquals(d, config.<Double>get("double").doubleValue()); } for (int i = 0; i < 1000; i++) { config.set("double", 0.123456); } config.close(); }
Example #3
Source File: JsonConfigTest.java From night-config with GNU Lesser General Public License v3.0 | 6 votes |
@Test public void testEmptyDataTolerance() throws IOException { File f = new File("empty.json"); assertEquals(0, f.length()); FileConfig cDefault = FileConfig.of(f); assertThrows(ParsingException.class, cDefault::load); JsonFormat<?> f1 = JsonFormat.fancyInstance(); FileConfig c1 = FileConfig.of(f, f1); assertThrows(ParsingException.class, c1::load); JsonFormat<?> f2 = JsonFormat.emptyTolerantInstance(); FileConfig c2 = FileConfig.of(f, f2); c2.load(); assertTrue(c2.isEmpty()); assertEquals(0, f.length()); assertThrows(ParsingException.class, ()->new JsonParser().parse("")); new JsonParser(true).parse(""); }
Example #4
Source File: Patchwork.java From patchwork-patcher with GNU Lesser General Public License v3.0 | 5 votes |
private ForgeModJar parseModManifest(Path jarPath) throws IOException, URISyntaxException, ManifestParseException { String mod = jarPath.getFileName().toString().split("\\.jar")[0]; // Load metadata LOGGER.trace("Loading and parsing metadata for %s", mod); URI inputJar = new URI("jar:" + jarPath.toUri()); FileConfig toml; ForgeAccessTransformer at = null; try (FileSystem fs = FileSystems.newFileSystem(inputJar, Collections.emptyMap())) { Path manifestPath = fs.getPath("/META-INF/mods.toml"); toml = FileConfig.of(manifestPath); toml.load(); Path atPath = fs.getPath("/META-INF/accesstransformer.cfg"); if (Files.exists(atPath)) { at = ForgeAccessTransformer.parse(atPath); } } Map<String, Object> map = toml.valueMap(); ModManifest manifest = ModManifest.parse(map); if (!manifest.getModLoader().equals("javafml")) { LOGGER.error("Unsupported modloader %s", manifest.getModLoader()); } if (at != null) { at.remap(accessTransformerRemapper, ex -> LOGGER.throwing(Level.WARN, ex)); } return new ForgeModJar(jarPath, manifest, at); }
Example #5
Source File: ForgeConfigSpec.java From patchwork-api with GNU Lesser General Public License v2.1 | 5 votes |
public void setConfig(CommentedConfig config) { this.childConfig = config; if (!isCorrect(config)) { String configName = config instanceof FileConfig ? ((FileConfig) config).getNioPath().toString() : config.toString(); LogManager.getLogger().warn(CORE, "Configuration file {} is not correct. Correcting", configName); correct(config, (action, path, incorrectValue, correctedValue) -> LogManager.getLogger().warn(CORE, "Incorrect key {} was corrected from {} to {}", DOT_JOINER.join(path), incorrectValue, correctedValue)); if (config instanceof FileConfig) { ((FileConfig) config).save(); } } }
Example #6
Source File: ForgeConfigSpec.java From patchwork-api with GNU Lesser General Public License v2.1 | 5 votes |
public void save() { Preconditions.checkNotNull(childConfig, "Cannot save config value without assigned Config object present"); if (childConfig instanceof FileConfig) { ((FileConfig) childConfig).save(); } }
Example #7
Source File: AutosaveExample.java From night-config with GNU Lesser General Public License v3.0 | 5 votes |
public static void main(String[] args) throws IOException, InterruptedException { // Creates an autosaved config: File configFile = new File("autosave.json"); FileConfig config = FileConfig.builder(configFile).autosave().build(); // Reads the file: printFile(configFile); // Modifies the config: config.set("value", 123); Thread.sleep(500); /* The config we built is in "asynchronous writing" mode, which means that the writing operations occurs in the background, without blocking the config.set method. That's why to see the modifications in our program we have to wait a little bit. Try adding sync() to the build chain and removing the Thread.sleep instruction! */ printFile(configFile); // Modifies the config again: config.set("value", 1234); Thread.sleep(500); printFile(configFile); /* Don't forget to close the config! In that case the program terminates so it's not a big deal, but otherwise it's important to close the FileConfig in order to release the associated resources. For an autosaved config, the close() method discards the channel/stream used to save the config. */ config.close(); }
Example #8
Source File: JsonConfigTest.java From night-config with GNU Lesser General Public License v3.0 | 5 votes |
@Test public void testReadEmptyFile() throws IOException { File f = new File("tmp.json"); FileConfig config = FileConfig.of(f); config.load(); config.close(); Config conf = new JsonParser().parse(f, FileNotFoundAction.THROW_ERROR); System.out.println(conf); assertTrue(conf.isEmpty()); System.out.println("tmp.json:\n" + Files.readAllLines(f.toPath()).get(0)); f.delete(); }
Example #9
Source File: FileConfigExample.java From night-config with GNU Lesser General Public License v3.0 | 4 votes |
public static void main(String[] args) { // Creates the FileConfig: FileConfig config = FileConfig.of("config.toml"); // Loads the config (reads the file): config.load(); // Note: the load() call is always blocking: it returns when the reading operation terminates. // load() is also used to reload the configuration. System.out.println("Config: " + config); // Modifies the config: config.set("key", "value"); config.set("number", 123456); config.set("floatingPoint", 3.1415926535); // You can also use sub configs! Config subConfig = Config.inMemory(); subConfig.set("subKey", "value"); config.set("subConfig", subConfig); // NightConfig supports dot-separated paths: config.set("subConfig.subKey", "newValue"); // If you want to use a key that contains a dot in its name, use a list: config.set(Arrays.asList("subConfig", "127.0.0.1"), "test");// the key "127.0.0.1" is in subConfig System.out.println("Config: " + config); // Saves the config: config.save(); // Note: by default, the save operation is done in the background, and config.save() // returns immediately without waiting for the operation to terminates. /* Once you don't need the FileConfig anymore, remember to close it, in order to release the associated resources. There aren't always such resources, but it's a good practise to call the close() method anyway. Closing the FileConfig also ensures that all the data has been written, in particular it waits for the background saving operations to complete. */ config.close(); }
Example #10
Source File: ConvertedFileConfig.java From night-config with GNU Lesser General Public License v3.0 | 4 votes |
public ConvertedFileConfig(FileConfig config, ConversionTable readTable, ConversionTable writeTable, Predicate<Class<?>> supportPredicate) { this(config, readTable::convert, writeTable::convert, supportPredicate); }
Example #11
Source File: ConvertedFileConfig.java From night-config with GNU Lesser General Public License v3.0 | 4 votes |
public ConvertedFileConfig(FileConfig config, Function<Object, Object> readConversion, Function<Object, Object> writeConversion, Predicate<Class<?>> supportPredicate) { super(config, readConversion, writeConversion, supportPredicate); }
Example #12
Source File: ConversionTable.java From night-config with GNU Lesser General Public License v3.0 | 2 votes |
/** * Returns an Config that converts "just-in-time" the values that are read from the specified * Config. * * @param config the config to wrap * @return a wrapper that converts the values read from the config */ public FileConfig wrapRead(FileConfig config) { return new ConvertedFileConfig(config, this::convert, v -> v, config.configFormat()::supportsType); }
Example #13
Source File: ConversionTable.java From night-config with GNU Lesser General Public License v3.0 | 2 votes |
/** * Returns an Config that converts "just-in-time" the values that are put into the specified * Config. * * @param config the config to wrap * @param supportValueTypePredicate Predicate that checks if a given class is supported by the * returned config * @return a wrapper that converts the values put into the config */ public FileConfig wrapWrite(FileConfig config, Predicate<Class<?>> supportValueTypePredicate) { return new ConvertedFileConfig(config, v -> v, this::convert, supportValueTypePredicate); }