001/**
002 * Copyright 2010-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.common.util;
017
018import java.io.PrintStream;
019import java.util.List;
020
021import org.codehaus.plexus.util.cli.StreamConsumer;
022import org.kuali.common.util.ignore.Ignore;
023import org.slf4j.Logger;
024import org.slf4j.LoggerFactory;
025
026public class PrintlnStreamConsumer implements StreamConsumer {
027
028        private static final Logger logger = LoggerFactory.getLogger(PrintlnStreamConsumer.class);
029
030        PrintStream printStream;
031        long lineCount = 0;
032        long skipCount = 0;
033        List<Ignore> ignorers;
034        boolean enableIgnorers = true;
035
036        public PrintlnStreamConsumer() {
037                this(null);
038        }
039
040        public PrintlnStreamConsumer(PrintStream printStream) {
041                this(printStream, null);
042        }
043
044        public PrintlnStreamConsumer(PrintStream printStream, List<Ignore> ignorers) {
045                this.printStream = printStream;
046                this.ignorers = ignorers;
047        }
048
049        @Override
050        public void consumeLine(String line) {
051                lineCount++;
052                if (enableIgnorers && ignore(line, ignorers)) {
053                        skipCount++;
054                        Object[] args = { skipCount, lineCount, line };
055                        logger.debug("{} Skipping line {} [{}]", args);
056                } else {
057                        printStream.println(line);
058                }
059        }
060
061        protected boolean ignore(String line, List<Ignore> ignorers) {
062                if (ignorers == null) {
063                        return false;
064                }
065                for (Ignore ignorer : ignorers) {
066                        if (ignorer.ignore(line)) {
067                                return true;
068                        }
069                }
070                return false;
071        }
072
073        public PrintStream getPrintStream() {
074                return printStream;
075        }
076
077        public void setPrintStream(PrintStream printStream) {
078                this.printStream = printStream;
079        }
080
081        public long getLineCount() {
082                return lineCount;
083        }
084
085        public void setLineCount(long lineCount) {
086                this.lineCount = lineCount;
087        }
088
089        public long getSkipCount() {
090                return skipCount;
091        }
092
093        public void setSkipCount(long skipCount) {
094                this.skipCount = skipCount;
095        }
096
097        public List<Ignore> getIgnorers() {
098                return ignorers;
099        }
100
101        public void setIgnorers(List<Ignore> ignorers) {
102                this.ignorers = ignorers;
103        }
104
105}