-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathCsvReaderContext.kt
145 lines (125 loc) · 3.88 KB
/
CsvReaderContext.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package com.github.doyaaaaaken.kotlincsv.dsl.context
import com.github.doyaaaaaken.kotlincsv.util.Const
import com.github.doyaaaaaken.kotlincsv.util.CsvDslMarker
import com.github.doyaaaaaken.kotlincsv.util.logger.Logger
import com.github.doyaaaaaken.kotlincsv.util.logger.LoggerNop
/**
* Interface for CSV Reader settings
*
* @author doyaaaaaken
*/
@CsvDslMarker
interface ICsvReaderContext {
/**
* Logger instance for logging debug statements.
* Default instance does not log anything.
*/
val logger: Logger
/**
* Charset encoding
*
* The name must be supported by [java.nio.charset.Charset].
*
* ex.)
* "UTF-8"
* "SJIS"
*/
val charset: String
/**
* Character used as quote between each fields
*
* ex.)
* '"'
* '\''
*/
val quoteChar: Char
/**
* Character used as delimiter between each fields
*
* ex.)
* ","
* "\t" (TSV file)
*/
val delimiter: Char
/**
* Character to escape quote inside field string.
* Normally, you don't have to change this option.
*
* According to [CSV specification](https://tools.ietf.org/html/rfc4180#section-2),
* > If double-quotes are used to enclose fields, then a double-quote appearing inside a field must be escaped by preceding it with another double quote.
* > For example:
* > "aaa","b""bb","ccc"
*/
val escapeChar: Char
/**
* If empty line is found, skip it or not (=throw an exception).
*/
val skipEmptyLine: Boolean
/**
* If a invalid row which has different number of fields from other rows is found, skip it or not (=throw an exception).
*/
@Deprecated("Use insufficientFieldsRowBehaviour and excessRowsBehaviour to specify 'ignore'")
val skipMissMatchedRow: Boolean
/**
* If a header occurs multiple times whether auto renaming should be applied when `readAllWithHeaderAsSequence()` (=throw an exception).
*
* Renaming is done based on occurrence and only applied from the first detected duplicate onwards.
* ex:
* [a,b,b,b,c,a] => [a,b,b_2,b_3,c,a_2]
*/
val autoRenameDuplicateHeaders: Boolean
/**
* If a row does not have the expected number of fields (columns), how, and if, the reader should proceed
*/
val insufficientFieldsRowBehaviour: InsufficientFieldsRowBehaviour
/**
* If a row exceeds have the expected number of fields (columns), how, and if, the reader should proceed
*/
val excessFieldsRowBehaviour: ExcessFieldsRowBehaviour
}
enum class InsufficientFieldsRowBehaviour {
/**
* Throw an exception (default)
*/
ERROR,
/**
* Ignore the row and skip to the next row
*/
IGNORE,
/**
* Treat missing fields as an empty string
*/
EMPTY_STRING
}
enum class ExcessFieldsRowBehaviour {
/**
* Throw an exception (default)
*/
ERROR,
/**
* Ignore the row and skip to the next row
*/
IGNORE,
/**
* Trim the excess fields from the row (e.g. if 8 fields are present and 7 are expected, return the first 7 fields)
*/
TRIM
}
/**
* CSV Reader settings used in `csvReader` DSL method.
*
* @author doyaaaaaken
*/
@CsvDslMarker
class CsvReaderContext : ICsvReaderContext {
override var logger: Logger = LoggerNop
override var charset = Const.defaultCharset
override var quoteChar: Char = '"'
override var delimiter: Char = ','
override var escapeChar: Char = '"'
override var skipEmptyLine: Boolean = false
override var skipMissMatchedRow: Boolean = false
override var autoRenameDuplicateHeaders: Boolean = false
override var insufficientFieldsRowBehaviour: InsufficientFieldsRowBehaviour = InsufficientFieldsRowBehaviour.ERROR
override var excessFieldsRowBehaviour: ExcessFieldsRowBehaviour = ExcessFieldsRowBehaviour.ERROR
}