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
38 public static final String PROPERTY_LABELED_COLUMNS = "labeledColumns";
39 private final boolean labeledColumns;
41 public XmlFormatter(FormatterContext formatterContext) {
42 super(formatterContext);
43 labeledColumns = formatterContext.getProperties().getBoolean(PROPERTY_LABELED_COLUMNS, false);
47 public void writeStartBatch() {
48 super.writeStartBatch();
50 printStartElement(qname("batchResults"), singleAttribute(qname("xmlns"), Xmlns.BATCH_RESULT));
54 public void writeEndBatch() {
55 super.writeEndBatch();
61 public void writeStartDatabase(DatabaseDefinition databaseDefinition) {
62 super.writeStartDatabase(databaseDefinition);
63 Map<QName, String> attributes = databaseDefinition.getName() == null ? null : singleAttribute(qname("name"), databaseDefinition.getName());
64 printStartElement(qname("database"), attributes);
68 public void writeEndDatabase() {
69 super.writeEndDatabase();
74 public void writeStartResultSet() {
75 super.writeStartResultSet();
76 printStartElement(qname("resultSet"));
80 public void writeEndResultSet() {
81 super.writeEndResultSet();
86 public void writeQuery(String sql) {
87 super.writeQuery(sql);
88 printTextElement(qname("sql"), null, sql);
92 public void writeParameters(List<? extends Parameter> parameters) {
93 super.writeParameters(parameters);
95 for (Parameter p : notNull(parameters)) {
97 Map<QName, String> attributes = new LinkedHashMap<>(2);
98 if (p instanceof NamedParameter) {
99 attributes.put(qname("name"), ((NamedParameter) p).getName());
101 attributes.put(qname("type"), p.getType().name());
103 printTextElement(qname("parameter"), attributes, String.valueOf(p.getValue()));
109 public void writeColumnsHeader(ColumnsHeader header) {
110 super.writeColumnsHeader(header);
112 for (ColumnDescriptor cd : header.getColumnDescriptors()) {
113 Map<QName, String> attributes = new LinkedHashMap<>(4);
114 attributes.put(qname("label"), cd.getLabel());
115 attributes.put(qname("name"), cd.getName());
116 attributes.put(qname("typeName"), cd.getTypeName());
117 attributes.put(qname("type"), String.valueOf(cd.getType()));
118 printEmptyElement(qname("columnHeader"), attributes);
123 public void writeStartRow() {
124 super.writeStartRow();
125 printStartElement(qname("row"));
129 public void writeColumnValue(Object value) {
130 super.writeColumnValue(value);
132 Map<QName, String> attributes = null;
133 if (labeledColumns) {
134 attributes = new LinkedHashMap<>(1);
135 attributes.put(qname("label"), getCurrentColumnsHeader().getColumnDescriptors().get(getCurrentColumnsCount() - 1).getLabel());
138 printTextElement(qname("column"), attributes, toString(value));
142 public void writeEndRow() {
148 public void writeStartUpdatesResult() {
149 super.writeStartUpdatesResult();
150 printStartElement(qname("updatesResult"));
154 public void writeUpdatedRowsCount(int updatedRowsCount) {
155 super.writeUpdatedRowsCount(updatedRowsCount);
156 printTextElement(qname("updatedRows"), null, String.valueOf(updatedRowsCount));
160 public void writeEndUpdatesResult() {
161 super.writeEndUpdatesResult();
165 protected String toString(Object value) {
166 return String.valueOf(value);