View Javadoc
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,
30   * for 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       * Formats a table with all data in a beautiful way.
47       * Can be useful to print out the table data on a console.
48       * @param table The table to be formatted in a beautiful way
49       * @return The table data as a formatted String
50       * @throws DataSetException
51       */
52      public String format(ITable table) throws DataSetException
53      {
54          StringBuffer sb = new StringBuffer();
55          ITableMetaData tableMetaData = table.getTableMetaData();
56          // Title line
57          sb.append("******");
58          sb.append(" table: ").append(tableMetaData.getTableName()).append(" ");
59          sb.append("**");
60          sb.append(" row count: ").append(table.getRowCount()).append(" ");
61          sb.append("******");
62          sb.append("\n");
63          
64          // Column headers
65          int width = 20;
66          Column[] cols = tableMetaData.getColumns();
67          for (int i = 0; i < cols.length; i++) {
68              sb.append(padRight(cols[i].getColumnName(), width, ' '));
69              sb.append("|");
70          }
71          sb.append("\n");
72  
73          // Separator
74          for (int i = 0; i < cols.length; i++) {
75              sb.append(padRight("", width, '='));
76              sb.append("|");
77          }
78          sb.append("\n");
79          
80          // Values
81          for (int i = 0; i < table.getRowCount(); i++) {
82              for (int j = 0; j < cols.length; j++) {
83                  Object value = table.getValue(i, cols[j].getColumnName());
84                  String stringValue = String.valueOf(value);
85                  sb.append(padRight(stringValue, 20, ' '));
86                  sb.append("|");
87              }
88              // New row
89              sb.append("\n");
90          }
91          
92          return sb.toString();
93      }
94      
95      /**
96       * Pads the given String with the given <code>padChar</code>
97       * up to the given <code>length</code>.
98       * @param s
99       * @param length
100      * @param padChar
101      * @return The padded string
102      */
103     public static final String padLeft(String s, int length, char padChar)
104     {
105         String result = s;
106         
107         char[] padCharArray = getPadCharArray(s, length, padChar);
108         if(padCharArray != null)
109             result = pad(s, padCharArray, true);
110 
111         return result;
112     }
113     
114     /**
115      * Pads the given String with the given <code>padChar</code>
116      * up to the given <code>length</code>.
117      * @param s
118      * @param length
119      * @param padChar
120      * @return The padded string
121      */
122     public static final String padRight(String s, int length, char padChar)
123     {
124         String result = s;
125         
126         char[] padCharArray = getPadCharArray(s, length, padChar);
127         if(padCharArray != null)
128             result = pad(s, padCharArray, false);
129 
130         return result;
131     }
132 
133 
134     private static final char[] getPadCharArray(String s, int length, char padChar) {
135         if(length > 0 && length > s.length())
136         {
137             int padCount = length - s.length();
138             char[] padArray = new char[padCount];
139             for(int i=0; i<padArray.length; i++){
140                 padArray[i] = padChar;
141             }
142             return padArray;
143         }
144         else
145         {
146             return null;
147         }
148     }
149 
150     private static final String pad(String s, char[] padArray, boolean padLeft) {
151         StringBuffer sb = new StringBuffer(s);
152         if(padLeft)
153         {
154             sb.insert(0, padArray);
155         }
156         else
157         {
158             sb.append(padArray);
159         }
160         return sb.toString();
161     }
162 
163 }