001package org.unix4j.unix.wc; 002 003import org.unix4j.context.ExecutionContext; 004import org.unix4j.io.FileInput; 005import org.unix4j.io.Input; 006import org.unix4j.line.Line; 007import org.unix4j.processor.DefaultInputProcessor; 008import org.unix4j.processor.LineProcessor; 009 010/** 011 * Input processor for line, word and char count for a single file. 012 */ 013class WcFileProcessor extends DefaultInputProcessor { 014 015 private final ExecutionContext context; 016 private final Counters current; 017 018 public WcFileProcessor(ExecutionContext context, WcArguments args) { 019 this.context = context; 020 current = new Counters(args); 021 } 022 023 @Override 024 public boolean processLine(Input input, Line line, LineProcessor output) { 025 current.update(line); 026 return true;//we want to count all lines 027 } 028 029 @Override 030 public void finish(Input input, LineProcessor output) { 031 final String fileInfo = input instanceof FileInput ? ((FileInput)input).getFileInfo(context.getCurrentDirectory()) : input.toString(); 032 current.writeCountsLineWithFileInfo(output, fileInfo); 033 current.reset(); 034 } 035}