FixedWidthParser sample

This sample parser parses a reader stream and tokenizes the stream based on the width that is passed in as an array of type integer. The stream is broken down into lines that are then tokenized based on the size of the width.

package com.ibm.ccd.api.samplecode.parser;

//other imports

import java.io.BufferedReader;
import java.io.StringReader;
import java.io.IOException;
import java.util.Vector;

public class FixedWidthParser 
    {
    public String line = "";
    protected int nbLines = 0;
    int[] tokenSizeArray;
    private BufferedReader reader;

    public FixedWidthParser(BufferedReader reader, int[] tokenSizeArray)
    {
        this.reader = reader;
        this.tokenSizeArray = tokenSizeArray;
    }

    public String[] splitLine() throws IOException
    {
        nbLines = 0;
        line = nextLine();
        if (line == null)
            return null;
        nbLines = 1;

        Vector v = new Vector();
        int pointer = 0;
        int end = line.length();
        for (int i = 0; i < tokenSizeArray.length; i++)
        {
            if (pointer + tokenSizeArray[i] > end)
            {
                if (pointer < end)
                    v.addElement(line.substring(pointer));
                else
                    v.addElement("");
            }
            else
            {
                v.addElement(line.substring(pointer, pointer + tokenSizeArray[i]));
            }
            pointer += tokenSizeArray[i];
        }
        return (String[])v.toArray(new String[0]);
    }
    public String nextLine()
        throws IOException
    {
        do
        {
            line = reader.readLine();
            if (line == null)
                return null;
        }
        while (line.trim().equals(""));
        return line;
    }

    public static void main (String args[])
    {
        BufferedReader reader = null;
        try
        {
            String doc = "This is a test String which is being parsed by FixedWidthParser";

            System.out.println("String being parsed = [" + doc + "]");
            reader = new BufferedReader(new StringReader(doc));
            int [] widths = {4, 3, 2, 5, 7, 6, 3, 6, 7, 3, 17};
            FixedWidthParser parser = new FixedWidthParser(reader,widths);
            String[] res;


            while ((res = parser.splitLine()) != null)
            {
                for (int i = 0; i < res.length; i++)
                {
                    System.out.println("token found ["+res[i]+"] \n");
                }
            }
        }
        catch(Exception e )
        {
            e.printStackTrace();
        }
    }


}