franta-hg@128
|
1 |
/**
|
franta-hg@128
|
2 |
* SQL-DK
|
franta-hg@128
|
3 |
* Copyright © 2014 František Kučera (frantovo.cz)
|
franta-hg@128
|
4 |
*
|
franta-hg@128
|
5 |
* This program is free software: you can redistribute it and/or modify
|
franta-hg@128
|
6 |
* it under the terms of the GNU General Public License as published by
|
franta-hg@128
|
7 |
* the Free Software Foundation, either version 3 of the License, or
|
franta-hg@128
|
8 |
* (at your option) any later version.
|
franta-hg@128
|
9 |
*
|
franta-hg@128
|
10 |
* This program is distributed in the hope that it will be useful,
|
franta-hg@128
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
franta-hg@128
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
franta-hg@128
|
13 |
* GNU General Public License for more details.
|
franta-hg@128
|
14 |
*
|
franta-hg@128
|
15 |
* You should have received a copy of the GNU General Public License
|
franta-hg@128
|
16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
franta-hg@128
|
17 |
*/
|
franta-hg@128
|
18 |
package info.globalcode.sql.dk.formatting;
|
franta-hg@128
|
19 |
|
franta-hg@134
|
20 |
import info.globalcode.sql.dk.Constants;
|
franta-hg@134
|
21 |
import info.globalcode.sql.dk.NamedParameter;
|
franta-hg@134
|
22 |
import info.globalcode.sql.dk.Parameter;
|
franta-hg@134
|
23 |
import info.globalcode.sql.dk.Xmlns;
|
franta-hg@134
|
24 |
import info.globalcode.sql.dk.configuration.DatabaseDefinition;
|
franta-hg@134
|
25 |
import info.globalcode.sql.dk.configuration.Properties;
|
franta-hg@134
|
26 |
import info.globalcode.sql.dk.configuration.Property;
|
franta-hg@134
|
27 |
import static info.globalcode.sql.dk.formatting.AbstractXmlFormatter.qname;
|
franta-hg@138
|
28 |
import java.sql.Array;
|
franta-hg@138
|
29 |
import java.sql.SQLException;
|
franta-hg@135
|
30 |
import java.util.Date;
|
franta-hg@134
|
31 |
import java.util.List;
|
franta-hg@134
|
32 |
import java.util.Map;
|
franta-hg@135
|
33 |
import java.util.Scanner;
|
franta-hg@138
|
34 |
import java.util.logging.Level;
|
franta-hg@138
|
35 |
import java.util.logging.Logger;
|
franta-hg@134
|
36 |
import javax.xml.namespace.QName;
|
franta-hg@134
|
37 |
|
franta-hg@128
|
38 |
/**
|
franta-hg@155
|
39 |
* Prints result sets and parameters as tables, SQL as preformatted and updates counts as
|
franta-hg@155
|
40 |
* paragraphs. You can pick XHTML fragments (usually tabular data) and use it on your website or use
|
franta-hg@155
|
41 |
* whole output as preview or report.
|
franta-hg@128
|
42 |
*
|
franta-hg@128
|
43 |
* @author Ing. František Kučera (frantovo.cz)
|
franta-hg@128
|
44 |
*/
|
franta-hg@128
|
45 |
public class XhtmlFormatter extends AbstractXmlFormatter {
|
franta-hg@128
|
46 |
|
franta-hg@138
|
47 |
private static final Logger log = Logger.getLogger(XhtmlFormatter.class.getName());
|
franta-hg@128
|
48 |
public static final String NAME = "xhtml"; // bash-completion:formatter
|
franta-hg@136
|
49 |
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\"";
|
franta-hg@135
|
50 |
private static final String CSS_FILE = "info/globalcode/sql/dk/formatter/XhtmlFormatter.css";
|
franta-hg@142
|
51 |
private int statementCounter = 0;
|
franta-hg@134
|
52 |
private int resultSetCounter = 0;
|
franta-hg@134
|
53 |
private int updatesResultCounter = 0;
|
franta-hg@128
|
54 |
|
franta-hg@128
|
55 |
public XhtmlFormatter(FormatterContext formatterContext) {
|
franta-hg@134
|
56 |
super(addDefaults(formatterContext));
|
franta-hg@134
|
57 |
}
|
franta-hg@134
|
58 |
|
franta-hg@134
|
59 |
/**
|
franta-hg@134
|
60 |
* Do not indent text – preserve whitespace for pre elements
|
franta-hg@134
|
61 |
*/
|
franta-hg@134
|
62 |
private static FormatterContext addDefaults(FormatterContext formatterContext) {
|
franta-hg@134
|
63 |
Properties defaults = new Properties(1);
|
franta-hg@134
|
64 |
defaults.add(new Property(PROPERTY_INDENT_TEXT, "false"));
|
franta-hg@134
|
65 |
formatterContext.getProperties().setLastDefaults(defaults);
|
franta-hg@134
|
66 |
return formatterContext;
|
franta-hg@134
|
67 |
}
|
franta-hg@134
|
68 |
|
franta-hg@134
|
69 |
@Override
|
franta-hg@134
|
70 |
public void writeStartBatch() {
|
franta-hg@134
|
71 |
super.writeStartBatch();
|
franta-hg@134
|
72 |
printStartDocument();
|
franta-hg@136
|
73 |
printDoctype(DOCTYPE);
|
franta-hg@152
|
74 |
printStartElement(qname("html"), singleAttribute(qname("xmlns"), Xmlns.XHTML));
|
franta-hg@134
|
75 |
|
franta-hg@134
|
76 |
printStartElement(qname("head"));
|
franta-hg@134
|
77 |
printTextElement(qname("title"), null, Constants.PROGRAM_NAME + ": batch results");
|
franta-hg@135
|
78 |
printCss();
|
franta-hg@134
|
79 |
printEndElement();
|
franta-hg@134
|
80 |
|
franta-hg@134
|
81 |
printStartElement(qname("body"));
|
franta-hg@134
|
82 |
}
|
franta-hg@134
|
83 |
|
franta-hg@135
|
84 |
private void printCss() {
|
franta-hg@135
|
85 |
|
franta-hg@135
|
86 |
try (Scanner css = new Scanner(getClass().getClassLoader().getResourceAsStream(CSS_FILE))) {
|
franta-hg@152
|
87 |
printStartElement(qname("style"), singleAttribute(qname("type"), "text/css"));
|
franta-hg@135
|
88 |
while (css.hasNext()) {
|
franta-hg@135
|
89 |
printText(css.nextLine(), true);
|
franta-hg@135
|
90 |
}
|
franta-hg@135
|
91 |
printEndElement();
|
franta-hg@135
|
92 |
}
|
franta-hg@135
|
93 |
}
|
franta-hg@135
|
94 |
|
franta-hg@134
|
95 |
@Override
|
franta-hg@134
|
96 |
public void writeEndBatch() {
|
franta-hg@134
|
97 |
super.writeEndBatch();
|
franta-hg@134
|
98 |
printEndElement();
|
franta-hg@134
|
99 |
printEndElement();
|
franta-hg@134
|
100 |
printEndDocument();
|
franta-hg@134
|
101 |
}
|
franta-hg@134
|
102 |
|
franta-hg@134
|
103 |
@Override
|
franta-hg@134
|
104 |
public void writeStartDatabase(DatabaseDefinition databaseDefinition) {
|
franta-hg@134
|
105 |
super.writeStartDatabase(databaseDefinition);
|
franta-hg@134
|
106 |
printTextElement(qname("h1"), null, "Database: " + databaseDefinition.getName());
|
franta-hg@135
|
107 |
|
franta-hg@135
|
108 |
printStartElement(qname("p"));
|
franta-hg@135
|
109 |
printText("This is XHTML output of batch executed at: ", true);
|
franta-hg@135
|
110 |
printText(new Date().toString(), true);
|
franta-hg@135
|
111 |
printEndElement();
|
franta-hg@134
|
112 |
}
|
franta-hg@134
|
113 |
|
franta-hg@134
|
114 |
@Override
|
franta-hg@134
|
115 |
public void writeQuery(String sql) {
|
franta-hg@134
|
116 |
super.writeQuery(sql);
|
franta-hg@134
|
117 |
printTextElement(qname("pre"), null, sql);
|
franta-hg@134
|
118 |
}
|
franta-hg@134
|
119 |
|
franta-hg@134
|
120 |
@Override
|
franta-hg@134
|
121 |
public void writeParameters(List<? extends Parameter> parameters) {
|
franta-hg@134
|
122 |
super.writeParameters(parameters);
|
franta-hg@134
|
123 |
|
franta-hg@134
|
124 |
if (parameters == null || parameters.isEmpty()) {
|
franta-hg@134
|
125 |
printTextElement(qname("p"), null, "(this query has no parameters)");
|
franta-hg@134
|
126 |
} else {
|
franta-hg@134
|
127 |
printTextElement(qname("h3"), null, "Parameters:");
|
franta-hg@134
|
128 |
|
franta-hg@134
|
129 |
printStartElement(qname("table"));
|
franta-hg@134
|
130 |
|
franta-hg@135
|
131 |
printStartElement(qname("thead"));
|
franta-hg@134
|
132 |
printStartElement(qname("tr"));
|
franta-hg@135
|
133 |
printTextElement(qname("td"), null, "id");
|
franta-hg@134
|
134 |
printTextElement(qname("td"), null, "type");
|
franta-hg@134
|
135 |
printTextElement(qname("td"), null, "value");
|
franta-hg@134
|
136 |
printEndElement();
|
franta-hg@134
|
137 |
printEndElement();
|
franta-hg@134
|
138 |
|
franta-hg@134
|
139 |
printStartElement(qname("tbody"));
|
franta-hg@134
|
140 |
for (int i = 0; i < parameters.size(); i++) {
|
franta-hg@134
|
141 |
Parameter p = parameters.get(i);
|
franta-hg@134
|
142 |
printStartElement(qname("tr"));
|
franta-hg@134
|
143 |
String numberOrName;
|
franta-hg@134
|
144 |
if (p instanceof NamedParameter) {
|
franta-hg@134
|
145 |
numberOrName = ((NamedParameter) p).getName();
|
franta-hg@134
|
146 |
} else {
|
franta-hg@134
|
147 |
numberOrName = String.valueOf(i + 1);
|
franta-hg@134
|
148 |
}
|
franta-hg@134
|
149 |
printTextElement(qname("td"), null, numberOrName);
|
franta-hg@134
|
150 |
printTextElement(qname("td"), null, p.getType().name());
|
franta-hg@135
|
151 |
printTableData(p.getValue());
|
franta-hg@134
|
152 |
printEndElement();
|
franta-hg@134
|
153 |
}
|
franta-hg@134
|
154 |
printEndElement();
|
franta-hg@134
|
155 |
|
franta-hg@134
|
156 |
printEndElement();
|
franta-hg@134
|
157 |
}
|
franta-hg@134
|
158 |
}
|
franta-hg@134
|
159 |
|
franta-hg@135
|
160 |
private void printTableData(Object value) {
|
franta-hg@138
|
161 |
|
franta-hg@138
|
162 |
if (value instanceof Array) {
|
franta-hg@138
|
163 |
Array sqlArray = (Array) value;
|
franta-hg@138
|
164 |
try {
|
franta-hg@138
|
165 |
Object[] array = (Object[]) sqlArray.getArray();
|
franta-hg@138
|
166 |
printStartElement(qname("td"));
|
franta-hg@138
|
167 |
printArray(array);
|
franta-hg@138
|
168 |
printEndElement();
|
franta-hg@138
|
169 |
} catch (SQLException e) {
|
franta-hg@138
|
170 |
log.log(Level.SEVERE, "Unable to format array", e);
|
franta-hg@138
|
171 |
printTableData(String.valueOf(value));
|
franta-hg@138
|
172 |
}
|
franta-hg@138
|
173 |
} else {
|
franta-hg@152
|
174 |
Map<QName, String> attributes = null;
|
franta-hg@138
|
175 |
if (value instanceof Number) {
|
franta-hg@152
|
176 |
attributes = singleAttribute(qname("class"), "number");
|
franta-hg@138
|
177 |
} else if (value instanceof Boolean) {
|
franta-hg@152
|
178 |
attributes = singleAttribute(qname("class"), "boolean");
|
franta-hg@138
|
179 |
}
|
franta-hg@138
|
180 |
printTextElement(qname("td"), attributes, String.valueOf(value));
|
franta-hg@135
|
181 |
}
|
franta-hg@138
|
182 |
}
|
franta-hg@138
|
183 |
|
franta-hg@138
|
184 |
private void printArray(Object[] array) {
|
franta-hg@138
|
185 |
printStartElement(qname("ul"));
|
franta-hg@138
|
186 |
for (Object o : array) {
|
franta-hg@138
|
187 |
if (o instanceof Object[]) {
|
franta-hg@138
|
188 |
printStartElement(qname("li"));
|
franta-hg@138
|
189 |
printTextElement(qname("p"), null, "nested array:");
|
franta-hg@138
|
190 |
printArray((Object[]) o);
|
franta-hg@138
|
191 |
printEndElement();
|
franta-hg@138
|
192 |
} else {
|
franta-hg@138
|
193 |
printTextElement(qname("li"), null, String.valueOf(o));
|
franta-hg@138
|
194 |
}
|
franta-hg@138
|
195 |
}
|
franta-hg@138
|
196 |
printEndElement();
|
franta-hg@135
|
197 |
}
|
franta-hg@135
|
198 |
|
franta-hg@134
|
199 |
@Override
|
franta-hg@142
|
200 |
public void writeStartResultSet(ColumnsHeader header) {
|
franta-hg@142
|
201 |
super.writeStartResultSet(header);
|
franta-hg@142
|
202 |
resultSetCounter++;
|
franta-hg@142
|
203 |
printEmptyElement(qname("hr"), null);
|
franta-hg@142
|
204 |
printTextElement(qname("h3"), null, "Result set #" + resultSetCounter);
|
franta-hg@134
|
205 |
printStartElement(qname("table"));
|
franta-hg@135
|
206 |
printStartElement(qname("thead"));
|
franta-hg@134
|
207 |
printStartElement(qname("tr"));
|
franta-hg@134
|
208 |
for (ColumnDescriptor cd : header.getColumnDescriptors()) {
|
franta-hg@134
|
209 |
// TODO: type
|
franta-hg@134
|
210 |
printTextElement(qname("td"), null, cd.getLabel());
|
franta-hg@134
|
211 |
}
|
franta-hg@134
|
212 |
printEndElement();
|
franta-hg@134
|
213 |
printEndElement();
|
franta-hg@134
|
214 |
|
franta-hg@134
|
215 |
printStartElement(qname("tbody"));
|
franta-hg@134
|
216 |
}
|
franta-hg@134
|
217 |
|
franta-hg@134
|
218 |
@Override
|
franta-hg@142
|
219 |
public void writeEndResultSet() {
|
franta-hg@142
|
220 |
super.writeEndResultSet();
|
franta-hg@142
|
221 |
printEndElement();
|
franta-hg@142
|
222 |
printEndElement();
|
franta-hg@142
|
223 |
printTextElement(qname("p"), null, "Record count: " + getCurrentRowCount());
|
franta-hg@142
|
224 |
}
|
franta-hg@142
|
225 |
|
franta-hg@142
|
226 |
@Override
|
franta-hg@134
|
227 |
public void writeStartRow() {
|
franta-hg@134
|
228 |
super.writeStartRow();
|
franta-hg@134
|
229 |
printStartElement(qname("tr"));
|
franta-hg@134
|
230 |
}
|
franta-hg@134
|
231 |
|
franta-hg@134
|
232 |
@Override
|
franta-hg@134
|
233 |
public void writeColumnValue(Object value) {
|
franta-hg@134
|
234 |
super.writeColumnValue(value);
|
franta-hg@135
|
235 |
printTableData(value);
|
franta-hg@134
|
236 |
}
|
franta-hg@134
|
237 |
|
franta-hg@134
|
238 |
@Override
|
franta-hg@134
|
239 |
public void writeEndRow() {
|
franta-hg@134
|
240 |
super.writeEndRow();
|
franta-hg@134
|
241 |
printEndElement();
|
franta-hg@134
|
242 |
}
|
franta-hg@134
|
243 |
|
franta-hg@134
|
244 |
@Override
|
franta-hg@142
|
245 |
public void writeStartStatement() {
|
franta-hg@142
|
246 |
super.writeStartStatement();
|
franta-hg@142
|
247 |
statementCounter++;
|
franta-hg@135
|
248 |
printEmptyElement(qname("hr"), null);
|
franta-hg@142
|
249 |
printTextElement(qname("h2"), null, "SQL statement #" + statementCounter);
|
franta-hg@142
|
250 |
resultSetCounter = 0;
|
franta-hg@142
|
251 |
updatesResultCounter = 0;
|
franta-hg@134
|
252 |
}
|
franta-hg@134
|
253 |
|
franta-hg@134
|
254 |
@Override
|
franta-hg@142
|
255 |
public void writeUpdatesResult(int updatedRowsCount) {
|
franta-hg@142
|
256 |
super.writeUpdatesResult(updatedRowsCount);
|
franta-hg@134
|
257 |
updatesResultCounter++;
|
franta-hg@135
|
258 |
printEmptyElement(qname("hr"), null);
|
franta-hg@142
|
259 |
printTextElement(qname("h3"), null, "Updates result #" + updatesResultCounter);
|
franta-hg@134
|
260 |
printTextElement(qname("p"), null, "Updated rows: " + updatedRowsCount);
|
franta-hg@134
|
261 |
}
|
franta-hg@128
|
262 |
}
|