001    package jp.osdl.jbento2.analyzer;
002    
003    import java.io.IOException;
004    import java.io.Reader;
005    import java.util.Iterator;
006    import java.util.List;
007    import java.util.StringTokenizer;
008    
009    import org.apache.commons.io.IOUtils;
010    
011    public class TextTableParser {
012    
013        private List lines;
014        private String delimiter;
015    
016        public TextTableParser(Reader r, String delimiter)
017            throws IOException {
018            this.lines = IOUtils.readLines(r);
019            this.delimiter = delimiter;
020        }
021    
022        public int getRowCount() {
023            return lines.size();
024        }
025    
026        public int getColumnCount() {
027            for (Iterator it = lines.iterator(); it.hasNext(); ) {
028                StringTokenizer st =
029                    new StringTokenizer((String)it.next(), delimiter);
030                if (st.countTokens() > 0) {
031                    return st.countTokens();
032                }
033            }
034            return 0;
035        }
036    
037        public int searchRowIndex(String s) {
038            int index = 0;
039            for (Iterator it = lines.iterator(); it.hasNext(); ) {
040                StringTokenizer st =
041                    new StringTokenizer((String)it.next(), delimiter);
042                while (st.hasMoreTokens()) {
043                    if (st.nextToken().trim().equals(s)) {
044                        return index;
045                    }
046                }
047                index++;
048            }
049            return -1;
050        }
051    
052        public int searchColumnIndex(String s) {
053            for (Iterator it = lines.iterator(); it.hasNext(); ) {
054                StringTokenizer st =
055                    new StringTokenizer((String)it.next(), delimiter);
056                int index = 0;
057                while (st.hasMoreTokens()) {
058                    if (st.nextToken().trim().equals(s)) {
059                        return index;
060                    }
061                    index++;
062                }
063            }
064            return -1;
065        }
066    
067        public String get(int row, int column) {
068            String s = (String)lines.get(row);
069            StringTokenizer st = new StringTokenizer(s, delimiter);
070            for (int i = 0; i < column; i++) {
071                st.nextToken();
072            }
073            return st.nextToken().trim();
074        }
075    
076    }