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

1    package net.server.servlets; 
2     
3    import java.util.ArrayList; 
4    import java.util.Iterator; 
5    import java.util.List; 
6     
7    /** 
8     * CsvParser Class 
9     */ 
10    
11   public class CsvParser { 
12    
13       StringBuffer sb; 
14       List list = new ArrayList(); 
15    
16       /** 
17        * CSVParser Constructor 
18        */ 
19    
20       public CsvParser(String s) { 
21           sb = new StringBuffer(s); 
22       } 
23    
24       /** 
25        * Returns an array of String tokens 
26        * 
27        * @return  String[]    tokens 
28        */ 
29    
30       public String[] getTokens() { 
31           int tc = 0; 
32           int start = 0; 
33    
34           for (int i = 0; i < sb.length(); i++) { 
35               if (sb.charAt(i) == ',') { 
36                   list.add(sb.substring(start, i)); 
37                   start = i + 1; 
38                   tc++; 
39               } 
40           } 
41    
42           list.add(sb.substring(start, sb.length())); 
43    
44           String strObj[] = new String[list.size()]; 
45    
46           Iterator it = list.iterator(); 
47    
48           for (int i = 0; it.hasNext(); i++) { 
49               strObj[i] = (String) it.next(); 
50           } 
51    
52           return strObj; 
53       } 
54    
55   } 
56    
57