3 * Copyright © 2013 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.Parameter;
21 import info.globalcode.sql.dk.Xmlns;
22 import info.globalcode.sql.dk.configuration.DatabaseDefinition;
23 import static info.globalcode.sql.dk.formatting.AbstractXmlFormatter.qname;
24 import static info.globalcode.sql.dk.Functions.notNull;
25 import info.globalcode.sql.dk.NamedParameter;
26 import java.util.LinkedHashMap;
27 import java.util.List;
29 import javax.xml.namespace.QName;
33 * @author Ing. František Kučera (frantovo.cz)
35 public class XmlFormatter extends AbstractXmlFormatter {
37 public static final String NAME = "xml"; // bash-completion:formatter
39 public XmlFormatter(FormatterContext formatterContext) {
40 super(formatterContext);
44 public void writeStartBatch() {
45 super.writeStartBatch();
47 printStartElement(qname("batchResults"), singleAttribute(qname("xmlns"), Xmlns.BATCH_RESULT));
51 public void writeEndBatch() {
52 super.writeEndBatch();
59 public void writeStartDatabase(DatabaseDefinition databaseDefinition) {
60 super.writeStartDatabase(databaseDefinition);
61 Map<QName, String> attributes = databaseDefinition.getName() == null ? null : singleAttribute(qname("name"), databaseDefinition.getName());
62 printStartElement(qname("database"), attributes);
66 public void writeEndDatabase() {
67 super.writeEndDatabase();
72 public void writeStartResultSet() {
73 super.writeStartResultSet();
74 printStartElement(qname("resultSet"));
78 public void writeEndResultSet() {
79 super.writeEndResultSet();
84 public void writeQuery(String sql) {
85 super.writeQuery(sql);
86 printTextElement(qname("sql"), null, sql);
90 public void writeParameters(List<? extends Parameter> parameters) {
91 super.writeParameters(parameters);
93 for (Parameter p : notNull(parameters)) {
95 Map<QName, String> attributes = new LinkedHashMap<>(2);
96 if (p instanceof NamedParameter) {
97 attributes.put(qname("name"), ((NamedParameter) p).getName());
99 attributes.put(qname("type"), p.getType().name());
101 printTextElement(qname("parameter"), attributes, String.valueOf(p.getValue()));
107 public void writeColumnsHeader(ColumnsHeader header) {
108 super.writeColumnsHeader(header);
110 for (ColumnDescriptor cd : header.getColumnDescriptors()) {
111 Map<QName, String> attributes = new LinkedHashMap<>(4);
112 attributes.put(qname("label"), cd.getLabel());
113 attributes.put(qname("name"), cd.getName());
114 attributes.put(qname("typeName"), cd.getTypeName());
115 attributes.put(qname("type"), String.valueOf(cd.getType()));
116 printEmptyElement(qname("columnHeader"), attributes);