Created
April 17, 2017 18:33
-
-
Save ssmusoke/ca4c55b4e52de97acb99a590644a677f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Available context bindings: | |
* COLUMNS List<DataColumn> | |
* ROWS Iterable<DataRow> | |
* OUT { append() } | |
* FORMATTER { format(row, col); formatValue(Object, col) } | |
* TRANSPOSED Boolean | |
* plus ALL_COLUMNS, TABLE, DIALECT | |
* | |
* where: | |
* DataRow { rowNumber(); first(); last(); data(): List<Object>; value(column): Object } | |
* DataColumn { columnNumber(), name() } | |
*/ | |
import java.util.regex.Pattern | |
NEWLINE = System.getProperty("line.separator") | |
pattern = Pattern.compile("[^\\w\\d]") | |
def escapeTag(name) { | |
name = pattern.matcher(name).replaceAll("_") | |
return name.isEmpty() || !Character.isLetter(name.charAt(0)) ? "_$name" : name | |
} | |
def printRow = { values, rowTag, namer, valueToString -> | |
OUT.append("$NEWLINE<$rowTag") | |
values.eachWithIndex { it, index -> | |
def tag = namer(it, index) | |
def str = valueToString(it) | |
OUT.append(/ $tag="$str"/) | |
} | |
OUT.append("/>") | |
} | |
OUT.append( | |
"""<?xml version="1.0" encoding="UTF-8"?> | |
<dataset>""") | |
if (!TRANSPOSED) { | |
ROWS.each { row -> printRow(COLUMNS, TABLE.getName(), {it, _ -> escapeTag(it.name())}) { FORMATTER.format(row, it) } } | |
} | |
else { | |
def values = COLUMNS.collect { new ArrayList<String>() } | |
ROWS.each { row -> COLUMNS.eachWithIndex { col, i -> values[i].add(FORMATTER.format(row, col)) } } | |
values.eachWithIndex { it, index -> printRow(it, escapeTag(COLUMNS[index].name()), { _, i -> "row${i + 1}" }, { it }) } | |
} | |
OUT.append(""" | |
</dataset> | |
""") |
Perfect work. But I got unexpected error in case of trying to export empty table. As I understand in line 40.
@brrshh Unfortunately I leveraged the existing code, I would expect the error for an empty table, just add one row and it should just work
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Perfect work. But I got unexpected error in case of trying to export empty table. As I understand in line 40.