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

1    package net.server.servlets; 
2     
3    import java.io.BufferedReader; 
4    import java.io.IOException; 
5    import java.util.Vector; 
6     
7    /** 
8     *  CsvLineConsumer Class 
9     */ 
10    
11   public class CsvLineConsumer implements LineConsumer { 
12    
13       /** 
14        * Components 
15        */ 
16    
17       private BufferedReader br; 
18       private Vector vData = new Vector();; 
19       private int[] column; 
20    
21    
22       /** 
23        * CsvLineConsumer() Constructor 
24        */ 
25    
26       public CsvLineConsumer(BufferedReader _br, int[] _column) { 
27           br = _br; 
28           column = _column; 
29    
30           readBufferedReader(); 
31       } 
32    
33    
34       /** 
35        * Returns CSV Data 
36        * 
37        * @return          void 
38        */ 
39    
40       public Vector getVData() { 
41           return vData; 
42    
43       } 
44    
45    
46       /** 
47        * readBufferedReader   Method 
48        * 
49        */ 
50    
51       private void readBufferedReader() { 
52    
53           String line = null; 
54    
55           try { 
56               line = br.readLine(); // to get the column names 
57    
58               while ((line = br.readLine()) != null) { 
59                   setVectorDataElement(line); 
60               } 
61           } catch (IOException e) { 
62           } 
63    
64           FileUtil.close(br); 
65    
66       } 
67    
68    
69       /** 
70        * setVectorDataElement   Method 
71        * 
72        */ 
73    
74       private void setVectorDataElement(String line) { 
75    
76           String sData[] = getValues(line, column); 
77           vData.addElement(sData); 
78       } 
79    
80    
81       /** 
82        * Return an array of columns 
83        * 
84        * @return  String[] 
85        */ 
86    
87       public String[] getValues(String s, int column[]) { 
88           CsvParser csvData = new CsvParser(s); 
89    
90           String strArray[] = csvData.getTokens(); 
91           String selArray[] = new String[column.length]; 
92    
93           for (int i = 0; i < column.length; i++) { 
94               selArray[i] = strArray[column[i]]; 
95           } 
96    
97    
98           return selArray; 
99       } 
100   
101  } 
102