3 // FORESTER -- software libraries and applications
4 // for evolutionary biology research and applications.
6 // Copyright (C) 2008-2009 Christian M. Zmasek
7 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 // Contact: phylosoft @ gmail . com
25 // WWW: www.phylosoft.org/
27 package org.forester.io.parsers.util;
29 import java.io.BufferedReader;
31 import java.io.FileNotFoundException;
32 import java.io.FileReader;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.io.InputStreamReader;
36 import java.io.StringReader;
38 public final class ParserUtils {
40 public static BufferedReader createReader( final Object source ) throws IOException, FileNotFoundException {
41 BufferedReader reader = null;
42 if ( ( source instanceof File ) || ( source instanceof String ) ) {
44 if ( source instanceof File ) {
48 f = new File( ( String ) source );
51 throw new IOException( "[" + f.getAbsolutePath() + "] does not exist" );
53 else if ( !f.isFile() ) {
54 throw new IOException( "[" + f.getAbsolutePath() + "] is not a file" );
56 else if ( !f.canRead() ) {
57 throw new IOException( "[" + f.getAbsolutePath() + "] is not a readable" );
59 reader = new BufferedReader( new FileReader( f ) );
61 else if ( source instanceof InputStream ) {
62 reader = new BufferedReader( new InputStreamReader( ( InputStream ) source ) );
64 else if ( ( source instanceof StringBuffer ) || ( source instanceof StringBuilder ) ) {
65 reader = new BufferedReader( new StringReader( source.toString() ) );
68 throw new IllegalArgumentException( "attempt to parse object of type [" + source.getClass()
69 + "] (can only parse objects of type File/String, InputStream, StringBuffer, or StringBuilder)" );