Skip to content

Commit 3310c0e

Browse files
giorgigajazzido
authored andcommitted
Cleaned up classes CSVWriter and TSVWriter
1 parent 6fe0c89 commit 3310c0e

2 files changed

Lines changed: 31 additions & 51 deletions

File tree

src/main/java/technology/tabula/writers/CSVWriter.java

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.IOException;
44
import java.util.ArrayList;
5+
import java.util.Collections;
56
import java.util.List;
67

78
import org.apache.commons.csv.CSVPrinter;
@@ -11,47 +12,34 @@
1112
import technology.tabula.Table;
1213

1314
public class CSVWriter implements Writer {
14-
15-
CSVPrinter printer;
16-
private boolean useLineReturns = true;
17-
18-
// public CSVWriter() {
19-
// super();
20-
// }
21-
//
22-
// public CSVWriter(boolean useLineReturns) {
23-
// super();
24-
// this.useLineReturns = useLineReturns;
25-
// }
26-
27-
void createWriter(Appendable out) {
28-
try {
29-
this.printer = new CSVPrinter(out, CSVFormat.EXCEL);
30-
} catch (IOException e) {
31-
// TODO Auto-generated catch block
32-
e.printStackTrace();
33-
}
34-
}
35-
36-
@Override
37-
public void write(Appendable out, Table table) throws IOException {
38-
this.createWriter(out);
39-
for (List<RectangularTextContainer> row: table.getRows()) {
40-
List<String> cells = new ArrayList<String>(row.size());
41-
for (RectangularTextContainer tc: row) {
42-
cells.add(tc.getText());
43-
}
44-
this.printer.printRecord(cells);
45-
}
46-
printer.flush();
47-
}
15+
16+
public CSVWriter() {
17+
this(CSVFormat.EXCEL);
18+
}
19+
20+
protected CSVWriter(CSVFormat format) {
21+
this.format = format;
22+
}
23+
24+
private final CSVFormat format;
25+
26+
@Override
27+
public void write(Appendable out, Table table) throws IOException {
28+
write(out, Collections.singletonList(table));
29+
}
4830

4931
@Override
5032
public void write(Appendable out, List<Table> tables) throws IOException {
51-
for (Table table : tables) {
52-
write(out, table);
33+
try (CSVPrinter printer = new CSVPrinter(out, format)) {
34+
for (Table table : tables) {
35+
for (List<RectangularTextContainer> row : table.getRows()) {
36+
List<String> cells = new ArrayList<>(row.size());
37+
for (RectangularTextContainer<?> tc : row) cells.add(tc.getText());
38+
printer.printRecord(cells);
39+
}
40+
}
41+
printer.flush();
5342
}
54-
5543
}
5644

5745
}
Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
11
package technology.tabula.writers;
22

3-
import java.io.IOException;
4-
53
import org.apache.commons.csv.CSVFormat;
6-
import org.apache.commons.csv.CSVPrinter;
74

85
public class TSVWriter extends CSVWriter {
9-
10-
@Override
11-
void createWriter(Appendable out) {
12-
try {
13-
this.printer = new CSVPrinter(out, CSVFormat.TDF);
14-
} catch (IOException e) {
15-
// TODO Auto-generated catch block
16-
e.printStackTrace();
17-
}
18-
}
19-
6+
7+
public TSVWriter() {
8+
super(CSVFormat.TDF);
9+
}
10+
11+
2012
}

0 commit comments

Comments
 (0)