Basic XHTML formatter v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 04 Jan 2014 22:38:59 +0100
branchv_0
changeset 134f2510551b5bf
parent 133 a1bf1465df89
child 135 ef432015fdee
Basic XHTML formatter
java/sql-dk/src/info/globalcode/sql/dk/Xmlns.java
java/sql-dk/src/info/globalcode/sql/dk/formatting/XhtmlFormatter.java
java/sql-dk/src/info/globalcode/sql/dk/formatting/XmlFormatter.java
     1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/Xmlns.java	Sat Jan 04 21:21:04 2014 +0100
     1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/Xmlns.java	Sat Jan 04 22:38:59 2014 +0100
     1.3 @@ -26,4 +26,8 @@
     1.4  
     1.5  	public static final String CONFIGURATION = "https://sql-dk.globalcode.info/xmlns/configuration";
     1.6  	public static final String BATCH_RESULT = "https://sql-dk.globalcode.info/xmlns/batchResult";
     1.7 +	public static final String XHTML = "http://www.w3.org/1999/xhtml";
     1.8 +
     1.9 +	private Xmlns() {
    1.10 +	}
    1.11  }
     2.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/XhtmlFormatter.java	Sat Jan 04 21:21:04 2014 +0100
     2.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/XhtmlFormatter.java	Sat Jan 04 22:38:59 2014 +0100
     2.3 @@ -17,6 +17,19 @@
     2.4   */
     2.5  package info.globalcode.sql.dk.formatting;
     2.6  
     2.7 +import info.globalcode.sql.dk.Constants;
     2.8 +import info.globalcode.sql.dk.NamedParameter;
     2.9 +import info.globalcode.sql.dk.Parameter;
    2.10 +import info.globalcode.sql.dk.Xmlns;
    2.11 +import info.globalcode.sql.dk.configuration.DatabaseDefinition;
    2.12 +import info.globalcode.sql.dk.configuration.Properties;
    2.13 +import info.globalcode.sql.dk.configuration.Property;
    2.14 +import static info.globalcode.sql.dk.formatting.AbstractXmlFormatter.qname;
    2.15 +import java.util.HashMap;
    2.16 +import java.util.List;
    2.17 +import java.util.Map;
    2.18 +import javax.xml.namespace.QName;
    2.19 +
    2.20  /**
    2.21   *
    2.22   * @author Ing. František Kučera (frantovo.cz)
    2.23 @@ -24,8 +37,162 @@
    2.24  public class XhtmlFormatter extends AbstractXmlFormatter {
    2.25  
    2.26  	public static final String NAME = "xhtml"; // bash-completion:formatter
    2.27 +	private int resultSetCounter = 0;
    2.28 +	private int updatesResultCounter = 0;
    2.29  
    2.30  	public XhtmlFormatter(FormatterContext formatterContext) {
    2.31 -		super(formatterContext);
    2.32 +		super(addDefaults(formatterContext));
    2.33 +	}
    2.34 +
    2.35 +	/**
    2.36 +	 * Do not indent text – preserve whitespace for pre elements
    2.37 +	 */
    2.38 +	private static FormatterContext addDefaults(FormatterContext formatterContext) {
    2.39 +		Properties defaults = new Properties(1);
    2.40 +		defaults.add(new Property(PROPERTY_INDENT_TEXT, "false"));
    2.41 +		formatterContext.getProperties().setLastDefaults(defaults);
    2.42 +		return formatterContext;
    2.43 +	}
    2.44 +
    2.45 +	@Override
    2.46 +	public void writeStartBatch() {
    2.47 +		super.writeStartBatch();
    2.48 +		printStartDocument();
    2.49 +		Map<QName, String> attributes = new HashMap<>(1);
    2.50 +		attributes.put(qname("xmlns"), Xmlns.XHTML);
    2.51 +		printStartElement(qname("html"), attributes);
    2.52 +
    2.53 +		printStartElement(qname("head"));
    2.54 +		printTextElement(qname("title"), null, Constants.PROGRAM_NAME + ": batch results");
    2.55 +		printEndElement();
    2.56 +
    2.57 +		printStartElement(qname("body"));
    2.58 +	}
    2.59 +
    2.60 +	@Override
    2.61 +	public void writeEndBatch() {
    2.62 +		super.writeEndBatch();
    2.63 +		printEndElement();
    2.64 +		printEndElement();
    2.65 +		printEndDocument();
    2.66 +	}
    2.67 +
    2.68 +	@Override
    2.69 +	public void writeStartDatabase(DatabaseDefinition databaseDefinition) {
    2.70 +		super.writeStartDatabase(databaseDefinition);
    2.71 +		printTextElement(qname("h1"), null, "Database: " + databaseDefinition.getName());
    2.72 +	}
    2.73 +
    2.74 +	@Override
    2.75 +	public void writeQuery(String sql) {
    2.76 +		super.writeQuery(sql);
    2.77 +		printTextElement(qname("h3"), null, "SQL:");
    2.78 +		printTextElement(qname("pre"), null, sql);
    2.79 +	}
    2.80 +
    2.81 +	@Override
    2.82 +	public void writeParameters(List<? extends Parameter> parameters) {
    2.83 +		super.writeParameters(parameters);
    2.84 +
    2.85 +		if (parameters == null || parameters.isEmpty()) {
    2.86 +			printTextElement(qname("p"), null, "(this query has no parameters)");
    2.87 +		} else {
    2.88 +			printTextElement(qname("h3"), null, "Parameters:");
    2.89 +
    2.90 +			printStartElement(qname("table"));
    2.91 +
    2.92 +			printStartElement(qname("theader"));
    2.93 +			printStartElement(qname("tr"));
    2.94 +			printTextElement(qname("td"), null, "number/name");
    2.95 +			printTextElement(qname("td"), null, "type");
    2.96 +			printTextElement(qname("td"), null, "value");
    2.97 +			printEndElement();
    2.98 +			printEndElement();
    2.99 +
   2.100 +			printStartElement(qname("tbody"));
   2.101 +			for (int i = 0; i < parameters.size(); i++) {
   2.102 +				Parameter p = parameters.get(i);
   2.103 +				printStartElement(qname("tr"));
   2.104 +				String numberOrName;
   2.105 +				if (p instanceof NamedParameter) {
   2.106 +					numberOrName = ((NamedParameter) p).getName();
   2.107 +				} else {
   2.108 +					numberOrName = String.valueOf(i + 1);
   2.109 +				}
   2.110 +				printTextElement(qname("td"), null, numberOrName);
   2.111 +				printTextElement(qname("td"), null, p.getType().name());
   2.112 +				printTextElement(qname("td"), null, toString(p.getValue()));
   2.113 +				printEndElement();
   2.114 +			}
   2.115 +			printEndElement();
   2.116 +
   2.117 +			printEndElement();
   2.118 +		}
   2.119 +	}
   2.120 +
   2.121 +	@Override
   2.122 +	public void writeColumnsHeader(ColumnsHeader header) {
   2.123 +		super.writeColumnsHeader(header);
   2.124 +		printTextElement(qname("h3"), null, "Data:");
   2.125 +		printStartElement(qname("table"));
   2.126 +		printStartElement(qname("theader"));
   2.127 +		printStartElement(qname("tr"));
   2.128 +		for (ColumnDescriptor cd : header.getColumnDescriptors()) {
   2.129 +			// TODO: type
   2.130 +			printTextElement(qname("td"), null, cd.getLabel());
   2.131 +		}
   2.132 +		printEndElement();
   2.133 +		printEndElement();
   2.134 +
   2.135 +		printStartElement(qname("tbody"));
   2.136 +	}
   2.137 +
   2.138 +	@Override
   2.139 +	public void writeStartRow() {
   2.140 +		super.writeStartRow();
   2.141 +		printStartElement(qname("tr"));
   2.142 +	}
   2.143 +
   2.144 +	@Override
   2.145 +	public void writeColumnValue(Object value) {
   2.146 +		super.writeColumnValue(value);
   2.147 +		printTextElement(qname("td"), null, toString(value));
   2.148 +	}
   2.149 +
   2.150 +	@Override
   2.151 +	public void writeEndRow() {
   2.152 +		super.writeEndRow();
   2.153 +		printEndElement();
   2.154 +	}
   2.155 +
   2.156 +	@Override
   2.157 +	public void writeStartResultSet() {
   2.158 +		super.writeStartResultSet();
   2.159 +		resultSetCounter++;
   2.160 +		printTextElement(qname("h2"), null, "Result set " + resultSetCounter);
   2.161 +	}
   2.162 +
   2.163 +	@Override
   2.164 +	public void writeEndResultSet() {
   2.165 +		super.writeEndResultSet();
   2.166 +		printEndElement();
   2.167 +		printEndElement();
   2.168 +	}
   2.169 +
   2.170 +	@Override
   2.171 +	public void writeStartUpdatesResult() {
   2.172 +		super.writeStartUpdatesResult();
   2.173 +		updatesResultCounter++;
   2.174 +		printTextElement(qname("h2"), null, "Updates result " + updatesResultCounter);
   2.175 +	}
   2.176 +
   2.177 +	@Override
   2.178 +	public void writeUpdatedRowsCount(int updatedRowsCount) {
   2.179 +		super.writeUpdatedRowsCount(updatedRowsCount);
   2.180 +		printTextElement(qname("p"), null, "Updated rows: " + updatedRowsCount);
   2.181 +	}
   2.182 +
   2.183 +	protected String toString(Object value) {
   2.184 +		return String.valueOf(value);
   2.185  	}
   2.186  }
     3.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/XmlFormatter.java	Sat Jan 04 21:21:04 2014 +0100
     3.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/XmlFormatter.java	Sat Jan 04 22:38:59 2014 +0100
     3.3 @@ -53,7 +53,6 @@
     3.4  	@Override
     3.5  	public void writeEndBatch() {
     3.6  		super.writeEndBatch();
     3.7 -
     3.8  		printEndElement();
     3.9  		printEndDocument();
    3.10  	}