1 package org.kuali.common.devops.logic; 2 3 import java.util.SortedSet; 4 5 import org.apache.commons.lang3.StringUtils; 6 import org.kuali.common.devops.table.TableContext; 7 8 import com.google.common.collect.Sets; 9 import com.google.common.collect.Table; 10 11 public class Html extends Examiner { 12 13 public static <R, C> String html(Table<? extends Comparable<R>, ? extends Comparable<C>, String> table) { 14 return html(table, 0); 15 } 16 17 public static <R, C> String html(Table<? extends Comparable<R>, ? extends Comparable<C>, String> table, int indent) { 18 return html(TableContext.builder().indent(indent).build(), table); 19 } 20 21 protected static <C> String getHeader(TableContext context, SortedSet<Comparable<C>> colKeys, String padding) { 22 if (context.isColumnLabels()) { 23 StringBuilder sb = new StringBuilder(); 24 sb.append("<thead>\n"); 25 sb.append(" <tr>\n"); 26 int count = 0; 27 for (Comparable<C> colKey : colKeys) { 28 String sort = (count++ == 0) ? "int" : "string"; 29 sb.append(padding + " <th data-sort='" + sort + "'>" + colKey + "</th>\n"); 30 } 31 sb.append(" </tr>\n"); 32 sb.append("</thead>\n"); 33 return sb.toString(); 34 } else { 35 return ""; 36 } 37 } 38 39 public static <R, C> String html(TableContext context, Table<? extends Comparable<R>, ? extends Comparable<C>, String> table) { 40 String padding = StringUtils.repeat(" ", context.getIndent()); 41 StringBuilder sb = new StringBuilder(); 42 sb.append(padding + "<table>\n"); 43 SortedSet<Comparable<R>> rowKeys = Sets.newTreeSet(table.rowKeySet()); 44 SortedSet<Comparable<C>> colKeys = Sets.newTreeSet(table.columnKeySet()); 45 sb.append(getHeader(context, colKeys, padding)); 46 sb.append("<tbody>\n"); 47 int count = 0; 48 for (Comparable<R> rowKey : rowKeys) { 49 boolean even = count++ % 2 == 0; 50 if (even) { 51 sb.append(padding + " <tr class='table-tr-even'>\n"); 52 } else { 53 sb.append(padding + " <tr class='table-tr-odd'>\n"); 54 } 55 if (context.isRowLabels()) { 56 sb.append(padding + " <td>\n"); 57 sb.append(padding + " " + rowKey.toString() + "\n"); 58 sb.append(padding + " </td>\n"); 59 } 60 for (Comparable<C> colKey : colKeys) { 61 String align = context.getTdalign().isPresent() ? " align=\"" + context.getTdalign().get() + "\"" : ""; 62 sb.append(padding + " <td" + align + ">\n"); 63 sb.append(padding + " " + table.get(rowKey, colKey) + "\n"); 64 sb.append(padding + " </td>\n"); 65 } 66 sb.append(padding + " </tr>\n"); 67 } 68 sb.append("</tbody>\n"); 69 sb.append(padding + "</table>\n"); 70 return sb.toString(); 71 } 72 }