15 * You should have received a copy of the GNU General Public License |
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/>. |
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 */ |
17 */ |
18 package info.globalcode.sql.dk.formatting; |
18 package info.globalcode.sql.dk.formatting; |
19 |
19 |
|
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; |
|
28 import java.util.Map; |
|
29 import javax.xml.namespace.QName; |
|
30 |
20 /** |
31 /** |
21 * |
32 * |
22 * @author Ing. František Kučera (frantovo.cz) |
33 * @author Ing. František Kučera (frantovo.cz) |
23 */ |
34 */ |
24 public class XmlFormatter extends AbstractFormatter { |
35 public class XmlFormatter extends AbstractXmlFormatter { |
25 |
36 |
26 public static final String NAME = "xml"; // bash-completion:formatter |
37 public static final String NAME = "xml"; // bash-completion:formatter |
27 |
38 |
28 public XmlFormatter(FormatterContext formatterContext) { |
39 public XmlFormatter(FormatterContext formatterContext) { |
29 super(formatterContext); |
40 super(formatterContext); |
30 } |
41 } |
|
42 |
|
43 @Override |
|
44 public void writeStartBatch() { |
|
45 super.writeStartBatch(); |
|
46 printStartDocument(); |
|
47 printStartElement(qname("batchResults"), singleAttribute(qname("xmlns"), Xmlns.BATCH_RESULT)); |
|
48 } |
|
49 |
|
50 @Override |
|
51 public void writeEndBatch() { |
|
52 super.writeEndBatch(); |
|
53 |
|
54 printEndElement(); |
|
55 printEndDocument(); |
|
56 } |
|
57 |
|
58 @Override |
|
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); |
|
63 } |
|
64 |
|
65 @Override |
|
66 public void writeEndDatabase() { |
|
67 super.writeEndDatabase(); |
|
68 printEndElement(); |
|
69 } |
|
70 |
|
71 @Override |
|
72 public void writeStartResultSet() { |
|
73 super.writeStartResultSet(); |
|
74 printStartElement(qname("resultSet")); |
|
75 } |
|
76 |
|
77 @Override |
|
78 public void writeEndResultSet() { |
|
79 super.writeEndResultSet(); |
|
80 printEndElement(); |
|
81 } |
|
82 |
|
83 @Override |
|
84 public void writeQuery(String sql) { |
|
85 super.writeQuery(sql); |
|
86 printTextElement(qname("sql"), null, sql); |
|
87 } |
|
88 |
|
89 @Override |
|
90 public void writeParameters(List<? extends Parameter> parameters) { |
|
91 super.writeParameters(parameters); |
|
92 |
|
93 for (Parameter p : notNull(parameters)) { |
|
94 |
|
95 Map<QName, String> attributes = new LinkedHashMap<>(2); |
|
96 if (p instanceof NamedParameter) { |
|
97 attributes.put(qname("name"), ((NamedParameter) p).getName()); |
|
98 } |
|
99 attributes.put(qname("type"), p.getType().name()); |
|
100 |
|
101 printTextElement(qname("parameter"), attributes, String.valueOf(p.getValue())); |
|
102 } |
|
103 |
|
104 } |
|
105 |
|
106 @Override |
|
107 public void writeColumnsHeader(ColumnsHeader header) { |
|
108 super.writeColumnsHeader(header); |
|
109 |
|
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); |
|
117 } |
|
118 } |
31 } |
119 } |