View Javadoc

1   /**
2    * Copyright 2010-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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 }