XmlFormatter: support (multidimensional) arrays v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Thu, 16 Jan 2014 12:28:08 +0100
branchv_0
changeset 165871185e406b3
parent 164 8cc7862605a1
child 166 5488c2dcf680
XmlFormatter: support (multidimensional) arrays
java/sql-dk/src/info/globalcode/sql/dk/formatting/XmlFormatter.java
     1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/XmlFormatter.java	Thu Jan 16 12:23:16 2014 +0100
     1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/XmlFormatter.java	Thu Jan 16 12:28:08 2014 +0100
     1.3 @@ -20,12 +20,16 @@
     1.4  import info.globalcode.sql.dk.Parameter;
     1.5  import info.globalcode.sql.dk.Xmlns;
     1.6  import info.globalcode.sql.dk.configuration.DatabaseDefinition;
     1.7 -import static info.globalcode.sql.dk.formatting.AbstractXmlFormatter.qname;
     1.8  import static info.globalcode.sql.dk.Functions.notNull;
     1.9  import info.globalcode.sql.dk.NamedParameter;
    1.10 +import static info.globalcode.sql.dk.formatting.AbstractXmlFormatter.qname;
    1.11 +import java.sql.Array;
    1.12 +import java.sql.SQLException;
    1.13  import java.util.LinkedHashMap;
    1.14  import java.util.List;
    1.15  import java.util.Map;
    1.16 +import java.util.logging.Level;
    1.17 +import java.util.logging.Logger;
    1.18  import javax.xml.namespace.QName;
    1.19  
    1.20  /**
    1.21 @@ -40,6 +44,7 @@
    1.22  
    1.23  	public static final String NAME = "xml"; // bash-completion:formatter
    1.24  	public static final String PROPERTY_LABELED_COLUMNS = "labeledColumns";
    1.25 +	private static final Logger log = Logger.getLogger(XmlFormatter.class.getName());
    1.26  	private final boolean labeledColumns;
    1.27  
    1.28  	public XmlFormatter(FormatterContext formatterContext) {
    1.29 @@ -152,10 +157,40 @@
    1.30  			}
    1.31  			attributes.put(qname("null"), "true");
    1.32  			printEmptyElement(qname("column"), attributes);
    1.33 +		} else if (value instanceof Array) {
    1.34 +			
    1.35 +			
    1.36 +			
    1.37 +			Array sqlArray = (Array) value;
    1.38 +			try {
    1.39 +				Object[] array = (Object[]) sqlArray.getArray();
    1.40 +				printStartElement(qname("column"), attributes);
    1.41 +				printArray(array);
    1.42 +				printEndElement();
    1.43 +			} catch (SQLException e) {
    1.44 +				log.log(Level.SEVERE, "Unable to format array", e);
    1.45 +				writeColumnValue(String.valueOf(value));
    1.46 +			}
    1.47 +			
    1.48 +			
    1.49  		} else {
    1.50  			printTextElement(qname("column"), attributes, toString(value));
    1.51  		}
    1.52  	}
    1.53 +	
    1.54 +	private void printArray(Object[] array) {
    1.55 +		printStartElement(qname("array"));
    1.56 +		for (Object o : array) {
    1.57 +			if (o instanceof Object[]) {
    1.58 +				printStartElement(qname("item"));
    1.59 +				printArray((Object[]) o);
    1.60 +				printEndElement();
    1.61 +			} else {
    1.62 +				printTextElement(qname("item"), null, String.valueOf(o));
    1.63 +			}
    1.64 +		}
    1.65 +		printEndElement();
    1.66 +	}
    1.67  
    1.68  	@Override
    1.69  	public void writeEndRow() {