1 24 25 package org.objectweb.cjdbc.console.text.formatter; 26 27 32 public class TableFormatter 33 { 34 44 public static String format(String [] headers, String [][] cells, 45 boolean headersAsRow) 46 { 47 StringBuffer buf = new StringBuffer (); 48 int[] longestLengths = findLongestDataLengths(headers, cells, headersAsRow); 49 String 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 76 private static String createHorizontalRule(int[] longestLengths) 77 { 78 StringBuffer buf = new StringBuffer ("+"); 79 for (int i = 0; i < longestLengths.length; i++) 80 { 81 appendStringTo(buf, "-", longestLengths[i] + 2); 84 buf.append("+"); 85 } 86 buf.append("\n"); 87 return buf.toString(); 88 } 89 90 91 98 private static void appendStringTo(StringBuffer buf, String string, int n) 99 { 100 for (int i = 0; i < n; i++) 101 { 102 buf.append(string); 103 } 104 } 105 106 119 private static int[] findLongestDataLengths(String [] headers, 120 String [][] cells, boolean headersAsRow) 121 { 122 if (headersAsRow) 123 { 124 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 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 cell = cells[i][j]; 155 longestLengths[1] = Math.max(longestLengths[1], cell.length()); 156 } 157 } 158 return longestLengths; 159 } 160 } 161 162 163 171 private static void appendCellsTo(String [][] cells, StringBuffer 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 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 196 private static void appendHeadersTo(String [] headers, StringBuffer buf, 197 int[] longestLengths) 198 { 199 buf.append("| "); 200 for (int i = 0; i < headers.length; i++) 201 { 202 String 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 222 private static void appendHeaderAndCells(String [] headers, String [][] cells, 223 StringBuffer buf, int[] longestLengths, String 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 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 |