com.electronwill.nightconfig.core.CommentedConfig Java Examples

The following examples show how to use com.electronwill.nightconfig.core.CommentedConfig. 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: 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 #2
Source File: TomlParserTest.java    From night-config with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void readWriteReadAgain() {
	File file = new File("test.toml");
	CommentedConfig parsed = new TomlParser().parse(file, FileNotFoundAction.THROW_ERROR);

	System.out.println("--- parsed --- \n" + parsed);
	System.out.println("--------------------------------------------");
	assertNull(parsed.getComment("without_comment"));
	assertNotNull(parsed.getComment("with_comments"));
	assertTrue(parsed.getComment("with_comments").contains("\n"));
	assertEquals(TestEnum.A, parsed.getEnum("enum", TestEnum.class));

	java.io.StringWriter sw = new StringWriter();
	TomlWriter writer = new TomlWriter();
	writer.write(parsed, sw);
	System.out.println("--- written --- \n" + sw);
	System.out.println("--------------------------------------------");

	CommentedConfig reparsed = new TomlParser().parse(new StringReader(sw.toString()));
	System.out.println("--- reparsed --- \n" + reparsed);
	assertEquals(parsed, reparsed);
}
 
Example #3
Source File: TableParser.java    From night-config with GNU Lesser General Public License v3.0 6 votes vote down vote up
static CommentedConfig parseInline(CharacterInput input, TomlParser parser) {
	CommentedConfig config = TomlFormat.instance().createConfig();
	while (true) {
		char keyFirst = Toml.readNonSpaceChar(input, false);
		if (keyFirst == '}') {
			return config;// handles {} and {k1=v1,... ,}
		}
		String key = parseKey(input, keyFirst, parser);
		char sep = Toml.readNonSpaceChar(input, false);
		checkInvalidSeparator(sep, key, parser);

		Object value = ValueParser.parse(input, parser);
		Object previous = parser.getParsingMode().put(config.valueMap(), key, value);
		checkDuplicateKey(key, previous, true);

		char after = Toml.readNonSpaceChar(input, false);
		if (after == '}') {
			return config;
		}
		if (after != ',') {
			throw new ParsingException(
					"Invalid entry separator '" + after + "' in inline table.");
		}
	}
}
 
Example #4
Source File: HoconParserTest.java    From night-config with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void readWriteReadAgain() {
	File file = new File("test.hocon");
	CommentedConfig parsed = new HoconParser().parse(file, FileNotFoundAction.THROW_ERROR);

	System.out.println("--- parsed --- \n" + parsed);
	System.out.println("--------------------------------------------");
	java.io.StringWriter sw = new StringWriter();
	HoconWriter writer = new HoconWriter();
	writer.write(parsed, sw);
	System.out.println("--- written --- \n" + sw);
	System.out.println("--------------------------------------------");

	CommentedConfig reparsed = new HoconParser().parse(new StringReader(sw.toString()));
	System.out.println("--- reparsed --- \n" + reparsed);
	assertEquals(parsed, reparsed);
}
 
Example #5
Source File: HoconParser.java    From night-config with GNU Lesser General Public License v3.0 6 votes vote down vote up
private static void put(ConfigObject typesafeConfig, CommentedConfig destination,
						ParsingMode parsingMode) {
	for (Map.Entry<String, ConfigValue> entry : typesafeConfig.entrySet()) {
		List<String> path = Collections.singletonList(entry.getKey());
		ConfigValue value = entry.getValue();
		if (value instanceof ConfigObject) {
			CommentedConfig subConfig = destination.createSubConfig();
			put((ConfigObject)value, subConfig, parsingMode);
			parsingMode.put(destination, path, subConfig);
		} else {
			parsingMode.put(destination, path, unwrap(value.unwrapped()));
		}
		List<String> comments = value.origin().comments();
		if (!comments.isEmpty()) {
			destination.setComment(path, String.join("\n", value.origin().comments()));
		}
	}
}
 
Example #6
Source File: HoconParser.java    From night-config with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void parse(Reader reader, Config destination, ParsingMode parsingMode) {
	try {
		ConfigObject parsed = ConfigFactory.parseReader(reader, OPTIONS).resolve().root();
		parsingMode.prepareParsing(destination);
		if (destination instanceof CommentedConfig) {
			put(parsed, (CommentedConfig)destination, parsingMode);
		} else {
			put(parsed, destination, parsingMode);
		}
	} catch (Exception e) {
		throw new ParsingException("HOCON parsing failed", e);
	}
}
 
Example #7
Source File: TableParser.java    From night-config with GNU Lesser General Public License v3.0 5 votes vote down vote up
static <T extends CommentedConfig> T parseNormal(CharacterInput input, TomlParser parser,
												 T config) {
	while (true) {
		List<Charray> commentsList = new ArrayList<>(2);
		int keyFirst = Toml.readUseful(input, commentsList);
		if (keyFirst == -1 || keyFirst == '[') {
			parser.setComment(commentsList);// Saves the comments that are above the next table
			return config;// No more data, or beginning of an other table
		}
		List<String> key = parseDottedKey(input, (char)keyFirst, parser);

		Object value = ValueParser.parse(input, parser);
		Object previous = parser.getParsingMode().put(config, key, value);
		checkDuplicateKey(key, previous, parser.configWasEmpty());

		int after = Toml.readNonSpace(input, false);
		if (after == -1) {// End of the stream
			return config;
		}
		if (after == '#') {
			Charray comment = Toml.readLine(input);
			commentsList.add(comment);
		} else if (after != '\n' && after != '\r') {
			throw new ParsingException("Invalid character '"
									   + (char)after
									   + "' after table entry \""
									   + key
									   + "\" = "
									   + value);
		}
		parser.setComment(commentsList);
		config.setComment(key, parser.consumeComment());
	}
}
 
Example #8
Source File: HoconWriterTest.java    From night-config with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testWrite() throws IOException {
	Config subConfig = CommentedConfig.inMemory();
	subConfig.set("string", "test");
	subConfig.set("enum", BasicTestEnum.C);
	subConfig.set("sub", CommentedConfig.inMemory());

	List<Config> configList = new ArrayList<>();
	configList.add(subConfig);
	configList.add(subConfig);
	configList.add(subConfig);

	CommentedConfig config = CommentedConfig.inMemory();
	config.set("string", "\"value\"");
	config.set("integer", 2);
	config.set("long", 123456789L);
	config.set("double", 3.1415926535);
	config.set("bool_array", Arrays.asList(true, false, true, false));
	config.set("config", subConfig);
	config.set("config_list", configList);
	config.setComment("string", " Comment 1\n Comment 2\n Comment 3");
	config.set("enum", TestEnum.A);

	StringWriter sw = new StringWriter();
	HoconWriter writer = new HoconWriter();
	writer.write(config, sw);
	System.out.println("Written:");
	System.out.println(sw);
}
 
Example #9
Source File: ConfigTracker.java    From patchwork-api with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void loadDefaultServerConfigs() {
	configSets.get(ModConfig.Type.SERVER).forEach(modConfig -> {
		final CommentedConfig commentedConfig = CommentedConfig.inMemory();
		modConfig.getSpec().correct(commentedConfig);
		modConfig.setConfigData(commentedConfig);
		modConfig.fireEvent(new ModConfig.Loading(modConfig));
	});
}
 
Example #10
Source File: ForgeConfigSpec.java    From patchwork-api with GNU Lesser General Public License v2.1 5 votes vote down vote up
public synchronized int correct(CommentedConfig config, CorrectionListener listener) {
	// Linked list for fast add/removes
	LinkedList<String> parentPath = new LinkedList<>();
	int ret = -1;

	try {
		isCorrecting = true;
		ret = correct(this.config, config, parentPath, Collections.unmodifiableList(parentPath), listener, false);
	} finally {
		isCorrecting = false;
	}

	return ret;
}
 
Example #11
Source File: ForgeConfigSpec.java    From patchwork-api with GNU Lesser General Public License v2.1 5 votes vote down vote up
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 #12
Source File: ForgeConfigSpec.java    From patchwork-api with GNU Lesser General Public License v2.1 4 votes vote down vote up
public synchronized boolean isCorrect(CommentedConfig config) {
	LinkedList<String> parentPath = new LinkedList<>();
	return correct(this.config, config, parentPath, Collections.unmodifiableList(parentPath), (a, b, c, d) -> {
	}, true) == 0;
}
 
Example #13
Source File: ModConfig.java    From patchwork-api with GNU Lesser General Public License v2.1 4 votes vote down vote up
void setConfigData(final CommentedConfig configData) {
	this.configData = configData;
	this.spec.setConfig(this.configData);
}
 
Example #14
Source File: TomlParserTest.java    From night-config with GNU Lesser General Public License v3.0 4 votes vote down vote up
private static void parseAndPrint(String tomlString) {
	TomlParser parser = new TomlParser();
	CommentedConfig parsed = parser.parse(new StringReader(tomlString));
	System.out.println("parsed: " + parsed);
}
 
Example #15
Source File: TomlParser.java    From night-config with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public ConfigFormat<CommentedConfig> getFormat() {
	return TomlFormat.instance();
}
 
Example #16
Source File: TomlParser.java    From night-config with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public CommentedConfig parse(Reader reader) {
	configWasEmpty = true;
	return parse(new ReaderInput(reader), TomlFormat.instance().createConfig(), ParsingMode.MERGE);
}
 
Example #17
Source File: TomlFormat.java    From night-config with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public CommentedConfig createConfig(Supplier<Map<String, Object>> mapCreator) {
	return CommentedConfig.of(mapCreator, this);
}
 
Example #18
Source File: TomlFormat.java    From night-config with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * @return a new thread-safe config with the toml format
 */
public static CommentedConfig newConcurrentConfig() {
	return INSTANCE.createConcurrentConfig();
}
 
Example #19
Source File: TomlFormat.java    From night-config with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * @return a new config with the given map creator
 */
public static CommentedConfig newConfig(Supplier<Map<String, Object>> s) {
	return INSTANCE.createConfig(s);
}
 
Example #20
Source File: TomlFormat.java    From night-config with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * @return a new config with the toml format
 */
public static CommentedConfig newConfig() {
	return INSTANCE.createConfig();
}
 
Example #21
Source File: TableParser.java    From night-config with GNU Lesser General Public License v3.0 4 votes vote down vote up
static CommentedConfig parseNormal(CharacterInput input, TomlParser parser) {
	return parseNormal(input, parser, TomlFormat.instance().createConfig());
}
 
Example #22
Source File: HoconFormat.java    From night-config with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * @return a new config with the hocon format
 */
public static CommentedConfig newConfig() {
	return INSTANCE.createConfig();
}
 
Example #23
Source File: ForgeConfigSpec.java    From patchwork-api with GNU Lesser General Public License v2.1 4 votes vote down vote up
public int correct(CommentedConfig config) {
	return correct(config, (action, path, incorrectValue, correctedValue) -> {
	});
}
 
Example #24
Source File: ConfigValue.java    From Sandbox with GNU Lesser General Public License v3.0 4 votes vote down vote up
public ConfigValue(CommentedConfig config, String path) {
    this.config = config;
    this.path = path;
}
 
Example #25
Source File: ModConfig.java    From patchwork-api with GNU Lesser General Public License v2.1 4 votes vote down vote up
public CommentedConfig getConfigData() {
	return this.configData;
}
 
Example #26
Source File: HoconParser.java    From night-config with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public CommentedConfig parse(Reader reader) {
	CommentedConfig config = HoconFormat.instance().createConfig();
	parse(reader, config, ParsingMode.MERGE);
	return config;
}
 
Example #27
Source File: HoconParser.java    From night-config with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public ConfigFormat<CommentedConfig> getFormat() {
	return HoconFormat.instance();
}
 
Example #28
Source File: HoconFormat.java    From night-config with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public CommentedConfig createConfig(Supplier<Map<String, Object>> mapCreator) {
	return CommentedConfig.of(mapCreator, this);
}
 
Example #29
Source File: HoconFormat.java    From night-config with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public ConfigParser<CommentedConfig> parser() {
	return new HoconParser();
}
 
Example #30
Source File: HoconFormat.java    From night-config with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * @return a new thread-safe config with the hocon format
 */
public static CommentedConfig newConcurrentConfig() {
	return INSTANCE.createConfig();
}