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 138 b765713c60e9
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
--- a/java/sql-dk/data/info/globalcode/sql/dk/formatter/XhtmlFormatter.css	Sat Jan 04 23:44:34 2014 +0100
+++ b/java/sql-dk/data/info/globalcode/sql/dk/formatter/XhtmlFormatter.css	Sun Jan 05 00:17:27 2014 +0100
@@ -42,4 +42,12 @@
 tbody tr:hover {
 	background-color: #eee;
 	color:black;
-}
\ No newline at end of file
+}
+
+table ul {
+	margin: 0px;
+}
+
+table li {
+	padding-right: 10px;
+}
--- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/XhtmlFormatter.java	Sat Jan 04 23:44:34 2014 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/XhtmlFormatter.java	Sun Jan 05 00:17:27 2014 +0100
@@ -25,11 +25,15 @@
 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.sql.Array;
+import java.sql.SQLException;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Scanner;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 import javax.xml.namespace.QName;
 
 /**
@@ -38,6 +42,7 @@
  */
 public class XhtmlFormatter extends AbstractXmlFormatter {
 
+	private static final Logger log = Logger.getLogger(XhtmlFormatter.class.getName());
 	public static final String NAME = "xhtml"; // bash-completion:formatter
 	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\"";
 	private static final String CSS_FILE = "info/globalcode/sql/dk/formatter/XhtmlFormatter.css";
@@ -155,13 +160,42 @@
 	}
 
 	private void printTableData(Object value) {
-		Map<QName, String> attributes = new HashMap<>(1);
-		if (value instanceof Number) {
-			attributes.put(qname("class"), "number");
-		} else if (value instanceof Boolean) {
-			attributes.put(qname("class"), "boolean");
+
+		if (value instanceof Array) {
+			Array sqlArray = (Array) value;
+			try {
+				Object[] array = (Object[]) sqlArray.getArray();
+				printStartElement(qname("td"));
+				printArray(array);
+				printEndElement();
+			} catch (SQLException e) {
+				log.log(Level.SEVERE, "Unable to format array", e);
+				printTableData(String.valueOf(value));
+			}
+		} else {
+			Map<QName, String> attributes = new HashMap<>(1);
+			if (value instanceof Number) {
+				attributes.put(qname("class"), "number");
+			} else if (value instanceof Boolean) {
+				attributes.put(qname("class"), "boolean");
+			}
+			printTextElement(qname("td"), attributes, String.valueOf(value));
 		}
-		printTextElement(qname("td"), attributes, String.valueOf(value));
+	}
+
+	private void printArray(Object[] array) {
+		printStartElement(qname("ul"));
+		for (Object o : array) {
+			if (o instanceof Object[]) {
+				printStartElement(qname("li"));
+				printTextElement(qname("p"), null, "nested array:");
+				printArray((Object[]) o);
+				printEndElement();
+			} else {
+				printTextElement(qname("li"), null, String.valueOf(o));
+			}
+		}
+		printEndElement();
 	}
 
 	@Override