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.sql.Array;
29 import java.sql.SQLException;
30 import java.util.Date;
31 import java.util.HashMap;
32 import java.util.List;
34 import java.util.Scanner;
35 import java.util.logging.Level;
36 import java.util.logging.Logger;
37 import javax.xml.namespace.QName;
41 * @author Ing. František Kučera (frantovo.cz)
43 public class XhtmlFormatter extends AbstractXmlFormatter {
45 private static final Logger log = Logger.getLogger(XhtmlFormatter.class.getName());
46 public static final String NAME = "xhtml"; // bash-completion:formatter
47 private static final String DOCTYPE = "html PUBLIC \"-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN\" \"http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg.dtd\"";
48 private static final String CSS_FILE = "info/globalcode/sql/dk/formatter/XhtmlFormatter.css";
49 private int statementCounter = 0;
50 private int resultSetCounter = 0;
51 private int updatesResultCounter = 0;
53 public XhtmlFormatter(FormatterContext formatterContext) {
54 super(addDefaults(formatterContext));
58 * Do not indent text – preserve whitespace for pre elements
60 private static FormatterContext addDefaults(FormatterContext formatterContext) {
61 Properties defaults = new Properties(1);
62 defaults.add(new Property(PROPERTY_INDENT_TEXT, "false"));
63 formatterContext.getProperties().setLastDefaults(defaults);
64 return formatterContext;
68 public void writeStartBatch() {
69 super.writeStartBatch();
71 printDoctype(DOCTYPE);
72 Map<QName, String> attributes = new HashMap<>(1);
73 attributes.put(qname("xmlns"), Xmlns.XHTML);
74 printStartElement(qname("html"), attributes);
76 printStartElement(qname("head"));
77 printTextElement(qname("title"), null, Constants.PROGRAM_NAME + ": batch results");
81 printStartElement(qname("body"));
84 private void printCss() {
86 try (Scanner css = new Scanner(getClass().getClassLoader().getResourceAsStream(CSS_FILE))) {
87 Map<QName, String> attributes = new HashMap<>(1);
88 attributes.put(qname("type"), "text/css");
89 printStartElement(qname("style"), attributes);
90 while (css.hasNext()) {
91 printText(css.nextLine(), true);
98 public void writeEndBatch() {
99 super.writeEndBatch();
106 public void writeStartDatabase(DatabaseDefinition databaseDefinition) {
107 super.writeStartDatabase(databaseDefinition);
108 printTextElement(qname("h1"), null, "Database: " + databaseDefinition.getName());
110 printStartElement(qname("p"));
111 printText("This is XHTML output of batch executed at: ", true);
112 printText(new Date().toString(), true);
117 public void writeQuery(String sql) {
118 super.writeQuery(sql);
119 printTextElement(qname("pre"), null, sql);
123 public void writeParameters(List<? extends Parameter> parameters) {
124 super.writeParameters(parameters);
126 if (parameters == null || parameters.isEmpty()) {
127 printTextElement(qname("p"), null, "(this query has no parameters)");
129 printTextElement(qname("h3"), null, "Parameters:");
131 printStartElement(qname("table"));
133 printStartElement(qname("thead"));
134 printStartElement(qname("tr"));
135 printTextElement(qname("td"), null, "id");
136 printTextElement(qname("td"), null, "type");
137 printTextElement(qname("td"), null, "value");
141 printStartElement(qname("tbody"));
142 for (int i = 0; i < parameters.size(); i++) {
143 Parameter p = parameters.get(i);
144 printStartElement(qname("tr"));
146 if (p instanceof NamedParameter) {
147 numberOrName = ((NamedParameter) p).getName();
149 numberOrName = String.valueOf(i + 1);
151 printTextElement(qname("td"), null, numberOrName);
152 printTextElement(qname("td"), null, p.getType().name());
153 printTableData(p.getValue());
162 private void printTableData(Object value) {
164 if (value instanceof Array) {
165 Array sqlArray = (Array) value;
167 Object[] array = (Object[]) sqlArray.getArray();
168 printStartElement(qname("td"));
171 } catch (SQLException e) {
172 log.log(Level.SEVERE, "Unable to format array", e);
173 printTableData(String.valueOf(value));
176 Map<QName, String> attributes = new HashMap<>(1);
177 if (value instanceof Number) {
178 attributes.put(qname("class"), "number");
179 } else if (value instanceof Boolean) {
180 attributes.put(qname("class"), "boolean");
182 printTextElement(qname("td"), attributes, String.valueOf(value));
186 private void printArray(Object[] array) {
187 printStartElement(qname("ul"));
188 for (Object o : array) {
189 if (o instanceof Object[]) {
190 printStartElement(qname("li"));
191 printTextElement(qname("p"), null, "nested array:");
192 printArray((Object[]) o);
195 printTextElement(qname("li"), null, String.valueOf(o));
202 public void writeStartResultSet(ColumnsHeader header) {
203 super.writeStartResultSet(header);
205 printEmptyElement(qname("hr"), null);
206 printTextElement(qname("h3"), null, "Result set #" + resultSetCounter);
207 printStartElement(qname("table"));
208 printStartElement(qname("thead"));
209 printStartElement(qname("tr"));
210 for (ColumnDescriptor cd : header.getColumnDescriptors()) {
212 printTextElement(qname("td"), null, cd.getLabel());
217 printStartElement(qname("tbody"));
221 public void writeEndResultSet() {
222 super.writeEndResultSet();
225 printTextElement(qname("p"), null, "Record count: " + getCurrentRowCount());
229 public void writeStartRow() {
230 super.writeStartRow();
231 printStartElement(qname("tr"));
235 public void writeColumnValue(Object value) {
236 super.writeColumnValue(value);
237 printTableData(value);
241 public void writeEndRow() {
247 public void writeStartStatement() {
248 super.writeStartStatement();
250 printEmptyElement(qname("hr"), null);
251 printTextElement(qname("h2"), null, "SQL statement #" + statementCounter);
252 resultSetCounter = 0;
253 updatesResultCounter = 0;
257 public void writeUpdatesResult(int updatedRowsCount) {
258 super.writeUpdatesResult(updatedRowsCount);
259 updatesResultCounter++;
260 printEmptyElement(qname("hr"), null);
261 printTextElement(qname("h3"), null, "Updates result #" + updatesResultCounter);
262 printTextElement(qname("p"), null, "Updated rows: " + updatedRowsCount);