DelimiterParser sample

This sample parser parses a reader stream and tokenizes the stream based on the delimiter that is passed as one of the argument. The stream is broken down into lines which are then tokenized. An empty string token is retuned if the parser finds two consecutive delimiters.

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


import com.ibm.ccd.api.samplecode.SampleTokenizer;

import java.io.BufferedReader;
import java.io.StringReader;


public class DelimiterParser 
{

    private String delim;
    private  String line = "";
    private int nbLines = 0;
    private BufferedReader reader;

    public DelimiterParser(BufferedReader reader, String delim)
    {
        this.reader = reader; 
        this.delim = delim;
    }

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

        SampleTokenizer st = new SampleTokenizer(line, delim.charAt(0));
        int numTokens = st.countTokens();
        String[] allTokens = new String[numTokens];

        for (int i = 0; i < numTokens; i++)
            allTokens[i] = st.nextToken();

        return allTokens;
    }

    public String nextLine()
        throws Exception
    {
        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 = "a,a  ab,c,d a\n" +
                     ",1 a\n" +
                     "1, \n" +
                     "a,\n" +
                     "1," +
                     "\"v \"\"a v\"";

            System.out.println("String being parsed = [" + doc + "]");
            reader = new BufferedReader(new StringReader(doc));
            DelimiterParser parser = new DelimiterParser(reader,",");
            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();
        }

    }


}