1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
| import java.beans.PropertyDescriptor; import java.lang.reflect.Method; import java.util.Collection; import java.util.LinkedHashMap; import java.util.Map;
public class HTMLGenerator extends LinkedHashMap<String, String> { private static final long serialVersionUID = -2844894776581720941L;
private Collection<?> collection;
public HTMLGenerator(Collection<?> collection) { super(); this.collection = collection; }
public Collection<?> getCollection() { return collection; }
public void setCollection(Collection<?> collection) { this.collection = collection; }
private static final String DEFAULT_TABLE_STYLE = "<style>.pure-table{text-align:center;border-collapse:collapse;border-spacing:0;empty-cells:show;border:1px solid #cbcbcb}.pure-table caption{color:#000;font:italic 85%/1 arial,sans-serif;padding:1em 0;text-align:center}.pure-table td,.pure-table th{border-left:1px solid #cbcbcb;border-width:0 0 0 1px;font-size:inherit;margin:0;overflow:visible;padding:.5em 1em}.pure-table thead{background-color:#e0e0e0;color:#000;text-align:left;vertical-align:bottom}.pure-table td{background-color:transparent}</style>";
public String style = ""; public String generateTable() { return generateTable(null, null, null); } public String generateTable(String tableStyle) { return generateTable(tableStyle, null, null); }
public String generateTable(String tableStyle, Map<Integer,String> columnsStyles, Map<Integer,RowColumnPair> cellStyles) { StringBuilder content = new StringBuilder(); content.append(DEFAULT_TABLE_STYLE); if(tableStyle == null) { content.append("<table class=\"pure-table\">"); } else { content.append(tableStyle); } content.append("<tr>"); for(String key:this.keySet()) { content.append("<td><h4>").append(get(key)).append("</h4>"); } content.append("</tr>"); int row = 0; for(Object element : collection) { content.append("<tr>"); for(String key:this.keySet()) { content.append("<td>").append(getFieldValueByName(element, key)).append("</td>"); } content.append("</tr>"); row++; } content.append("</table>"); return content.toString(); }
private String getFieldValueByName(Object o, String fieldName) { try { PropertyDescriptor pd = new PropertyDescriptor(fieldName, o.getClass()); Method getMethod = pd.getReadMethod(); Object obj = getMethod.invoke(o); return null == obj ? null : obj.toString(); } catch (Exception e) { return null; }
}
public class RowColumnPair {
private String columnField;
private String style;
public RowColumnPair(String columnField, String style) { super(); this.columnField = columnField; this.style = style; }
public String getColumnField() { return columnField; }
public void setColumnField(String columnField) { this.columnField = columnField; }
public String getStyle() { return style; }
public void setStyle(String style) { this.style = style; } } }
|