java/sql-dk/src/info/globalcode/sql/dk/formatting/XmlFormatter.java
author František Kučera <franta-hg@frantovo.cz>
Sat, 04 Jan 2014 19:39:35 +0100
branchv_0
changeset 128 67f5ff139da0
parent 79 e19a13ed19a9
child 129 331634456bf8
permissions -rw-r--r--
XML formatter: abstract + part of basic XML formatter

/**
 * SQL-DK
 * Copyright © 2013 František Kučera (frantovo.cz)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package info.globalcode.sql.dk.formatting;

import info.globalcode.sql.dk.Parameter;
import info.globalcode.sql.dk.Xmlns;
import info.globalcode.sql.dk.configuration.DatabaseDefinition;
import static info.globalcode.sql.dk.formatting.AbstractXmlFormatter.qname;
import static info.globalcode.sql.dk.Functions.notNull;
import info.globalcode.sql.dk.NamedParameter;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.xml.namespace.QName;

/**
 *
 * @author Ing. František Kučera (frantovo.cz)
 */
public class XmlFormatter extends AbstractXmlFormatter {

	public static final String NAME = "xml"; // bash-completion:formatter

	public XmlFormatter(FormatterContext formatterContext) {
		super(formatterContext);
	}

	@Override
	public void writeStartBatch() {
		super.writeStartBatch();
		printStartDocument();
		printStartElement(qname("batchResults"), singleAttribute(qname("xmlns"), Xmlns.BATCH_RESULT));
	}

	@Override
	public void writeEndBatch() {
		super.writeEndBatch();

		printEndElement();
		printEndDocument();
	}

	@Override
	public void writeStartDatabase(DatabaseDefinition databaseDefinition) {
		super.writeStartDatabase(databaseDefinition);
		Map<QName, String> attributes = databaseDefinition.getName() == null ? null : singleAttribute(qname("name"), databaseDefinition.getName());
		printStartElement(qname("database"), attributes);
	}

	@Override
	public void writeEndDatabase() {
		super.writeEndDatabase();
		printEndElement();
	}

	@Override
	public void writeStartResultSet() {
		super.writeStartResultSet();
		printStartElement(qname("resultSet"));
	}

	@Override
	public void writeEndResultSet() {
		super.writeEndResultSet();
		printEndElement();
	}

	@Override
	public void writeQuery(String sql) {
		super.writeQuery(sql);
		printTextElement(qname("sql"), null, sql);
	}

	@Override
	public void writeParameters(List<? extends Parameter> parameters) {
		super.writeParameters(parameters);

		for (Parameter p : notNull(parameters)) {

			Map<QName, String> attributes = new LinkedHashMap<>(2);
			if (p instanceof NamedParameter) {
				attributes.put(qname("name"), ((NamedParameter) p).getName());
			}
			attributes.put(qname("type"), p.getType().name());

			printTextElement(qname("parameter"), attributes, String.valueOf(p.getValue()));
		}

	}

	@Override
	public void writeColumnsHeader(ColumnsHeader header) {
		super.writeColumnsHeader(header);

		for (ColumnDescriptor cd : header.getColumnDescriptors()) {
			Map<QName, String> attributes = new LinkedHashMap<>(4);
			attributes.put(qname("label"), cd.getLabel());
			attributes.put(qname("name"), cd.getName());
			attributes.put(qname("typeName"), cd.getTypeName());
			attributes.put(qname("type"), String.valueOf(cd.getType()));
			printEmptyElement(qname("columnHeader"), attributes);
		}
	}
}