Skip to content

Commit 1657c94

Browse files
author
eberhardtj
committed
Add feature to disable quotes
Adds a flag that tells the CSV writer to omit quotations. The quotation char will be empty.
1 parent 8cc5e00 commit 1657c94

File tree

3 files changed

+94
-74
lines changed

3 files changed

+94
-74
lines changed

README.adoc

Lines changed: 73 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,73 @@
1-
= metafacture-csv-plugin
2-
:toc:
3-
4-
A plugin for link:https://github.com/metafacture/metafacture-core[metafacture] that extends the standard CSV module.
5-
6-
== Build
7-
8-
image::https://jitpack.io/v/eberhardtj/metafacture-csv-plugin.svg[link="https://jitpack.io/#eberhardtj/metafacture-csv-plugin"]
9-
10-
```
11-
gradlew test fatJar
12-
```
13-
14-
Produces `metafacture-csv-VERSION-plugin.jar` in `build/libs` .
15-
16-
Place the build JAR inside the `plugins` directory of your `metafacture-core` distribution.
17-
18-
== Command Reference
19-
20-
|===
21-
|Command | In | Out
22-
23-
|encode-csv
24-
|StreamReceiver
25-
|String
26-
27-
|===
28-
29-
=== encode-csv
30-
31-
==== Description
32-
33-
A simple encoder that constructs a table of __Comma-separated values__ (CSV).
34-
35-
Structure:
36-
37-
* Each record represents a row
38-
** The sequential order of literal _values_ forms a *row*
39-
** The sequential order of literal _names_ of the first record is used as _header_ (Optional)
40-
41-
CSV Dialect:
42-
43-
* Delimiter: ,
44-
* Quote Char: "
45-
* Line Terminator: \n
46-
* Quoting: Quote All
47-
48-
Example Output:
49-
50-
```
51-
"output","example","2018"
52-
```
53-
54-
==== Syntax
55-
56-
```
57-
encode-csv([separator],[includeHeader],[includeRecordId])
58-
```
59-
60-
==== Parameters
61-
62-
* `separator`: Delimiter symbol (Default: comma)
63-
* `includeHeader`: Add a header with column names (Default: false).
64-
* `includeRecordId`: Add the record id as first element of each row (Default: false).
65-
66-
==== Example
67-
68-
Flux:
69-
70-
```
71-
... | encoder-csv(separator=",", includeRecordId="true") | print;
72-
```
1+
= metafacture-csv-plugin
2+
:toc:
3+
4+
A plugin for link:https://github.com/metafacture/metafacture-core[metafacture] that extends the standard CSV module.
5+
6+
== Build
7+
8+
image::https://jitpack.io/v/eberhardtj/metafacture-csv-plugin.svg[link="https://jitpack.io/#eberhardtj/metafacture-csv-plugin"]
9+
10+
```
11+
gradlew test fatJar
12+
```
13+
14+
Produces `metafacture-csv-VERSION-plugin.jar` in `build/libs` .
15+
16+
Place the build JAR inside the `plugins` directory of your `metafacture-core` distribution.
17+
18+
== Command Reference
19+
20+
|===
21+
|Command | In | Out
22+
23+
|encode-csv
24+
|StreamReceiver
25+
|String
26+
27+
|===
28+
29+
=== encode-csv
30+
31+
==== Description
32+
33+
A simple encoder that constructs a table of __Comma-separated values__ (CSV).
34+
35+
Structure:
36+
37+
* Each record represents a row
38+
** The sequential order of literal _values_ forms a *row*
39+
** The sequential order of literal _names_ of the first record is used as _header_ (Optional)
40+
41+
CSV Dialect:
42+
43+
* Delimiter: ,
44+
* Quote Char: "
45+
* Line Terminator: \n
46+
* Quoting: Quote All
47+
48+
Example Output:
49+
50+
```
51+
"output","example","2018"
52+
```
53+
54+
==== Syntax
55+
56+
```
57+
encode-csv([separator],[includeHeader],[includeRecordId])
58+
```
59+
60+
==== Parameters
61+
62+
* `separator`: Delimiter symbol (Default: comma)
63+
* `noQuotes`: Disables the usage of a quote character.
64+
* `includeHeader`: Add a header with column names (Default: false).
65+
* `includeRecordId`: Add the record id as first element of each row (Default: false).
66+
67+
==== Example
68+
69+
Flux:
70+
71+
```
72+
... | encoder-csv(separator=",", includeRecordId="true") | print;
73+
```

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public class SimpleCsvEncoder extends DefaultStreamPipe<ObjectReceiver<String>>
5555

5656
private List<String> header;
5757
private char separator;
58+
private boolean noQuotes;
5859
private boolean includeHeader;
5960
private boolean includeRecordId;
6061

@@ -64,8 +65,9 @@ public SimpleCsvEncoder() {
6465

6566
public SimpleCsvEncoder(char separator) {
6667
this.separator = separator;
67-
this.includeRecordId = false;
68+
this.noQuotes = false;
6869
this.includeHeader = false;
70+
this.includeRecordId = false;
6971
this.header = new ArrayList<>();
7072

7173
this.isFirstRecord = true;
@@ -93,6 +95,10 @@ public void setSeparator(String separator) {
9395
this.separator = separator.charAt(0);
9496
}
9597

98+
public void setNoQuotes(boolean noQuotes) {
99+
this.noQuotes = noQuotes;
100+
}
101+
96102
public void setSeparator(char separator) {
97103
this.separator = separator;
98104
}
@@ -102,7 +108,7 @@ private void initialize() {
102108
String emptyLineEnd = "";
103109
csvWriter = new CSVWriter(writer,
104110
separator,
105-
CSVWriter.DEFAULT_QUOTE_CHARACTER,
111+
noQuotes ? CSVWriter.NO_QUOTE_CHARACTER : CSVWriter.DEFAULT_QUOTE_CHARACTER,
106112
CSVWriter.DEFAULT_ESCAPE_CHARACTER,
107113
emptyLineEnd);
108114
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,19 @@ public void shouldReceiveSingleRecord() {
5050
ordered.verify(receiver).process(ticksToQuotes("'a' 'b'"));
5151
}
5252

53+
@Test
54+
public void shouldHaveNoQuotes() {
55+
encoder.setNoQuotes(true);
56+
encoder.startRecord("1");
57+
encoder.literal("column 1", "a");
58+
encoder.literal("column 2", "b");
59+
encoder.endRecord();
60+
encoder.closeStream();
61+
62+
final InOrder ordered = inOrder(receiver);
63+
ordered.verify(receiver).process("a b");
64+
}
65+
5366
@Test
5467
public void shouldReceiveSingleRecordWithHeader() {
5568
encoder.setIncludeHeader(true);

0 commit comments

Comments
 (0)