2917a3190ea7df605736cd6846f5182f40351600
[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, false, START_OF_COMMENT_LINE_DEFAULT, false )
43                 .get( 0 );
44     }
45
46     public static BasicTable<String> parse( final Object source,
47                                             final String column_delimiter,
48                                             final boolean use_first_separator_only,
49                                             final boolean use_last_separator_only ) throws IOException {
50         return BasicTableParser.parse( source,
51                                        column_delimiter,
52                                        use_first_separator_only,
53                                        use_last_separator_only,
54                                        START_OF_COMMENT_LINE_DEFAULT,
55                                        false ).get( 0 );
56     }
57
58     public static List<BasicTable<String>> parse( final Object source,
59                                                   final String column_delimiter,
60                                                   final boolean use_first_separator_only,
61                                                   final boolean use_last_separator_only,
62                                                   final String start_of_comment_line,
63                                                   final boolean tables_separated_by_single_string_line )
64             throws IOException {
65         if ( use_first_separator_only && use_last_separator_only ) {
66             throw new IllegalArgumentException();
67         }
68         final BufferedReader reader = ForesterUtil.obtainReader( source );
69         final List<BasicTable<String>> tables = new ArrayList<BasicTable<String>>();
70         BasicTable<String> table = new BasicTable<String>();
71         int row = 0;
72         String line;
73         boolean saw_first_table = false;
74         final boolean use_start_of_comment_line = !( ForesterUtil.isEmpty( start_of_comment_line ) );
75         while ( ( line = reader.readLine() ) != null ) {
76             line = line.trim();
77             if ( !ForesterUtil.isEmpty( line )
78                     && ( ( ( line.charAt( 0 ) == '"' ) && ( line.charAt( line.length() - 1 ) == '"' ) && ( ForesterUtil
79                             .countChars( line, '"' ) == 2 ) ) || ( ( line.charAt( 0 ) == '\'' )
80                             && ( line.charAt( line.length() - 1 ) == '\'' ) && ( ForesterUtil.countChars( line, '\'' ) == 2 ) ) ) ) {
81                 line = line.substring( 1, line.length() - 1 ).trim();
82             }
83             if ( saw_first_table
84                     && ( ForesterUtil.isEmpty( line ) || ( tables_separated_by_single_string_line && ( line
85                             .indexOf( column_delimiter ) < 0 ) ) ) ) {
86                 if ( !table.isEmpty() ) {
87                     tables.add( table );
88                 }
89                 table = new BasicTable<String>();
90                 row = 0;
91             }
92             else if ( !ForesterUtil.isEmpty( line )
93                     && ( !use_start_of_comment_line || !line.startsWith( start_of_comment_line ) ) ) {
94                 saw_first_table = true;
95                 if ( use_last_separator_only ) {
96                     final String e[] = line.split( column_delimiter );
97                     final StringBuffer rest = new StringBuffer();
98                     for( int i = 0; i < ( e.length - 1 ); ++i ) {
99                         rest.append( e[ i ].trim() );
100                     }
101                     table.setValue( 0, row, rest.toString() );
102                     table.setValue( 1, row, e[ e.length - 1 ] );
103                 }
104                 else {
105                     final StringTokenizer st = new StringTokenizer( line, column_delimiter );
106                     int col = 0;
107                     if ( st.hasMoreTokens() ) {
108                         table.setValue( col++, row, st.nextToken().trim() );
109                     }
110                     if ( use_first_separator_only ) {
111                         final StringBuffer rest = new StringBuffer();
112                         while ( st.hasMoreTokens() ) {
113                             rest.append( st.nextToken() );
114                         }
115                         table.setValue( col++, row, rest.toString() );
116                     }
117                     else {
118                         while ( st.hasMoreTokens() ) {
119                             table.setValue( col++, row, st.nextToken().trim() );
120                         }
121                     }
122                 }
123                 ++row;
124             }
125         }
126         if ( !table.isEmpty() ) {
127             tables.add( table );
128         }
129         reader.close();
130         return tables;
131     }
132 }