001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.pipeline.stage;
019    
020    import java.io.BufferedReader;
021    import java.io.IOException;
022    import java.io.InputStream;
023    import java.io.InputStreamReader;
024    import org.apache.commons.pipeline.stage.BaseStage;
025    import org.apache.commons.pipeline.StageException;
026    
027    /**
028     * Breaks up an InputStream by line and exqueues each resulting line.
029     */
030    public class InputStreamLineBreakStage extends BaseStage {
031        /**
032         * Holds value of property ignoringBlankLines.
033         */
034        private boolean ignoringBlankLines = false;
035        
036        /** Creates a new instance of InputStreamLineBreakStage */
037        public InputStreamLineBreakStage() {
038            super();
039        }
040    
041        /** Creates a new instance of InputStreamLineBreakStage */
042        public InputStreamLineBreakStage(boolean ignoringBlankLines) {
043            this.ignoringBlankLines = ignoringBlankLines;
044        }
045    
046        public void process(Object obj) throws org.apache.commons.pipeline.StageException {
047            InputStream is = (InputStream) obj;
048            try {
049                InputStreamReader reader = new InputStreamReader(is);
050                BufferedReader buffered = new BufferedReader(reader);
051                String line = buffered.readLine();
052                while (line != null){
053                    if (!(ignoringBlankLines && line.trim().equals(""))) {
054                        this.emit(line);
055                    }
056                    line = buffered.readLine();
057                }
058            } catch (IOException e){
059                throw new StageException(this, e);
060            }
061        }
062    
063        /**
064         * Getter for property ignoreBlankLines.
065         * @return Value of property ignoreBlankLines.
066         */
067        public boolean isIgnoringBlankLines()  {
068            return this.ignoringBlankLines;
069        }
070    
071        /**
072         * Specifies that this stage will not exqueue blank lines.
073         * @param ignoreBlankLines New value of property ignoreBlankLines.
074         */
075        public void setIgnoringBlankLines(boolean ignoringBlankLines)  {
076            this.ignoringBlankLines = ignoringBlankLines;
077        }
078    }