Permalink
Browse files

Fix for Issue #58 and generally for grep with single file input

  • Loading branch information...
terzerm committed Aug 8, 2018
1 parent da614bc commit d3220f747ae86e9f0a357b6b131f2d13f8cc6f2a
@@ -24,7 +24,7 @@ public boolean processLine(Input input, Line line, LineProcessor output) {
@Override
public void finish(Input input, LineProcessor output) {
//default: no op
output.finish();
}
}
@@ -22,6 +22,7 @@
private final List<? extends Input> inputs;
private final InputProcessor processor;
private final LineProcessor output;
private final LineProcessor nonFinishingOutput;
/**
* Constructor with input objects (usually file operands of the command) and
@@ -33,10 +34,21 @@
* the operation applied to every input in the given
* {@code inputs} list
*/
public MultipleInputLineProcessor(List<? extends Input> inputs, InputProcessor processor, LineProcessor output) {
public MultipleInputLineProcessor(List<? extends Input> inputs, InputProcessor processor, final LineProcessor output) {
this.inputs = inputs;
this.processor = processor;
this.output = output;
this.nonFinishingOutput = new LineProcessor() {
@Override
public boolean processLine(final Line line) {
return output.processLine(line);
}
@Override
public void finish() {
//ignore, we finish only at the very end of all files
}
};
}
@Override
@@ -70,7 +82,7 @@ public void finish() {
break;// wants no more lines
}
}
processor.finish(input, output);
processor.finish(input, nonFinishingOutput);
} catch (ExitValueException e) {
e.setInput(input);
throw e;
@@ -0,0 +1,38 @@
package org.unix4j.unix.grep;
import org.unix4j.context.ExecutionContext;
import org.unix4j.line.Line;
import org.unix4j.line.SimpleLine;
import org.unix4j.processor.LineProcessor;
import org.unix4j.util.Counter;
/**
* Writes the matching line count to the output. The matching operation is delegated
* to the {@link LineMatcher} passed to the constructor.
*/
final class CountMatchingLinesProcessor extends AbstractGrepProcessor {
private final Counter counter = new Counter();
public CountMatchingLinesProcessor(GrepCommand command, ExecutionContext context, LineProcessor output, LineMatcher matcher) {
super(command, context, output, matcher);
}
@Override
protected boolean processLine(Line line, boolean isMatch) {
if (isMatch) {
counter.increment();
}
return true;//even if line is not a match, we still want the next line
}
@Override
public void finish() {
try {
getOutput().processLine(new SimpleLine(String.valueOf(counter.getCount())));
super.finish();
} finally {
counter.reset();
}
}
}
@@ -3,10 +3,9 @@
import org.unix4j.command.AbstractCommand;
import org.unix4j.context.ExecutionContext;
import org.unix4j.io.FileInput;
import org.unix4j.processor.InputProcessor;
import org.unix4j.processor.LineProcessor;
import org.unix4j.processor.MultipleInputLineProcessor;
import org.unix4j.processor.RedirectInputLineProcessor;
import org.unix4j.io.Input;
import org.unix4j.io.NullInput;
import org.unix4j.processor.*;
import org.unix4j.unix.Grep;
import org.unix4j.util.FileUtil;
@@ -64,23 +63,56 @@ private LineProcessor getStandardInputProcessor(ExecutionContext context, LinePr
}
return new WriteMatchingLinesProcessor(this, context, output, matcher);
}
private LineProcessor getFileInputProcessor(List<FileInput> inputs, ExecutionContext context, LineProcessor output, GrepArguments args) {
switch (inputs.size()) {
case 0:
return getInputProcessor(NullInput.INSTANCE, context, output, args);
case 1:
return getInputProcessor(inputs.get(0), context, output, args);
default:
return getMultipleFilesInputProcessor(inputs, context, output, args);
}
}
private LineProcessor getInputProcessor(Input input, ExecutionContext context, LineProcessor output, GrepArguments args) {
if (args.isCount()) {
final LineMatcher matcher = getMatcher(args);
final InputProcessor processor = new CountMatchingLinesInputProcessor(this, context, output, matcher);
return new MultipleInputLineProcessor(inputs, processor, output);
final LineProcessor lineProcessor = new CountMatchingLinesProcessor(this, context, output, matcher);
return new InputLineProcessor(input, new DefaultInputProcessor(), lineProcessor);
} else if (args.isMatchingFiles()) {
final LineMatcher matcher = getMatcher(args);
final InputProcessor processor = new WriteFilesWithMatchingLinesProcessor(this, context, output, matcher);
return new MultipleInputLineProcessor(inputs, processor, output);
final InputProcessor inputProcessor = new WriteFilesWithMatchingLinesProcessor(this, context, output, matcher);
return new InputLineProcessor(input, inputProcessor, output);
} else if (args.isLineNumber()) {
final LineMatcher matcher = getMatcher(args);
final LineProcessor processor = new WriteMatchingLinesProcessor(this, context, output, matcher);
final LineProcessor lineProcessor = new WriteMatchingLinesWithLineNumberProcessor(this, context, output, matcher);
return new InputLineProcessor(input, new DefaultInputProcessor(), lineProcessor);
} else {
final LineMatcher matcher = getMatcher(args);
final LineProcessor lineProcessor = new WriteMatchingLinesProcessor(this, context, output, matcher);
return new InputLineProcessor(input, new DefaultInputProcessor(), lineProcessor);
}
}
private LineProcessor getMultipleFilesInputProcessor(List<FileInput> inputs, ExecutionContext context, LineProcessor output, GrepArguments args) {
if (args.isCount()) {
final LineMatcher matcher = getMatcher(args);
final InputProcessor inputProcessor = new CountMatchingLinesInputProcessor(this, context, output, matcher);
return new MultipleInputLineProcessor(inputs, inputProcessor, output);
} else if (args.isMatchingFiles()) {
final LineMatcher matcher = getMatcher(args);
final InputProcessor inputProcessor = new WriteFilesWithMatchingLinesProcessor(this, context, output, matcher);
return new MultipleInputLineProcessor(inputs, inputProcessor, output);
} else if (args.isLineNumber()) {
final LineMatcher matcher = getMatcher(args);
final LineProcessor inputProcessor = new WriteMatchingLinesProcessor(this, context, output, matcher);
return new MultipleInputLineProcessor(inputs,
new WriteMatchingLinesInputProcessor(this, context, matcher), processor);
new WriteMatchingLinesInputProcessor(this, context, matcher), inputProcessor);
} else {
final LineMatcher matcher = getMatcher(args);
final InputProcessor inputProcessor = new WriteMatchingLinesInputProcessor(this, context, matcher);
return new MultipleInputLineProcessor(inputs, inputProcessor, output);
}
//standard input
final LineProcessor standardInputProcessor = getStandardInputProcessor(context, output, args);
return new RedirectInputLineProcessor(inputs, standardInputProcessor);
}
}
@@ -34,7 +34,7 @@ public ExecutionContext createExecutionContext() {
@Test
public void testCountOnRelativeFile(){
final File testFile = new File(outputDir.getPath() + "/commuting.txt" );
assertEquals("118: commuting.txt", Unix4j.use(contextFactory).grep(Grep.Options.count, "the", testFile).toStringResult());
assertEquals("118", Unix4j.use(contextFactory).grep(Grep.Options.count, "the", testFile).toStringResult());
}
@Test
@@ -48,9 +48,11 @@ public void testCountMultipleAbsolutePaths(){
final String outputDirPath = outputDir.getPath();
final String path1 = outputDirPath + "/org-unix4j-unix/GrepTest/bb.txt";
final String path2 = outputDirPath + "/org-unix4j-unix/GrepTest/folder/bb.txt";
//Unix4j.use(contextFactory).grep("everything", path1, path2).toStdOut();
assertEquals("2", Unix4j.use(contextFactory).grep("everything", path1, path2).grep(Grep.Options.count, ".*").toStringResult());
assertEquals("2", Unix4j.use(contextFactory).grep("everything", path1, path2).wc(Wc.Options.l).toStringResult());
assertEquals("2", Unix4j.use(contextFactory).grep(Pattern.compile(".*everything.*"), path1, path2).wc(Wc.Options.l).toStringResult());
assertEquals2( "1: ./org-unix4j-unix/GrepTest/bb.txt",
assertEquals2( "1: ./org-unix4j-unix/GrepTest/bb.txt",
"1: ./org-unix4j-unix/GrepTest/folder/bb.txt", Unix4j.use(contextFactory).grep(Grep.Options.count, "everything", path1, path2));
}
@@ -81,12 +83,46 @@ public void testCountOnRelativeFilesWithCd(){
}
@Test
public void testLineNumberOnRelativeFiles(){
public void testLineNumber(){
final File testFile = new File(outputDir.getPath() + "/commuting.txt" );
assertEquals("3:Subject: Commuting for beginners.",
Unix4j.use(contextFactory).grep(Grep.Options.lineNumber, "Subject", testFile).toStringResult());
assertEquals("3:Subject: Commuting for beginners.",
Unix4j.use(contextFactory).grep(Grep.Options.lineNumber, "Subject", testFile.getPath()).toStringResult());
assertEquals("3:Subject: Commuting for beginners.",
Unix4j.use(contextFactory).grep(Grep.Options.lineNumber, "Subject", testFile.getAbsoluteFile()).toStringResult());
assertEquals("3:Subject: Commuting for beginners.",
Unix4j.use(contextFactory).grep(Grep.Options.lineNumber, "Subject", testFile.getAbsolutePath()).toStringResult());
}
@Test
public void testLineNumberOnRelativeFiles(){
assertEquals2(
"commuting.txt:3:Subject: Commuting for beginners.",
"commuting2.txt:3:Subject: Commuting for beginners.",
Unix4j.use(contextFactory).grep(Grep.Options.lineNumber, "Subject", "*.txt").sort());
final File[] testFiles = new File[] {
new File(outputDir.getPath() + "/commuting.txt" ),
new File(outputDir.getPath() + "/commuting2.txt" ),
};
assertEquals2(
"commuting.txt:3:Subject: Commuting for beginners.",
"commuting2.txt:3:Subject: Commuting for beginners.",
Unix4j.use(contextFactory).grep(Grep.Options.lineNumber, "Subject", testFiles).sort()
);
}
/**
* Unit test for Issue #58.
* <p>
* See <a href="https://github.com/tools4j/unix4j/issues/58">Issue 58</a>
*/
@Test
public void testLineNumberWithRegex() {
final String inputPath = new File(outputDir, "theFileName.theFileExtension").getAbsolutePath();
final String evilRegex = "anyStringOrRegexpITried";
//Unix4j.fromFile(inputPath).grep(Grep.Options.lineNumber, evilRegex).toStdOut();
assertEquals("1:anyStringOrRegexpITried", Unix4j.fromFile(inputPath).grep(Grep.Options.lineNumber, evilRegex).toStringResult());
}
/**
@@ -0,0 +1 @@
anyStringOrRegexpITried

0 comments on commit d3220f7

Please sign in to comment.