001package org.unix4j.unix.xargs;
002
003import org.unix4j.command.AbstractCommand;
004import org.unix4j.command.Command;
005import org.unix4j.context.ExecutionContext;
006import org.unix4j.processor.LineProcessor;
007import org.unix4j.unix.Echo;
008import org.unix4j.unix.Xargs;
009import org.unix4j.variable.Arg;
010
011/**
012 * Implementation of the {@link Xargs xargs} command.
013 */
014class XargsCommand extends AbstractCommand<XargsArguments> {
015        
016        private final Command<?> invokedCommand;
017        
018        public XargsCommand(XargsArguments arguments) {
019                this(arguments, null);
020        }
021        protected XargsCommand(XargsArguments arguments, Command<?> invokedCommand) {
022                super(Xargs.NAME, arguments);
023                this.invokedCommand = invokedCommand;
024        }
025        
026        protected Command<?> getInvokedCommand() {
027                return invokedCommand == null ? Echo.Factory.echo(Arg.$all) : invokedCommand;
028        }
029        
030        @Override
031        public Command<?> join(Command<?> next) {
032                return invokedCommand == null ? new XargsCommand(getArguments(null), next) : super.join(next);
033        }
034
035        @Override
036        public LineProcessor execute(ExecutionContext context, LineProcessor output) {
037                return new XargsLineProcessor(this, context, output);
038        }
039        
040        @Override
041        public String toString() {
042                return super.toString() + " " + invokedCommand;
043        }
044
045}