View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.pipeline.stage;
19  
20  import java.io.BufferedReader;
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.io.InputStreamReader;
24  import org.apache.commons.pipeline.stage.BaseStage;
25  import org.apache.commons.pipeline.StageException;
26  
27  /**
28   * Breaks up an InputStream by line and exqueues each resulting line.
29   */
30  public class InputStreamLineBreakStage extends BaseStage {
31      /**
32       * Holds value of property ignoringBlankLines.
33       */
34      private boolean ignoringBlankLines = false;
35      
36      /** Creates a new instance of InputStreamLineBreakStage */
37      public InputStreamLineBreakStage() {
38          super();
39      }
40  
41      /** Creates a new instance of InputStreamLineBreakStage */
42      public InputStreamLineBreakStage(boolean ignoringBlankLines) {
43          this.ignoringBlankLines = ignoringBlankLines;
44      }
45  
46      public void process(Object obj) throws org.apache.commons.pipeline.StageException {
47          InputStream is = (InputStream) obj;
48          try {
49              InputStreamReader reader = new InputStreamReader(is);
50              BufferedReader buffered = new BufferedReader(reader);
51              String line = buffered.readLine();
52              while (line != null){
53                  if (!(ignoringBlankLines && line.trim().equals(""))) {
54                      this.emit(line);
55                  }
56                  line = buffered.readLine();
57              }
58          } catch (IOException e){
59              throw new StageException(this, e);
60          }
61      }
62  
63      /**
64       * Getter for property ignoreBlankLines.
65       * @return Value of property ignoreBlankLines.
66       */
67      public boolean isIgnoringBlankLines()  {
68          return this.ignoringBlankLines;
69      }
70  
71      /**
72       * Specifies that this stage will not exqueue blank lines.
73       * @param ignoreBlankLines New value of property ignoreBlankLines.
74       */
75      public void setIgnoringBlankLines(boolean ignoringBlankLines)  {
76          this.ignoringBlankLines = ignoringBlankLines;
77      }
78  }