Correcting error introduced - column headers are now printed on exporting to CSV.
git-svn-id: http://google-refine.googlecode.com/svn/trunk@901 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
parent
27a8e8db6e
commit
c21c472434
@ -1,109 +1,111 @@
|
|||||||
package com.metaweb.gridworks.exporters;
|
package com.metaweb.gridworks.exporters;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import com.metaweb.gridworks.browsing.Engine;
|
import com.metaweb.gridworks.browsing.Engine;
|
||||||
import com.metaweb.gridworks.browsing.FilteredRows;
|
import com.metaweb.gridworks.browsing.FilteredRows;
|
||||||
import com.metaweb.gridworks.browsing.RowVisitor;
|
import com.metaweb.gridworks.browsing.RowVisitor;
|
||||||
import com.metaweb.gridworks.model.Cell;
|
import com.metaweb.gridworks.model.Cell;
|
||||||
import com.metaweb.gridworks.model.Column;
|
import com.metaweb.gridworks.model.Column;
|
||||||
import com.metaweb.gridworks.model.Project;
|
import com.metaweb.gridworks.model.Project;
|
||||||
import com.metaweb.gridworks.model.Row;
|
import com.metaweb.gridworks.model.Row;
|
||||||
|
|
||||||
import au.com.bytecode.opencsv.CSVWriter;
|
import au.com.bytecode.opencsv.CSVWriter;
|
||||||
|
|
||||||
|
|
||||||
public class CsvExporter implements Exporter {
|
public class CsvExporter implements Exporter {
|
||||||
|
|
||||||
final static Logger logger = LoggerFactory.getLogger("CsvExporter");
|
final static Logger logger = LoggerFactory.getLogger("CsvExporter");
|
||||||
char separator;
|
char separator;
|
||||||
|
|
||||||
public CsvExporter() {
|
public CsvExporter() {
|
||||||
separator = ','; //Comma separated-value is default
|
separator = ','; //Comma separated-value is default
|
||||||
}
|
}
|
||||||
|
|
||||||
public CsvExporter(char separator) {
|
public CsvExporter(char separator) {
|
||||||
this.separator = separator;
|
this.separator = separator;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void export(Project project, Properties options, Engine engine, OutputStream outputStream) throws IOException {
|
public void export(Project project, Properties options, Engine engine, OutputStream outputStream) throws IOException {
|
||||||
throw new RuntimeException("Not implemented");
|
throw new RuntimeException("Not implemented");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void export(Project project, Properties options, Engine engine, Writer writer) throws IOException {
|
public void export(Project project, Properties options, Engine engine, Writer writer) throws IOException {
|
||||||
|
|
||||||
boolean printColumnHeader = true;
|
boolean printColumnHeader = true;
|
||||||
|
|
||||||
if (options != null) {
|
if (options != null) {
|
||||||
printColumnHeader = Boolean.parseBoolean(options.getProperty("printColumnHeader"));
|
String printColHead = options.getProperty("printColumnHeader");
|
||||||
}
|
if(printColHead != null)
|
||||||
|
printColumnHeader = !printColHead.toLowerCase().equals("false");
|
||||||
RowVisitor visitor = new RowVisitor() {
|
}
|
||||||
CSVWriter csvWriter;
|
|
||||||
boolean printColumnHeader = true;
|
RowVisitor visitor = new RowVisitor() {
|
||||||
boolean isFirstRow = true; //the first row should also add the column headers
|
CSVWriter csvWriter;
|
||||||
|
boolean printColumnHeader = true;
|
||||||
public RowVisitor init(CSVWriter writer, boolean printColumnHeader){
|
boolean isFirstRow = true; //the first row should also add the column headers
|
||||||
this.csvWriter = writer;
|
|
||||||
this.printColumnHeader = printColumnHeader;
|
public RowVisitor init(CSVWriter writer, boolean printColumnHeader){
|
||||||
return this;
|
this.csvWriter = writer;
|
||||||
}
|
this.printColumnHeader = printColumnHeader;
|
||||||
|
return this;
|
||||||
public boolean visit(Project project, int rowIndex, Row row) {
|
}
|
||||||
String[] cols = new String[project.columnModel.columns.size()];
|
|
||||||
String[] vals = new String[row.cells.size()];
|
public boolean visit(Project project, int rowIndex, Row row) {
|
||||||
|
String[] cols = new String[project.columnModel.columns.size()];
|
||||||
int i = 0;
|
String[] vals = new String[row.cells.size()];
|
||||||
for(Column col : project.columnModel.columns){
|
|
||||||
int cellIndex = col.getCellIndex();
|
int i = 0;
|
||||||
cols[i] = col.getName();
|
for(Column col : project.columnModel.columns){
|
||||||
|
int cellIndex = col.getCellIndex();
|
||||||
Cell cell = row.cells.get(cellIndex);
|
cols[i] = col.getName();
|
||||||
if(cell != null){
|
|
||||||
vals[i] = cell.value.toString();
|
Cell cell = row.cells.get(cellIndex);
|
||||||
}
|
if(cell != null){
|
||||||
i++;
|
vals[i] = cell.value.toString();
|
||||||
}
|
}
|
||||||
|
i++;
|
||||||
if( printColumnHeader && isFirstRow ){
|
}
|
||||||
csvWriter.writeNext(cols,false);
|
|
||||||
isFirstRow = false; //switch off flag
|
if( printColumnHeader && isFirstRow ){
|
||||||
}
|
csvWriter.writeNext(cols,false);
|
||||||
csvWriter.writeNext(vals,false);
|
isFirstRow = false; //switch off flag
|
||||||
|
}
|
||||||
return false;
|
csvWriter.writeNext(vals,false);
|
||||||
}
|
|
||||||
|
return false;
|
||||||
public void start(Project project) {
|
}
|
||||||
// nothing to do
|
|
||||||
}
|
public void start(Project project) {
|
||||||
|
// nothing to do
|
||||||
public void end(Project project) {
|
}
|
||||||
try {
|
|
||||||
csvWriter.close();
|
public void end(Project project) {
|
||||||
} catch (IOException e) {
|
try {
|
||||||
logger.error("CsvExporter could not close writer : " + e.getMessage());
|
csvWriter.close();
|
||||||
}
|
} catch (IOException e) {
|
||||||
}
|
logger.error("CsvExporter could not close writer : " + e.getMessage());
|
||||||
|
}
|
||||||
}.init(new CSVWriter(writer, separator), printColumnHeader);
|
}
|
||||||
|
|
||||||
FilteredRows filteredRows = engine.getAllFilteredRows();
|
}.init(new CSVWriter(writer, separator), printColumnHeader);
|
||||||
filteredRows.accept(project, visitor);
|
|
||||||
}
|
FilteredRows filteredRows = engine.getAllFilteredRows();
|
||||||
|
filteredRows.accept(project, visitor);
|
||||||
public String getContentType() {
|
}
|
||||||
return "application/x-unknown";
|
|
||||||
}
|
public String getContentType() {
|
||||||
|
return "application/x-unknown";
|
||||||
public boolean takeWriter() {
|
}
|
||||||
return true;
|
|
||||||
}
|
public boolean takeWriter() {
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user