001package org.unix4j.unix.sed; 002 003import org.unix4j.command.AbstractCommand; 004import org.unix4j.context.ExecutionContext; 005import org.unix4j.processor.LineProcessor; 006import org.unix4j.unix.Sed; 007 008/** 009 * Implementation of the {@link Sed sed} command. 010 */ 011class SedCommand extends AbstractCommand<SedArguments> { 012 public SedCommand(SedArguments arguments) { 013 super(Sed.NAME, arguments); 014 } 015 016 @Override 017 public LineProcessor execute(ExecutionContext context, final LineProcessor output) { 018 final SedArguments args = getArguments(context); 019 if (args.isScriptSet()) { 020 //command specified in script 021 final String script = args.getScript(); 022 final Command command = Command.fromScript(script); 023 if (command == null) { 024 throw new IllegalArgumentException("command missing or invalid in sed script: " + script); 025 } 026 return command.createProcessorFor(script, args, output); 027 } 028 029 //command from args, or default if not specified 030 Command command = Command.fromArgs(args); 031 if (command == null) { 032 //default command 033 if (args.isReplacementSet() || args.isString2Set()) { 034 command = Command.substitute; 035 } else { 036 command = Command.print; 037 } 038 } 039 return command.createProcessorFor(args, output); 040 } 041}