# HG changeset patch # User František Kučera # Date 1388871539 -3600 # Node ID f2510551b5bf1445393985e919b97b8576d906b2 # Parent a1bf1465df89f56512c6639b3210e7a01fc816fc Basic XHTML formatter diff -r a1bf1465df89 -r f2510551b5bf java/sql-dk/src/info/globalcode/sql/dk/Xmlns.java --- a/java/sql-dk/src/info/globalcode/sql/dk/Xmlns.java Sat Jan 04 21:21:04 2014 +0100 +++ b/java/sql-dk/src/info/globalcode/sql/dk/Xmlns.java Sat Jan 04 22:38:59 2014 +0100 @@ -26,4 +26,8 @@ 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"; + public static final String XHTML = "http://www.w3.org/1999/xhtml"; + + private Xmlns() { + } } diff -r a1bf1465df89 -r f2510551b5bf java/sql-dk/src/info/globalcode/sql/dk/formatting/XhtmlFormatter.java --- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/XhtmlFormatter.java Sat Jan 04 21:21:04 2014 +0100 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/XhtmlFormatter.java Sat Jan 04 22:38:59 2014 +0100 @@ -17,6 +17,19 @@ */ package info.globalcode.sql.dk.formatting; +import info.globalcode.sql.dk.Constants; +import info.globalcode.sql.dk.NamedParameter; +import info.globalcode.sql.dk.Parameter; +import info.globalcode.sql.dk.Xmlns; +import info.globalcode.sql.dk.configuration.DatabaseDefinition; +import info.globalcode.sql.dk.configuration.Properties; +import info.globalcode.sql.dk.configuration.Property; +import static info.globalcode.sql.dk.formatting.AbstractXmlFormatter.qname; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import javax.xml.namespace.QName; + /** * * @author Ing. František Kučera (frantovo.cz) @@ -24,8 +37,162 @@ public class XhtmlFormatter extends AbstractXmlFormatter { public static final String NAME = "xhtml"; // bash-completion:formatter + private int resultSetCounter = 0; + private int updatesResultCounter = 0; public XhtmlFormatter(FormatterContext formatterContext) { - super(formatterContext); + super(addDefaults(formatterContext)); + } + + /** + * Do not indent text – preserve whitespace for pre elements + */ + private static FormatterContext addDefaults(FormatterContext formatterContext) { + Properties defaults = new Properties(1); + defaults.add(new Property(PROPERTY_INDENT_TEXT, "false")); + formatterContext.getProperties().setLastDefaults(defaults); + return formatterContext; + } + + @Override + public void writeStartBatch() { + super.writeStartBatch(); + printStartDocument(); + Map attributes = new HashMap<>(1); + attributes.put(qname("xmlns"), Xmlns.XHTML); + printStartElement(qname("html"), attributes); + + printStartElement(qname("head")); + printTextElement(qname("title"), null, Constants.PROGRAM_NAME + ": batch results"); + printEndElement(); + + printStartElement(qname("body")); + } + + @Override + public void writeEndBatch() { + super.writeEndBatch(); + printEndElement(); + printEndElement(); + printEndDocument(); + } + + @Override + public void writeStartDatabase(DatabaseDefinition databaseDefinition) { + super.writeStartDatabase(databaseDefinition); + printTextElement(qname("h1"), null, "Database: " + databaseDefinition.getName()); + } + + @Override + public void writeQuery(String sql) { + super.writeQuery(sql); + printTextElement(qname("h3"), null, "SQL:"); + printTextElement(qname("pre"), null, sql); + } + + @Override + public void writeParameters(List parameters) { + super.writeParameters(parameters); + + if (parameters == null || parameters.isEmpty()) { + printTextElement(qname("p"), null, "(this query has no parameters)"); + } else { + printTextElement(qname("h3"), null, "Parameters:"); + + printStartElement(qname("table")); + + printStartElement(qname("theader")); + printStartElement(qname("tr")); + printTextElement(qname("td"), null, "number/name"); + printTextElement(qname("td"), null, "type"); + printTextElement(qname("td"), null, "value"); + printEndElement(); + printEndElement(); + + printStartElement(qname("tbody")); + for (int i = 0; i < parameters.size(); i++) { + Parameter p = parameters.get(i); + printStartElement(qname("tr")); + String numberOrName; + if (p instanceof NamedParameter) { + numberOrName = ((NamedParameter) p).getName(); + } else { + numberOrName = String.valueOf(i + 1); + } + printTextElement(qname("td"), null, numberOrName); + printTextElement(qname("td"), null, p.getType().name()); + printTextElement(qname("td"), null, toString(p.getValue())); + printEndElement(); + } + printEndElement(); + + printEndElement(); + } + } + + @Override + public void writeColumnsHeader(ColumnsHeader header) { + super.writeColumnsHeader(header); + printTextElement(qname("h3"), null, "Data:"); + printStartElement(qname("table")); + printStartElement(qname("theader")); + printStartElement(qname("tr")); + for (ColumnDescriptor cd : header.getColumnDescriptors()) { + // TODO: type + printTextElement(qname("td"), null, cd.getLabel()); + } + printEndElement(); + printEndElement(); + + printStartElement(qname("tbody")); + } + + @Override + public void writeStartRow() { + super.writeStartRow(); + printStartElement(qname("tr")); + } + + @Override + public void writeColumnValue(Object value) { + super.writeColumnValue(value); + printTextElement(qname("td"), null, toString(value)); + } + + @Override + public void writeEndRow() { + super.writeEndRow(); + printEndElement(); + } + + @Override + public void writeStartResultSet() { + super.writeStartResultSet(); + resultSetCounter++; + printTextElement(qname("h2"), null, "Result set " + resultSetCounter); + } + + @Override + public void writeEndResultSet() { + super.writeEndResultSet(); + printEndElement(); + printEndElement(); + } + + @Override + public void writeStartUpdatesResult() { + super.writeStartUpdatesResult(); + updatesResultCounter++; + printTextElement(qname("h2"), null, "Updates result " + updatesResultCounter); + } + + @Override + public void writeUpdatedRowsCount(int updatedRowsCount) { + super.writeUpdatedRowsCount(updatedRowsCount); + printTextElement(qname("p"), null, "Updated rows: " + updatedRowsCount); + } + + protected String toString(Object value) { + return String.valueOf(value); } } diff -r a1bf1465df89 -r f2510551b5bf java/sql-dk/src/info/globalcode/sql/dk/formatting/XmlFormatter.java --- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/XmlFormatter.java Sat Jan 04 21:21:04 2014 +0100 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/XmlFormatter.java Sat Jan 04 22:38:59 2014 +0100 @@ -53,7 +53,6 @@ @Override public void writeEndBatch() { super.writeEndBatch(); - printEndElement(); printEndDocument(); }