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();
62 public void writeStartDatabase(DatabaseDefinition databaseDefinition) {
63 super.writeStartDatabase(databaseDefinition);
64 Map<QName, String> attributes = databaseDefinition.getName() == null ? null : singleAttribute(qname("name"), databaseDefinition.getName());
65 printStartElement(qname("database"), attributes);
69 public void writeEndDatabase() {
70 super.writeEndDatabase();
75 public void writeStartResultSet() {
76 super.writeStartResultSet();
77 printStartElement(qname("resultSet"));
81 public void writeEndResultSet() {
82 super.writeEndResultSet();
87 public void writeQuery(String sql) {
88 super.writeQuery(sql);
89 printTextElement(qname("sql"), null, sql);
93 public void writeParameters(List<? extends Parameter> parameters) {
94 super.writeParameters(parameters);
96 for (Parameter p : notNull(parameters)) {
98 Map<QName, String> attributes = new LinkedHashMap<>(2);
99 if (p instanceof NamedParameter) {
100 attributes.put(qname("name"), ((NamedParameter) p).getName());
102 attributes.put(qname("type"), p.getType().name());
104 printTextElement(qname("parameter"), attributes, String.valueOf(p.getValue()));
110 public void writeColumnsHeader(ColumnsHeader header) {
111 super.writeColumnsHeader(header);
113 for (ColumnDescriptor cd : header.getColumnDescriptors()) {
114 Map<QName, String> attributes = new LinkedHashMap<>(4);
115 attributes.put(qname("label"), cd.getLabel());
116 attributes.put(qname("name"), cd.getName());
117 attributes.put(qname("typeName"), cd.getTypeName());
118 attributes.put(qname("type"), String.valueOf(cd.getType()));
119 printEmptyElement(qname("columnHeader"), attributes);
124 public void writeStartRow() {
125 super.writeStartRow();
126 printStartElement(qname("row"));
130 public void writeColumnValue(Object value) {
131 super.writeColumnValue(value);
133 Map<QName, String> attributes = null;
134 if (labeledColumns) {
135 attributes = new LinkedHashMap<>(1);
136 attributes.put(qname("label"), getCurrentColumnsHeader().getColumnDescriptors().get(getCurrentColumnsCount() - 1).getLabel());
139 printTextElement(qname("column"), attributes, toString(value));
143 public void writeEndRow() {
149 public void writeStartUpdatesResult() {
150 super.writeStartUpdatesResult();
151 printStartElement(qname("updatesResult"));
155 public void writeUpdatedRowsCount(int updatedRowsCount) {
156 super.writeUpdatedRowsCount(updatedRowsCount);
157 printTextElement(qname("updatedRows"), null, String.valueOf(updatedRowsCount));
161 public void writeEndUpdatesResult() {
162 super.writeEndUpdatesResult();
166 protected String toString(Object value) {
167 return String.valueOf(value);