public static void createDefaults(CommentedConfigurationNode config) { for (Field field : VoxelSniperConfiguration.class.getDeclaredFields()) { if (field.isAnnotationPresent(ConfigValue.class)) { ConfigValue anno = field.getAnnotation(ConfigValue.class); CommentedConfigurationNode node = config.getNode((Object[]) anno.name().split("\\.")); node.setComment(anno.comment()); try { node.setValue(field.get(null)); VoxelSniper.getLogger().debug("Created " + anno.name() + " = " + node.getValue()); } catch (IllegalArgumentException | IllegalAccessException e) { VoxelSniper.getLogger().error("Failed to extract default value for configuration field " + field.toGenericString()); e.printStackTrace(); } } } }
private Object parseConfiguration(CommentedConfigurationNode config) { if (config.hasListChildren()) { List<Object> cfg = new ArrayList<>(); for (CommentedConfigurationNode node : config.getChildrenList()) { cfg.add(parseConfiguration(node)); } return cfg; } else if (config.hasMapChildren()) { Map<String, Object> cfg = new HashMap<>(); for (CommentedConfigurationNode node : config.getChildrenMap().values()) { cfg.put(node.getKey().toString(), parseConfiguration(node)); } return cfg; } return config.getValue(); } }
public ConfigCategory loadConfig() { try { File file = new File(instance.configDir, "MagiBridge.conf"); if (!file.exists()) { file.createNewFile(); } ConfigurationLoader<CommentedConfigurationNode> loader = HoconConfigurationLoader.builder().setFile(file).build(); CommentedConfigurationNode config = loader.load(ConfigurationOptions.defaults().setObjectMapperFactory(instance.factory).setShouldCopyDefaults(true).setHeader(HEADER)); root = config.getValue(TypeToken.of(ConfigCategory.class), new ConfigCategory()); loader.save(config); return root; } catch (Exception e) { MagiBridge.getLogger().error("Could not load config.", e); return root; } }
if (!n.isVirtual()) { try { VoxelSniper.getLogger().debug("Loaded " + anno.name() + " = " + n.getValue()); field.set(null, n.getValue()); } catch (IllegalArgumentException | IllegalAccessException e) { VoxelSniper.getLogger().error("Error loading configuration value " + anno.name()); try { n.setValue(field.get(null)); VoxelSniper.getLogger().debug("Created " + anno.name() + " = " + n.getValue()); } catch (IllegalArgumentException | IllegalAccessException e) { VoxelSniper.getLogger().error("Failed to extract default value for configuration field " + field.toGenericString());
public static <T extends BaseConfig> T load(Path path, T defaultConfig) { ConfigurationLoader<CommentedConfigurationNode> loader = HoconConfigurationLoader.builder() .setPath(path) .build(); CommentedConfigurationNode node; try { node = loader.load(); } catch (IOException e) { e.printStackTrace(); node = loader.createEmptyNode(); } T config = null; try { config = (T)node.getValue(TypeToken.of(defaultConfig.getClass())); } catch (ObjectMappingException e) { e.printStackTrace(); } if (config == null) { config = defaultConfig; } config.setLoader(loader); config.setNode(node); config.save(); return config; } }
@Test public void testSimpleLoading(@TempDirectory.TempDir Path tempDir) throws IOException { URL url = getClass().getResource("/example.conf"); final Path saveTest = tempDir.resolve("text1.txt"); HoconConfigurationLoader loader = HoconConfigurationLoader.builder() .setSource(() -> new BufferedReader(new InputStreamReader(url.openStream(), UTF_8))) .setSink(AtomicFiles.createAtomicWriterFactory(saveTest, UTF_8)).build(); CommentedConfigurationNode node = loader.load(); assertEquals("unicorn", node.getNode("test", "op-level").getValue()); assertEquals("dragon", node.getNode("other", "op-level").getValue()); CommentedConfigurationNode testNode = node.getNode("test"); assertEquals(" Test node", testNode.getComment().orElse(null)); assertEquals("dog park", node.getNode("other", "location").getValue()); loader.save(node); assertEquals(Resources.readLines(getClass().getResource("/roundtrip-test.conf"), UTF_8), Files .readAllLines(saveTest, UTF_8)); }
@Test public void testCommentsTransferred() { CommentedConfigurationNode subject = SimpleCommentedConfigurationNode.root(); CommentedConfigurationNode firstChild = subject.getNode("first"); firstChild.setValue("test value"); firstChild.setComment("Such comment. Very wow."); CommentedConfigurationNode secondChild = subject.getNode("second"); secondChild.setValue("test value's evil twin"); assertFalse(secondChild.isVirtual()); secondChild.setValue(firstChild); assertEquals("test value", secondChild.getValue()); assertEquals("Such comment. Very wow.", secondChild.getComment().orElse(null)); }
@Test public void testNestedCommentsTransferred() { CommentedConfigurationNode subject = SimpleCommentedConfigurationNode.root(); CommentedConfigurationNode firstChild = subject.getNode("first"); CommentedConfigurationNode firstChildChild = firstChild.getNode("child"); firstChildChild.setValue("test value"); firstChildChild.setComment("Such comment. Very wow."); CommentedConfigurationNode secondChild = subject.getNode("second"); secondChild.setValue("test value's evil twin"); assertFalse(secondChild.isVirtual()); secondChild.setValue(firstChild); assertEquals("test value", secondChild.getNode("child").getValue()); assertEquals("Such comment. Very wow.", secondChild.getNode("child").getComment().orElse(null)); }