001    package jp.osdl.jbento2.analyzer;
002    
003    import java.util.ArrayList;
004    import java.util.HashMap;
005    import java.util.Iterator;
006    import java.util.List;
007    import java.util.Map;
008    import org.apache.commons.lang.StringUtils;
009    
010    public class TextTable {
011    
012        public static final int ALIGN_LEFT = 0;
013        public static final int ALIGN_RIGHT = 1;
014        private static final String LINE_SEPARATOR =
015            System.getProperty("line.separator");
016    
017        private int columns;
018        private String delimiter;
019        private Map alignMap = new HashMap();
020        private List contentsList = new ArrayList();
021    
022        public TextTable(int columns, int defaultAlign, String delimiter) {
023            this.columns = columns;
024            for (int i = 0; i < columns; i++) {
025                setAlign(i, defaultAlign);
026            }
027            this.delimiter = delimiter;
028        }
029    
030        public void setAlign(int column, int align) {
031            alignMap.put(new Integer(column), new Integer(align));
032        }
033    
034        public int getAlign(int column) {
035            return ((Integer)alignMap.get(new Integer(column))).intValue();
036        }
037    
038        public void addSeparator(String s) {
039            contentsList.add(new Separator(s));
040        }
041    
042        public void addRow(String[] contents) {
043            contentsList.add(contents);
044        }
045    
046        public String toString() {
047            Map lengthMap = new HashMap();
048            for (int i = 0; i < columns; i++) {
049                int max = 0;
050                for (Iterator it = contentsList.iterator(); it.hasNext(); ) {
051                    Object o = it.next();
052                    if (o instanceof Separator) {
053                        continue;
054                    }
055                    String s = ((String[])o)[i];
056                    if (max < s.length()) {
057                        max = s.length();
058                    }
059                }
060                lengthMap.put(new Integer(i), new Integer(max));
061            }
062    
063            int totalLength = delimiter.length() * (columns - 1);
064            for (Iterator it = lengthMap.values().iterator(); it.hasNext(); ) {
065                totalLength += ((Integer)it.next()).intValue();
066            }
067    
068            StringBuffer result = new StringBuffer();
069            for (Iterator it = contentsList.iterator(); it.hasNext(); ) {
070                Object o = it.next();
071                if (o instanceof Separator) {
072                    result.append(
073                        StringUtils.leftPad("", totalLength, ((Separator)o).s));
074                    result.append(LINE_SEPARATOR);
075                    continue;
076                }
077                String[] row = (String[])o;
078                for (int i = 0; i < columns; i++) {
079                    if (getAlign(i) == ALIGN_LEFT) {
080                        result.append(
081                            StringUtils.rightPad(
082                                row[i],
083                                ((Integer)lengthMap.get(new Integer(i))).intValue()));
084                        result.append(delimiter);
085                    } else if (getAlign(i) == ALIGN_RIGHT) {
086                        result.append(
087                            StringUtils.leftPad(
088                                row[i],
089                                ((Integer)lengthMap.get(new Integer(i))).intValue()));
090                        result.append(delimiter);
091                    }
092                }
093                if (result.length() != 0) {
094                    result.deleteCharAt(result.length() - 1);
095                }
096                result.append(LINE_SEPARATOR);
097            }
098            return result.toString();
099        }
100    
101        private class Separator {
102            private String s;
103            public Separator(String s) {
104                this.s = s;
105            }
106        }
107    
108    }