001/*
002 * JDrupes Builder
003 * Copyright (C) 2025 Michael N. Lipp
004 * 
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
017 */
018
019package org.jdrupes.builder.eclipse;
020
021import java.io.FilterWriter;
022import java.io.IOException;
023import java.io.Writer;
024import java.util.Arrays;
025
026/// Add a comment to the written properties file and remove any
027/// comments from upstream. This seems to be the only possible
028/// way to avoid a time stamp in the written properties file
029/// which may lead to confusion if nothing else has changed.
030///
031public class FixCommentsFilter extends FilterWriter {
032
033    @SuppressWarnings("PMD.AvoidStringBufferField")
034    private final StringBuilder buffer = new StringBuilder();
035
036    /// Instantiates a new fix comments filter.
037    ///
038    /// @param out the out
039    /// @param comment the comment
040    ///
041    public FixCommentsFilter(Writer out, String comment) {
042        super(out);
043        if (comment != null) {
044            Arrays.stream(comment.split("\n")).map(String::trim)
045                .forEach(s -> {
046                    try {
047                        out.append("# ").append(s)
048                            .append(System.lineSeparator());
049                    } catch (IOException e) { // NOPMD
050                        // Not really important
051                    }
052                });
053        }
054    }
055
056    @Override
057    public void write(int chr) throws IOException {
058        buffer.append((char) chr);
059        checkBuffer();
060    }
061
062    @Override
063    public void write(char[] cbuf, int off, int len) throws IOException {
064        buffer.append(cbuf, off, len);
065        checkBuffer();
066    }
067
068    @Override
069    public void write(String str, int off, int len) throws IOException {
070        buffer.append(str, off, len);
071        checkBuffer();
072    }
073
074    @SuppressWarnings("PMD.AvoidLiteralsInIfCondition")
075    private void checkBuffer() throws IOException {
076        while (true) {
077            int idx = buffer.indexOf("\n");
078            if (idx < 0) {
079                break;
080            }
081            if (buffer.charAt(0) != '#') {
082                out.write(buffer.substring(0, idx + 1));
083            }
084            buffer.delete(0, idx + 1);
085        }
086    }
087
088    @Override
089    public void close() throws IOException {
090        out.write(buffer.toString());
091        out.close();
092    }
093}