001    ///////////////////////////////////////////////////////////////////////////////
002    // Copyright (c) 2006, Frank S. Nestel, All Rights Reserved.
003    //
004    // This library is free software; you can redistribute it and/or
005    // modify it under the terms of the GNU Lesser General Public
006    // License as published by the Free Software Foundation; either
007    // version 2.1 of the License, or (at your option) any later version.
008    //
009    // This library is distributed in the hope that it will be useful,
010    // but WITHOUT ANY WARRANTY; without even the implied warranty of
011    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012    // GNU General Public License for more details.
013    //
014    // You should have received a copy of the GNU Lesser General Public
015    // License along with this program; if not, write to the Free Software
016    // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
017    ///////////////////////////////////////////////////////////////////////////////
018    
019    package de.spieleck.app.turn;
020    
021    import java.io.IOException;
022    import java.io.BufferedReader;
023    import java.io.Reader;
024    import java.io.File;
025    import java.io.FileReader;
026    
027    /**
028     * Quick hack to make reading lines of file a bit like iterating.
029     *
030     * <p><a href="$URL: https://svn.sourceforge.net/svnroot/jtourney/src/de/spieleck/app/turn/LineSource.java $">$URL: https://svn.sourceforge.net/svnroot/jtourney/src/de/spieleck/app/turn/LineSource.java $</a></p>
031     *
032     * @author Frank S. Nestel
033     * @author $Author: nestefan $
034     * @version $Revision: 2 $ $Date: 2006-03-20 14:33:27 +0100 (Mo, 20 Mrz 2006) $ $Author: nestefan $
035     */
036    public class LineSource
037    {
038        public final static String COMMENTSTR = "#";
039    
040        private BufferedReader reader;
041    
042        private long lastModified = -1;
043    
044        private String fName = "<reader>";
045    
046        public LineSource(Reader reader)
047        {
048            this.reader = new BufferedReader(reader);
049        }
050    
051        public LineSource(File root, String file)
052            throws IOException
053        {
054            File fi = new File(root, file);
055            this.fName = file;
056            lastModified = fi.lastModified();
057            reader = new BufferedReader(new FileReader(fi));
058        }
059    
060        public String getFName()
061        {
062            return fName;
063        }
064    
065        public String line()
066            throws IOException
067        {
068            String line = "";
069            while ( true )
070            {
071                line = reader.readLine();
072                if ( line == null )
073                {
074                    reader.close();
075                    return null;
076                }
077                int i = line.indexOf(COMMENTSTR);
078                if ( i != -1 )
079                    line = line.substring(0, i);
080                line = line.trim();
081                if ( ! "".equals(line) )
082                    return line;
083            }
084        }
085    
086        public long lastModified()
087        {
088            return lastModified;
089        }
090    
091        public void close()
092            throws IOException
093        {
094            reader.close();
095        }
096    
097    }