1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.util;
17
18 import java.io.PrintStream;
19 import java.util.List;
20
21 import org.codehaus.plexus.util.cli.StreamConsumer;
22 import org.kuali.common.util.ignore.Ignore;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 public class PrintlnStreamConsumer implements StreamConsumer {
27
28 private static final Logger logger = LoggerFactory.getLogger(PrintlnStreamConsumer.class);
29
30 PrintStream printStream;
31 long lineCount = 0;
32 long skipCount = 0;
33 List<Ignore> ignorers;
34 boolean enableIgnorers = true;
35
36 public PrintlnStreamConsumer() {
37 this(null);
38 }
39
40 public PrintlnStreamConsumer(PrintStream printStream) {
41 this(printStream, null);
42 }
43
44 public PrintlnStreamConsumer(PrintStream printStream, List<Ignore> ignorers) {
45 this.printStream = printStream;
46 this.ignorers = ignorers;
47 }
48
49 @Override
50 public void consumeLine(String line) {
51 lineCount++;
52 if (enableIgnorers && ignore(line, ignorers)) {
53 skipCount++;
54 Object[] args = { skipCount, lineCount, line };
55 logger.debug("{} Skipping line {} [{}]", args);
56 } else {
57 printStream.println(line);
58 }
59 }
60
61 protected boolean ignore(String line, List<Ignore> ignorers) {
62 if (ignorers == null) {
63 return false;
64 }
65 for (Ignore ignorer : ignorers) {
66 if (ignorer.ignore(line)) {
67 return true;
68 }
69 }
70 return false;
71 }
72
73 public PrintStream getPrintStream() {
74 return printStream;
75 }
76
77 public void setPrintStream(PrintStream printStream) {
78 this.printStream = printStream;
79 }
80
81 public long getLineCount() {
82 return lineCount;
83 }
84
85 public void setLineCount(long lineCount) {
86 this.lineCount = lineCount;
87 }
88
89 public long getSkipCount() {
90 return skipCount;
91 }
92
93 public void setSkipCount(long skipCount) {
94 this.skipCount = skipCount;
95 }
96
97 public List<Ignore> getIgnorers() {
98 return ignorers;
99 }
100
101 public void setIgnorers(List<Ignore> ignorers) {
102 this.ignorers = ignorers;
103 }
104
105 }