View Javadoc

    1   package sharin.csv.parser;
    2
    3   import java.util.ArrayList;
    4   import java.util.List;
    5
    6   public class BasicCsvHandler implements CsvHandler {
    7
    8       private final List<String[]> recordList;
    9
    10      private List<String> valueList;
    11
    12      private StringBuilder valueBuilder;
    13
    14      public BasicCsvHandler() {
    15          recordList = new ArrayList<String[]>();
    16      }
    17
    18      public void startDocument() {
    19          /* do nothing */
    20      }
    21
    22      public void startRecord() {
    23          valueList = new ArrayList<String>();
    24      }
    25
    26      public void startValue() {
    27          valueBuilder = new StringBuilder();
    28      }
    29
    30      public void character(char ch) {
    31          valueBuilder.append(ch);
    32      }
    33
    34      public void endValue() {
    35          valueList.add(valueBuilder.toString());
    36      }
    37
    38      public void endRecord() {
    39          recordList.add(valueList.toArray(new String[valueList.size()]));
    40      }
    41
    42      public void endDocument() {
    43          /* do nothing */
    44      }
    45
    46      public String[][] getRecords() {
    47          return recordList.toArray(new String[recordList.size()][]);
    48      }
    49  }