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  package org.apache.commons.functor.example.lines;
18  
19  import java.io.BufferedReader;
20  import java.io.File;
21  import java.io.FileNotFoundException;
22  import java.io.FileReader;
23  import java.io.Reader;
24  
25  import org.apache.commons.functor.UnaryProcedure;
26  import org.apache.commons.functor.generator.BaseGenerator;
27  
28  /**
29   * @version $Revision: 665786 $ $Date: 2008-06-09 19:17:39 +0200 (Mon, 09 Jun 2008) $
30   * @author Rodney Waldhoff
31   */
32  public class Lines extends BaseGenerator<String> {
33      public static Lines from(Reader reader) {
34          return new Lines(reader);
35      }
36  
37      public static Lines from(File file) throws FileNotFoundException {
38          return new Lines(new FileReader(file));
39      }
40  
41      public Lines(Reader reader) {
42          if (reader instanceof BufferedReader) {
43              in = (BufferedReader) reader;
44          } else {
45              in = new BufferedReader(reader);
46          }
47      }
48      
49      public void run(UnaryProcedure<? super String> proc) {
50          try {
51              for (String line = in.readLine(); line != null; line = in.readLine()) {
52                  proc.run(line);
53              }
54          } catch(RuntimeException e) {
55              throw e;
56          } catch(Exception e) {
57              throw new TunneledException(e);
58          } finally {
59              stop();
60          }
61      }
62  
63      public void stop() {
64          super.stop();
65          try {
66              in.close();
67          } catch(RuntimeException e) {
68              throw e;
69          } catch(Exception e) {
70              throw new TunneledException(e);
71          }
72      }
73  
74      private BufferedReader in = null;
75      
76      private class TunneledException extends RuntimeException {
77          private Exception exception = null;
78          TunneledException(Exception exception) {
79              super(exception.toString());
80              this.exception = exception;
81          }
82          //this is an override if compiled against Java >= 1.4, but just another method on 1.3
83          /**
84           * Get the cause of this TunneledException
85           */
86          public Throwable getCause() {
87              return exception;
88          }
89      }
90  }