fix CSV generation
This commit is contained in:
parent
78835f875b
commit
31f27e33ef
2 changed files with 35 additions and 9 deletions
|
@ -37,19 +37,20 @@ public class Generator implements Constants, Closeable, Flushable {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void write(String value) throws IOException {
|
public synchronized void write(String value) throws IOException {
|
||||||
|
if (col >= keys.size()) {
|
||||||
|
writer.write(LF);
|
||||||
|
row++;
|
||||||
|
col = 0;
|
||||||
|
} else {
|
||||||
if (col > 0) {
|
if (col > 0) {
|
||||||
writer.write(COMMA);
|
writer.write(COMMA);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
writer.write(escape(value));
|
writer.write(escape(value));
|
||||||
}
|
}
|
||||||
col++;
|
col++;
|
||||||
if (col > keys.size()) {
|
|
||||||
writer.write(LF);
|
|
||||||
row++;
|
|
||||||
col = 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getColumn() {
|
public int getColumn() {
|
||||||
|
|
|
@ -10,7 +10,32 @@ import java.util.Arrays;
|
||||||
public class GeneratorTest {
|
public class GeneratorTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test() throws IOException {
|
public void testOneColumn() throws IOException {
|
||||||
|
StringWriter writer = new StringWriter();
|
||||||
|
Generator gen = new Generator(writer);
|
||||||
|
gen.keys(Arrays.asList("a"));
|
||||||
|
for (int i = 0; i < 2; i++) {
|
||||||
|
gen.write("val" + i);
|
||||||
|
}
|
||||||
|
gen.close();
|
||||||
|
assertEquals("val0\nval1", writer.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testTwoColumns() throws IOException {
|
||||||
|
StringWriter writer = new StringWriter();
|
||||||
|
Generator gen = new Generator(writer);
|
||||||
|
gen.keys(Arrays.asList("a", "b"));
|
||||||
|
for (int i = 0; i < 2; i++) {
|
||||||
|
gen.write("val" + i);
|
||||||
|
gen.write("val" + i);
|
||||||
|
}
|
||||||
|
gen.close();
|
||||||
|
assertEquals("val0,val0\nval1,val1", writer.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testThreeColumns() throws IOException {
|
||||||
StringWriter writer = new StringWriter();
|
StringWriter writer = new StringWriter();
|
||||||
Generator gen = new Generator(writer);
|
Generator gen = new Generator(writer);
|
||||||
gen.keys(Arrays.asList("a", "b", "c"));
|
gen.keys(Arrays.asList("a", "b", "c"));
|
||||||
|
|
Loading…
Reference in a new issue