// $Id: // // FORESTER -- software libraries and applications // for evolutionary biology research and applications. // // Copyright (C) 2008-2009 Christian M. Zmasek // Copyright (C) 2008-2009 Burnham Institute for Medical Research // All rights reserved // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA // // Contact: phylosoft @ gmail . com // WWW: www.phylosoft.org/ package org.forester.io.parsers.util; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.StringReader; public final class ParserUtils { public static BufferedReader createReader( final Object source ) throws IOException, FileNotFoundException { BufferedReader reader = null; if ( ( source instanceof File ) || ( source instanceof String ) ) { File f = null; if ( source instanceof File ) { f = ( File ) source; } else { f = new File( ( String ) source ); } if ( !f.exists() ) { throw new IOException( "[" + f.getAbsolutePath() + "] does not exist" ); } else if ( !f.isFile() ) { throw new IOException( "[" + f.getAbsolutePath() + "] is not a file" ); } else if ( !f.canRead() ) { throw new IOException( "[" + f.getAbsolutePath() + "] is not a readable" ); } reader = new BufferedReader( new FileReader( f ) ); } else if ( source instanceof InputStream ) { reader = new BufferedReader( new InputStreamReader( ( InputStream ) source ) ); } else if ( ( source instanceof StringBuffer ) || ( source instanceof StringBuilder ) ) { reader = new BufferedReader( new StringReader( source.toString() ) ); } else { throw new IllegalArgumentException( "attempt to parse object of type [" + source.getClass() + "] (can only parse objects of type File/String, InputStream, StringBuffer, or StringBuilder)" ); } return reader; } }