View Javadoc

1   package jp.osdl.jbento2.analyzer;
2   
3   import java.util.ArrayList;
4   import java.util.HashMap;
5   import java.util.Iterator;
6   import java.util.List;
7   import java.util.Map;
8   import org.apache.commons.lang.StringUtils;
9   
10  public class TextTable {
11  
12      public static final int ALIGN_LEFT = 0;
13      public static final int ALIGN_RIGHT = 1;
14      private static final String LINE_SEPARATOR =
15          System.getProperty("line.separator");
16  
17      private int columns;
18      private String delimiter;
19      private Map alignMap = new HashMap();
20      private List contentsList = new ArrayList();
21  
22      public TextTable(int columns, int defaultAlign, String delimiter) {
23          this.columns = columns;
24          for (int i = 0; i < columns; i++) {
25              setAlign(i, defaultAlign);
26          }
27          this.delimiter = delimiter;
28      }
29  
30      public void setAlign(int column, int align) {
31          alignMap.put(new Integer(column), new Integer(align));
32      }
33  
34      public int getAlign(int column) {
35          return ((Integer)alignMap.get(new Integer(column))).intValue();
36      }
37  
38      public void addSeparator(String s) {
39          contentsList.add(new Separator(s));
40      }
41  
42      public void addRow(String[] contents) {
43          contentsList.add(contents);
44      }
45  
46      public String toString() {
47          Map lengthMap = new HashMap();
48          for (int i = 0; i < columns; i++) {
49              int max = 0;
50              for (Iterator it = contentsList.iterator(); it.hasNext(); ) {
51                  Object o = it.next();
52                  if (o instanceof Separator) {
53                      continue;
54                  }
55                  String s = ((String[])o)[i];
56                  if (max < s.length()) {
57                      max = s.length();
58                  }
59              }
60              lengthMap.put(new Integer(i), new Integer(max));
61          }
62  
63          int totalLength = delimiter.length() * (columns - 1);
64          for (Iterator it = lengthMap.values().iterator(); it.hasNext(); ) {
65              totalLength += ((Integer)it.next()).intValue();
66          }
67  
68          StringBuffer result = new StringBuffer();
69          for (Iterator it = contentsList.iterator(); it.hasNext(); ) {
70              Object o = it.next();
71              if (o instanceof Separator) {
72                  result.append(
73                      StringUtils.leftPad("", totalLength, ((Separator)o).s));
74                  result.append(LINE_SEPARATOR);
75                  continue;
76              }
77              String[] row = (String[])o;
78              for (int i = 0; i < columns; i++) {
79                  if (getAlign(i) == ALIGN_LEFT) {
80                      result.append(
81                          StringUtils.rightPad(
82                              row[i],
83                              ((Integer)lengthMap.get(new Integer(i))).intValue()));
84                      result.append(delimiter);
85                  } else if (getAlign(i) == ALIGN_RIGHT) {
86                      result.append(
87                          StringUtils.leftPad(
88                              row[i],
89                              ((Integer)lengthMap.get(new Integer(i))).intValue()));
90                      result.append(delimiter);
91                  }
92              }
93              if (result.length() != 0) {
94                  result.deleteCharAt(result.length() - 1);
95              }
96              result.append(LINE_SEPARATOR);
97          }
98          return result.toString();
99      }
100 
101     private class Separator {
102         private String s;
103         public Separator(String s) {
104             this.s = s;
105         }
106     }
107 
108 }