1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package net.sf.statcvs.renderer;
24
25 import java.util.Iterator;
26
27 import net.sf.statcvs.pages.HTML;
28 import net.sf.statcvs.pages.MarkupSyntax;
29 import net.sf.statcvs.reportmodel.Column;
30 import net.sf.statcvs.reportmodel.Table;
31
32 /**
33 * Renders a {@link net.sf.statcvs.reportmodel.Table} to HTML
34 *
35 * @author Richard Cyganiak <rcyg@gmx.de>
36 * @version $Id: TableRenderer.java,v 1.41 2008/04/02 11:22:15 benoitx Exp $
37 */
38 public class TableRenderer {
39
40 private final Table table;
41 private final HTMLTableCellRenderer renderer = new HTMLTableCellRenderer();
42
43 /**
44 * Creates a new table renderer for the given table model
45 * @param table the table to render
46 */
47 public TableRenderer(final Table table, final MarkupSyntax output) {
48 this.table = table;
49 renderer.setOutput(output);
50 }
51
52 /**
53 * Renders the table to HTML
54 * @return a String of HTML
55 */
56 public String getRenderedTable() {
57 final StringBuffer result = new StringBuffer(" <table ").append(renderer.getOutput().getTableFormat());
58 result.append(" rules=\"groups\" summary=\"").append(HTML.escape(table.getSummary()));
59 result.append("\">\n");
60 result.append(getColumnDescriptions());
61 result.append(getTableHead());
62 if (table.showTotals()) {
63 result.append(getTableTotals());
64 }
65 result.append(getTableBody());
66 result.append(" </table>\n\n");
67 return result.toString();
68 }
69
70 private String getColumnDescriptions() {
71 final StringBuffer result = new StringBuffer();
72 final Iterator it = table.getColumnIterator();
73 boolean isFirstColumn = true;
74 while (it.hasNext()) {
75 it.next();
76 if (table.hasKeysInFirstColumn() && isFirstColumn) {
77 result.append(" <colgroup align=\"left\"/>\n");
78 isFirstColumn = false;
79 } else {
80 result.append(" <colgroup align=\"right\"/>\n");
81 }
82 }
83 return result.toString();
84 }
85
86 private String getTableHead() {
87 final StringBuffer result = new StringBuffer(" <thead>\n <tr>\n");
88 final Iterator it = table.getColumnIterator();
89 while (it.hasNext()) {
90 final Column column = (Column) it.next();
91 column.renderHead(renderer);
92 result.append(" ").append(renderer.getColumnHead()).append("\n");
93 }
94 result.append(" </tr>\n </thead>\n");
95 return result.toString();
96 }
97
98 private String getTableTotals() {
99 final StringBuffer result = new StringBuffer(" <tfoot>\n <tr>\n");
100 final Iterator it = table.getColumnIterator();
101 boolean isFirstColumn = true;
102 while (it.hasNext()) {
103 final Column column = (Column) it.next();
104 column.renderTotal(renderer);
105 if (isFirstColumn && table.hasKeysInFirstColumn()) {
106 result.append(" ").append(renderer.getRowHead()).append("\n");
107 isFirstColumn = false;
108 } else {
109 result.append(" ").append(renderer.getTableCell()).append("\n");
110 }
111 }
112 result.append(" </tr>\n </tfoot>\n");
113 return result.toString();
114 }
115
116 private String getTableBody() {
117 final StringBuffer result = new StringBuffer(" <tbody>\n");
118 for (int i = 0; i < table.getRowCount(); i++) {
119 result.append(getTableRow(i));
120 }
121 result.append(" </tbody>\n");
122 return result.toString();
123 }
124
125 private String getTableRow(final int rowIndex) {
126 final StringBuffer result = new StringBuffer();
127 if (rowIndex % 2 == 0) {
128 result.append(" <tr ").append(renderer.getEvenRowFormat()).append(">\n");
129 } else {
130 result.append(" <tr ").append(renderer.getOddRowFormat()).append(">\n");
131 }
132 final Iterator it = table.getColumnIterator();
133 boolean isFirstColumn = true;
134 while (it.hasNext()) {
135 final Column column = (Column) it.next();
136 column.renderCell(rowIndex, renderer);
137 if (isFirstColumn && table.hasKeysInFirstColumn()) {
138 result.append(" ").append(renderer.getRowHead()).append("\n");
139 isFirstColumn = false;
140 } else {
141 result.append(" ").append(renderer.getTableCell()).append("\n");
142 }
143 }
144 result.append(" </tr>\n");
145 return result.toString();
146 }
147 }