XHTML formatter: simple formatting of (multidimensional) arrays v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sun, 05 Jan 2014 00:17:27 +0100
branchv_0
changeset 138b765713c60e9
parent 137 3a24be5d8dac
child 139 5c0e344c3b60
XHTML formatter: simple formatting of (multidimensional) arrays
java/sql-dk/data/info/globalcode/sql/dk/formatter/XhtmlFormatter.css
java/sql-dk/src/info/globalcode/sql/dk/formatting/XhtmlFormatter.java
     1.1 --- a/java/sql-dk/data/info/globalcode/sql/dk/formatter/XhtmlFormatter.css	Sat Jan 04 23:44:34 2014 +0100
     1.2 +++ b/java/sql-dk/data/info/globalcode/sql/dk/formatter/XhtmlFormatter.css	Sun Jan 05 00:17:27 2014 +0100
     1.3 @@ -42,4 +42,12 @@
     1.4  tbody tr:hover {
     1.5  	background-color: #eee;
     1.6  	color:black;
     1.7 -}
     1.8 \ No newline at end of file
     1.9 +}
    1.10 +
    1.11 +table ul {
    1.12 +	margin: 0px;
    1.13 +}
    1.14 +
    1.15 +table li {
    1.16 +	padding-right: 10px;
    1.17 +}
     2.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/XhtmlFormatter.java	Sat Jan 04 23:44:34 2014 +0100
     2.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/XhtmlFormatter.java	Sun Jan 05 00:17:27 2014 +0100
     2.3 @@ -25,11 +25,15 @@
     2.4  import info.globalcode.sql.dk.configuration.Properties;
     2.5  import info.globalcode.sql.dk.configuration.Property;
     2.6  import static info.globalcode.sql.dk.formatting.AbstractXmlFormatter.qname;
     2.7 +import java.sql.Array;
     2.8 +import java.sql.SQLException;
     2.9  import java.util.Date;
    2.10  import java.util.HashMap;
    2.11  import java.util.List;
    2.12  import java.util.Map;
    2.13  import java.util.Scanner;
    2.14 +import java.util.logging.Level;
    2.15 +import java.util.logging.Logger;
    2.16  import javax.xml.namespace.QName;
    2.17  
    2.18  /**
    2.19 @@ -38,6 +42,7 @@
    2.20   */
    2.21  public class XhtmlFormatter extends AbstractXmlFormatter {
    2.22  
    2.23 +	private static final Logger log = Logger.getLogger(XhtmlFormatter.class.getName());
    2.24  	public static final String NAME = "xhtml"; // bash-completion:formatter
    2.25  	private static final String DOCTYPE = "html PUBLIC \"-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN\" \"http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg.dtd\"";
    2.26  	private static final String CSS_FILE = "info/globalcode/sql/dk/formatter/XhtmlFormatter.css";
    2.27 @@ -155,13 +160,42 @@
    2.28  	}
    2.29  
    2.30  	private void printTableData(Object value) {
    2.31 -		Map<QName, String> attributes = new HashMap<>(1);
    2.32 -		if (value instanceof Number) {
    2.33 -			attributes.put(qname("class"), "number");
    2.34 -		} else if (value instanceof Boolean) {
    2.35 -			attributes.put(qname("class"), "boolean");
    2.36 +
    2.37 +		if (value instanceof Array) {
    2.38 +			Array sqlArray = (Array) value;
    2.39 +			try {
    2.40 +				Object[] array = (Object[]) sqlArray.getArray();
    2.41 +				printStartElement(qname("td"));
    2.42 +				printArray(array);
    2.43 +				printEndElement();
    2.44 +			} catch (SQLException e) {
    2.45 +				log.log(Level.SEVERE, "Unable to format array", e);
    2.46 +				printTableData(String.valueOf(value));
    2.47 +			}
    2.48 +		} else {
    2.49 +			Map<QName, String> attributes = new HashMap<>(1);
    2.50 +			if (value instanceof Number) {
    2.51 +				attributes.put(qname("class"), "number");
    2.52 +			} else if (value instanceof Boolean) {
    2.53 +				attributes.put(qname("class"), "boolean");
    2.54 +			}
    2.55 +			printTextElement(qname("td"), attributes, String.valueOf(value));
    2.56  		}
    2.57 -		printTextElement(qname("td"), attributes, String.valueOf(value));
    2.58 +	}
    2.59 +
    2.60 +	private void printArray(Object[] array) {
    2.61 +		printStartElement(qname("ul"));
    2.62 +		for (Object o : array) {
    2.63 +			if (o instanceof Object[]) {
    2.64 +				printStartElement(qname("li"));
    2.65 +				printTextElement(qname("p"), null, "nested array:");
    2.66 +				printArray((Object[]) o);
    2.67 +				printEndElement();
    2.68 +			} else {
    2.69 +				printTextElement(qname("li"), null, String.valueOf(o));
    2.70 +			}
    2.71 +		}
    2.72 +		printEndElement();
    2.73  	}
    2.74  
    2.75  	@Override