Skip to content

Commit 3d22ec5

Browse files
authored
feat: Improve CSV file parsing (#357)
1 parent 8c50f92 commit 3d22ec5

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

src/main/java/org/casbin/jcasbin/util/Util.java

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static void logPrint(String v) {
5454
* logPrintf prints the log with the format.
5555
*
5656
* @param format the format of the log.
57-
* @param v the log.
57+
* @param v the log.
5858
*/
5959
public static void logPrintf(String format, String... v) {
6060
if (enableLog) {
@@ -67,7 +67,7 @@ public static void logPrintf(String format, String... v) {
6767
* logPrintf prints the log with the format as a warning.
6868
*
6969
* @param format the format of the log.
70-
* @param v the log.
70+
* @param v the log.
7171
*/
7272
public static void logPrintfWarn(String format, Object... v) {
7373
if (enableLog) {
@@ -79,7 +79,7 @@ public static void logPrintfWarn(String format, Object... v) {
7979
* logPrintf prints the log with the format as an error.
8080
*
8181
* @param format the format of the log.
82-
* @param v the log.
82+
* @param v the log.
8383
*/
8484
public static void logPrintfError(String format, Object... v) {
8585
if (enableLog) {
@@ -104,7 +104,8 @@ public static void logEnforce(Object[] request, boolean result, List<String> exp
104104
}
105105

106106
/**
107-
* escapeAssertion escapes the dots in the assertion, because the expression evaluation doesn't support such variable names.
107+
* escapeAssertion escapes the dots in the assertion, because the expression
108+
* evaluation doesn't support such variable names.
108109
*
109110
* @param s the value of the matcher and effect assertions.
110111
* @return the escaped value.
@@ -114,15 +115,16 @@ public static String escapeAssertion(String s) {
114115
StringBuffer sb = new StringBuffer();
115116

116117
while (m.find()) {
117-
m.appendReplacement(sb, m.group().replace(".", "_") );
118+
m.appendReplacement(sb, m.group().replace(".", "_"));
118119
}
119120

120121
m.appendTail(sb);
121122
return sb.toString();
122123
}
123124

124125
/**
125-
* convertInSyntax Convert 'in' to 'include' to fit aviatorscript,because aviatorscript don't support native 'in' syntax
126+
* convertInSyntax Convert 'in' to 'include' to fit aviatorscript,because
127+
* aviatorscript don't support native 'in' syntax
126128
*
127129
* @param expString the value of the matcher
128130
* @return the 'include' expression.
@@ -139,6 +141,7 @@ public static String convertInSyntax(String expString) {
139141
m1.appendTail(sb);
140142
return flag ? sb.toString() : expString;
141143
}
144+
142145
/**
143146
* removeComments removes the comments starting with # in the text.
144147
*
@@ -150,7 +153,7 @@ public static String removeComments(String s) {
150153
if (pos == -1) {
151154
return s;
152155
}
153-
return s.substring(0,pos).trim();
156+
return s.substring(0, pos).trim();
154157
}
155158

156159
/**
@@ -171,7 +174,7 @@ public static boolean arrayEquals(List<String> a, List<String> b) {
171174
return false;
172175
}
173176

174-
for (int i = 0; i < a.size(); i ++) {
177+
for (int i = 0; i < a.size(); i++) {
175178
if (!a.get(i).equals(b.get(i))) {
176179
return false;
177180
}
@@ -180,7 +183,8 @@ public static boolean arrayEquals(List<String> a, List<String> b) {
180183
}
181184

182185
/**
183-
* array2DEquals determines whether two 2-dimensional string arrays are identical.
186+
* array2DEquals determines whether two 2-dimensional string arrays are
187+
* identical.
184188
*
185189
* @param a the first 2-dimensional array.
186190
* @param b the second 2-dimensional array.
@@ -197,7 +201,7 @@ public static boolean array2DEquals(List<List<String>> a, List<List<String>> b)
197201
return false;
198202
}
199203

200-
for (int i = 0; i < a.size(); i ++) {
204+
for (int i = 0; i < a.size(); i++) {
201205
if (!arrayEquals(a.get(i), b.get(i))) {
202206
return false;
203207
}
@@ -206,7 +210,8 @@ public static boolean array2DEquals(List<List<String>> a, List<List<String>> b)
206210
}
207211

208212
/**
209-
* arrayRemoveDuplicates removes any duplicated elements in a string array preserving the order.
213+
* arrayRemoveDuplicates removes any duplicated elements in a string array
214+
* preserving the order.
210215
*
211216
* @param s the array.
212217
* @return the array without duplicates.
@@ -237,9 +242,12 @@ public static String paramsToString(String[] s) {
237242
}
238243

239244
/**
240-
* splitCommaDelimited splits a comma-delimited string according to the default processing method of the CSV file
241-
* into a string array. It assumes that any number of whitespace might exist before or after the word and that tokens do not include
242-
* whitespace as part of their value.
245+
* splitCommaDelimited splits a comma-delimited string according to the default
246+
* processing method of the CSV file
247+
* into a string array. It assumes that any number of whitespace might exist
248+
* before or after the token and that tokens do not include
249+
* whitespace as part of their value unless they are enclosed by double
250+
* quotes.
243251
*
244252
* @param s the string.
245253
* @return the array with the string tokens.
@@ -248,7 +256,8 @@ public static String[] splitCommaDelimited(String s) {
248256
String[] records = null;
249257
if (s != null) {
250258
try {
251-
CSVParser csvParser = CSVFormat.DEFAULT.withEscape('\\').parse(new StringReader(s));
259+
CSVFormat csvFormat = CSVFormat.Builder.create().setIgnoreSurroundingSpaces(true).build();
260+
CSVParser csvParser = csvFormat.parse(new StringReader(s));
252261
List<CSVRecord> csvRecords = csvParser.getRecords();
253262
records = new String[csvRecords.get(0).size()];
254263
for (int i = 0; i < csvRecords.get(0).size(); i++) {
@@ -283,7 +292,7 @@ public static boolean setEquals(List<String> a, List<String> b) {
283292
Collections.sort(a);
284293
Collections.sort(b);
285294

286-
for (int i = 0; i < a.size(); i ++) {
295+
for (int i = 0; i < a.size(); i++) {
287296
if (!a.get(i).equals(b.get(i))) {
288297
return false;
289298
}

src/test/java/org/casbin/jcasbin/main/UtilTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ public void testSplitCommaDelimited(){
6767
assertArrayEquals(new String[]{"a", "b", "c"}, Util.splitCommaDelimited("a, b, c"));
6868
assertArrayEquals(new String[]{"a", "b", "c"}, Util.splitCommaDelimited("a ,b ,c"));
6969
assertArrayEquals(new String[]{"a", "b", "c"}, Util.splitCommaDelimited(" a, b ,c "));
70+
assertArrayEquals(new String[]{"a,b,c", "d,e", "f"}, Util.splitCommaDelimited("\"a,b,c\", \"d,e\", f"));
71+
assertArrayEquals(new String[]{"a", "b", "c"}, Util.splitCommaDelimited("\"a\", \"b\", \"c\""));
72+
assertArrayEquals(new String[]{"\"a", "\"b\"", "c\""}, Util.splitCommaDelimited("\"\"\"a\",\"\"\"b\"\"\",\"c\"\"\""));
73+
assertArrayEquals(new String[]{"\"a", "\"b\"", "c\""}, Util.splitCommaDelimited("\"\"\"a\",\"\"\"b\"\"\",\"c\"\"\""));
74+
assertArrayEquals(new String[]{"a b", "c", "d"}, Util.splitCommaDelimited("\"a b\", c, d"));
7075
}
7176

7277
@Test

0 commit comments

Comments
 (0)