KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > console > text > formatter > TableFormatter


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Emmanuel Cecchet.
22  * Contributor(s): ______________________.
23  */

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

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

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

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

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

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

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

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

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