java/sql-dk/src/info/globalcode/sql/dk/formatting/XmlFormatter.java
author František Kučera <franta-hg@frantovo.cz>
Sat, 15 Aug 2015 10:34:18 +0200
branchv_0
changeset 207 2bba68ef47c1
parent 206 e2f24eea8543
child 225 906f767ef9b3
permissions -rw-r--r--
property annotations: default values
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@206
    25
import info.globalcode.sql.dk.configuration.PropertyDeclaration;
franta-hg@165
    26
import static info.globalcode.sql.dk.formatting.AbstractXmlFormatter.qname;
franta-hg@165
    27
import java.sql.Array;
franta-hg@165
    28
import java.sql.SQLException;
franta-hg@128
    29
import java.util.LinkedHashMap;
franta-hg@128
    30
import java.util.List;
franta-hg@128
    31
import java.util.Map;
franta-hg@165
    32
import java.util.logging.Level;
franta-hg@165
    33
import java.util.logging.Logger;
franta-hg@128
    34
import javax.xml.namespace.QName;
franta-hg@128
    35
franta-hg@29
    36
/**
franta-hg@206
    37
 * <p>
franta-hg@206
    38
 * Prints machine-readable output – XML document containing resultsets and updates count. Good
franta-hg@155
    39
 * choice for further processing – e.g. XSL transformation.</p>
franta-hg@155
    40
 *
franta-hg@206
    41
 * <p>
franta-hg@206
    42
 * TODO: XSD</p>
franta-hg@29
    43
 *
franta-hg@29
    44
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@29
    45
 */
franta-hg@207
    46
@PropertyDeclaration(name = XmlFormatter.PROPERTY_LABELED_COLUMNS, defaultValue = "false", type = Boolean.class, description = "whether to add 'label' attribute to each 'column' element")
franta-hg@128
    47
public class XmlFormatter extends AbstractXmlFormatter {
franta-hg@29
    48
franta-hg@79
    49
	public static final String NAME = "xml"; // bash-completion:formatter
franta-hg@131
    50
	public static final String PROPERTY_LABELED_COLUMNS = "labeledColumns";
franta-hg@165
    51
	private static final Logger log = Logger.getLogger(XmlFormatter.class.getName());
franta-hg@131
    52
	private final boolean labeledColumns;
franta-hg@29
    53
franta-hg@29
    54
	public XmlFormatter(FormatterContext formatterContext) {
franta-hg@29
    55
		super(formatterContext);
franta-hg@131
    56
		labeledColumns = formatterContext.getProperties().getBoolean(PROPERTY_LABELED_COLUMNS, false);
franta-hg@29
    57
	}
franta-hg@128
    58
franta-hg@128
    59
	@Override
franta-hg@128
    60
	public void writeStartBatch() {
franta-hg@128
    61
		super.writeStartBatch();
franta-hg@128
    62
		printStartDocument();
franta-hg@154
    63
		printStartElement(qname("batchResult"), singleAttribute(qname("xmlns"), Xmlns.BATCH_RESULT));
franta-hg@128
    64
	}
franta-hg@128
    65
franta-hg@128
    66
	@Override
franta-hg@128
    67
	public void writeEndBatch() {
franta-hg@128
    68
		super.writeEndBatch();
franta-hg@128
    69
		printEndElement();
franta-hg@128
    70
		printEndDocument();
franta-hg@128
    71
	}
franta-hg@128
    72
franta-hg@128
    73
	@Override
franta-hg@128
    74
	public void writeStartDatabase(DatabaseDefinition databaseDefinition) {
franta-hg@128
    75
		super.writeStartDatabase(databaseDefinition);
franta-hg@128
    76
		Map<QName, String> attributes = databaseDefinition.getName() == null ? null : singleAttribute(qname("name"), databaseDefinition.getName());
franta-hg@128
    77
		printStartElement(qname("database"), attributes);
franta-hg@128
    78
	}
franta-hg@128
    79
franta-hg@128
    80
	@Override
franta-hg@128
    81
	public void writeEndDatabase() {
franta-hg@128
    82
		super.writeEndDatabase();
franta-hg@128
    83
		printEndElement();
franta-hg@128
    84
	}
franta-hg@128
    85
franta-hg@128
    86
	@Override
franta-hg@142
    87
	public void writeStartStatement() {
franta-hg@142
    88
		super.writeStartStatement();
franta-hg@142
    89
		printStartElement(qname("statement"));
franta-hg@128
    90
	}
franta-hg@128
    91
franta-hg@128
    92
	@Override
franta-hg@142
    93
	public void writeEndStatement() {
franta-hg@142
    94
		super.writeEndStatement();
franta-hg@128
    95
		printEndElement();
franta-hg@128
    96
	}
franta-hg@128
    97
franta-hg@128
    98
	@Override
franta-hg@128
    99
	public void writeQuery(String sql) {
franta-hg@128
   100
		super.writeQuery(sql);
franta-hg@128
   101
		printTextElement(qname("sql"), null, sql);
franta-hg@128
   102
	}
franta-hg@128
   103
franta-hg@128
   104
	@Override
franta-hg@128
   105
	public void writeParameters(List<? extends Parameter> parameters) {
franta-hg@128
   106
		super.writeParameters(parameters);
franta-hg@128
   107
franta-hg@128
   108
		for (Parameter p : notNull(parameters)) {
franta-hg@128
   109
franta-hg@128
   110
			Map<QName, String> attributes = new LinkedHashMap<>(2);
franta-hg@128
   111
			if (p instanceof NamedParameter) {
franta-hg@128
   112
				attributes.put(qname("name"), ((NamedParameter) p).getName());
franta-hg@128
   113
			}
franta-hg@128
   114
			attributes.put(qname("type"), p.getType().name());
franta-hg@128
   115
franta-hg@128
   116
			printTextElement(qname("parameter"), attributes, String.valueOf(p.getValue()));
franta-hg@128
   117
		}
franta-hg@128
   118
franta-hg@128
   119
	}
franta-hg@128
   120
franta-hg@128
   121
	@Override
franta-hg@142
   122
	public void writeStartResultSet(ColumnsHeader header) {
franta-hg@142
   123
		super.writeStartResultSet(header);
franta-hg@142
   124
		printStartElement(qname("resultSet"));
franta-hg@128
   125
franta-hg@128
   126
		for (ColumnDescriptor cd : header.getColumnDescriptors()) {
franta-hg@128
   127
			Map<QName, String> attributes = new LinkedHashMap<>(4);
franta-hg@128
   128
			attributes.put(qname("label"), cd.getLabel());
franta-hg@128
   129
			attributes.put(qname("name"), cd.getName());
franta-hg@128
   130
			attributes.put(qname("typeName"), cd.getTypeName());
franta-hg@128
   131
			attributes.put(qname("type"), String.valueOf(cd.getType()));
franta-hg@128
   132
			printEmptyElement(qname("columnHeader"), attributes);
franta-hg@128
   133
		}
franta-hg@128
   134
	}
franta-hg@129
   135
franta-hg@129
   136
	@Override
franta-hg@142
   137
	public void writeEndResultSet() {
franta-hg@142
   138
		super.writeEndResultSet();
franta-hg@142
   139
		printEndElement();
franta-hg@142
   140
	}
franta-hg@142
   141
franta-hg@142
   142
	@Override
franta-hg@129
   143
	public void writeStartRow() {
franta-hg@129
   144
		super.writeStartRow();
franta-hg@129
   145
		printStartElement(qname("row"));
franta-hg@129
   146
	}
franta-hg@129
   147
franta-hg@129
   148
	@Override
franta-hg@129
   149
	public void writeColumnValue(Object value) {
franta-hg@129
   150
		super.writeColumnValue(value);
franta-hg@131
   151
franta-hg@131
   152
		Map<QName, String> attributes = null;
franta-hg@131
   153
		if (labeledColumns) {
franta-hg@151
   154
			attributes = new LinkedHashMap<>(2);
franta-hg@131
   155
			attributes.put(qname("label"), getCurrentColumnsHeader().getColumnDescriptors().get(getCurrentColumnsCount() - 1).getLabel());
franta-hg@131
   156
		}
franta-hg@131
   157
franta-hg@151
   158
		if (value == null) {
franta-hg@151
   159
			if (attributes == null) {
franta-hg@151
   160
				attributes = new LinkedHashMap<>(2);
franta-hg@151
   161
			}
franta-hg@163
   162
			attributes.put(qname("null"), "true");
franta-hg@163
   163
			printEmptyElement(qname("column"), attributes);
franta-hg@165
   164
		} else if (value instanceof Array) {
franta-hg@206
   165
franta-hg@165
   166
			Array sqlArray = (Array) value;
franta-hg@165
   167
			try {
franta-hg@165
   168
				Object[] array = (Object[]) sqlArray.getArray();
franta-hg@165
   169
				printStartElement(qname("column"), attributes);
franta-hg@165
   170
				printArray(array);
franta-hg@165
   171
				printEndElement();
franta-hg@165
   172
			} catch (SQLException e) {
franta-hg@165
   173
				log.log(Level.SEVERE, "Unable to format array", e);
franta-hg@165
   174
				writeColumnValue(String.valueOf(value));
franta-hg@165
   175
			}
franta-hg@206
   176
franta-hg@151
   177
		} else {
franta-hg@151
   178
			printTextElement(qname("column"), attributes, toString(value));
franta-hg@151
   179
		}
franta-hg@129
   180
	}
franta-hg@206
   181
franta-hg@165
   182
	private void printArray(Object[] array) {
franta-hg@165
   183
		printStartElement(qname("array"));
franta-hg@165
   184
		for (Object o : array) {
franta-hg@165
   185
			if (o instanceof Object[]) {
franta-hg@165
   186
				printStartElement(qname("item"));
franta-hg@165
   187
				printArray((Object[]) o);
franta-hg@165
   188
				printEndElement();
franta-hg@165
   189
			} else {
franta-hg@165
   190
				printTextElement(qname("item"), null, String.valueOf(o));
franta-hg@165
   191
			}
franta-hg@165
   192
		}
franta-hg@165
   193
		printEndElement();
franta-hg@165
   194
	}
franta-hg@129
   195
franta-hg@129
   196
	@Override
franta-hg@129
   197
	public void writeEndRow() {
franta-hg@129
   198
		super.writeEndRow();
franta-hg@129
   199
		printEndElement();
franta-hg@129
   200
	}
franta-hg@129
   201
franta-hg@129
   202
	@Override
franta-hg@142
   203
	public void writeUpdatesResult(int updatedRowsCount) {
franta-hg@142
   204
		super.writeUpdatesResult(updatedRowsCount);
franta-hg@129
   205
		printTextElement(qname("updatedRows"), null, String.valueOf(updatedRowsCount));
franta-hg@129
   206
	}
franta-hg@129
   207
franta-hg@129
   208
	protected String toString(Object value) {
franta-hg@129
   209
		return String.valueOf(value);
franta-hg@129
   210
	}
franta-hg@29
   211
}