@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); }
options = options.setShouldCopyDefaults(true); node = config.load(options); } catch (IOException e) { logger.warn("Error loading WorldEdit configuration", e);
/** * Create a new configuration node populated with the appropriate data. * * @return The newly constructed node * @throws java.io.IOException if any sort of error occurs with reading or parsing the configuration */ default NodeType load() throws IOException { return load(getDefaultOptions()); }
/** * Loads configuration */ public void load() { try { baseNode = loader.load(); configuration = mapper.populate(Objects.requireNonNull(baseNode).getNode(baseNodeName)); } catch (ObjectMappingException | IOException e) { throw new RuntimeException(e); } }
@Override public PermissionsExConfiguration reload() throws IOException { ConfigurationNode node = this.loader.load(); FilePermissionsExConfiguration ret = new FilePermissionsExConfiguration(this.loader, node); ret.load(); return ret; }
/** * Attempts to load a {@link ConfigurationNode} using this loader, from the defined source. * * <p>The resultant node represents the root of the configuration being loaded.</p> * * <p>The {@link #getDefaultOptions() default options} will be used to construct the resultant * configuration nodes.</p> * * @return The newly constructed node * @throws IOException if any sort of error occurs with reading or parsing the configuration */ @NonNull default NodeType load() throws IOException { return load(getDefaultOptions()); }
/** * Attempts to load a {@link ConfigurationNode} using this loader, from the defined source. * * <p>The resultant node represents the root of the configuration being loaded.</p> * * <p>The {@link #getDefaultOptions() default options} will be used to construct the resultant * configuration nodes.</p> * * @return The newly constructed node * @throws IOException if any sort of error occurs with reading or parsing the configuration */ @NonNull default NodeType load() throws IOException { return load(getDefaultOptions()); }
public void load() { try { if (!mainConfig.exists()) { mainConfig.getParentFile().mkdirs(); mainConfig.createNewFile(); } config = configManager.load(CraftBookPlugin.spongeInst().configurationOptions); enabledMechanics.load(config); dataOnlyMode.load(config); configManager.save(config); } catch (IOException e) { plugin.getLogger().error("The CraftBook Configuration could not be read!", e); } }
private void convertFromBukkit() throws IOException { Path bukkitConfigPath = Paths.get("plugins/PermissionsEx"); if (Files.isDirectory(bukkitConfigPath) && isDirectoryEmpty(configDir)) { logger.info(t("Migrating configuration data from Bukkit")); Files.move(bukkitConfigPath, configDir, StandardCopyOption.REPLACE_EXISTING); } Path bukkitConfigFile = configDir.resolve("config.yml"); if (Files.exists(bukkitConfigFile)) { ConfigurationLoader<ConfigurationNode> yamlReader = YAMLConfigurationLoader.builder().setPath(bukkitConfigFile).build(); ConfigurationNode bukkitConfig = yamlReader.load(); configLoader.save(bukkitConfig); Files.move(bukkitConfigFile, configDir.resolve("config.yml.bukkit")); } }
private Path migrateLegacy(Path permissionsFile, String extension, ConfigurationLoader<?> loader, String formatName) throws PermissionsLoadingException { Path legacyPermissionsFile = permissionsFile; file = file.replace(extension, ".json"); permissionsFile = getManager().getBaseDirectory().resolve(file); permissionsFileLoader = createLoader(permissionsFile); try { permissionsConfig = loader.load(); permissionsFileLoader.save(permissionsConfig); Files.move(legacyPermissionsFile, legacyPermissionsFile.resolveSibling(legacyPermissionsFile.getFileName().toString() + ".legacy-backup")); } catch (IOException e) { throw new PermissionsLoadingException(t("While loading legacy %s permissions from %s", formatName, legacyPermissionsFile), e); } return permissionsFile; }
public void save() { try { // Reload the actual config to account for changes. But don't load it back into the ConfigValues. config = configManager.load(CraftBookPlugin.spongeInst().configurationOptions); enabledMechanics.save(config); dataOnlyMode.save(config); List<String> disabledMechanicsList = plugin.moduleController.getModules().stream().filter(entry -> !enabledMechanics.getValue().contains(entry.getName())).map(ModuleWrapper::getName).collect(Collectors.toList()); disabledMechanics.setValue(disabledMechanicsList); disabledMechanics.save(config); configManager.save(config); } catch (IOException e) { CraftBookPlugin.spongeInst().getLogger().error("The CraftBook configuration could not be saved!", e); } }
public static void loadConfig(VotifierPlugin pl) { plugin = pl; if (!plugin.getConfigDir().exists()) { plugin.getConfigDir().mkdirs(); } try { File file = new File(plugin.getConfigDir(), "config.yml"); if (!file.exists()) { file.createNewFile(); } ConfigurationLoader loader = YAMLConfigurationLoader.builder().setFile(file).build(); ConfigurationNode config = loader.load(ConfigurationOptions.defaults().setShouldCopyDefaults(true)); spongeConfig = config.getValue(TypeToken.of(SpongeConfig.class), new SpongeConfig()); loader.save(config); } catch (Exception e) { plugin.getLogger().error("Could not load config.", e); } }
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; } }
@Test public void testLoadingEmptyFile(@TempDirectory.TempDir Path tempDir) throws IOException { final File tempFile = tempDir.resolve("text3.txt").toFile(); tempFile.createNewFile(); ConfigurationLoader<ConfigurationNode> loader = GsonConfigurationLoader.builder() .setFile(tempFile) .build(); loader.load(); }
@Test public void testLoadingFileWithEmptyObject(@TempDirectory.TempDir Path tempDir) throws IOException { URL url = getClass().getResource("/emptyObject.json"); final Path tempFile = tempDir.resolve("text4.txt"); ConfigurationLoader<ConfigurationNode> loader = GsonConfigurationLoader.builder() .setSource(() -> new BufferedReader(new InputStreamReader(url.openStream()))) .setSink(AtomicFiles.createAtomicWriterFactory(tempFile, UTF_8)).setLenient(true).build(); ConfigurationNode node = loader.load(loader.getDefaultOptions().setMapFactory(MapFactories.sortedNatural())); assertNull(node.getValue()); assertFalse(node.hasMapChildren()); }
@Test public void testSimpleLoading() throws IOException { URL url = getClass().getResource("/example.yml"); ConfigurationLoader loader = YAMLConfigurationLoader.builder() .setURL(url).build(); ConfigurationNode node = loader.load(); assertEquals("unicorn", node.getNode("test", "op-level").getValue()); assertEquals("dragon", node.getNode("other", "op-level").getValue()); assertEquals("dog park", node.getNode("other", "location").getValue()); Function<Object, Map<String, List>> f = o -> (HashMap<String, List>)o; List<Map<String, List>> fooList = new ArrayList<>(node.getNode("foo").getList(f)); assertEquals(0, fooList.get(0).get("bar").size()); } }
@Test @Disabled("Gson currently makes it rather difficult to get the correct number type") public void testRoundtrippingLong(@TempDirectory.TempDir Path tempDir) throws IOException { final Path tempFile = tempDir.resolve("text5.txt"); ConfigurationLoader<ConfigurationNode> loader = GsonConfigurationLoader.builder().setPath(tempFile).build(); ConfigurationNode start = loader.createEmptyNode(); start.getNode("long-num").setValue(TEST_LONG_VAL); loader.save(start); System.out.println(Files.readAllLines(tempFile)); ConfigurationNode ret = loader.load(); System.out.println(ret.getNode("long-num").getValue().getClass()); assertEquals(TEST_LONG_VAL, ret.getNode("long-num").getValue()); } }
@Test public void testSimpleLoading(@TempDirectory.TempDir Path tempDir) throws IOException { URL url = getClass().getResource("/example.json"); final Path tempFile = tempDir.resolve("text1.txt"); ConfigurationLoader loader = JSONConfigurationLoader.builder() .setSource(() -> new BufferedReader(new InputStreamReader(url.openStream(), UTF_8))) .setSink(AtomicFiles.createAtomicWriterFactory(tempFile, UTF_8)).build(); ConfigurationNode node = loader.load(ConfigurationOptions.defaults().setMapFactory(MapFactories.sortedNatural())); assertEquals("unicorn", node.getNode("test", "op-level").getValue()); assertEquals("dragon", node.getNode("other", "op-level").getValue()); assertEquals("dog park", node.getNode("other", "location").getValue()); /*CommentedConfigurationNode commentNode = SimpleCommentedConfigurationNode.root(); commentNode.getNode("childOne").setValue("a").setComment("Test comment"); commentNode.getNode("childTwo", "something").setValue("b").setComment("Test comment 2"); commentNode.getNode("childTwo", "another").setValue("b").setComment("Test comment 3"); */ loader.save(node); assertEquals(Resources.readLines(url, UTF_8), Files .readAllLines(tempFile, UTF_8)); }
private void testRoundtripValue(Path tempDir, Object value) throws IOException { final Path tempFile = tempDir.resolve("text2.txt"); ConfigurationLoader<ConfigurationNode> loader = JSONConfigurationLoader.builder().setPath(tempFile).build(); ConfigurationNode start = loader.createEmptyNode(); start.getNode("value").setValue(value); loader.save(start); ConfigurationNode ret = loader.load(); assertEquals(value, ret.getNode("value").getValue()); }
@Test public void testSimpleLoading(@TempDirectory.TempDir Path tempDir) throws IOException { URL url = getClass().getResource("/example.json"); final Path tempFile = tempDir.resolve("text1.txt"); ConfigurationLoader<ConfigurationNode> loader = GsonConfigurationLoader.builder() .setSource(() -> new BufferedReader(new InputStreamReader(url.openStream()))) .setSink(AtomicFiles.createAtomicWriterFactory(tempFile, UTF_8)).setLenient(true).build(); ConfigurationNode node = loader.load(loader.getDefaultOptions().setMapFactory(MapFactories.sortedNatural())); assertEquals("unicorn", node.getNode("test", "op-level").getValue()); assertEquals("dragon", node.getNode("other", "op-level").getValue()); assertEquals("dog park", node.getNode("other", "location").getValue()); assertTrue(node.getNode("int-val").getValue() instanceof Integer); assertTrue(node.getNode("double-val").getValue() instanceof Double); loader.save(node); assertEquals(Resources.readLines(url, UTF_8), Files.readAllLines(tempFile, UTF_8)); }