java/sql-dk/src/info/globalcode/sql/dk/formatting/XmlFormatter.java
branchv_0
changeset 238 4a1864c3e867
parent 237 7e08730da258
child 239 39e6c2ad3571
--- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/XmlFormatter.java	Mon Mar 04 17:06:42 2019 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,245 +0,0 @@
-/**
- * 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.Functions.notNull;
-import info.globalcode.sql.dk.NamedParameter;
-import info.globalcode.sql.dk.configuration.PropertyDeclaration;
-import static info.globalcode.sql.dk.formatting.AbstractXmlFormatter.qname;
-import java.sql.Array;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.SQLXML;
-import java.util.ArrayList;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-import javax.xml.namespace.QName;
-
-/**
- * <p>
- * Prints machine-readable output – XML document containing resultsets and updates count. Good
- * choice for further processing – e.g. XSL transformation.</p>
- *
- * <p>
- * TODO: XSD</p>
- *
- * @author Ing. František Kučera (frantovo.cz)
- */
-@PropertyDeclaration(name = XmlFormatter.PROPERTY_LABELED_COLUMNS, defaultValue = "false", type = Boolean.class, description = "whether to add 'label' attribute to each 'column' element")
-public class XmlFormatter extends AbstractXmlFormatter {
-
-	public static final String NAME = "xml"; // bash-completion:formatter
-	public static final String PROPERTY_LABELED_COLUMNS = "labeledColumns";
-	private static final Logger log = Logger.getLogger(XmlFormatter.class.getName());
-	private final boolean labeledColumns;
-
-	public XmlFormatter(FormatterContext formatterContext) {
-		super(formatterContext);
-		labeledColumns = formatterContext.getProperties().getBoolean(PROPERTY_LABELED_COLUMNS, false);
-	}
-
-	@Override
-	public void writeStartBatch() {
-		super.writeStartBatch();
-		printStartDocument();
-		printStartElement(qname("batchResult"), 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 writeStartStatement() {
-		super.writeStartStatement();
-		printStartElement(qname("statement"));
-	}
-
-	@Override
-	public void writeEndStatement() {
-		super.writeEndStatement();
-		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 writeStartResultSet(ColumnsHeader header) {
-		super.writeStartResultSet(header);
-		printStartElement(qname("resultSet"));
-
-		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);
-		}
-	}
-
-	@Override
-	public void writeEndResultSet() {
-		super.writeEndResultSet();
-		printEndElement();
-	}
-
-	@Override
-	public void writeStartRow() {
-		super.writeStartRow();
-		printStartElement(qname("row"));
-	}
-
-	@Override
-	public void writeColumnValue(Object value) {
-		super.writeColumnValue(value);
-
-		Map<QName, String> attributes = null;
-		if (labeledColumns) {
-			attributes = new LinkedHashMap<>(2);
-			attributes.put(qname("label"), getCurrentColumnsHeader().getColumnDescriptors().get(getCurrentColumnsCount() - 1).getLabel());
-		}
-
-		if (value == null) {
-			if (attributes == null) {
-				attributes = new LinkedHashMap<>(2);
-			}
-			attributes.put(qname("null"), "true");
-			printEmptyElement(qname("column"), attributes);
-		} else if (value instanceof Array) {
-
-			Array sqlArray = (Array) value;
-			try {
-				Object[] array = (Object[]) sqlArray.getArray();
-				printStartElement(qname("column"), attributes);
-				printArray(array);
-				printEndElement();
-			} catch (SQLException e) {
-				// FIXME: rewrite array formatting, remember array mode, don't try sqlArray.getArray() again and again if it has failed
-				log.log(Level.SEVERE, "Unable to format array", e);
-				try {
-					ResultSet arrayResultSet = sqlArray.getResultSet();
-					//int columnCount = arrayResultSet.getMetaData().getColumnCount();
-					ArrayList<Object> arrayList = new ArrayList<>();
-					while (arrayResultSet.next()) {
-						arrayList.add(arrayResultSet.getObject(2));
-						// for (int i = 1; i <= columnCount; i++) {
-						// 	log.log(Level.INFO, "Array column {0} = {1}", new Object[]{i, arrayResultSet.getObject(i)});
-						// }
-					}
-
-					printStartElement(qname("column"), attributes);
-					// FIXME: instanceof SQLXML, see below
-					printArray(arrayList.toArray());
-					printEndElement();
-
-				} catch (SQLException e2) {
-					// FIXME: fix logging, error recovery
-					log.log(Level.SEVERE, "Second level fuck up !!!", e2);
-				}
-
-				writeColumnValue(String.valueOf(value));
-			}
-
-		} else if (value instanceof SQLXML) { // FIXME: move to separate method, to AbstractFormatter?
-			SQLXML xml = (SQLXML) value;
-			// TODO: parse DOM/SAX and transplant XML, don't escape (optional)
-			try {
-				printTextElement(qname("column"), attributes, xml.getString());
-			} catch (SQLException e) {
-				log.log(Level.SEVERE, "Unable to format XML", e);
-				writeColumnValue(String.valueOf(value));
-			}
-		} else {
-			printTextElement(qname("column"), attributes, toString(value));
-		}
-	}
-
-	private void printArray(Object[] array) {
-		printStartElement(qname("array"));
-		for (Object o : array) {
-			if (o instanceof Object[]) {
-				printStartElement(qname("item"));
-				printArray((Object[]) o);
-				printEndElement();
-			} else {
-				printTextElement(qname("item"), null, String.valueOf(o));
-			}
-		}
-		printEndElement();
-	}
-
-	@Override
-	public void writeEndRow() {
-		super.writeEndRow();
-		printEndElement();
-	}
-
-	@Override
-	public void writeUpdatesResult(int updatedRowsCount) {
-		super.writeUpdatesResult(updatedRowsCount);
-		printTextElement(qname("updatedRows"), null, String.valueOf(updatedRowsCount));
-	}
-
-	protected String toString(Object value) {
-		return String.valueOf(value);
-	}
-}