clean up
[jalview.git] / forester / java / src / org / forester / io / parsers / util / ParserUtils.java
1 // $Id:
2 //
3 // FORESTER -- software libraries and applications
4 // for evolutionary biology research and applications.
5 //
6 // Copyright (C) 2008-2009 Christian M. Zmasek
7 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
8 // All rights reserved
9 //
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.
14 //
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.
19 //
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
23 //
24 // Contact: phylosoft @ gmail . com
25 // WWW: www.phylosoft.org/
26
27 package org.forester.io.parsers.util;
28
29 import java.io.BufferedReader;
30 import java.io.File;
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;
37
38 public final class ParserUtils {
39
40     public static BufferedReader createReader( final Object source ) throws IOException, FileNotFoundException {
41         BufferedReader reader = null;
42         if ( ( source instanceof File ) || ( source instanceof String ) ) {
43             File f = null;
44             if ( source instanceof File ) {
45                 f = ( File ) source;
46             }
47             else {
48                 f = new File( ( String ) source );
49             }
50             if ( !f.exists() ) {
51                 throw new IOException( "[" + f.getAbsolutePath() + "] does not exist" );
52             }
53             else if ( !f.isFile() ) {
54                 throw new IOException( "[" + f.getAbsolutePath() + "] is not a file" );
55             }
56             else if ( !f.canRead() ) {
57                 throw new IOException( "[" + f.getAbsolutePath() + "] is not a readable" );
58             }
59             reader = new BufferedReader( new FileReader( f ) );
60         }
61         else if ( source instanceof InputStream ) {
62             reader = new BufferedReader( new InputStreamReader( ( InputStream ) source ) );
63         }
64         else if ( ( source instanceof StringBuffer ) || ( source instanceof StringBuilder ) ) {
65             reader = new BufferedReader( new StringReader( source.toString() ) );
66         }
67         else {
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)" );
70         }
71         return reader;
72     }
73 }