Skip to content

Commit

Permalink
feat: Improve CSV file parsing (#357)
Browse files Browse the repository at this point in the history
  • Loading branch information
pzet123 authored Aug 26, 2023
1 parent 8c50f92 commit 3d22ec5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
41 changes: 25 additions & 16 deletions src/main/java/org/casbin/jcasbin/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static void logPrint(String v) {
* logPrintf prints the log with the format.
*
* @param format the format of the log.
* @param v the log.
* @param v the log.
*/
public static void logPrintf(String format, String... v) {
if (enableLog) {
Expand All @@ -67,7 +67,7 @@ public static void logPrintf(String format, String... v) {
* logPrintf prints the log with the format as a warning.
*
* @param format the format of the log.
* @param v the log.
* @param v the log.
*/
public static void logPrintfWarn(String format, Object... v) {
if (enableLog) {
Expand All @@ -79,7 +79,7 @@ public static void logPrintfWarn(String format, Object... v) {
* logPrintf prints the log with the format as an error.
*
* @param format the format of the log.
* @param v the log.
* @param v the log.
*/
public static void logPrintfError(String format, Object... v) {
if (enableLog) {
Expand All @@ -104,7 +104,8 @@ public static void logEnforce(Object[] request, boolean result, List<String> exp
}

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

while (m.find()) {
m.appendReplacement(sb, m.group().replace(".", "_") );
m.appendReplacement(sb, m.group().replace(".", "_"));
}

m.appendTail(sb);
return sb.toString();
}

/**
* convertInSyntax Convert 'in' to 'include' to fit aviatorscript,because aviatorscript don't support native 'in' syntax
* convertInSyntax Convert 'in' to 'include' to fit aviatorscript,because
* aviatorscript don't support native 'in' syntax
*
* @param expString the value of the matcher
* @return the 'include' expression.
Expand All @@ -139,6 +141,7 @@ public static String convertInSyntax(String expString) {
m1.appendTail(sb);
return flag ? sb.toString() : expString;
}

/**
* removeComments removes the comments starting with # in the text.
*
Expand All @@ -150,7 +153,7 @@ public static String removeComments(String s) {
if (pos == -1) {
return s;
}
return s.substring(0,pos).trim();
return s.substring(0, pos).trim();
}

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

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

/**
* array2DEquals determines whether two 2-dimensional string arrays are identical.
* array2DEquals determines whether two 2-dimensional string arrays are
* identical.
*
* @param a the first 2-dimensional array.
* @param b the second 2-dimensional array.
Expand All @@ -197,7 +201,7 @@ public static boolean array2DEquals(List<List<String>> a, List<List<String>> b)
return false;
}

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

/**
* arrayRemoveDuplicates removes any duplicated elements in a string array preserving the order.
* arrayRemoveDuplicates removes any duplicated elements in a string array
* preserving the order.
*
* @param s the array.
* @return the array without duplicates.
Expand Down Expand Up @@ -237,9 +242,12 @@ public static String paramsToString(String[] s) {
}

/**
* splitCommaDelimited splits a comma-delimited string according to the default processing method of the CSV file
* into a string array. It assumes that any number of whitespace might exist before or after the word and that tokens do not include
* whitespace as part of their value.
* splitCommaDelimited splits a comma-delimited string according to the default
* processing method of the CSV file
* into a string array. It assumes that any number of whitespace might exist
* before or after the token and that tokens do not include
* whitespace as part of their value unless they are enclosed by double
* quotes.
*
* @param s the string.
* @return the array with the string tokens.
Expand All @@ -248,7 +256,8 @@ public static String[] splitCommaDelimited(String s) {
String[] records = null;
if (s != null) {
try {
CSVParser csvParser = CSVFormat.DEFAULT.withEscape('\\').parse(new StringReader(s));
CSVFormat csvFormat = CSVFormat.Builder.create().setIgnoreSurroundingSpaces(true).build();
CSVParser csvParser = csvFormat.parse(new StringReader(s));
List<CSVRecord> csvRecords = csvParser.getRecords();
records = new String[csvRecords.get(0).size()];
for (int i = 0; i < csvRecords.get(0).size(); i++) {
Expand Down Expand Up @@ -283,7 +292,7 @@ public static boolean setEquals(List<String> a, List<String> b) {
Collections.sort(a);
Collections.sort(b);

for (int i = 0; i < a.size(); i ++) {
for (int i = 0; i < a.size(); i++) {
if (!a.get(i).equals(b.get(i))) {
return false;
}
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/org/casbin/jcasbin/main/UtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ public void testSplitCommaDelimited(){
assertArrayEquals(new String[]{"a", "b", "c"}, Util.splitCommaDelimited("a, b, c"));
assertArrayEquals(new String[]{"a", "b", "c"}, Util.splitCommaDelimited("a ,b ,c"));
assertArrayEquals(new String[]{"a", "b", "c"}, Util.splitCommaDelimited(" a, b ,c "));
assertArrayEquals(new String[]{"a,b,c", "d,e", "f"}, Util.splitCommaDelimited("\"a,b,c\", \"d,e\", f"));
assertArrayEquals(new String[]{"a", "b", "c"}, Util.splitCommaDelimited("\"a\", \"b\", \"c\""));
assertArrayEquals(new String[]{"\"a", "\"b\"", "c\""}, Util.splitCommaDelimited("\"\"\"a\",\"\"\"b\"\"\",\"c\"\"\""));
assertArrayEquals(new String[]{"\"a", "\"b\"", "c\""}, Util.splitCommaDelimited("\"\"\"a\",\"\"\"b\"\"\",\"c\"\"\""));
assertArrayEquals(new String[]{"a b", "c", "d"}, Util.splitCommaDelimited("\"a b\", c, d"));
}

@Test
Expand Down

0 comments on commit 3d22ec5

Please sign in to comment.