View Javadoc

    1   package baseCode.io.reader;
    2
    3   import java.io.BufferedReader;
    4   import java.io.IOException;
    5   import java.io.InputStream;
    6   import java.util.StringTokenizer;
    7   import java.util.Vector;
    8
    9   import org.apache.commons.logging.Log;
    10  import org.apache.commons.logging.LogFactory;
    11
    12  import baseCode.dataStructure.matrix.NamedMatrix;
    13
    14  /***
    15   * Abstract class representing an object that can read in a {@link NamedMatrix}from a file.
    16   * <p>
    17   * Copyright (c) 2004
    18   * </p>
    19   * <p>
    20   * Institution:: Columbia University
    21   * </p>
    22   *
    23   * @author Paul Pavlidis
    24   * @version $Id: AbstractNamedMatrixReader.java,v 1.3 2004/08/11 22:53:43 pavlidis Exp $
    25   */
    26  public abstract class AbstractNamedMatrixReader {
    27
    28     public abstract NamedMatrix read( String filename ) throws IOException;
    29
    30     public abstract NamedMatrix read( InputStream stream ) throws IOException;
    31
    32     public abstract NamedMatrix readOneRow(BufferedReader dis ) throws IOException;
    33
    34
    35     protected static final Log log = LogFactory
    36           .getLog( AbstractNamedMatrixReader.class );
    37
    38     protected Vector readHeader( BufferedReader dis ) throws IOException {
    39        Vector headerVec = new Vector();
    40        String header = dis.readLine();
    41        StringTokenizer st = new StringTokenizer( header, "\t", true ); // return
    42        // delims.
    43
    44        String previousToken = "";
    45        int columnNumber = 0;
    46        while ( st.hasMoreTokens() ) {
    47           String s = st.nextToken();
    48           boolean missing = false;
    49
    50           if ( s.compareTo( "\t" ) == 0 ) {
    51              /* two tabs in a row */
    52              if ( previousToken.compareTo( "\t" ) == 0 ) {
    53                 missing = true;
    54              } else if ( !st.hasMoreTokens() ) { // at end of line.
    55                 missing = true;
    56              } else {
    57                 previousToken = s;
    58                 continue;
    59              }
    60           } else if ( s.compareTo( " " ) == 0 ) {
    61              if ( previousToken.compareTo( "\t" ) == 0 ) {
    62                 missing = true;
    63              }
    64           }
    65
    66           if ( missing ) {
    67              throw new IOException(
    68                    "Warning: Missing values not allowed in the header (column "
    69                          + columnNumber + ")" );
    70           } else if ( columnNumber > 0 ) {
    71              headerVec.add( s );
    72           }
    73           // otherwise, just the corner string.
    74           columnNumber++;
    75           previousToken = s;
    76        }
    77
    78        //return columnNumber - 1;
    79        if ( headerVec.size() == 0 ) {
    80           log.warn( "No headings found" );
    81        }
    82
    83        return headerVec;
    84
    85     }
    86
    87  }