CsvSupport.java
package cz.vsb.crm.service.export;
import java.util.ArrayList;
import java.util.List;
final class CsvSupport {
private CsvSupport() {
}
static String escape(Object value) {
if (value == null) {
return "";
}
String text = String.valueOf(value);
if (text.contains(",") || text.contains("\"") || text.contains("\n") || text.contains("\r")) {
return "\"" + text.replace("\"", "\"\"") + "\"";
}
return text;
}
static List<String> parseLine(String line) {
List<String> fields = new ArrayList<>();
StringBuilder current = new StringBuilder();
boolean inQuotes = false;
for (int i = 0; i < line.length(); i++) {
char c = line.charAt(i);
if (inQuotes) {
if (c == '"') {
if (i + 1 < line.length() && line.charAt(i + 1) == '"') {
current.append('"');
i++;
} else {
inQuotes = false;
}
} else {
current.append(c);
}
} else if (c == '"') {
inQuotes = true;
} else if (c == ',') {
fields.add(current.toString());
current.setLength(0);
} else {
current.append(c);
}
}
fields.add(current.toString());
return fields;
}
}