/** This configuration loader's default options */ private ConfigurationOptions getDefaultOptions() { ConfigurationOptions options = ConfigurationOptions.defaults(); if(header != null) options = options.setHeader(header); return options; }
public void load() { Path configFile = dataFolder.resolve("config.conf"); Path textFile = dataFolder.resolve("locale.conf"); try { if (Files.notExists(dataFolder)) { Files.createDirectories(dataFolder); } if (Files.notExists(configFile)) { Files.createFile(configFile); } if (Files.notExists(textFile)) { Files.createFile(textFile); } } catch (IOException ioEx) { logger.error("Failed to create default config file", ioEx); } loadMapper(configMapper, configFile, options); loadMapper(textMapper, textFile, options .setHeader("Visit: https://github.com/games647/FlexibleLogin/wiki for community given templates")); }
@Override public NodeType load(ConfigurationOptions options) throws IOException { if (!canLoad()) { throw new IOException("No source present to read from!"); } try (BufferedReader reader = source.call()) { NodeType node; if (headerMode == HeaderMode.PRESERVE || headerMode == HeaderMode.NONE) { String comment = CommentHandlers.extractComment(reader, commentHandlers); if (comment != null && comment.length() > 0) { options = options.setHeader(comment); } } node = createEmptyNode(options); loadInternal(node, reader); return node; } catch (FileNotFoundException | NoSuchFileException e) { // Squash -- there's nothing to read } catch (Exception e) { if (e instanceof IOException) { throw (IOException) e; } else { throw new IOException(e); } } return createEmptyNode(options); }
@NonNull @Override public NodeType load(@NonNull ConfigurationOptions options) throws IOException { if (source == null) { throw new IOException("No source present to read from!"); } try (BufferedReader reader = source.call()) { if (headerMode == HeaderMode.PRESERVE || headerMode == HeaderMode.NONE) { String comment = CommentHandlers.extractComment(reader, commentHandlers); if (comment != null && comment.length() > 0) { options = options.setHeader(comment); } } NodeType node = createEmptyNode(options); loadInternal(node, reader); return node; } catch (FileNotFoundException | NoSuchFileException e) { // Squash -- there's nothing to read return createEmptyNode(options); } catch (Exception e) { if (e instanceof IOException) { throw (IOException) e; } else { throw new IOException(e); } } }
/** * 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(); } }
@NonNull @Override public NodeType load(@NonNull ConfigurationOptions options) throws IOException { if (source == null) { throw new IOException("No source present to read from!"); } try (BufferedReader reader = source.call()) { if (headerMode == HeaderMode.PRESERVE || headerMode == HeaderMode.NONE) { String comment = CommentHandlers.extractComment(reader, commentHandlers); if (comment != null && comment.length() > 0) { options = options.setHeader(comment); } } NodeType node = createEmptyNode(options); loadInternal(node, reader); return node; } catch (FileNotFoundException | NoSuchFileException e) { // Squash -- there's nothing to read return createEmptyNode(options); } catch (Exception e) { if (e instanceof IOException) { throw (IOException) e; } else { throw new IOException(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 testComments(@TempDirectory.TempDir Path tempDir) throws IOException { URL url = getClass().getResource("/example3.xml"); final Path saveTest = tempDir.resolve("text3.txt"); XMLConfigurationLoader loader = XMLConfigurationLoader.builder() .setWriteExplicitType(true) .setIncludeXmlDeclaration(true) .setIndent(4) .setSource(() -> new BufferedReader(new InputStreamReader(url.openStream(), UTF_8))) .setSink(AtomicFiles.createAtomicWriterFactory(saveTest, UTF_8)).build(); AttributedConfigurationNode node = loader.createEmptyNode( loader.getDefaultOptions().setHeader("test header\ndo multiple lines work\nyes they do!!") ); node.setValue("something").setTagName("test"); loader.save(node); assertEquals(Resources.readLines(url, UTF_8), Files.readAllLines(saveTest)); } }
@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)); }