Skip to content

Instantly share code, notes, and snippets.

@matthesrieke
Last active August 29, 2015 14:16
Show Gist options
  • Save matthesrieke/3de28bfd0ab14f225ae8 to your computer and use it in GitHub Desktop.
Save matthesrieke/3de28bfd0ab14f225ae8 to your computer and use it in GitHub Desktop.
CSV to TWiki Table Markup
package org.n52.wps.csv2wiki;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
/**
* CSV to twiki table markup. Uses http://commons.apache.org/proper/commons-csv/
*
* @author matthes rieke
*
*/
public class CSV2TWikiProcess {
public static void main( String[] args ) throws IOException {
InputStream res = CSV2TWikiProcess.class.getResourceAsStream("/test.csv");
String s = new CSV2TWikiProcess().transform(res);
System.out.println(s);
}
public String transform(InputStream csv) throws IOException {
CSVParser parser = CSVFormat.DEFAULT.parse(new InputStreamReader(csv));
StringBuilder sb = new StringBuilder();
boolean header = true;
for (CSVRecord h : parser.getRecords()) {
for (int i = 0; i < h.size(); i++) {
if (header) {
sb.append("| *");
sb.append(h.get(i));
sb.append("* ");
}
else {
sb.append("| ");
sb.append(h.get(i));
sb.append(" ");
}
}
sb.append("|");
sb.append(System.getProperty("line.separator"));
header = false;
}
return sb.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment