--- a/java/sql-dk/src/info/globalcode/sql/dk/Xmlns.java Sat Jan 04 19:38:51 2014 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/Xmlns.java Sat Jan 04 19:39:35 2014 +0100
@@ -25,4 +25,5 @@
public class Xmlns {
public static final String CONFIGURATION = "https://sql-dk.globalcode.info/xmlns/configuration";
+ public static final String BATCH_RESULT = "https://sql-dk.globalcode.info/xmlns/batchResult";
}
--- a/java/sql-dk/src/info/globalcode/sql/dk/configuration/Configuration.java Sat Jan 04 19:38:51 2014 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/configuration/Configuration.java Sat Jan 04 19:39:35 2014 +0100
@@ -24,6 +24,7 @@
import info.globalcode.sql.dk.formatting.TabularFormatter;
import info.globalcode.sql.dk.formatting.TabularPrefetchingFormatter;
import info.globalcode.sql.dk.formatting.TabularWrappingFormatter;
+import info.globalcode.sql.dk.formatting.XhtmlFormatter;
import info.globalcode.sql.dk.formatting.XmlFormatter;
import java.util.ArrayList;
import java.util.Collection;
@@ -62,6 +63,7 @@
l.add(new FormatterDefinition(SilentFormatter.NAME, SilentFormatter.class.getName()));
l.add(new FormatterDefinition(SingleValueFormatter.NAME, SingleValueFormatter.class.getName()));
l.add(new FormatterDefinition(XmlFormatter.NAME, XmlFormatter.class.getName()));
+ l.add(new FormatterDefinition(XhtmlFormatter.NAME, XhtmlFormatter.class.getName()));
l.add(new FormatterDefinition(TabularFormatter.NAME, TabularFormatter.class.getName()));
l.add(new FormatterDefinition(TabularPrefetchingFormatter.NAME, TabularPrefetchingFormatter.class.getName()));
l.add(new FormatterDefinition(TabularWrappingFormatter.NAME, TabularWrappingFormatter.class.getName()));
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/AbstractXmlFormatter.java Sat Jan 04 19:39:35 2014 +0100
@@ -0,0 +1,206 @@
+/**
+ * SQL-DK
+ * Copyright © 2014 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.ColorfulPrintWriter;
+import info.globalcode.sql.dk.ColorfulPrintWriter.TerminalColor;
+import java.util.Stack;
+import javax.xml.namespace.QName;
+import static info.globalcode.sql.dk.Functions.isEmpty;
+import static info.globalcode.sql.dk.Functions.toHex;
+import java.nio.charset.Charset;
+import java.util.EmptyStackException;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ *
+ * @author Ing. František Kučera (frantovo.cz)
+ */
+public abstract class AbstractXmlFormatter extends AbstractFormatter {
+
+ private static final Logger log = Logger.getLogger(AbstractXmlFormatter.class.getName());
+ public static final String PROPERTY_COLORFUL = "color";
+ public static final String PROPERTY_INDENT = "indent";
+ private static final TerminalColor ELEMENT_COLOR = TerminalColor.Magenta;
+ private static final TerminalColor ATTRIBUTE_NAME_COLOR = TerminalColor.Green;
+ private static final TerminalColor ATTRIBUTE_VALUE_COLOR = TerminalColor.Yellow;
+ private static final TerminalColor XML_DECLARATION_COLOR = TerminalColor.Red;
+ private Stack<QName> treePosition = new Stack<>();
+ private final ColorfulPrintWriter out;
+ private final String indent;
+
+ public AbstractXmlFormatter(FormatterContext formatterContext) {
+ super(formatterContext);
+ boolean colorful = formatterContext.getProperties().getBoolean(PROPERTY_COLORFUL, false);
+ out = new ColorfulPrintWriter(formatterContext.getOutputStream(), false, colorful);
+ indent = formatterContext.getProperties().getString(PROPERTY_INDENT, "\t");
+
+ if (!indent.matches("\\s*")) {
+ log.log(Level.WARNING, "Setting indent to „{0}“ is weird & freaky; in hex: {1}", new Object[]{indent, toHex(indent.getBytes())});
+ }
+
+ }
+
+ protected void printStartDocument() {
+ out.print(XML_DECLARATION_COLOR, "<?xml version=\"1.0\" encoding=\"" + Charset.defaultCharset().name() + "\"?>");
+ }
+
+ protected void printEndDocument() {
+ out.println();
+ out.flush();
+ if (!treePosition.empty()) {
+ throw new IllegalStateException("Some elements are not closed: " + treePosition);
+ }
+ }
+
+ protected void printStartElement(QName element) {
+ printStartElement(element, null);
+ }
+
+ protected Map<QName, String> singleAttribute(QName name, String value) {
+ Map<QName, String> attributes = new HashMap<>(1);
+ attributes.put(name, value);
+ return attributes;
+ }
+
+ protected void printStartElement(QName element, Map<QName, String> attributes) {
+ printStartElement(element, attributes, false);
+ }
+
+ /**
+ * @param empty whether element should be closed <codfe>… /></code> (has no content, do not
+ * call {@linkplain #printEndElement()})
+ */
+ private void printStartElement(QName element, Map<QName, String> attributes, boolean empty) {
+ printIndent();
+
+ out.print(ELEMENT_COLOR, "<" + toString(element));
+
+ if (attributes != null) {
+ for (Entry<QName, String> attribute : attributes.entrySet()) {
+ out.print(" ");
+ out.print(ATTRIBUTE_NAME_COLOR, toString(attribute.getKey()));
+ out.print("=");
+ out.print(ATTRIBUTE_VALUE_COLOR, '"' + escapeXmlAttribute(attribute.getValue()) + '"');
+ }
+ }
+
+ if (empty) {
+ out.print(ELEMENT_COLOR, "/>");
+ } else {
+ out.print(ELEMENT_COLOR, ">");
+ treePosition.add(element);
+ }
+
+ out.flush();
+ }
+
+ /**
+ * Prints text node wrapped in given element without indenting the text and adding line breaks
+ * (useful for short texts).
+ *
+ * @param attributes use {@linkplain LinkedHashMap} to preserve attributes order
+ */
+ protected void printTextElement(QName element, Map<QName, String> attributes, String text) {
+ printStartElement(element, attributes);
+ printText(text, false);
+ printEndElement(false);
+ }
+
+ protected void printEmptyElement(QName element, Map<QName, String> attributes) {
+ printStartElement(element, attributes, true);
+ }
+
+ protected void printEndElement() {
+ printEndElement(true);
+ }
+
+ private void printEndElement(boolean indent) {
+ try {
+ QName name = treePosition.pop();
+
+ if (indent) {
+ printIndent();
+ }
+
+ out.print(ELEMENT_COLOR, "</" + toString(name) + ">");
+ out.flush();
+
+ } catch (EmptyStackException e) {
+ throw new IllegalStateException("No more elements to end.", e);
+ }
+ }
+
+ protected void printText(String s) {
+ printText(s, true);
+ }
+
+ private void printText(String s, boolean indent) {
+ if (indent) {
+ printIndent();
+ }
+ out.print(escapeXmlText(s));
+ out.flush();
+ }
+
+ protected void printIndent() {
+ out.println();
+ for (int i = 0; i < treePosition.size(); i++) {
+ out.print(indent);
+ }
+ }
+
+ protected static QName qname(String name) {
+ return new QName(name);
+ }
+
+ protected static QName qname(String prefix, String name) {
+ return new QName(null, name, prefix);
+ }
+
+ private String toString(QName name) {
+ if (isEmpty(name.getPrefix(), true)) {
+ return escapeName(name.getLocalPart());
+ } else {
+ return escapeName(name.getPrefix()) + ":" + escapeName(name.getLocalPart());
+ }
+ }
+
+ private String escapeName(String s) {
+ // TODO: avoid ugly values in <name name="…"/>
+ return s;
+ }
+
+ private static String escapeXmlText(String s) {
+ return s.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">");
+ // Not needed:
+ // return s.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">").replaceAll("\"", """).replaceAll("'", "'");
+ }
+
+ /**
+ * Expects attribute values enclosed in "quotes" not 'apostrophes'.
+ */
+ private static String escapeXmlAttribute(String s) {
+ return s.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">").replaceAll("\"", """);
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/XhtmlFormatter.java Sat Jan 04 19:39:35 2014 +0100
@@ -0,0 +1,31 @@
+/**
+ * SQL-DK
+ * Copyright © 2014 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;
+
+/**
+ *
+ * @author Ing. František Kučera (frantovo.cz)
+ */
+public class XhtmlFormatter extends AbstractXmlFormatter {
+
+ public static final String NAME = "xhtml"; // bash-completion:formatter
+
+ public XhtmlFormatter(FormatterContext formatterContext) {
+ super(formatterContext);
+ }
+}
--- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/XmlFormatter.java Sat Jan 04 19:38:51 2014 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/XmlFormatter.java Sat Jan 04 19:39:35 2014 +0100
@@ -17,15 +17,103 @@
*/
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 AbstractFormatter {
+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);
+ }
+ }
}