X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=forester%2Fjava%2Fsrc%2Forg%2Fforester%2Futil%2FBasicTableParser.java;h=8d7f887c776271875deadee0c26f13ee5d3847c4;hb=50c2ff100214fbb04b5f9665466e6d98d26b051d;hp=59e5cd28fb0fcd27930451bb56b9f44476fe1d93;hpb=0cbfc79c69ccbfca7ac42a1381d62d449bf1adf6;p=jalview.git diff --git a/forester/java/src/org/forester/util/BasicTableParser.java b/forester/java/src/org/forester/util/BasicTableParser.java index 59e5cd2..8d7f887 100644 --- a/forester/java/src/org/forester/util/BasicTableParser.java +++ b/forester/java/src/org/forester/util/BasicTableParser.java @@ -21,7 +21,7 @@ // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA // // Contact: phylosoft @ gmail . com -// WWW: www.phylosoft.org/forester +// WWW: https://sites.google.com/site/cmzmasek/home/software/forester package org.forester.util; @@ -38,26 +38,33 @@ public class BasicTableParser { private BasicTableParser() { } - public static BasicTable parse( final Object source, final String column_delimiter ) throws IOException { - return BasicTableParser.parse( source, column_delimiter, false, START_OF_COMMENT_LINE_DEFAULT, false ).get( 0 ); + public static BasicTable parse( final Object source, final char column_delimiter ) throws IOException { + return BasicTableParser.parse( source, column_delimiter, false, false, START_OF_COMMENT_LINE_DEFAULT, false ) + .get( 0 ); } public static BasicTable parse( final Object source, - final String column_delimiter, - final boolean use_first_separator_only ) throws IOException { + final char column_delimiter, + final boolean use_first_separator_only, + final boolean use_last_separator_only ) throws IOException { return BasicTableParser.parse( source, column_delimiter, use_first_separator_only, + use_last_separator_only, START_OF_COMMENT_LINE_DEFAULT, false ).get( 0 ); } public static List> parse( final Object source, - final String column_delimiter, + final char column_delimiter, final boolean use_first_separator_only, + final boolean use_last_separator_only, final String start_of_comment_line, final boolean tables_separated_by_single_string_line ) - throws IOException { + throws IOException { + if ( use_first_separator_only && use_last_separator_only ) { + throw new IllegalArgumentException(); + } final BufferedReader reader = ForesterUtil.obtainReader( source ); final List> tables = new ArrayList>(); BasicTable table = new BasicTable(); @@ -67,18 +74,12 @@ public class BasicTableParser { final boolean use_start_of_comment_line = !( ForesterUtil.isEmpty( start_of_comment_line ) ); while ( ( line = reader.readLine() ) != null ) { line = line.trim(); - if ( !ForesterUtil.isEmpty( line) && - - - (( line.charAt( 0 ) == '"' && line.charAt( line.length() -1 ) == '"' && ForesterUtil.countChars( line, '"' ) == 2 ) - - || - - - ( line.charAt( 0 ) == '\'' && line.charAt( line.length() -1 ) == '\'' && ForesterUtil.countChars( line, '\'' ) == 2 ) ) ) { - line = line.substring( 1, line.length() -1 ).trim(); + if ( !ForesterUtil.isEmpty( line ) + && ( ( ( line.charAt( 0 ) == '"' ) && ( line.charAt( line.length() - 1 ) == '"' ) && ( ForesterUtil + .countChars( line, '"' ) == 2 ) ) || ( ( line.charAt( 0 ) == '\'' ) + && ( line.charAt( line.length() - 1 ) == '\'' ) && ( ForesterUtil.countChars( line, '\'' ) == 2 ) ) ) ) { + line = line.substring( 1, line.length() - 1 ).trim(); } - if ( saw_first_table && ( ForesterUtil.isEmpty( line ) || ( tables_separated_by_single_string_line && ( line .indexOf( column_delimiter ) < 0 ) ) ) ) { @@ -91,22 +92,33 @@ public class BasicTableParser { else if ( !ForesterUtil.isEmpty( line ) && ( !use_start_of_comment_line || !line.startsWith( start_of_comment_line ) ) ) { saw_first_table = true; - final StringTokenizer st = new StringTokenizer( line, column_delimiter ); - int col = 0; - if ( st.hasMoreTokens() ) { - table.setValue( col++, row, st.nextToken().trim() ); - } - if ( !use_first_separator_only ) { - while ( st.hasMoreTokens() ) { - table.setValue( col++, row, st.nextToken().trim() ); + if ( use_last_separator_only ) { + final String e[] = line.split( column_delimiter + "" ); + final StringBuffer rest = new StringBuffer(); + for( int i = 0; i < ( e.length - 1 ); ++i ) { + rest.append( e[ i ].trim() ); } + table.setValue( 0, row, rest.toString() ); + table.setValue( 1, row, e[ e.length - 1 ] ); } else { - final StringBuffer rest = new StringBuffer(); - while ( st.hasMoreTokens() ) { - rest.append( st.nextToken() ); + final StringTokenizer st = new StringTokenizer( line, column_delimiter + "" ); + int col = 0; + if ( st.hasMoreTokens() ) { + table.setValue( col++, row, st.nextToken().trim() ); + } + if ( use_first_separator_only ) { + final StringBuffer rest = new StringBuffer(); + while ( st.hasMoreTokens() ) { + rest.append( st.nextToken() ); + } + table.setValue( col++, row, rest.toString() ); + } + else { + while ( st.hasMoreTokens() ) { + table.setValue( col++, row, st.nextToken().trim() ); + } } - table.setValue( col++, row, rest.toString().trim() ); } ++row; }