protected TestConfigurationLoader(Builder builder) { super(builder, CommentHandlers.values()); }
@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); }
@Test public void testExtractBlockCommentHeader() throws IOException { final String testDocument = "/*\n" + " * First header line\n" + " * more header\n" + " * even more header\n" + " */"; Optional<String> head = CommentHandlers.SLASH_BLOCK.extractHeader(new BufferedReader(new StringReader(testDocument))); assertTrue(head.isPresent()); assertEquals("First header line\n" + "more header\n" + "even more header", head.get()); assertEquals(testDocument, Joiner.on('\n').join(CommentHandlers.SLASH_BLOCK.toComment(AbstractConfigurationLoader .LINE_SPLITTER .splitToList(head.get())))); }
@Test public void testExtractLineCommentHeader() throws IOException { final String testDocument = "# First header line\n" + "# more header\n" + "# even more header"; Optional<String> head = CommentHandlers.HASH.extractHeader(new BufferedReader(new StringReader(testDocument))); assertTrue(head.isPresent()); assertEquals("First header line\n" + "more header\n" + "even more header", head.get()); } }
@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); } } }
@Test public void testExtractSingleLineBlockComment() throws IOException { final String testDocument = "/* single line */\n"; try (BufferedReader read = new BufferedReader(new StringReader(testDocument))) { Optional<String> head = CommentHandlers.SLASH_BLOCK.extractHeader(read); assertTrue(head.isPresent()); assertEquals("single line", head.get()); } }
@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); } } }