All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.infomata.data.TabFormat Maven / Gradle / Ivy

Go to download

Maven Package for datafile.sourceforge.net Java data file read/write utility that provides a convenient set of interfaces for reading and writing data to and from files in widely accepted format such as comma separated values (CSV), fixed width, tab separated, as well as others.

The newest version!
/*
 * $Id: TabFormat.java,v 1.3 2005/12/19 12:31:29 oldman1004 Exp $
 *
 * Copyright(c) 2002 Infomata
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package com.infomata.data;

import java.util.Iterator;

/**
 * Implementation of DataFormat interface for tab delimited
 * data file.
 *
 * @author Sam Kim
 * @version $Revision: 1.3 $
 */
public class TabFormat implements DataFormat {

    /**
     * parses a line of data separated by tab
     * into DataRow object.
     *
     * @param line String containing
     *             data separated by tab character.
     * @return DataRow object containing
     *         data parsed from line
     */
    public DataRow parseLine(String line) {

        DataRow row = null;

        if (line != null) {
      
            row = new DataRow();

            int last = 0;
            int idx = line.indexOf(TAB, last);
      
            while (idx >= 0) {
                row.add(line.substring(last, idx));
                last = idx + 1;
                idx = line.indexOf(TAB, last);
            }

            row.add(line.substring(last));

        }

        return row;

    } // parseLine(String)

    /**
     * Converts DataRow instance into a tab-separated list of data.
     * @param row DataRow instance containing data.
     */
    public String format(DataRow row) {
    
        StringBuffer o = new StringBuffer();
        Iterator i = row.iterator();
        while (i.hasNext()) {
            if (o.length() > 0) {
                o.append(TAB);
            }
            o.append((String)i.next());
        }

        return o.toString();

    } // format(DataRow)
    
    /**
     * constant for tab character in String format.
     */
    private static final String TAB = "\t";

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy