3 * Copyright © 2014 František Kučera (frantovo.cz)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package info.globalcode.sql.dk.formatting;
20 import info.globalcode.sql.dk.Constants;
21 import info.globalcode.sql.dk.NamedParameter;
22 import info.globalcode.sql.dk.Parameter;
23 import info.globalcode.sql.dk.Xmlns;
24 import info.globalcode.sql.dk.configuration.DatabaseDefinition;
25 import info.globalcode.sql.dk.configuration.Properties;
26 import info.globalcode.sql.dk.configuration.Property;
27 import static info.globalcode.sql.dk.formatting.AbstractXmlFormatter.qname;
28 import java.util.Date;
29 import java.util.HashMap;
30 import java.util.List;
32 import java.util.Scanner;
33 import javax.xml.namespace.QName;
37 * @author Ing. František Kučera (frantovo.cz)
39 public class XhtmlFormatter extends AbstractXmlFormatter {
41 public static final String NAME = "xhtml"; // bash-completion:formatter
42 private static final String CSS_FILE = "info/globalcode/sql/dk/formatter/XhtmlFormatter.css";
43 private int resultSetCounter = 0;
44 private int updatesResultCounter = 0;
46 public XhtmlFormatter(FormatterContext formatterContext) {
47 super(addDefaults(formatterContext));
51 * Do not indent text – preserve whitespace for pre elements
53 private static FormatterContext addDefaults(FormatterContext formatterContext) {
54 Properties defaults = new Properties(1);
55 defaults.add(new Property(PROPERTY_INDENT_TEXT, "false"));
56 formatterContext.getProperties().setLastDefaults(defaults);
57 return formatterContext;
61 public void writeStartBatch() {
62 super.writeStartBatch();
64 Map<QName, String> attributes = new HashMap<>(1);
65 attributes.put(qname("xmlns"), Xmlns.XHTML);
66 printStartElement(qname("html"), attributes);
68 printStartElement(qname("head"));
69 printTextElement(qname("title"), null, Constants.PROGRAM_NAME + ": batch results");
73 printStartElement(qname("body"));
76 private void printCss() {
78 try (Scanner css = new Scanner(getClass().getClassLoader().getResourceAsStream(CSS_FILE))) {
79 Map<QName, String> attributes = new HashMap<>(1);
80 attributes.put(qname("type"), "text/css");
81 printStartElement(qname("style"), attributes);
82 while (css.hasNext()) {
83 printText(css.nextLine(), true);
90 public void writeEndBatch() {
91 super.writeEndBatch();
98 public void writeStartDatabase(DatabaseDefinition databaseDefinition) {
99 super.writeStartDatabase(databaseDefinition);
100 printTextElement(qname("h1"), null, "Database: " + databaseDefinition.getName());
102 printStartElement(qname("p"));
103 printText("This is XHTML output of batch executed at: ", true);
104 printText(new Date().toString(), true);
109 public void writeQuery(String sql) {
110 super.writeQuery(sql);
111 printTextElement(qname("h3"), null, "SQL:");
112 printTextElement(qname("pre"), null, sql);
116 public void writeParameters(List<? extends Parameter> parameters) {
117 super.writeParameters(parameters);
119 if (parameters == null || parameters.isEmpty()) {
120 printTextElement(qname("p"), null, "(this query has no parameters)");
122 printTextElement(qname("h3"), null, "Parameters:");
124 printStartElement(qname("table"));
126 printStartElement(qname("thead"));
127 printStartElement(qname("tr"));
128 printTextElement(qname("td"), null, "id");
129 printTextElement(qname("td"), null, "type");
130 printTextElement(qname("td"), null, "value");
134 printStartElement(qname("tbody"));
135 for (int i = 0; i < parameters.size(); i++) {
136 Parameter p = parameters.get(i);
137 printStartElement(qname("tr"));
139 if (p instanceof NamedParameter) {
140 numberOrName = ((NamedParameter) p).getName();
142 numberOrName = String.valueOf(i + 1);
144 printTextElement(qname("td"), null, numberOrName);
145 printTextElement(qname("td"), null, p.getType().name());
146 printTableData(p.getValue());
155 private void printTableData(Object value) {
156 Map<QName, String> attributes = new HashMap<>(1);
157 if (value instanceof Number || value instanceof Boolean) {
158 attributes.put(qname("class"), "number");
160 printTextElement(qname("td"), attributes, String.valueOf(value));
164 public void writeColumnsHeader(ColumnsHeader header) {
165 super.writeColumnsHeader(header);
166 printTextElement(qname("h3"), null, "Data:");
167 printStartElement(qname("table"));
168 printStartElement(qname("thead"));
169 printStartElement(qname("tr"));
170 for (ColumnDescriptor cd : header.getColumnDescriptors()) {
172 printTextElement(qname("td"), null, cd.getLabel());
177 printStartElement(qname("tbody"));
181 public void writeStartRow() {
182 super.writeStartRow();
183 printStartElement(qname("tr"));
187 public void writeColumnValue(Object value) {
188 super.writeColumnValue(value);
189 printTableData(value);
193 public void writeEndRow() {
199 public void writeStartResultSet() {
200 super.writeStartResultSet();
202 printEmptyElement(qname("hr"), null);
203 printTextElement(qname("h2"), null, "Result set #" + resultSetCounter);
207 public void writeEndResultSet() {
208 super.writeEndResultSet();
212 printTextElement(qname("p"), null, "Record count: " + getCurrentRowCount());
216 public void writeStartUpdatesResult() {
217 super.writeStartUpdatesResult();
218 updatesResultCounter++;
219 printEmptyElement(qname("hr"), null);
220 printTextElement(qname("h2"), null, "Updates result #" + updatesResultCounter);
224 public void writeUpdatedRowsCount(int updatedRowsCount) {
225 super.writeUpdatedRowsCount(updatedRowsCount);
226 printTextElement(qname("p"), null, "Updated rows: " + updatedRowsCount);