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