FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
codeql/go/extractor/opencsv/CSVParser.java at codeql-cli-2.11.3 · github/codeql · GitHub
github
/
codeql
Public
Notifications
You must be signed in to change notification settings
Fork
2.1k
Star
10.1k
Code
Issues
1k
Pull requests
470
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
codeql
/
go
/
extractor
/
opencsv
/
CSVParser.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
207 lines (178 loc) · 5.99 KB
Breadcrumbs
codeql
/
go
/
extractor
/
opencsv
/
CSVParser.java
Copy path
File metadata and controls
207 lines (178 loc) · 5.99 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/**
Copyright 2005 Bytecode Pty Ltd.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package
opencsv
;
import
java
.
io
.
IOException
;
import
java
.
util
.
ArrayList
;
import
java
.
util
.
List
;
/**
* A very simple CSV parser released under a commercial-friendly license.
* This just implements splitting a single line into fields.
*
* @author Glen Smith
* @author Rainer Pruy
*
*/
public
class
CSVParser
{
private
final
char
separator
;
private
final
char
quotechar
;
private
final
char
escape
;
private
final
boolean
strictQuotes
;
private
StringBuilder
buf
=
new
StringBuilder
(
INITIAL_READ_SIZE
);
/** The default separator to use if none is supplied to the constructor. */
public
static
final
char
DEFAULT_SEPARATOR
=
','
;
private
static
final
int
INITIAL_READ_SIZE
=
128
;
/**
* The default quote character to use if none is supplied to the
* constructor.
*/
public
static
final
char
DEFAULT_QUOTE_CHARACTER
=
'"'
;
/**
* The default escape character to use if none is supplied to the
* constructor.
*/
public
static
final
char
DEFAULT_ESCAPE_CHARACTER
=
'"'
;
/**
* The default strict quote behavior to use if none is supplied to the
* constructor
*/
public
static
final
boolean
DEFAULT_STRICT_QUOTES
=
false
;
/**
* Constructs CSVReader with supplied separator and quote char.
* Allows setting the "strict quotes" flag
* @param separator
* the delimiter to use for separating entries
* @param quotechar
* the character to use for quoted elements
* @param escape
* the character to use for escaping a separator or quote
* @param strictQuotes
* if true, characters outside the quotes are ignored
*/
CSVParser
(
char
separator
,
char
quotechar
,
char
escape
,
boolean
strictQuotes
) {
this
.
separator
=
separator
;
this
.
quotechar
=
quotechar
;
this
.
escape
=
escape
;
this
.
strictQuotes
=
strictQuotes
;
}
/**
*
* @return true if something was left over from last call(s)
*/
public
boolean
isPending
() {
return
buf
.
length
() !=
0
;
}
public
String
[]
parseLineMulti
(
String
nextLine
)
throws
IOException
{
return
parseLine
(
nextLine
,
true
);
}
public
String
[]
parseLine
(
String
nextLine
)
throws
IOException
{
return
parseLine
(
nextLine
,
false
);
}
/**
* Parses an incoming String and returns an array of elements.
*
* @param nextLine
* the string to parse
* @return the comma-tokenized list of elements, or null if nextLine is null
* @throws IOException if bad things happen during the read
*/
private
String
[]
parseLine
(
String
nextLine
,
boolean
multi
)
throws
IOException
{
if
(!
multi
&&
isPending
()) {
clear
();
}
if
(
nextLine
==
null
) {
if
(
isPending
()) {
String
s
=
buf
.
toString
();
clear
();
return
new
String
[] {
s
};
}
else
{
return
null
;
}
}
List
<
String
>
tokensOnThisLine
=
new
ArrayList
<
String
>();
boolean
inQuotes
=
isPending
();
for
(
int
i
=
0
;
i
<
nextLine
.
length
();
i
++) {
char
c
=
nextLine
.
charAt
(
i
);
if
(
c
==
this
.
escape
&&
isNextCharacterEscapable
(
nextLine
,
inQuotes
,
i
)) {
buf
.
append
(
nextLine
.
charAt
(
i
+
1
));
i
++;
}
else
if
(
c
==
quotechar
) {
if
(
isNextCharacterEscapedQuote
(
nextLine
,
inQuotes
,
i
) ){
buf
.
append
(
nextLine
.
charAt
(
i
+
1
));
i
++;
}
else
{
inQuotes
= !
inQuotes
;
// the tricky case of an embedded quote in the middle: a,bc"d"ef,g
if
(!
strictQuotes
) {
if
(
i
>
2
//not on the beginning of the line
&&
nextLine
.
charAt
(
i
-
1
) !=
this
.
separator
//not at the beginning of an escape sequence
&&
nextLine
.
length
()>(
i
+
1
) &&
nextLine
.
charAt
(
i
+
1
) !=
this
.
separator
//not at the end of an escape sequence
){
buf
.
append
(
c
);
}
}
}
}
else
if
(
c
==
separator
&& !
inQuotes
) {
tokensOnThisLine
.
add
(
buf
.
toString
());
clear
();
// start work on next token
}
else
{
if
(!
strictQuotes
||
inQuotes
)
buf
.
append
(
c
);
}
}
// line is done - check status
if
(
inQuotes
) {
if
(
multi
) {
// continuing a quoted section, re-append newline
buf
.
append
(
'\n'
);
// this partial content is not to be added to field list yet
}
else
{
throw
new
IOException
(
"Un-terminated quoted field at end of CSV line"
);
}
}
else
{
tokensOnThisLine
.
add
(
buf
.
toString
());
clear
();
}
return
tokensOnThisLine
.
toArray
(
new
String
[
tokensOnThisLine
.
size
()]);
}
/**
* precondition: the current character is a quote or an escape
* @param nextLine the current line
* @param inQuotes true if the current context is quoted
* @param i current index in line
* @return true if the following character is a quote
*/
private
boolean
isNextCharacterEscapedQuote
(
String
nextLine
,
boolean
inQuotes
,
int
i
) {
return
inQuotes
// we are in quotes, therefore there can be escaped quotes in here.
&&
nextLine
.
length
() > (
i
+
1
)
// there is indeed another character to check.
&&
nextLine
.
charAt
(
i
+
1
) ==
quotechar
;
}
/**
* precondition: the current character is an escape
* @param nextLine the current line
* @param inQuotes true if the current context is quoted
* @param i current index in line
* @return true if the following character is a quote
*/
protected
boolean
isNextCharacterEscapable
(
String
nextLine
,
boolean
inQuotes
,
int
i
) {
return
inQuotes
// we are in quotes, therefore there can be escaped quotes in here.
&&
nextLine
.
length
() > (
i
+
1
)
// there is indeed another character to check.
&& (
nextLine
.
charAt
(
i
+
1
) ==
quotechar
||
nextLine
.
charAt
(
i
+
1
) ==
this
.
escape
);
}
/**
* Reset the buffer used for storing the current field's value
*/
private
void
clear
() {
buf
.
setLength
(
0
);
}
}
Back
|
FazBrowse Home
|
New Git URL