Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'src/current/tools/csv2xml')
-rwxr-xr-xsrc/current/tools/csv2xml110
1 files changed, 110 insertions, 0 deletions
diff --git a/src/current/tools/csv2xml b/src/current/tools/csv2xml
new file mode 100755
index 0000000..3a5d1b8
--- /dev/null
+++ b/src/current/tools/csv2xml
@@ -0,0 +1,110 @@
+#!/usr/bin/awk -f
+#
+# Compiles the given CSV into a table definition
+
+
+function columngen( header )
+{
+ # output a field constant for each field in the header
+ i = 0
+ while ( field = header[ ++i ] )
+ {
+ printf " <t:table-column name=\"%s\" " \
+ "index=\"%d\" seq=\"%s\" />\n",
+ field,
+ ( i - 1 ),
+ ( seq[ i ] ) ? "true" : "false"
+ }
+}
+
+
+function seqchk( last )
+{
+ # if there's no last row, then do not bother
+ i = 0
+ while ( i++ < NF )
+ {
+ if ( seq[ i ] == "" ) seq[ i ] = 1
+
+ # this field is sequential if it is greater than or equal to the last field
+ # (we don't check for descending [yet]); note that on the first check, last
+ # will be empty and therefore this check will succeed (properly
+ # initializing seq[i] to 1)
+ seq[ i ] = seq[ i ] && ( $(i) >= last[ i ] )
+ }
+}
+
+
+# header
+BEGIN {
+ rootpath = "../../../"
+ file = ARGV[1]
+
+ # grab only the filename (remove all preceding directories and the file ext)
+ name = gensub( /^.*\/|\.[^.]+$/, "", "g", file )
+
+
+ # output package header
+ printf \
+ "<?xml-stylesheet type=\"text/xsl\" href=\"%1$srater/summary.xsl\"?>\n" \
+ "<package\n" \
+ " xmlns=\"http://www.lovullo.com/rater\"\n" \
+ " xmlns:c=\"http://www.lovullo.com/calc\"\n" \
+ " xmlns:t=\"http://www.lovullo.com/rater/apply-template\"\n" \
+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" \
+ " xsi:schemaLocation=\"http://www.lovullo.com/rater %1$srater/rater.xsd\"\n\n" \
+ " name=\"suppliers/rates/tables/%2$s\"\n" \
+ " desc=\"%2$s rate table\">\n\n" \
+ " <!--\n" \
+ " WARNING: This file was generated by csv2xml; do not modify!\n" \
+ " -->\n\n" \
+ " <import package=\"/rater/core\" />\n" \
+ " <import package=\"/rater/core/vector/table\" />\n\n", \
+ rootpath, name
+
+ # the first row of the CSV is the header representing the column identifiers
+ getline
+ split( $0, header, /,/ )
+
+ # table constant identifier
+ tconst = toupper( gensub( /-/, "_", "g", name ) ) "_RATE_TABLE"
+
+ # generate the header for the table constant
+ printf " <t:create-table name=\"%s\">\n", name
+
+ printf "%s", " <t:table-rows data=\"\n"
+
+ # delimit fields by commas (the field separator for CSVs); note that this
+ # won't work properly if strings contain commas
+ FS = ","
+}
+
+
+# each row of the CSV
+{
+ # generate value string for each field
+ i = 0
+ while ( i++ < NF )
+ {
+ printf "%s", ( ( i > 1 ) ? "," : "" ) $(i)
+ }
+
+ print ";"
+
+ seqchk( last )
+ split( $0, last )
+}
+
+
+# footer
+END {
+ # end of table-rows node
+ print "\" />"
+
+ # columns can't be generated until after we know which ones represent
+ # sequential data
+ columngen( header )
+
+ print " </t:create-table>"
+ print "</package>"
+}