Skip to content
Snippets Groups Projects
Commit 8df7f1d9 authored by srosse's avatar srosse
Browse files

OO-3070: add a utility method to convert a list of string in a CSV like string (patch S. Clemenz)

parent a6a531c1
No related branches found
No related tags found
No related merge requests found
...@@ -35,6 +35,7 @@ import java.text.DecimalFormat; ...@@ -35,6 +35,7 @@ import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols; import java.text.DecimalFormatSymbols;
import java.text.NumberFormat; import java.text.NumberFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
...@@ -42,7 +43,6 @@ import java.util.Iterator; ...@@ -42,7 +43,6 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
...@@ -542,67 +542,44 @@ public class StringHelper { ...@@ -542,67 +542,44 @@ public class StringHelper {
} }
/** /**
* set of strings to one string comma separated.<br> * Collection of strings to one string comma separated. This method doesn't do any escaping
* and will never do escaping to be backwards compatible.<br>
* e.g. ["a","b","c","s"] -> "a,b,c,s" * e.g. ["a","b","c","s"] -> "a,b,c,s"
* @param selection * @param selection
* @return * @return
*/ */
public static String formatAsCSVString(Set<String> entries) { public static String formatAsCSVString(Collection<String> entries) {
boolean isFirst = true; StringBuilder csv = new StringBuilder(256);
String csvStr = null; for (String group:entries) {
for (Iterator<String> iter = entries.iterator(); iter.hasNext();) { if (csv.length() > 0) {
String group = iter.next(); csv.append(",");
if (isFirst) {
csvStr = group;
isFirst = false;
} else {
csvStr += ", " + group;
} }
csv.append(group);
} }
return csvStr; return csv.toString();
} }
/** /**
* list of strings to one string comma separated.<br> * Collection of strings to one string comma separated. The method escaped
* e.g. ["a","b","c","s"] -> "a,b,c,s" * " and ,<br>
* e.g. ["a","b,1","c","s"] -> "a,"b,1",c,s"
* @param selection * @param selection
* @return * @return
*/ */
public static String formatAsCSVString(List<String> entries) { public static String formatAsEscapedCSVString(Collection<String> entries) {
boolean isFirst = true; StringBuilder csv = new StringBuilder(256);
String csvStr = null; for (String entry:entries) {
for (Iterator<String> iter = entries.iterator(); iter.hasNext();) { entry = entry.replace("\"", "\"\"");
String group = iter.next(); if (entry.contains(",")) {
if (isFirst) { entry = "\"" + entry + "\"";
csvStr = group;
isFirst = false;
} else {
csvStr += ", " + group;
} }
if (csv.length() > 0) {
csv.append(",");
}
csv.append(entry);
} }
return csvStr; return csv.toString();
}
/**
* list of strings to one string comma separated.<br>
* e.g. ["z","a","b","c","s","a"] -> "a, b, c, s, z"
* No duplicates, alphabetically sorted
* @param selection
* @return
*/
public static String formatAsSortUniqCSVString(List<String> s) {
Map <String,String>u = new HashMap<String,String>();
for (Iterator <String> si = s.iterator(); si.hasNext();) {
u.put(si.next().trim(), null);
}
List <String>rv = new ArrayList<String>();
rv.addAll(u.keySet());
rv.remove("");
Collections.sort(rv);
return formatAsCSVString (rv);
} }
/** /**
...@@ -612,18 +589,17 @@ public class StringHelper { ...@@ -612,18 +589,17 @@ public class StringHelper {
* @param selection * @param selection
* @return * @return
*/ */
public static String formatAsSortUniqCSVString(Set<String> s) { public static String formatAsSortUniqCSVString(Collection<String> s) {
Map<String,String> u = new HashMap<>();
Map <String,String>u = new HashMap<String,String>(); for (Iterator<String> si = s.iterator(); si.hasNext();) {
for (Iterator <String> si = s.iterator(); si.hasNext();) {
u.put(si.next().trim(), null); u.put(si.next().trim(), null);
} }
List <String>rv = new ArrayList<String>(); List <String>rv = new ArrayList<>(u.size());
rv.addAll(u.keySet()); rv.addAll(u.keySet());
rv.remove(""); rv.remove("");
Collections.sort(rv); Collections.sort(rv);
return formatAsCSVString (rv); return formatAsCSVString(rv);
} }
} }
\ No newline at end of file
...@@ -125,7 +125,7 @@ public class LogLineConverter { ...@@ -125,7 +125,7 @@ public class LogLineConverter {
PropertyDescriptor pd = it.next(); PropertyDescriptor pd = it.next();
propertyNames.add(pd.getName()); propertyNames.add(pd.getName());
} }
return StringHelper.formatAsCSVString(propertyNames); return StringHelper.formatAsEscapedCSVString(propertyNames);
} }
/** /**
...@@ -160,7 +160,7 @@ public class LogLineConverter { ...@@ -160,7 +160,7 @@ public class LogLineConverter {
loggingObjectList.add(strValue); loggingObjectList.add(strValue);
} }
return StringHelper.formatAsCSVString(loggingObjectList); return StringHelper.formatAsEscapedCSVString(loggingObjectList);
} }
/** /**
......
...@@ -22,6 +22,9 @@ package org.olat.core.util; ...@@ -22,6 +22,9 @@ package org.olat.core.util;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.List;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
...@@ -176,4 +179,25 @@ public class StringHelperTest { ...@@ -176,4 +179,25 @@ public class StringHelperTest {
Assert.assertTrue(StringHelper.isHtml("<ul><li>Hello<li>world</ul>")); Assert.assertTrue(StringHelper.isHtml("<ul><li>Hello<li>world</ul>"));
Assert.assertTrue(StringHelper.isHtml("Hello<br>world")); Assert.assertTrue(StringHelper.isHtml("Hello<br>world"));
} }
@Test
public void formatAsCSVString() {
List<String> entries = new ArrayList<>();
entries.add("Hell\"o\"");
entries.add("Test,dru,");
entries.add("Final");
String csv = StringHelper.formatAsCSVString(entries);
Assert.assertEquals("Hell\"o\",Test,dru,,Final", csv);
}
@Test
public void formatAsEscapedCSVString() {
List<String> entries = new ArrayList<>();
entries.add("Hell\"o\"");
entries.add("Test,dru,");
entries.add("Final");
String csv = StringHelper.formatAsEscapedCSVString(entries);
Assert.assertEquals("Hell\"\"o\"\",\"Test,dru,\",Final", csv);
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment