1 /*
2 *
3 * The DbUnit Database Testing Framework
4 * Copyright (C)2002-2008, DbUnit.org
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21 package org.dbunit.util;
22
23 import org.dbunit.dataset.Column;
24 import org.dbunit.dataset.DataSetException;
25 import org.dbunit.dataset.ITable;
26 import org.dbunit.dataset.ITableMetaData;
27
28 /**
29 * Simple formatter to print out {@link ITable} objects in a beautiful way, for
30 * example on a console.
31 *
32 * @author gommma (gommma AT users.sourceforge.net)
33 * @author Last changed by: $Author$
34 * @version $Revision$ $Date$
35 * @since 2.4.1
36 */
37 public class TableFormatter
38 {
39
40 public TableFormatter()
41 {
42
43 }
44
45 /**
46 * Pads the given String with the given <code>padChar</code> up to the
47 * given
48 * <code>length</code>.
49 *
50 * @param s
51 * @param length
52 * @param padChar
53 * @return The padded string
54 */
55 public static final String padLeft(String s, int length, char padChar)
56 {
57 String result = s;
58
59 char[] padCharArray = getPadCharArray(s, length, padChar);
60 if (padCharArray != null)
61 result = pad(s, padCharArray, true);
62
63 return result;
64 }
65
66 /**
67 * Pads the given String with the given <code>padChar</code> up to the given
68 * <code>length</code>.
69 *
70 * @param s
71 * @param length
72 * @param padChar
73 * @return The padded string
74 */
75 public static final String padRight(String s, int length, char padChar)
76 {
77 String result = s;
78
79 char[] padCharArray = getPadCharArray(s, length, padChar);
80 if (padCharArray != null)
81 result = pad(s, padCharArray, false);
82
83 return result;
84 }
85
86 private static final char[] getPadCharArray(String s, int length,
87 char padChar)
88 {
89 if (length > 0 && length > s.length())
90 {
91 int padCount = length - s.length();
92 char[] padArray = new char[padCount];
93 for (int i = 0; i < padArray.length; i++)
94 {
95 padArray[i] = padChar;
96 }
97 return padArray;
98 } else
99 {
100 return null;
101 }
102 }
103
104 private static final String pad(String s, char[] padArray, boolean padLeft)
105 {
106 final StringBuilder sb = new StringBuilder(s);
107 if (padLeft)
108 {
109 sb.insert(0, padArray);
110 } else
111 {
112 sb.append(padArray);
113 }
114 return sb.toString();
115 }
116
117 /**
118 * Formats a table with all data in a beautiful way. Can be useful to print
119 * out the table data on a console.
120 *
121 * @param table
122 * The table to be formatted in a beautiful way
123 * @return The table data as a formatted String
124 * @throws DataSetException
125 */
126 public String format(ITable table) throws DataSetException
127 {
128 final StringBuilder sb = new StringBuilder();
129 ITableMetaData tableMetaData = table.getTableMetaData();
130 // Title line
131 sb.append("******");
132 sb.append(" table: ").append(tableMetaData.getTableName()).append(" ");
133 sb.append("**");
134 sb.append(" row count: ").append(table.getRowCount()).append(" ");
135 sb.append("******");
136 sb.append("\n");
137
138 // Column headers
139 int width = 20;
140 Column[] cols = tableMetaData.getColumns();
141 for (int i = 0; i < cols.length; i++)
142 {
143 sb.append(padRight(cols[i].getColumnName(), width, ' '));
144 sb.append("|");
145 }
146 sb.append("\n");
147
148 // Separator
149 for (int i = 0; i < cols.length; i++)
150 {
151 sb.append(padRight("", width, '='));
152 sb.append("|");
153 }
154 sb.append("\n");
155
156 // Values
157 for (int i = 0; i < table.getRowCount(); i++)
158 {
159 for (int j = 0; j < cols.length; j++)
160 {
161 Object value = table.getValue(i, cols[j].getColumnName());
162 String stringValue = String.valueOf(value);
163 sb.append(padRight(stringValue, 20, ' '));
164 sb.append("|");
165 }
166 // New row
167 sb.append("\n");
168 }
169
170 return sb.toString();
171 }
172
173 }