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    package org.apache.commons.functor.example.lines;
018    
019    import java.io.BufferedReader;
020    import java.io.File;
021    import java.io.FileNotFoundException;
022    import java.io.FileReader;
023    import java.io.Reader;
024    
025    import org.apache.commons.functor.UnaryProcedure;
026    import org.apache.commons.functor.generator.BaseGenerator;
027    
028    /**
029     * @version $Revision: 665786 $ $Date: 2008-06-09 19:17:39 +0200 (Mon, 09 Jun 2008) $
030     * @author Rodney Waldhoff
031     */
032    public class Lines extends BaseGenerator<String> {
033        public static Lines from(Reader reader) {
034            return new Lines(reader);
035        }
036    
037        public static Lines from(File file) throws FileNotFoundException {
038            return new Lines(new FileReader(file));
039        }
040    
041        public Lines(Reader reader) {
042            if (reader instanceof BufferedReader) {
043                in = (BufferedReader) reader;
044            } else {
045                in = new BufferedReader(reader);
046            }
047        }
048        
049        public void run(UnaryProcedure<? super String> proc) {
050            try {
051                for (String line = in.readLine(); line != null; line = in.readLine()) {
052                    proc.run(line);
053                }
054            } catch(RuntimeException e) {
055                throw e;
056            } catch(Exception e) {
057                throw new TunneledException(e);
058            } finally {
059                stop();
060            }
061        }
062    
063        public void stop() {
064            super.stop();
065            try {
066                in.close();
067            } catch(RuntimeException e) {
068                throw e;
069            } catch(Exception e) {
070                throw new TunneledException(e);
071            }
072        }
073    
074        private BufferedReader in = null;
075        
076        private class TunneledException extends RuntimeException {
077            private Exception exception = null;
078            TunneledException(Exception exception) {
079                super(exception.toString());
080                this.exception = exception;
081            }
082            //this is an override if compiled against Java >= 1.4, but just another method on 1.3
083            /**
084             * Get the cause of this TunneledException
085             */
086            public Throwable getCause() {
087                return exception;
088            }
089        }
090    }