001package org.unix4j.unix.sed;
002
003import org.unix4j.line.Line;
004import org.unix4j.processor.LineProcessor;
005import org.unix4j.util.StringUtil;
006
007class DeleteProcessor extends AbstractRegexpProcessor {
008        public DeleteProcessor(Command command, SedArguments args, LineProcessor output) {
009                super(command, args, output);
010        }
011        public DeleteProcessor(Command command, String script, SedArguments args, LineProcessor output) {
012                this(command, deriveArgs(command, script, args), output);
013        }
014
015        private static SedArguments deriveArgs(Command command, String script, SedArguments args) {
016                final int start = StringUtil.findStartTrimWhitespace(script);
017                final int end = indexOfNextDelimiter(script, start);
018                if (end < 0) {
019                        throw new IllegalArgumentException("invalid script for sed " + command + " command: " + script);
020                }
021                args = parsePatternFlags(command, args, script, end + 1);
022                args.setRegexp(script.substring(start + 1, end));
023                return args;
024        }
025
026        @Override
027        public boolean processLine(Line line) {
028                final boolean matches = regexp.matcher(line).find();
029                if (!matches) {
030                        return output.processLine(line);
031                }
032                return true;
033        }
034}