Skip to content

Commit 1fb11e6

Browse files
author
eberhardtj
committed
Removed nested rows
Removes the construction of nested rows (rows in rows), when a literal stream contains a sequence of literals that has the same name. This feature adds more confusion to the output, due to the fact that a nested row is a csv row on its own. A removal of this feature keeps the implementation simple and predictable.
1 parent 71d76ce commit 1fb11e6

File tree

3 files changed

+3
-59
lines changed

3 files changed

+3
-59
lines changed

README.adoc

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ CSV Dialect:
4545
* Line Terminator: \n
4646
* Quoting: Quote All
4747

48-
NOTE: If a sequence of literals (2 or more) share the same name,
49-
the values of those literals will be collected in the corresponding row.
50-
The column will contain a inner row (a nested csv row).
51-
5248
==== Syntax
5349

5450
```

src/main/java/org/metafacture/csv/SimpleCsvEncoder.java

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
package org.metafacture.csv;
1717

1818
import com.opencsv.CSVWriter;
19-
import com.opencsv.CSVWriterBuilder;
20-
import com.opencsv.ICSVWriter;
2119
import org.metafacture.framework.FluxCommand;
2220
import org.metafacture.framework.MetafactureException;
2321
import org.metafacture.framework.ObjectReceiver;
@@ -52,14 +50,8 @@ public class SimpleCsvEncoder extends DefaultStreamPipe<ObjectReceiver<String>>
5250

5351
/** List of items that will be written to a row */
5452
private List<String> rowItems;
55-
/** Last encountered literal name */
56-
private String lastLiteralName;
57-
/** List of literal values that has the same name */
58-
private List<String> literalValues;
5953
/** Flag for the first record encounter */
6054
private boolean isFirstRecord;
61-
/** Flag for the first literal encounter in a record */
62-
private boolean isFirstLiteral;
6355

6456
private List<String> header;
6557
private char separator;
@@ -77,11 +69,7 @@ public SimpleCsvEncoder(char separator) {
7769
this.header = new ArrayList<>();
7870

7971
this.isFirstRecord = true;
80-
this.isFirstLiteral = true;
81-
8272
this.rowItems = new ArrayList<>();
83-
this.lastLiteralName = null;
84-
this.literalValues = new ArrayList<>();
8573
}
8674

8775
/**
@@ -124,23 +112,7 @@ private String[] arrayOf(List<String> list) {
124112
return list.toArray(new String[length]);
125113
}
126114

127-
private String innerRowOf(List<String> items) {
128-
StringWriter writer = new StringWriter();
129-
ICSVWriter csvWriter = new CSVWriterBuilder(writer)
130-
.withSeparator(separator)
131-
.withQuoteChar(CSVWriter.DEFAULT_QUOTE_CHARACTER)
132-
.withLineEnd("")
133-
.build();
134-
135-
String row[] = arrayOf(items);
136-
csvWriter.writeNext(row);
137-
String line = writer.toString().trim();
138-
return line;
139-
}
140-
141115
private void resetCaches() {
142-
isFirstLiteral = true;
143-
literalValues = new ArrayList<>();
144116
rowItems = new ArrayList<>();
145117
}
146118

@@ -180,9 +152,6 @@ public void endRecord() {
180152
isFirstRecord = false;
181153
}
182154

183-
String rowItem = literalValues.size() == 1 ? literalValues.get(0) : innerRowOf(literalValues);
184-
rowItems.add(rowItem);
185-
186155
writeRow(rowItems);
187156

188157
resetCaches();
@@ -193,23 +162,7 @@ public void literal(final String name, final String value) {
193162
if (isFirstRecord) {
194163
header.add(name);
195164
}
196-
197-
if (isFirstLiteral) {
198-
lastLiteralName = name;
199-
isFirstLiteral = false;
200-
}
201-
202-
if (name.equals(lastLiteralName)) {
203-
literalValues.add(value);
204-
} else {
205-
String rowItem = literalValues.size() == 1 ? literalValues.get(0) : innerRowOf(literalValues);
206-
rowItems.add(rowItem);
207-
208-
literalValues = new ArrayList<>();
209-
literalValues.add(value);
210-
}
211-
212-
lastLiteralName = name;
165+
rowItems.add(value);
213166
}
214167

215168
@Override
@@ -228,11 +181,7 @@ public void onResetStream() {
228181
this.header = new ArrayList<>();
229182

230183
this.isFirstRecord = true;
231-
this.isFirstLiteral = true;
232-
233184
this.rowItems = new ArrayList<>();
234-
this.lastLiteralName = null;
235-
this.literalValues = new ArrayList<>();
236185
}
237186

238187
@Override

src/test/java/org/metafacture/csv/SimpleCsvEncoderTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ public void shouldReceiveSingleRecordWithRecordIdAndHeader() {
9595
ordered.verify(receiver).process(ticksToQuotes("'1' 'a' 'b'"));
9696
}
9797

98-
9998
@Test
10099
public void shouldReceiveThreeRows() {
101100
encoder.startRecord("1");
@@ -126,7 +125,7 @@ public void shouldUseCommaAsSeparator() {
126125
encoder.literal("column 1", "a");
127126
encoder.literal("column 2", "b");
128127
encoder.endRecord();
129-
encoder.startRecord("1");
128+
encoder.startRecord("2");
130129
encoder.literal("column 1", "c");
131130
encoder.literal("column 2", "d");
132131
encoder.endRecord();
@@ -148,7 +147,7 @@ public void shouldCreateNestedCsvInColumn() {
148147
encoder.closeStream();
149148

150149
final InOrder ordered = inOrder(receiver);
151-
ordered.verify(receiver).process(ticksToQuotes("'a' '''a1'' ''a2'' ''a3'''"));
150+
ordered.verify(receiver).process(ticksToQuotes("'a' 'a1' 'a2' 'a3'"));
152151
}
153152

154153
private String ticksToQuotes(String s) {

0 commit comments

Comments
 (0)