public SpongeConfig(File folder, String fileName, ServerCaller serverCaller) { super(folder, fileName, serverCaller); try { loader = HoconConfigurationLoader.builder().setFile(new File(folder, fileName)).build(); file = loader.load(); } catch (IOException e) { e.printStackTrace(); } }
public static ConfigurationNode loadDefaultConfiguration() throws IOException { final URL defaultConfig = FilePermissionsExConfiguration.class.getResource("default.conf"); if (defaultConfig == null) { throw new Error(t("Default config file is not present in jar!").translate(Locale.getDefault())); } HoconConfigurationLoader fallbackLoader = HoconConfigurationLoader.builder().setURL(defaultConfig).build(); return fallbackLoader.load(); } }
/** * Loads the given configuration file. */ public static void load(Path path) { System.out.println("Loading config from " + path.toString()); try { Files.createDirectories(path.getParent()); if (Files.notExists(path)) { Files.createFile(path); } loader = HoconConfigurationLoader.builder().setPath(path).build(); configMapper = ObjectMapper.forClass(ConfigBase.class).bindToNew(); node = loader.load(ConfigurationOptions.defaults().setHeader(HEADER)); config = configMapper.populate(node); configMapper.serialize(node); loader.save(node); } catch (Exception e) { System.err.println("Error loading configuration:"); e.printStackTrace(); } }
private <T> void loadMapper(ObjectMapper<T>.BoundInstance mapper, Path file, ConfigurationOptions options) { ConfigurationNode rootNode; if (mapper != null) { HoconConfigurationLoader loader = HoconConfigurationLoader.builder().setPath(file).build(); try { rootNode = loader.load(options.setShouldCopyDefaults(true)); ConfigurationNode hashNode = rootNode.getNode("hashAlgo"); if ("bcrypt".equalsIgnoreCase(hashNode.getString())) { hashNode.setValue("BCrypt"); } //load the config into the object mapper.populate(rootNode); //add missing default values loader.save(rootNode); } catch (ObjectMappingException objMappingExc) { logger.error("Error loading the configuration", objMappingExc); } catch (IOException ioExc) { logger.error("Error saving the default configuration", ioExc); } } }
CommentedConfigurationNode node = loader.load(); boolean modified = false; for (Field field : VoxelSniperConfiguration.class.getDeclaredFields()) {
@Test public void testSplitLineCommentInput(@TempDirectory.TempDir Path tempDir) throws IOException { final Path saveTo = tempDir.resolve("text2.txt"); HoconConfigurationLoader loader = HoconConfigurationLoader.builder() .setPath(saveTo) .setURL(getClass().getResource("/splitline-comment-input.conf")) .build(); CommentedConfigurationNode node = loader.load(); System.out.println(node.getOptions().getHeader()); loader.save(node); assertEquals(Resources.readLines(getClass().getResource("/splitline-comment-output.conf"), UTF_8), Files.readAllLines(saveTo, UTF_8)); }
@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)); }