KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > console > text > formatter > TableFormatter


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: sequoia@continuent.org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * Initial developer(s): Emmanuel Cecchet.
20  * Contributor(s): ______________________.
21  */

22
23 package org.continuent.sequoia.console.text.formatter;
24
25 /**
26  * Utility class to format a table which can be pretty displayed in the text console.
27  *
28  * @author <a HREF="mailto:jeff.mesnil@emicnetworks.com">Jeff Mesnil</a>
29  */

30 public class TableFormatter
31 {
32   /**
33    * Format as a table.
34    *
35    * @param headers the headers of the table
36    * @param cells the cells of the table
37    * @param headersAsRow <code>true</code> if the headers must be displayed on the same row,
38    * <code>false</code> if the headers must be displayed on the same column
39    *
40    * @return a String representing a pretty formatted table
41    */

42   public static String JavaDoc format(String JavaDoc[] headers, String JavaDoc[][] cells,
43       boolean headersAsRow)
44   {
45     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
46     int[] longestLengths = findLongestDataLengths(headers, cells, headersAsRow);
47     String JavaDoc horizontalRule = createHorizontalRule(longestLengths);
48     if (headersAsRow)
49     {
50       buf.append(horizontalRule);
51       appendHeadersTo(headers, buf, longestLengths);
52       buf.append(horizontalRule);
53       appendCellsTo(cells, buf, longestLengths);
54       buf.append(horizontalRule);
55     }
56     else
57     {
58       appendHeaderAndCells(headers, cells, buf, longestLengths, horizontalRule);
59     }
60     return buf.toString();
61
62   }
63
64   /**
65    * Create an horizontal rule following the format:
66    * <code>+-----+-------+-----+</code>
67    * where the size of each part is determinged based on the longest length
68    * for each column
69    *
70    * @param longestLengths an array of int corresponding to the longest length of cells for a given column
71    *
72    * @return a <code>String</code> to used as an horizontal rule
73    */

74   private static String JavaDoc createHorizontalRule(int[] longestLengths)
75   {
76     StringBuffer JavaDoc buf = new StringBuffer JavaDoc("+");
77     for (int i = 0; i < longestLengths.length; i++)
78     {
79       // we add 2 to the longest length for each column to put one
80
// space before and after any cell content
81
appendStringTo(buf, "-", longestLengths[i] + 2);
82       buf.append("+");
83     }
84     buf.append("\n");
85     return buf.toString();
86   }
87
88   
89   /**
90    * Append <code>n</code> times the <code>string</code> to <code>buf</code>.
91    *
92    * @param buf a <code>StringBuffer</code>
93    * @param string the <code>String</code> to append
94    * @param n the number of times the string should be appended
95    */

96   private static void appendStringTo(StringBuffer JavaDoc buf, String JavaDoc string, int n)
97   {
98     for (int i = 0; i < n; i++)
99     {
100       buf.append(string);
101     }
102   }
103
104   /**
105    * Find the longest lengths for the data.
106    * Used internally to create an horizontal rule and to
107    * append whitespaces in each cell so that everything
108    * is propertly formatted.
109    *
110    * @param headers headers of the table
111    * @param cells cells of the table
112    * @param headersAsRow <code>true</code> if the headers must be displayed on the same row,
113    * <code>false</code> if the headers must be displayed on the same column
114    *
115    * @return an array of <code>int</code> containing the longest length for each column
116    */

117   private static int[] findLongestDataLengths(String JavaDoc[] headers,
118       String JavaDoc[][] cells, boolean headersAsRow)
119   {
120     if (headersAsRow)
121     {
122       // there is has much columns as heafers.length
123
int[] longestLengths = new int[headers.length];
124       for (int j = 0; j < longestLengths.length; j++)
125       {
126         int maxLength = 0;
127         maxLength = Math.max(maxLength, headers[j].length());
128         for (int i = 0; i < cells.length; i++)
129         {
130           maxLength = Math.max(maxLength, cells[i][j].length());
131         }
132         longestLengths[j] = maxLength;
133       }
134       return longestLengths;
135     }
136     else
137     {
138       // there is only two columns:
139
// - the first for the header
140
// - the second for the cell
141
int[] longestLengths = new int[2];
142       longestLengths[0] = 0;
143       for (int i = 0; i < headers.length; i++)
144       {
145         longestLengths[0] = Math.max(longestLengths[0], headers[i].length());
146       }
147       longestLengths[1] = 0;
148       for (int i = 0; i < cells.length; i++)
149       {
150         for (int j = 0; j < cells[i].length; j++)
151         {
152           String JavaDoc cell = cells[i][j];
153           longestLengths[1] = Math.max(longestLengths[1], cell.length());
154         }
155       }
156       return longestLengths;
157     }
158   }
159
160   
161   /**
162    * Append a table of cells to the buffer
163    *
164    * @param cells the table of cells to append
165    * @param buf the <code>StringBuffer</code> where to append
166    * @param longestLengths used to fill each cell with whitespaces so
167    * that everything is properly formatted
168    */

169   private static void appendCellsTo(String JavaDoc[][] cells, StringBuffer JavaDoc buf,
170       int[] longestLengths)
171   {
172     for (int i = 0; i < cells.length; i++)
173     {
174       buf.append("| ");
175       for (int j = 0; j < cells[i].length; j++)
176       {
177         String JavaDoc cell = cells[i][j];
178         buf.append(cell);
179         appendStringTo(buf, " ", longestLengths[j] - cell.length());
180         buf.append(" | ");
181       }
182       buf.append("\n");
183     }
184   }
185
186   /**
187    * Append table's headers to the buffer
188    *
189    * @param headers the headers of the table
190    * @param buf the <code>StringBuffer</code> where to append
191    * @param longestLengths used to fill each cell with whitespaces so
192    * that everything is properly formatted
193    */

194   private static void appendHeadersTo(String JavaDoc[] headers, StringBuffer JavaDoc buf,
195       int[] longestLengths)
196   {
197     buf.append("| ");
198     for (int i = 0; i < headers.length; i++)
199     {
200       String JavaDoc header = headers[i];
201       buf.append(header);
202       appendStringTo(buf, " ", longestLengths[i] - header.length());
203       buf.append(" | ");
204     }
205     buf.append("\n");
206   }
207
208   /**
209    * Append table's headers and cells in one pass.
210    * used when the headers should be displayed on the same colum
211    * (i.e. <code>headersAsRow</code> was <code>false</code>).
212    *
213    * @param headers the headers of the table
214    * @param cells the cells of the table
215    * @param buf the <code>StringBuffer</code> where to append
216    * @param longestLengths used to fill each cell with whitespaces so
217    * that everything is properly formatted
218    * @param horizontalRule an horizontal rule
219    */

220   private static void appendHeaderAndCells(String JavaDoc[] headers, String JavaDoc[][] cells,
221       StringBuffer JavaDoc buf, int[] longestLengths, String JavaDoc horizontalRule)
222   {
223     buf.append(horizontalRule);
224     for (int i = 0; i < cells.length; i++)
225     {
226       for (int j = 0; j < cells[i].length; j++)
227       {
228         buf.append("| ");
229         buf.append(headers[j]);
230         appendStringTo(buf, " ", longestLengths[0] - headers[j].length());
231         buf.append(" | ");
232         String JavaDoc cell = cells[i][j];
233         buf.append(cell);
234         appendStringTo(buf, " ", longestLengths[1] - cell.length());
235         buf.append(" |\n");
236       }
237       buf.append(horizontalRule);
238     }
239   }
240 }
241
Popular Tags