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