001    package jp.osdl.jbento2.chart;
002    
003    import java.io.File;
004    import java.io.FileReader;
005    import java.io.IOException;
006    import java.io.Reader;
007    import java.util.Arrays;
008    import java.util.List;
009    
010    import org.apache.commons.io.IOUtils;
011    import org.jfree.data.general.AbstractDataset;
012    
013    public abstract class DataSetBuilderBase implements DataSetBuilder {
014    
015        private String srcFile;
016        private String[] categories;
017        private String endOfData;
018    
019        public AbstractDataset createDataSet(File srcFile) throws IOException {
020    
021            AbstractDataset dataset = newDataset();
022    
023            // read srcFile
024            Reader reader = null;
025            List lines = null;
026            try {
027                reader = new FileReader(srcFile);
028                lines = IOUtils.readLines(reader);
029                if (lines.isEmpty()) {
030                    throw new IllegalArgumentException("srcFile");
031                }
032            } finally {
033                IOUtils.closeQuietly(reader);
034            }
035    
036            // find first line
037            int firstLineIndex = getFirstLineIndex(lines);
038            if (firstLineIndex == -1) {
039                throw new IllegalArgumentException("srcFile");
040            }
041            String firstLine = (String)lines.get(firstLineIndex);
042    
043            // initialize categories
044            String[] columns = splitLine(firstLine);
045            if (categories == null) {
046                categories = new String[columns.length - 1];
047                System.arraycopy(columns, 1, categories, 0, columns.length - 1);
048            }
049            int[] categoryIndexes = getCategoryIndexes(categories, columns);
050    
051            // process body
052            for (int i = firstLineIndex + 1; i < lines.size(); i++) {
053                String line = (String)lines.get(i);
054                if (endOfData != null && line.startsWith(endOfData)) {
055                    break;
056                }
057                String[] data = splitLine(line);
058                if (data.length != columns.length) {
059                    continue;
060                }
061                for (int j = 0; j < categories.length; j++) {
062                    add(dataset, data[0], categories[j], data[categoryIndexes[j]]);
063                }
064            }
065    
066            return dataset;
067        }
068    
069        abstract protected AbstractDataset newDataset();
070    
071        abstract protected void add(
072            AbstractDataset dataset, String row, String column, String value);
073    
074        public String[] getCategories() {
075            return categories;
076        }
077    
078        public void setCategories(String[] categories) {
079            this.categories = categories;
080        }
081    
082        public String getEndOfData() {
083            return endOfData;
084        }
085    
086        public void setEndOfData(String endOfData) {
087            this.endOfData = endOfData;
088        }
089    
090        public String getSrcFile() {
091            return srcFile;
092        }
093    
094        public void setSrcFile(String srcFile) {
095            this.srcFile = srcFile;
096        }
097    
098        private int getFirstLineIndex(List lines) {
099            for (int i = 0; i < lines.size(); i++) {
100                String line = (String)lines.get(i);
101                if (line != null && line.split(",").length != 0) {
102                    return i;
103                }
104            }
105            return -1;
106        }
107    
108        private String[] splitLine(String line) {
109            String[] result = line.split(",");
110            for (int i = 0; i < result.length; i++) {
111                result[i] = result[i].trim();
112            }
113            return result;
114        }
115    
116        private int[] getCategoryIndexes(String[] categories, String[] columns) {
117            int[] result = new int[categories.length];
118            List list = Arrays.asList(columns);
119            for (int i = 0; i < categories.length; i++) {
120                String category = categories[i];
121                int index = list.indexOf(category);
122                if (index == -1) {
123                    throw new IllegalArgumentException(
124                        "category " + category + " is not found in " + srcFile);
125                }
126                result[i] = index;
127            }
128            return result;
129        }
130    
131    }