001    package jp.osdl.jbento2.report;
002    
003    import java.io.BufferedReader;
004    import java.io.File;
005    import java.io.FileReader;
006    import java.util.ArrayList;
007    import java.util.HashMap;
008    import java.util.List;
009    import java.util.Map;
010    
011    public class CsvReader {
012    
013        public static Map readData(File csvFile) throws Exception {
014            Map data = new HashMap();
015            BufferedReader reader = new BufferedReader(new FileReader(csvFile));
016        
017            String line = reader.readLine();
018            if (line == null) {
019                return data;
020            }
021        
022            data.put("header", splitLine(line, ","));
023        
024            List body = new ArrayList();
025            boolean appearedSepalator = false;
026            while ((line = reader.readLine()) != null) {
027                if (line.startsWith("---")) {
028                    if (appearedSepalator) {
029                        break;
030                    } else {
031                        appearedSepalator = true;
032                        continue;
033                    }
034                }
035                body.add(splitLine(line, ","));
036            }
037            data.put("body", body);
038        
039            line = reader.readLine();
040            if (line == null) {
041                return data;
042            }
043        
044            data.put("footer", splitLine(line, ","));
045            return data;
046        }
047        
048        private static String[] splitLine(String line, String delimiter) {
049            String[] result = line.split(delimiter);
050            for (int i = 0; i < result.length; i++) {
051                result[i] = result[i].trim();
052            }
053            return result;
054        }
055    
056    }