com.electronwill.nightconfig.core.io.WritingMode Java Examples

The following examples show how to use com.electronwill.nightconfig.core.io.WritingMode. 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: ConfigFileTypeHandler.java    From patchwork-api with GNU Lesser General Public License v2.1 6 votes vote down vote up
public Function<ModConfig, CommentedFileConfig> reader(Path configBasePath) {
	return (c) -> {
		final Path configPath = configBasePath.resolve(c.getFileName());
		final CommentedFileConfig configData = CommentedFileConfig.builder(configPath).sync()
				.preserveInsertionOrder()
				.autosave()
				.onFileNotFound((newfile, configFormat) -> setupConfigFile(c, newfile, configFormat))
				.writingMode(WritingMode.REPLACE)
				.build();
		LOGGER.debug(CONFIG, "Built TOML config for {}", configPath.toString());
		configData.load();
		LOGGER.debug(CONFIG, "Loaded TOML config file {}", configPath.toString());

		try {
			FileWatcher.defaultInstance().addWatch(configPath, new ConfigWatcher(c, configData, Thread.currentThread().getContextClassLoader()));
			LOGGER.debug(CONFIG, "Watching TOML config file {} for changes", configPath.toString());
		} catch (IOException e) {
			throw new RuntimeException("Couldn't watch config file", e);
		}

		return configData;
	};
}
 
Example #2
Source File: CommentedConfigExample.java    From night-config with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static void main(String[] args) {
	// Creates a CommentedConfig in memory (ie not linked to a file)
	CommentedConfig config = CommentedConfig.inMemory();
	config.set("key", "value");
	config.setComment("key", "This is a comment.");

	String comment = config.getComment("key");
	System.out.println("Comment of the key: " + comment);
	System.out.println("Config: " + config);

	/* An non-FileConfig cannot be saved with a save() method, but any config can be saved by
	 an appropriate ConfigWriter. Here we'll use a TomlWriter to write the config in the TOML
	 format. */
	File configFile = new File("commentedConfig.toml");
	TomlWriter writer = new TomlWriter();
	writer.write(config, configFile, WritingMode.REPLACE);
	/* That's it! The config has been written to the file. Note that there is no need to close
	 the writer nor to catch any exception. */
}
 
Example #3
Source File: Config.java    From MiningGadgets with MIT License 5 votes vote down vote up
public static void loadConfig(ForgeConfigSpec spec, Path path) {

        final CommentedFileConfig configData = CommentedFileConfig.builder(path)
                .sync()
                .autosave()
                .writingMode(WritingMode.REPLACE)
                .build();

        configData.load();
        spec.setConfig(configData);
    }
 
Example #4
Source File: YamlTest.java    From night-config with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testReadWrite() {
	Config config = Config.inMemory();
	config.set("null", null);
	config.set("nullObject", NULL_OBJECT);
	config.set("string", "this is a string");
	config.set("sub.null", null);
	config.set("sub.nullObject", NULL_OBJECT);
	config.set("enum", BasicTestEnum.A); // complex enums doesn't appear to work with SnakeYAML

	System.out.println("Config: " + config);
	System.out.println("classOf[sub] = " + config.get("sub").getClass());
	System.out.println("sub.null = " + config.get("sub.null"));
	System.out.println("sub.nullObject = " + config.get("sub.nullObject"));
	YamlFormat yamlFormat = YamlFormat.defaultInstance();
	yamlFormat.writer().write(config, file, WritingMode.REPLACE);

	Config parsed = yamlFormat.createConcurrentConfig();
	yamlFormat.parser().parse(file, parsed, ParsingMode.REPLACE, THROW_ERROR);
	System.out.println("\nParsed: " + parsed);
	System.out.println("classOf[sub] = " + parsed.get("sub").getClass());
	assertNull(parsed.get("sub.null"));
	assertNull(parsed.get("sub.nullObject"));
	assertSame(NULL_OBJECT, parsed.valueMap().get("null"));
	assertSame(NULL_OBJECT,	parsed.valueMap().get("nullObject"));
	assertEquals(BasicTestEnum.A, parsed.getEnum("enum", BasicTestEnum.class));

	Assertions.assertEquals(config, parsed, "Error: written != parsed");
}
 
Example #5
Source File: JsonConfigTest.java    From night-config with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testWriteThenRead() throws IOException {
	FancyJsonWriter writer = new FancyJsonWriter();
	writer.write(config, file, WritingMode.REPLACE);

	Config read = new JsonParser().parse(file, FileNotFoundAction.THROW_ERROR);
	assertEquals(TestEnum.A, read.getEnum("enum", TestEnum.class));

	System.out.println("config: " + config);
	System.out.println("read: " + read);

	assertEquals(read.toString(), config.toString());
}
 
Example #6
Source File: WriteSyncFileConfig.java    From night-config with GNU Lesser General Public License v3.0 5 votes vote down vote up
WriteSyncFileConfig(C config, Path nioPath, Charset charset, ConfigWriter writer,
					 WritingMode writingMode, ConfigParser parser,
					 ParsingMode parsingMode, FileNotFoundAction nefAction) {
	super(config);
	this.nioPath = nioPath;
	this.charset = charset;
	this.writer = writer;
	this.parser = parser;
	this.parsingMode = parsingMode;
	this.nefAction = nefAction;
	this.writingMode = writingMode;
}
 
Example #7
Source File: JsonConfigTest.java    From night-config with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Test
public void testWrite() throws IOException {
	new FancyJsonWriter().setIndent(IndentStyle.SPACES_4).write(config, file, WritingMode.REPLACE);
}