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.Functions.notNull;
24 import info.globalcode.sql.dk.NamedParameter;
25 import info.globalcode.sql.dk.configuration.PropertyDeclaration;
26 import static info.globalcode.sql.dk.formatting.AbstractXmlFormatter.qname;
27 import java.sql.Array;
28 import java.sql.SQLException;
29 import java.util.LinkedHashMap;
30 import java.util.List;
32 import java.util.logging.Level;
33 import java.util.logging.Logger;
34 import javax.xml.namespace.QName;
38 * Prints machine-readable output – XML document containing resultsets and updates count. Good
39 * choice for further processing – e.g. XSL transformation.</p>
44 * @author Ing. František Kučera (frantovo.cz)
46 @PropertyDeclaration(name = XmlFormatter.PROPERTY_LABELED_COLUMNS, defaultValue = "false", type = Boolean.class, description = "whether to add 'label' attribute to each 'column' element")
47 public class XmlFormatter extends AbstractXmlFormatter {
49 public static final String NAME = "xml"; // bash-completion:formatter
50 public static final String PROPERTY_LABELED_COLUMNS = "labeledColumns";
51 private static final Logger log = Logger.getLogger(XmlFormatter.class.getName());
52 private final boolean labeledColumns;
54 public XmlFormatter(FormatterContext formatterContext) {
55 super(formatterContext);
56 labeledColumns = formatterContext.getProperties().getBoolean(PROPERTY_LABELED_COLUMNS, false);
60 public void writeStartBatch() {
61 super.writeStartBatch();
63 printStartElement(qname("batchResult"), singleAttribute(qname("xmlns"), Xmlns.BATCH_RESULT));
67 public void writeEndBatch() {
68 super.writeEndBatch();
74 public void writeStartDatabase(DatabaseDefinition databaseDefinition) {
75 super.writeStartDatabase(databaseDefinition);
76 Map<QName, String> attributes = databaseDefinition.getName() == null ? null : singleAttribute(qname("name"), databaseDefinition.getName());
77 printStartElement(qname("database"), attributes);
81 public void writeEndDatabase() {
82 super.writeEndDatabase();
87 public void writeStartStatement() {
88 super.writeStartStatement();
89 printStartElement(qname("statement"));
93 public void writeEndStatement() {
94 super.writeEndStatement();
99 public void writeQuery(String sql) {
100 super.writeQuery(sql);
101 printTextElement(qname("sql"), null, sql);
105 public void writeParameters(List<? extends Parameter> parameters) {
106 super.writeParameters(parameters);
108 for (Parameter p : notNull(parameters)) {
110 Map<QName, String> attributes = new LinkedHashMap<>(2);
111 if (p instanceof NamedParameter) {
112 attributes.put(qname("name"), ((NamedParameter) p).getName());
114 attributes.put(qname("type"), p.getType().name());
116 printTextElement(qname("parameter"), attributes, String.valueOf(p.getValue()));
122 public void writeStartResultSet(ColumnsHeader header) {
123 super.writeStartResultSet(header);
124 printStartElement(qname("resultSet"));
126 for (ColumnDescriptor cd : header.getColumnDescriptors()) {
127 Map<QName, String> attributes = new LinkedHashMap<>(4);
128 attributes.put(qname("label"), cd.getLabel());
129 attributes.put(qname("name"), cd.getName());
130 attributes.put(qname("typeName"), cd.getTypeName());
131 attributes.put(qname("type"), String.valueOf(cd.getType()));
132 printEmptyElement(qname("columnHeader"), attributes);
137 public void writeEndResultSet() {
138 super.writeEndResultSet();
143 public void writeStartRow() {
144 super.writeStartRow();
145 printStartElement(qname("row"));
149 public void writeColumnValue(Object value) {
150 super.writeColumnValue(value);
152 Map<QName, String> attributes = null;
153 if (labeledColumns) {
154 attributes = new LinkedHashMap<>(2);
155 attributes.put(qname("label"), getCurrentColumnsHeader().getColumnDescriptors().get(getCurrentColumnsCount() - 1).getLabel());
159 if (attributes == null) {
160 attributes = new LinkedHashMap<>(2);
162 attributes.put(qname("null"), "true");
163 printEmptyElement(qname("column"), attributes);
164 } else if (value instanceof Array) {
166 Array sqlArray = (Array) value;
168 Object[] array = (Object[]) sqlArray.getArray();
169 printStartElement(qname("column"), attributes);
172 } catch (SQLException e) {
173 log.log(Level.SEVERE, "Unable to format array", e);
174 writeColumnValue(String.valueOf(value));
178 printTextElement(qname("column"), attributes, toString(value));
182 private void printArray(Object[] array) {
183 printStartElement(qname("array"));
184 for (Object o : array) {
185 if (o instanceof Object[]) {
186 printStartElement(qname("item"));
187 printArray((Object[]) o);
190 printTextElement(qname("item"), null, String.valueOf(o));
197 public void writeEndRow() {
203 public void writeUpdatesResult(int updatedRowsCount) {
204 super.writeUpdatesResult(updatedRowsCount);
205 printTextElement(qname("updatedRows"), null, String.valueOf(updatedRowsCount));
208 protected String toString(Object value) {
209 return String.valueOf(value);