@Nullable @Override protected Object parseValue(CommandSource source, CommandArgs args) throws ArgumentParseException { String argument = (String) super.parseValue(source, args); Callable<BufferedReader> reader = () -> new BufferedReader(new StringReader(argument)); ConfigurationLoader<? extends ConfigurationNode> loader = HoconConfigurationLoader.builder() .setSource(reader).build(); ConfigurationNode node; try { node = loader.load(); } catch (IOException ex) { throw args.createError(Text.of("Node parsing failed: ", ex.getMessage())); } return DataTranslators.CONFIGURATION_NODE.translate(node); }
private HoconConfigurationLoader(Builder build) { super(build, new CommentHandler[] {CommentHandlers.HASH, CommentHandlers.DOUBLE_SLASH}); this.render = build.getRenderOptions(); this.parse = build.getParseOptions(); }
@Override public HoconConfigurationLoader build() { return new HoconConfigurationLoader(this); } }
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); } } }
@Test public void testHeaderSaved(@TempDirectory.TempDir Path tempDir) throws IOException { final Path saveTo = tempDir.resolve("text3.txt"); HoconConfigurationLoader loader = HoconConfigurationLoader.builder() .setPath(saveTo) .build(); CommentedConfigurationNode node = loader.createEmptyNode(ConfigurationOptions.defaults().setHeader("Hi! I am a header!\n" + "Look at meeeeeee!!!")); node.getNode("node").setComment("I have a comment").getNode("party").setValue("now"); loader.save(node); assertEquals(Resources.readLines(getClass().getResource("/header.conf"), UTF_8), Files.readAllLines(saveTo, UTF_8)); }
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(); } }
/** * Saves the config back to disk to persist and changes made. */ public static void update() { try { configMapper.serialize(node); loader.save(node); } catch (Exception e) { System.err.println("Error saving configuration:"); e.printStackTrace(); } }
@Test public void testNewConfigObject() { Map<String, ConfigValue> entries = ImmutableMap.of("a", ConfigValueFactory.fromAnyRef("hi"), "b", ConfigValueFactory.fromAnyRef("bye")); HoconConfigurationLoader.newConfigObject(entries); }
@Test public void testNewConfigList() { List<ConfigValue> entries = ImmutableList.of(ConfigValueFactory.fromAnyRef("hello"), ConfigValueFactory.fromAnyRef("goodbye")); HoconConfigurationLoader.newConfigList(entries); } }
public static Builder builder() { return new Builder(); }
@Override protected void saveInternal(ConfigurationNode node, Writer writer) throws IOException { if (!node.hasMapChildren()) { if (node.getValue() == null) { writer.write(SYSTEM_LINE_SEPARATOR); return; } else { throw new IOException("HOCON cannot write nodes not in map format!"); } } final ConfigValue value = fromValue(node); final String renderedValue = value.render(render); writer.write(renderedValue); }
@Override public void loadInternal(CommentedConfigurationNode node, BufferedReader reader) throws IOException { Config hoconConfig = ConfigFactory.parseReader(reader, parse); hoconConfig = hoconConfig.resolve(); for (Map.Entry<String, ConfigValue> ent : hoconConfig.root().entrySet()) { readConfigValue(ent.getValue(), node.getNode(ent.getKey())); } }
@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)); }
configPath.toFile().mkdirs(); File configFile = new File(configPath.toFile(), "config.conf"); HoconConfigurationLoader configurationLoader = HoconConfigurationLoader.builder().setFile(configFile).build(); CommentedConfigurationNode node; if (!configFile.exists()) { configFile.createNewFile(); node = configurationLoader.load(); ); configurationLoader.save(node); } else { node = configurationLoader.load();
@Override public void setValue(String path, Object value) { file.getNode(path).setValue(value); try { loader.save(file); } catch (IOException e) { e.printStackTrace(); } }
private HoconConfigurationLoader(Builder build) { super(build, new CommentHandler[] {CommentHandlers.HASH, CommentHandlers.DOUBLE_SLASH}); this.render = build.getRenderOptions(); this.parse = build.getParseOptions(); }
@NonNull @Override public HoconConfigurationLoader build() { return new HoconConfigurationLoader(this); } }
/** * Creates a new {@link HoconConfigurationLoader} builder. * * @return A new builder */ @NonNull public static Builder builder() { return new Builder(); }
@Override protected void saveInternal(ConfigurationNode node, Writer writer) throws IOException { if (!node.hasMapChildren()) { if (node.getValue() == null) { writer.write(SYSTEM_LINE_SEPARATOR); return; } else { throw new IOException("HOCON cannot write nodes not in map format!"); } } final ConfigValue value = fromValue(node); final String renderedValue = value.render(render); writer.write(renderedValue); }