/Users/lyon/j4p/src/net/server/servlets/CsvLineParser.java

1    package net.server.servlets; 
2     
3     
4    /** 
5     * The CsvLineParser class is a utility class which 
6     * enables extraction of values from a comma separated value, 
7     * or CSV, file. 
8     * 
9     * @author Robert Lysik 
10    * @version 1.00 
11    */ 
12   public class CsvLineParser { 
13    
14       /** 
15        * This is the default constructor for the CsvLineParser class. 
16        */ 
17       public CsvLineParser() { 
18           return; 
19       } 
20    
21       /** 
22        * This method takes the unparsed comma separated value string 
23        * and an array of integers representing the indices of the 
24        * strings to extract from the CSV string as arguments. The 
25        * return value of this function is an array of the requested 
26        * strings. 
27        * 
28        */ 
29       public String[] getValues(String s, int plays[]) { 
30           // Set the size of our array to the number of values 
31           // requested. 
32           String values[] = new String[plays.length]; 
33    
34           String tokens[] = getTokens(s); 
35    
36           for (int index = 0; index < plays.length; index++) { 
37               values[index] = tokens[plays[index]]; 
38           } 
39    
40           return values; 
41       } 
42    
43       /** 
44        * This function takes the unparsed comma separated value string 
45        * as an argument. Each character in the string is examined to 
46        * determine whether or not it is a comma. If so, a substring 
47        * is extracted from the CSV string up to the comma and the 
48        * starting index of the string is incremented one position beyond 
49        * the location of the comma. 
50        */ 
51       public String[] getTokens(String csvString) { 
52           StringBuffer sb = new StringBuffer(csvString); 
53           String s [] = new String[getTokenCount(sb)]; 
54           int tc = 0; 
55           int start = 0; 
56           for (int i = 0; i < sb.length(); i++) { 
57               if (sb.charAt(i) == ',') { 
58                   s[tc] = sb.substring(start, i); 
59                   start = i + 1; 
60                   tc++; 
61               } 
62           } 
63           s[tc] = sb.substring(start, sb.length()); 
64           return s; 
65       } 
66    
67       /** 
68        * This function calculates the number of values in the comma 
69        * separated value string which is passed in as a parameter 
70        * in the form of a StringBuffer object. Each character in the 
71        * StringBuffer object is examined to determine whether or not 
72        * it is a comma. If a comma is found, the count of tokens is 
73        * incremented by one. 
74        */ 
75       public int getTokenCount(StringBuffer sb) { 
76           int tc = 0; 
77           for (int i = 0; i < sb.length(); i++) { 
78               if (sb.charAt(i) == ',') 
79                   tc++; 
80           } 
81           return tc + 1; 
82       } 
83   }