20 import info.globalcode.sql.dk.Parameter; |
20 import info.globalcode.sql.dk.Parameter; |
21 import info.globalcode.sql.dk.Xmlns; |
21 import info.globalcode.sql.dk.Xmlns; |
22 import info.globalcode.sql.dk.configuration.DatabaseDefinition; |
22 import info.globalcode.sql.dk.configuration.DatabaseDefinition; |
23 import static info.globalcode.sql.dk.Functions.notNull; |
23 import static info.globalcode.sql.dk.Functions.notNull; |
24 import info.globalcode.sql.dk.NamedParameter; |
24 import info.globalcode.sql.dk.NamedParameter; |
|
25 import info.globalcode.sql.dk.configuration.PropertyDeclaration; |
25 import static info.globalcode.sql.dk.formatting.AbstractXmlFormatter.qname; |
26 import static info.globalcode.sql.dk.formatting.AbstractXmlFormatter.qname; |
26 import java.sql.Array; |
27 import java.sql.Array; |
27 import java.sql.SQLException; |
28 import java.sql.SQLException; |
28 import java.util.LinkedHashMap; |
29 import java.util.LinkedHashMap; |
29 import java.util.List; |
30 import java.util.List; |
31 import java.util.logging.Level; |
32 import java.util.logging.Level; |
32 import java.util.logging.Logger; |
33 import java.util.logging.Logger; |
33 import javax.xml.namespace.QName; |
34 import javax.xml.namespace.QName; |
34 |
35 |
35 /** |
36 /** |
36 * <p>Prints machine-readable output – XML document containing resultsets and updates count. Good |
37 * <p> |
|
38 * Prints machine-readable output – XML document containing resultsets and updates count. Good |
37 * choice for further processing – e.g. XSL transformation.</p> |
39 * choice for further processing – e.g. XSL transformation.</p> |
38 * |
40 * |
39 * <p>TODO: XSD</p> |
41 * <p> |
|
42 * TODO: XSD</p> |
40 * |
43 * |
41 * @author Ing. František Kučera (frantovo.cz) |
44 * @author Ing. František Kučera (frantovo.cz) |
42 */ |
45 */ |
|
46 @PropertyDeclaration(name = XmlFormatter.PROPERTY_LABELED_COLUMNS, type = Boolean.class, description = "whether to add 'label' attribute to each 'column' element") |
43 public class XmlFormatter extends AbstractXmlFormatter { |
47 public class XmlFormatter extends AbstractXmlFormatter { |
44 |
48 |
45 public static final String NAME = "xml"; // bash-completion:formatter |
49 public static final String NAME = "xml"; // bash-completion:formatter |
46 public static final String PROPERTY_LABELED_COLUMNS = "labeledColumns"; |
50 public static final String PROPERTY_LABELED_COLUMNS = "labeledColumns"; |
47 private static final Logger log = Logger.getLogger(XmlFormatter.class.getName()); |
51 private static final Logger log = Logger.getLogger(XmlFormatter.class.getName()); |
156 attributes = new LinkedHashMap<>(2); |
160 attributes = new LinkedHashMap<>(2); |
157 } |
161 } |
158 attributes.put(qname("null"), "true"); |
162 attributes.put(qname("null"), "true"); |
159 printEmptyElement(qname("column"), attributes); |
163 printEmptyElement(qname("column"), attributes); |
160 } else if (value instanceof Array) { |
164 } else if (value instanceof Array) { |
161 |
165 |
162 |
|
163 |
|
164 Array sqlArray = (Array) value; |
166 Array sqlArray = (Array) value; |
165 try { |
167 try { |
166 Object[] array = (Object[]) sqlArray.getArray(); |
168 Object[] array = (Object[]) sqlArray.getArray(); |
167 printStartElement(qname("column"), attributes); |
169 printStartElement(qname("column"), attributes); |
168 printArray(array); |
170 printArray(array); |
169 printEndElement(); |
171 printEndElement(); |
170 } catch (SQLException e) { |
172 } catch (SQLException e) { |
171 log.log(Level.SEVERE, "Unable to format array", e); |
173 log.log(Level.SEVERE, "Unable to format array", e); |
172 writeColumnValue(String.valueOf(value)); |
174 writeColumnValue(String.valueOf(value)); |
173 } |
175 } |
174 |
176 |
175 |
|
176 } else { |
177 } else { |
177 printTextElement(qname("column"), attributes, toString(value)); |
178 printTextElement(qname("column"), attributes, toString(value)); |
178 } |
179 } |
179 } |
180 } |
180 |
181 |
181 private void printArray(Object[] array) { |
182 private void printArray(Object[] array) { |
182 printStartElement(qname("array")); |
183 printStartElement(qname("array")); |
183 for (Object o : array) { |
184 for (Object o : array) { |
184 if (o instanceof Object[]) { |
185 if (o instanceof Object[]) { |
185 printStartElement(qname("item")); |
186 printStartElement(qname("item")); |