franta-hg@29
|
1 |
/**
|
franta-hg@29
|
2 |
* SQL-DK
|
franta-hg@29
|
3 |
* Copyright © 2013 František Kučera (frantovo.cz)
|
franta-hg@29
|
4 |
*
|
franta-hg@29
|
5 |
* This program is free software: you can redistribute it and/or modify
|
franta-hg@29
|
6 |
* it under the terms of the GNU General Public License as published by
|
franta-hg@29
|
7 |
* the Free Software Foundation, either version 3 of the License, or
|
franta-hg@29
|
8 |
* (at your option) any later version.
|
franta-hg@29
|
9 |
*
|
franta-hg@29
|
10 |
* This program is distributed in the hope that it will be useful,
|
franta-hg@29
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
franta-hg@29
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
franta-hg@29
|
13 |
* GNU General Public License for more details.
|
franta-hg@29
|
14 |
*
|
franta-hg@29
|
15 |
* You should have received a copy of the GNU General Public License
|
franta-hg@29
|
16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
franta-hg@29
|
17 |
*/
|
franta-hg@29
|
18 |
package info.globalcode.sql.dk.formatting;
|
franta-hg@29
|
19 |
|
franta-hg@128
|
20 |
import info.globalcode.sql.dk.Parameter;
|
franta-hg@128
|
21 |
import info.globalcode.sql.dk.Xmlns;
|
franta-hg@128
|
22 |
import info.globalcode.sql.dk.configuration.DatabaseDefinition;
|
franta-hg@128
|
23 |
import static info.globalcode.sql.dk.Functions.notNull;
|
franta-hg@128
|
24 |
import info.globalcode.sql.dk.NamedParameter;
|
franta-hg@165
|
25 |
import static info.globalcode.sql.dk.formatting.AbstractXmlFormatter.qname;
|
franta-hg@165
|
26 |
import java.sql.Array;
|
franta-hg@165
|
27 |
import java.sql.SQLException;
|
franta-hg@128
|
28 |
import java.util.LinkedHashMap;
|
franta-hg@128
|
29 |
import java.util.List;
|
franta-hg@128
|
30 |
import java.util.Map;
|
franta-hg@165
|
31 |
import java.util.logging.Level;
|
franta-hg@165
|
32 |
import java.util.logging.Logger;
|
franta-hg@128
|
33 |
import javax.xml.namespace.QName;
|
franta-hg@128
|
34 |
|
franta-hg@29
|
35 |
/**
|
franta-hg@155
|
36 |
* <p>Prints machine-readable output – XML document containing resultsets and updates count. Good
|
franta-hg@155
|
37 |
* choice for further processing – e.g. XSL transformation.</p>
|
franta-hg@155
|
38 |
*
|
franta-hg@155
|
39 |
* <p>TODO: XSD</p>
|
franta-hg@29
|
40 |
*
|
franta-hg@29
|
41 |
* @author Ing. František Kučera (frantovo.cz)
|
franta-hg@29
|
42 |
*/
|
franta-hg@128
|
43 |
public class XmlFormatter extends AbstractXmlFormatter {
|
franta-hg@29
|
44 |
|
franta-hg@79
|
45 |
public static final String NAME = "xml"; // bash-completion:formatter
|
franta-hg@131
|
46 |
public static final String PROPERTY_LABELED_COLUMNS = "labeledColumns";
|
franta-hg@165
|
47 |
private static final Logger log = Logger.getLogger(XmlFormatter.class.getName());
|
franta-hg@131
|
48 |
private final boolean labeledColumns;
|
franta-hg@29
|
49 |
|
franta-hg@29
|
50 |
public XmlFormatter(FormatterContext formatterContext) {
|
franta-hg@29
|
51 |
super(formatterContext);
|
franta-hg@131
|
52 |
labeledColumns = formatterContext.getProperties().getBoolean(PROPERTY_LABELED_COLUMNS, false);
|
franta-hg@29
|
53 |
}
|
franta-hg@128
|
54 |
|
franta-hg@128
|
55 |
@Override
|
franta-hg@128
|
56 |
public void writeStartBatch() {
|
franta-hg@128
|
57 |
super.writeStartBatch();
|
franta-hg@128
|
58 |
printStartDocument();
|
franta-hg@154
|
59 |
printStartElement(qname("batchResult"), singleAttribute(qname("xmlns"), Xmlns.BATCH_RESULT));
|
franta-hg@128
|
60 |
}
|
franta-hg@128
|
61 |
|
franta-hg@128
|
62 |
@Override
|
franta-hg@128
|
63 |
public void writeEndBatch() {
|
franta-hg@128
|
64 |
super.writeEndBatch();
|
franta-hg@128
|
65 |
printEndElement();
|
franta-hg@128
|
66 |
printEndDocument();
|
franta-hg@128
|
67 |
}
|
franta-hg@128
|
68 |
|
franta-hg@128
|
69 |
@Override
|
franta-hg@128
|
70 |
public void writeStartDatabase(DatabaseDefinition databaseDefinition) {
|
franta-hg@128
|
71 |
super.writeStartDatabase(databaseDefinition);
|
franta-hg@128
|
72 |
Map<QName, String> attributes = databaseDefinition.getName() == null ? null : singleAttribute(qname("name"), databaseDefinition.getName());
|
franta-hg@128
|
73 |
printStartElement(qname("database"), attributes);
|
franta-hg@128
|
74 |
}
|
franta-hg@128
|
75 |
|
franta-hg@128
|
76 |
@Override
|
franta-hg@128
|
77 |
public void writeEndDatabase() {
|
franta-hg@128
|
78 |
super.writeEndDatabase();
|
franta-hg@128
|
79 |
printEndElement();
|
franta-hg@128
|
80 |
}
|
franta-hg@128
|
81 |
|
franta-hg@128
|
82 |
@Override
|
franta-hg@142
|
83 |
public void writeStartStatement() {
|
franta-hg@142
|
84 |
super.writeStartStatement();
|
franta-hg@142
|
85 |
printStartElement(qname("statement"));
|
franta-hg@128
|
86 |
}
|
franta-hg@128
|
87 |
|
franta-hg@128
|
88 |
@Override
|
franta-hg@142
|
89 |
public void writeEndStatement() {
|
franta-hg@142
|
90 |
super.writeEndStatement();
|
franta-hg@128
|
91 |
printEndElement();
|
franta-hg@128
|
92 |
}
|
franta-hg@128
|
93 |
|
franta-hg@128
|
94 |
@Override
|
franta-hg@128
|
95 |
public void writeQuery(String sql) {
|
franta-hg@128
|
96 |
super.writeQuery(sql);
|
franta-hg@128
|
97 |
printTextElement(qname("sql"), null, sql);
|
franta-hg@128
|
98 |
}
|
franta-hg@128
|
99 |
|
franta-hg@128
|
100 |
@Override
|
franta-hg@128
|
101 |
public void writeParameters(List<? extends Parameter> parameters) {
|
franta-hg@128
|
102 |
super.writeParameters(parameters);
|
franta-hg@128
|
103 |
|
franta-hg@128
|
104 |
for (Parameter p : notNull(parameters)) {
|
franta-hg@128
|
105 |
|
franta-hg@128
|
106 |
Map<QName, String> attributes = new LinkedHashMap<>(2);
|
franta-hg@128
|
107 |
if (p instanceof NamedParameter) {
|
franta-hg@128
|
108 |
attributes.put(qname("name"), ((NamedParameter) p).getName());
|
franta-hg@128
|
109 |
}
|
franta-hg@128
|
110 |
attributes.put(qname("type"), p.getType().name());
|
franta-hg@128
|
111 |
|
franta-hg@128
|
112 |
printTextElement(qname("parameter"), attributes, String.valueOf(p.getValue()));
|
franta-hg@128
|
113 |
}
|
franta-hg@128
|
114 |
|
franta-hg@128
|
115 |
}
|
franta-hg@128
|
116 |
|
franta-hg@128
|
117 |
@Override
|
franta-hg@142
|
118 |
public void writeStartResultSet(ColumnsHeader header) {
|
franta-hg@142
|
119 |
super.writeStartResultSet(header);
|
franta-hg@142
|
120 |
printStartElement(qname("resultSet"));
|
franta-hg@128
|
121 |
|
franta-hg@128
|
122 |
for (ColumnDescriptor cd : header.getColumnDescriptors()) {
|
franta-hg@128
|
123 |
Map<QName, String> attributes = new LinkedHashMap<>(4);
|
franta-hg@128
|
124 |
attributes.put(qname("label"), cd.getLabel());
|
franta-hg@128
|
125 |
attributes.put(qname("name"), cd.getName());
|
franta-hg@128
|
126 |
attributes.put(qname("typeName"), cd.getTypeName());
|
franta-hg@128
|
127 |
attributes.put(qname("type"), String.valueOf(cd.getType()));
|
franta-hg@128
|
128 |
printEmptyElement(qname("columnHeader"), attributes);
|
franta-hg@128
|
129 |
}
|
franta-hg@128
|
130 |
}
|
franta-hg@129
|
131 |
|
franta-hg@129
|
132 |
@Override
|
franta-hg@142
|
133 |
public void writeEndResultSet() {
|
franta-hg@142
|
134 |
super.writeEndResultSet();
|
franta-hg@142
|
135 |
printEndElement();
|
franta-hg@142
|
136 |
}
|
franta-hg@142
|
137 |
|
franta-hg@142
|
138 |
@Override
|
franta-hg@129
|
139 |
public void writeStartRow() {
|
franta-hg@129
|
140 |
super.writeStartRow();
|
franta-hg@129
|
141 |
printStartElement(qname("row"));
|
franta-hg@129
|
142 |
}
|
franta-hg@129
|
143 |
|
franta-hg@129
|
144 |
@Override
|
franta-hg@129
|
145 |
public void writeColumnValue(Object value) {
|
franta-hg@129
|
146 |
super.writeColumnValue(value);
|
franta-hg@131
|
147 |
|
franta-hg@131
|
148 |
Map<QName, String> attributes = null;
|
franta-hg@131
|
149 |
if (labeledColumns) {
|
franta-hg@151
|
150 |
attributes = new LinkedHashMap<>(2);
|
franta-hg@131
|
151 |
attributes.put(qname("label"), getCurrentColumnsHeader().getColumnDescriptors().get(getCurrentColumnsCount() - 1).getLabel());
|
franta-hg@131
|
152 |
}
|
franta-hg@131
|
153 |
|
franta-hg@151
|
154 |
if (value == null) {
|
franta-hg@151
|
155 |
if (attributes == null) {
|
franta-hg@151
|
156 |
attributes = new LinkedHashMap<>(2);
|
franta-hg@151
|
157 |
}
|
franta-hg@163
|
158 |
attributes.put(qname("null"), "true");
|
franta-hg@163
|
159 |
printEmptyElement(qname("column"), attributes);
|
franta-hg@165
|
160 |
} else if (value instanceof Array) {
|
franta-hg@165
|
161 |
|
franta-hg@165
|
162 |
|
franta-hg@165
|
163 |
|
franta-hg@165
|
164 |
Array sqlArray = (Array) value;
|
franta-hg@165
|
165 |
try {
|
franta-hg@165
|
166 |
Object[] array = (Object[]) sqlArray.getArray();
|
franta-hg@165
|
167 |
printStartElement(qname("column"), attributes);
|
franta-hg@165
|
168 |
printArray(array);
|
franta-hg@165
|
169 |
printEndElement();
|
franta-hg@165
|
170 |
} catch (SQLException e) {
|
franta-hg@165
|
171 |
log.log(Level.SEVERE, "Unable to format array", e);
|
franta-hg@165
|
172 |
writeColumnValue(String.valueOf(value));
|
franta-hg@165
|
173 |
}
|
franta-hg@165
|
174 |
|
franta-hg@165
|
175 |
|
franta-hg@151
|
176 |
} else {
|
franta-hg@151
|
177 |
printTextElement(qname("column"), attributes, toString(value));
|
franta-hg@151
|
178 |
}
|
franta-hg@129
|
179 |
}
|
franta-hg@165
|
180 |
|
franta-hg@165
|
181 |
private void printArray(Object[] array) {
|
franta-hg@165
|
182 |
printStartElement(qname("array"));
|
franta-hg@165
|
183 |
for (Object o : array) {
|
franta-hg@165
|
184 |
if (o instanceof Object[]) {
|
franta-hg@165
|
185 |
printStartElement(qname("item"));
|
franta-hg@165
|
186 |
printArray((Object[]) o);
|
franta-hg@165
|
187 |
printEndElement();
|
franta-hg@165
|
188 |
} else {
|
franta-hg@165
|
189 |
printTextElement(qname("item"), null, String.valueOf(o));
|
franta-hg@165
|
190 |
}
|
franta-hg@165
|
191 |
}
|
franta-hg@165
|
192 |
printEndElement();
|
franta-hg@165
|
193 |
}
|
franta-hg@129
|
194 |
|
franta-hg@129
|
195 |
@Override
|
franta-hg@129
|
196 |
public void writeEndRow() {
|
franta-hg@129
|
197 |
super.writeEndRow();
|
franta-hg@129
|
198 |
printEndElement();
|
franta-hg@129
|
199 |
}
|
franta-hg@129
|
200 |
|
franta-hg@129
|
201 |
@Override
|
franta-hg@142
|
202 |
public void writeUpdatesResult(int updatedRowsCount) {
|
franta-hg@142
|
203 |
super.writeUpdatesResult(updatedRowsCount);
|
franta-hg@129
|
204 |
printTextElement(qname("updatedRows"), null, String.valueOf(updatedRowsCount));
|
franta-hg@129
|
205 |
}
|
franta-hg@129
|
206 |
|
franta-hg@129
|
207 |
protected String toString(Object value) {
|
franta-hg@129
|
208 |
return String.valueOf(value);
|
franta-hg@129
|
209 |
}
|
franta-hg@29
|
210 |
}
|