001package org.unix4j.unix.cat;
002
003import org.unix4j.command.Command;
004import org.unix4j.context.ExecutionContext;
005import org.unix4j.line.Line;
006import org.unix4j.processor.AbstractLineProcessor;
007import org.unix4j.processor.LineProcessor;
008
009class SqueezeEmptyLinesProcessor extends AbstractLineProcessor<CatArguments> {
010        
011        private boolean wasEmpty = false;
012
013        public SqueezeEmptyLinesProcessor(Command<CatArguments> command, ExecutionContext context, LineProcessor output) {
014                super(command, context, output);
015        }
016
017        @Override
018        public boolean processLine(Line line) {
019                if (line.getContentLength() > 0) {
020                        if (wasEmpty) {
021                                wasEmpty = false;
022                        }
023                        return getOutput().processLine(line);
024                }
025                //empty line
026                if (!wasEmpty) {
027                        //print first empty line
028                        wasEmpty = true;
029                        return getOutput().processLine(line);
030                }
031                //suppress repeated empty liens
032                return true;//we still want the next line
033        }
034
035        @Override
036        public void finish() {
037                getOutput().finish();
038        }
039}