public T setPath(Path path) { Path absPath = Objects.requireNonNull(path, "path").toAbsolutePath(); this.source = () -> Files.newBufferedReader(absPath, UTF_8); this.sink = AtomicFiles.createAtomicWriterFactory(absPath, UTF_8); return self(); }
public static Callable<BufferedWriter> createAtomicWriterFactory(Path path, Charset charset) { Preconditions.checkNotNull(path, "path"); return () -> createAtomicBufferedWriter(path, charset); }
public static BufferedWriter createAtomicBufferedWriter(Path path, Charset charset) throws IOException { path = path.toAbsolutePath(); Path writePath = getTemporaryPath(path.getParent(), path.getFileName().toString()); if (Files.exists(path)) { Files.copy(path, writePath, StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING); } BufferedWriter output = Files.newBufferedWriter(writePath, charset); return new BufferedWriter(new AtomicFileWriter(writePath, path, output)); }
/** * Sets the sink and source of the resultant loader to the given path. * * <p>The {@link #getSource() source} is defined using * {@link Files#newBufferedReader(Path)} with UTF-8 encoding.</p> * * <p>The {@link #getSink() sink} is defined using {@link AtomicFiles} with UTF-8 * encoding.</p> * * @param path The path of the configuration file * @return This builder (for chaining) */ @NonNull public T setPath(@NonNull Path path) { Path absPath = Objects.requireNonNull(path, "path").toAbsolutePath(); this.source = () -> Files.newBufferedReader(absPath, UTF_8); this.sink = AtomicFiles.createAtomicWriterFactory(absPath, UTF_8); return self(); }
/** * Creates and returns an "atomic" writer factory for the given path. * * @param path The path * @param charset The charset to be used by the writer * @return The writer factory */ @NonNull public static Callable<BufferedWriter> createAtomicWriterFactory(@NonNull Path path, @NonNull Charset charset) { Preconditions.checkNotNull(path, "path"); return () -> createAtomicBufferedWriter(path, charset); }
/** * Creates and returns an "atomic" writer for the given path. * * @param path The path * @param charset The charset to be used by the writer * @return The writer factory */ @NonNull public static BufferedWriter createAtomicBufferedWriter(@NonNull Path path, @NonNull Charset charset) throws IOException { path = path.toAbsolutePath(); Path writePath = getTemporaryPath(path.getParent(), path.getFileName().toString()); if (Files.exists(path)) { Files.copy(path, writePath, StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING); } BufferedWriter output = Files.newBufferedWriter(writePath, charset); return new BufferedWriter(new AtomicFileWriter(writePath, path, output)); }
/** * Sets the sink and source of the resultant loader to the given path. * * <p>The {@link #getSource() source} is defined using * {@link Files#newBufferedReader(Path)} with UTF-8 encoding.</p> * * <p>The {@link #getSink() sink} is defined using {@link AtomicFiles} with UTF-8 * encoding.</p> * * @param path The path of the configuration file * @return This builder (for chaining) */ @NonNull public T setPath(@NonNull Path path) { Path absPath = Objects.requireNonNull(path, "path").toAbsolutePath(); this.source = () -> Files.newBufferedReader(absPath, UTF_8); this.sink = AtomicFiles.createAtomicWriterFactory(absPath, UTF_8); return self(); }
/** * Creates and returns an "atomic" writer factory for the given path. * * @param path The path * @param charset The charset to be used by the writer * @return The writer factory */ @NonNull public static Callable<BufferedWriter> createAtomicWriterFactory(@NonNull Path path, @NonNull Charset charset) { Preconditions.checkNotNull(path, "path"); return () -> createAtomicBufferedWriter(path, charset); }
/** * Creates and returns an "atomic" writer for the given path. * * @param path The path * @param charset The charset to be used by the writer * @return The writer factory */ @NonNull public static BufferedWriter createAtomicBufferedWriter(@NonNull Path path, @NonNull Charset charset) throws IOException { path = path.toAbsolutePath(); Path writePath = getTemporaryPath(path.getParent(), path.getFileName().toString()); if (Files.exists(path)) { Files.copy(path, writePath, StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING); } BufferedWriter output = Files.newBufferedWriter(writePath, charset); return new BufferedWriter(new AtomicFileWriter(writePath, path, output)); }
@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 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 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 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)); }
@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)); }
@Test public void testExplicitTypes(@TempDirectory.TempDir Path tempDir) throws IOException { URL url = getClass().getResource("/example2.xml"); final Path saveTest = tempDir.resolve("text2.txt"); XMLConfigurationLoader loader = XMLConfigurationLoader.builder() .setWriteExplicitType(true) .setIncludeXmlDeclaration(false) .setIndent(4) .setSource(() -> new BufferedReader(new InputStreamReader(url.openStream(), UTF_8))) .setSink(AtomicFiles.createAtomicWriterFactory(saveTest, UTF_8)).build(); AttributedConfigurationNode node = loader.load(); AttributedConfigurationNode list1 = node.getNode("list1"); assertTrue(list1.hasListChildren()); AttributedConfigurationNode list2 = node.getNode("list2"); assertTrue(list2.hasListChildren()); AttributedConfigurationNode map1 = node.getNode("map1"); assertTrue(map1.hasMapChildren()); AttributedConfigurationNode map2 = node.getNode("map2"); assertTrue(map2.hasMapChildren()); // roundtrip! loader.save(node); assertEquals(Resources.readLines(url, UTF_8), Files.readAllLines(saveTest)); }
.setIndent(4) .setSource(() -> new BufferedReader(new InputStreamReader(url.openStream(), UTF_8))) .setSink(AtomicFiles.createAtomicWriterFactory(saveTest, UTF_8)).build();