59e5cd28fb0fcd27930451bb56b9f44476fe1d93
[jalview.git] / forester / java / src / org / forester / util / BasicTableParser.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
7 // All rights reserved
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 //
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org/forester
25
26 package org.forester.util;
27
28 import java.io.BufferedReader;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.List;
32 import java.util.StringTokenizer;
33
34 public class BasicTableParser {
35
36     private final static String START_OF_COMMENT_LINE_DEFAULT = "#";
37
38     private BasicTableParser() {
39     }
40
41     public static BasicTable<String> parse( final Object source, final String column_delimiter ) throws IOException {
42         return BasicTableParser.parse( source, column_delimiter, false, START_OF_COMMENT_LINE_DEFAULT, false ).get( 0 );
43     }
44
45     public static BasicTable<String> parse( final Object source,
46                                             final String column_delimiter,
47                                             final boolean use_first_separator_only ) throws IOException {
48         return BasicTableParser.parse( source,
49                                        column_delimiter,
50                                        use_first_separator_only,
51                                        START_OF_COMMENT_LINE_DEFAULT,
52                                        false ).get( 0 );
53     }
54
55     public static List<BasicTable<String>> parse( final Object source,
56                                                   final String column_delimiter,
57                                                   final boolean use_first_separator_only,
58                                                   final String start_of_comment_line,
59                                                   final boolean tables_separated_by_single_string_line )
60             throws IOException {
61         final BufferedReader reader = ForesterUtil.obtainReader( source );
62         final List<BasicTable<String>> tables = new ArrayList<BasicTable<String>>();
63         BasicTable<String> table = new BasicTable<String>();
64         int row = 0;
65         String line;
66         boolean saw_first_table = false;
67         final boolean use_start_of_comment_line = !( ForesterUtil.isEmpty( start_of_comment_line ) );
68         while ( ( line = reader.readLine() ) != null ) {
69             line = line.trim();
70             if ( !ForesterUtil.isEmpty( line) && 
71                     
72                     
73                      (( line.charAt( 0 ) == '"' && line.charAt( line.length() -1 ) == '"' && ForesterUtil.countChars( line, '"' ) == 2 ) 
74                              
75                              ||
76                              
77                              
78                       ( line.charAt( 0 ) == '\'' && line.charAt( line.length() -1 ) == '\'' && ForesterUtil.countChars( line, '\'' ) == 2  ) ) ) {
79                 line = line.substring( 1, line.length() -1  ).trim();
80             }
81             
82             if ( saw_first_table
83                     && ( ForesterUtil.isEmpty( line ) || ( tables_separated_by_single_string_line && ( line
84                             .indexOf( column_delimiter ) < 0 ) ) ) ) {
85                 if ( !table.isEmpty() ) {
86                     tables.add( table );
87                 }
88                 table = new BasicTable<String>();
89                 row = 0;
90             }
91             else if ( !ForesterUtil.isEmpty( line )
92                     && ( !use_start_of_comment_line || !line.startsWith( start_of_comment_line ) ) ) {
93                 saw_first_table = true;
94                 final StringTokenizer st = new StringTokenizer( line, column_delimiter );
95                 int col = 0;
96                 if ( st.hasMoreTokens() ) {
97                     table.setValue( col++, row, st.nextToken().trim() );
98                 }
99                 if ( !use_first_separator_only ) {
100                     while ( st.hasMoreTokens() ) {
101                         table.setValue( col++, row, st.nextToken().trim() );
102                     }
103                 }
104                 else {
105                     final StringBuffer rest = new StringBuffer();
106                     while ( st.hasMoreTokens() ) {
107                         rest.append( st.nextToken() );
108                     }
109                     table.setValue( col++, row, rest.toString().trim() );
110                 }
111                 ++row;
112             }
113         }
114         if ( !table.isEmpty() ) {
115             tables.add( table );
116         }
117         reader.close();
118         return tables;
119     }
120 }