/Users/lyon/j4p/src/futils/CsvParser.java

1    package futils; 
2     
3    import java.util.Vector; 
4     
5    /** 
6     * CsvParser Class 
7     */ 
8    public class CsvParser { 
9     
10       private StringBuffer sb; 
11       private Vector list = new Vector(); 
12       private char delim = ','; 
13    
14       /** 
15        * Parse the string into an array of string, 
16        * breaking up along the comma delimiter. The 
17        * delimiters are removed when you get the 
18        * tokens. 
19        * 
20        * @param s 
21        */ 
22       public CsvParser(String s) { 
23           this(s, ','); 
24       } 
25    
26       /** 
27        * Use the delimiter to perform the parsing. 
28        * Defaults to a comma. 
29        * 
30        * @param delimiter 
31        */ 
32       public CsvParser(String s, char delimiter) { 
33           sb = new StringBuffer(s); 
34           delim = delimiter; 
35       } 
36    
37    
38       /** 
39        * getTokens Method 
40        * 
41        * @return  String[]    tokens 
42        */ 
43       public String[] getTokens() { 
44           int start = 0; 
45    
46           for (int i = 0; i < sb.length(); i++) { 
47               if (sb.charAt(i) == delim) { 
48                   addElement(sb.toString() 
49                              .substring(start, i)); 
50                   start = i + 1; 
51               } 
52           } 
53    
54           addElement( 
55                   sb.toString().substring(start, 
56                                           sb.length())); 
57    
58           String strObj[] = new String[list.size()]; 
59    
60    
61           for (int i = 0; i < list.size(); i++) { 
62               strObj[i] = 
63               (String) list.elementAt(i); 
64           } 
65           return strObj; 
66       } 
67    
68       private void addElement(String s) { 
69           list.addElement(s.trim()); 
70       } 
71    
72       public static void main(String[] args) { 
73           testCsdParser(); 
74       } 
75    
76       public static void testCsdParser() { 
77           System.out.println("ParseOn comma"); 
78    
79           String sample = "these, are,  " + 
80                           ";43 345 &*(); :colon, " + 
81                           "+ comma, :values"; 
82           System.out.println("sample=" + sample); 
83           CsvParser cp = new CsvParser(sample, 
84                                        ','); 
85           print(cp.getTokens()); 
86           System.out.println("sample=" + sample); 
87           System.out.println( 
88                   "now parse on :----->"); 
89           cp = new CsvParser(sample, ':'); 
90           print(cp.getTokens()); 
91       } 
92    
93    
94       public static void testCsvParser() { 
95    
96           String sample = "these, are,  ;43 345 &*(); comma, + separated, :values"; 
97           System.out.println("sample=" + sample); 
98           CsvParser cp = new CsvParser(sample); 
99           print(cp.getTokens()); 
100   
101          System.out.println( 
102                  "now parse on ;----->"); 
103          cp = new CsvParser(sample, ';'); 
104          print(cp.getTokens()); 
105      } 
106   
107      public static void print(String[] s) { 
108          for (int i = 0; i < s.length; i++) 
109              System.out.println(s[i]); 
110      } 
111   
112  } 
113