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.HashMap;
29 import java.util.List;
31 import javax.xml.namespace.QName;
35 * @author Ing. František Kučera (frantovo.cz)
37 public class XhtmlFormatter extends AbstractXmlFormatter {
39 public static final String NAME = "xhtml"; // bash-completion:formatter
40 private int resultSetCounter = 0;
41 private int updatesResultCounter = 0;
43 public XhtmlFormatter(FormatterContext formatterContext) {
44 super(addDefaults(formatterContext));
48 * Do not indent text – preserve whitespace for pre elements
50 private static FormatterContext addDefaults(FormatterContext formatterContext) {
51 Properties defaults = new Properties(1);
52 defaults.add(new Property(PROPERTY_INDENT_TEXT, "false"));
53 formatterContext.getProperties().setLastDefaults(defaults);
54 return formatterContext;
58 public void writeStartBatch() {
59 super.writeStartBatch();
61 Map<QName, String> attributes = new HashMap<>(1);
62 attributes.put(qname("xmlns"), Xmlns.XHTML);
63 printStartElement(qname("html"), attributes);
65 printStartElement(qname("head"));
66 printTextElement(qname("title"), null, Constants.PROGRAM_NAME + ": batch results");
69 printStartElement(qname("body"));
73 public void writeEndBatch() {
74 super.writeEndBatch();
81 public void writeStartDatabase(DatabaseDefinition databaseDefinition) {
82 super.writeStartDatabase(databaseDefinition);
83 printTextElement(qname("h1"), null, "Database: " + databaseDefinition.getName());
87 public void writeQuery(String sql) {
88 super.writeQuery(sql);
89 printTextElement(qname("h3"), null, "SQL:");
90 printTextElement(qname("pre"), null, sql);
94 public void writeParameters(List<? extends Parameter> parameters) {
95 super.writeParameters(parameters);
97 if (parameters == null || parameters.isEmpty()) {
98 printTextElement(qname("p"), null, "(this query has no parameters)");
100 printTextElement(qname("h3"), null, "Parameters:");
102 printStartElement(qname("table"));
104 printStartElement(qname("theader"));
105 printStartElement(qname("tr"));
106 printTextElement(qname("td"), null, "number/name");
107 printTextElement(qname("td"), null, "type");
108 printTextElement(qname("td"), null, "value");
112 printStartElement(qname("tbody"));
113 for (int i = 0; i < parameters.size(); i++) {
114 Parameter p = parameters.get(i);
115 printStartElement(qname("tr"));
117 if (p instanceof NamedParameter) {
118 numberOrName = ((NamedParameter) p).getName();
120 numberOrName = String.valueOf(i + 1);
122 printTextElement(qname("td"), null, numberOrName);
123 printTextElement(qname("td"), null, p.getType().name());
124 printTextElement(qname("td"), null, toString(p.getValue()));
134 public void writeColumnsHeader(ColumnsHeader header) {
135 super.writeColumnsHeader(header);
136 printTextElement(qname("h3"), null, "Data:");
137 printStartElement(qname("table"));
138 printStartElement(qname("theader"));
139 printStartElement(qname("tr"));
140 for (ColumnDescriptor cd : header.getColumnDescriptors()) {
142 printTextElement(qname("td"), null, cd.getLabel());
147 printStartElement(qname("tbody"));
151 public void writeStartRow() {
152 super.writeStartRow();
153 printStartElement(qname("tr"));
157 public void writeColumnValue(Object value) {
158 super.writeColumnValue(value);
159 printTextElement(qname("td"), null, toString(value));
163 public void writeEndRow() {
169 public void writeStartResultSet() {
170 super.writeStartResultSet();
172 printTextElement(qname("h2"), null, "Result set " + resultSetCounter);
176 public void writeEndResultSet() {
177 super.writeEndResultSet();
183 public void writeStartUpdatesResult() {
184 super.writeStartUpdatesResult();
185 updatesResultCounter++;
186 printTextElement(qname("h2"), null, "Updates result " + updatesResultCounter);
190 public void writeUpdatedRowsCount(int updatedRowsCount) {
191 super.writeUpdatedRowsCount(updatedRowsCount);
192 printTextElement(qname("p"), null, "Updated rows: " + updatedRowsCount);
195 protected String toString(Object value) {
196 return String.valueOf(value);