Skip to main content
summaryrefslogtreecommitdiffstats
blob: c8dd04716d69f497eab6b1c7178caccd4a3dbb51 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.jdk.core.util.io;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StreamTokenizer;
import java.util.Arrays;

/**
 * @author Ryan D. Brooks
 */
public class CsvReader {
   private final Reader reader;
   private final StreamTokenizer streamTokenizer;
   private final boolean[] fieldsUsed;
   private int fieldCount;
   private String[] nextRow;

   /**
    * @param file a comma separate value file
    * @param totalNumFields the largest number of fields on any row (whether they are used or not)
    * @param enabled whether to enable or disable all the fields initially
    */
   public CsvReader(File file, int totalNumFields, boolean enabled) throws IOException {
      this(new BufferedReader(new FileReader(file)), totalNumFields, enabled);
   }

   /**
    * All fields will be enabled initially
    * 
    * @param file a comma separate value file
    * @param totalNumFields the largest number of fields on any row (whether they are used or not)
    */
   public CsvReader(File file, int totalNumFields) throws IOException {
      this(new BufferedReader(new FileReader(file)), totalNumFields);
   }

   public CsvReader(Reader reader, int totalNumFields) throws IOException {
      this(reader, totalNumFields, true);
   }

   public CsvReader(Reader reader, int totalNumFields, boolean enabled) throws IOException {
      this.reader = reader;
      this.streamTokenizer = new StreamTokenizer(reader);
      this.fieldsUsed = new boolean[totalNumFields];
      Arrays.fill(fieldsUsed, enabled);
      countFieldsUsed();

      streamTokenizer.resetSyntax();
      streamTokenizer.eolIsSignificant(true);
      streamTokenizer.whitespaceChars(0, ' ' - 1);
      streamTokenizer.wordChars(' ', 255); // make all non-white space characters part of the returned string
      streamTokenizer.ordinaryChar(','); // except the delimiter ','
      streamTokenizer.quoteChar('\"'); // and the quote char

      getRow(); //prime so hasNext might return true (and getRow will return the first row the next time it is called)
   }

   private void countFieldsUsed() {
      this.fieldCount = 0;
      for (int i = 0; i < fieldsUsed.length; i++) {
         if (fieldsUsed[i]) {
            fieldCount++;
         }
      }
   }

   /**
    * inclusive range
    */
   public void setFieldsEnabled(int start, int end, boolean enable) {
      for (int i = start; i <= end; i++) {
         setFieldEnabled(i, enable);
      }
   }

   public void setFieldEnabled(int index, boolean enable) {
      fieldsUsed[index] = enable;
      countFieldsUsed();
   }

   public String[] getRow() throws IOException {
      String[] rowToReturn = nextRow;
      this.nextRow = getRowInternal();
      return rowToReturn;
   }

   public boolean hasNext() {
      return nextRow != null;
   }

   public void skipHeaderRow() throws IOException {
      getRow();
   }

   /**
    * @return an array
    */
   private String[] getRowInternal() throws IOException {
      String[] values = new String[fieldCount];
      int fieldIndex = 0;
      int valuesIndex = 0;
      boolean hasValueBeenRead = false;
      while (streamTokenizer.nextToken() != StreamTokenizer.TT_EOL) {
         if (streamTokenizer.ttype == ',') {
            if (fieldsUsed[fieldIndex]) {
               valuesIndex++; // accounts for fields that are used even if they are empty (i.e. two consecutive commas)
            }
            fieldIndex++;
         } else if (streamTokenizer.ttype == StreamTokenizer.TT_WORD || streamTokenizer.ttype == '\"') {
            if (fieldsUsed[fieldIndex]) {
               values[valuesIndex] = streamTokenizer.sval;
               hasValueBeenRead = true;
            }
         } else if (streamTokenizer.ttype == StreamTokenizer.TT_EOF) {
            if (hasValueBeenRead) {
               return values;
            } else {
               return null;
            }
         } else {
            throw new IllegalArgumentException("The token type was: " + streamTokenizer.ttype);
         }
      }
      return values;
   }

   public void close() {
      try {
         reader.close();
      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

Back to the top