Create custom CSV escape method

This commit is contained in:
SiboVG 2023-03-06 20:09:14 +01:00
parent 80e33ba01b
commit 9c7106a253
9 changed files with 33 additions and 44 deletions

View File

@ -27,7 +27,6 @@
<classpathentry kind="lib" path="lib/logback-classic-1.2.11.jar"/> <classpathentry kind="lib" path="lib/logback-classic-1.2.11.jar"/>
<classpathentry kind="lib" path="lib/logback-core-1.2.11.jar"/> <classpathentry kind="lib" path="lib/logback-core-1.2.11.jar"/>
<classpathentry kind="lib" path="lib/opencsv-5.7.1.jar"/> <classpathentry kind="lib" path="lib/opencsv-5.7.1.jar"/>
<classpathentry kind="lib" path="lib/commons-text-1.10.0.jar"/>
<classpathentry kind="lib" path="/OpenRocket Test Libraries/hamcrest-core-2.2.jar"/> <classpathentry kind="lib" path="/OpenRocket Test Libraries/hamcrest-core-2.2.jar"/>
<classpathentry kind="lib" path="/OpenRocket Test Libraries/hamcrest-2.2.jar"/> <classpathentry kind="lib" path="/OpenRocket Test Libraries/hamcrest-2.2.jar"/>
<classpathentry kind="lib" path="/OpenRocket Test Libraries/jmock-2.12.0.jar"/> <classpathentry kind="lib" path="/OpenRocket Test Libraries/jmock-2.12.0.jar"/>

View File

@ -134,24 +134,6 @@
<SOURCES /> <SOURCES />
</library> </library>
</orderEntry> </orderEntry>
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="jar://$MODULE_DIR$/lib/commons-lang3-3.12.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="jar://$MODULE_DIR$/lib/commons-text-1.10.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library"> <orderEntry type="module-library">
<library> <library>
<CLASSES> <CLASSES>

Binary file not shown.

View File

@ -1,5 +1,7 @@
package net.sf.openrocket.util; package net.sf.openrocket.util;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
public class StringUtils { public class StringUtils {
@ -65,5 +67,35 @@ public class StringUtils {
return Double.parseDouble(input); return Double.parseDouble(input);
} }
/**
* Returns an escaped version of the String so that it can be safely used as a value in a CSV file.
* The goal is to surround the input string in double quotes if it contains any double quotes, commas,
* newlines, or carriage returns, and to escape any double quotes within the string by doubling them up.
* @param input the string to escape
* @return the escaped string that can be safely used in a CSV file
*/
public static String escapeCSV(String input) {
final List<Character> CSV_SEARCH_CHARS = new ArrayList<>(Arrays.asList(',', '"', '\r', '\n'));
StringBuilder sb = new StringBuilder();
boolean quoted = false;
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (CSV_SEARCH_CHARS.contains(c)) {
quoted = true;
sb.append('\"');
}
if (c == '\"') {
sb.append('\"');
}
sb.append(c);
}
if (quoted) {
sb.insert(0, '\"');
sb.append('\"');
}
return sb.toString();
}
} }

View File

@ -32,7 +32,6 @@
<classpathentry kind="lib" path="/OpenRocket Test Libraries/hamcrest-core-2.2.jar"/> <classpathentry kind="lib" path="/OpenRocket Test Libraries/hamcrest-core-2.2.jar"/>
<classpathentry kind="lib" path="/OpenRocket Test Libraries/hamcrest-2.2.jar"/> <classpathentry kind="lib" path="/OpenRocket Test Libraries/hamcrest-2.2.jar"/>
<classpathentry kind="lib" path="/OpenRocket Core/lib/opencsv-5.7.1.jar"/> <classpathentry kind="lib" path="/OpenRocket Core/lib/opencsv-5.7.1.jar"/>
<classpathentry kind="lib" path="/OpenRocket Core/lib/commons-text-1.10.0.jar"/>
<classpathentry kind="lib" path="/OpenRocket Test Libraries/jmock-2.12.0.jar"/> <classpathentry kind="lib" path="/OpenRocket Test Libraries/jmock-2.12.0.jar"/>
<classpathentry kind="lib" path="/OpenRocket Test Libraries/jmock-junit4-2.12.0.jar"/> <classpathentry kind="lib" path="/OpenRocket Test Libraries/jmock-junit4-2.12.0.jar"/>
<classpathentry kind="lib" path="/OpenRocket Test Libraries/junit-4.13.2.jar"/> <classpathentry kind="lib" path="/OpenRocket Test Libraries/junit-4.13.2.jar"/>

View File

@ -254,24 +254,6 @@
<SOURCES /> <SOURCES />
</library> </library>
</orderEntry> </orderEntry>
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="jar://$MODULE_DIR$/../core/lib/commons-lang3-3.12.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="jar://$MODULE_DIR$/../core/lib/commons-text-1.10.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library"> <orderEntry type="module-library">
<library> <library>
<CLASSES> <CLASSES>

View File

@ -117,7 +117,6 @@
<zipfileset src="${lib.dir}/jcommon-1.0.18.jar"/> <zipfileset src="${lib.dir}/jcommon-1.0.18.jar"/>
<zipfileset src="${lib.dir}/jfreechart-1.0.15.jar"/> <zipfileset src="${lib.dir}/jfreechart-1.0.15.jar"/>
<zipfileset src="${core.dir}/lib/opencsv-5.7.1.jar"/> <zipfileset src="${core.dir}/lib/opencsv-5.7.1.jar"/>
<zipfileset src="${core.dir}/lib/commons-text-1.10.0.jar"/>
<zipfileset src="${core.dir}/lib/annotation-detector-3.0.5.jar"/> <zipfileset src="${core.dir}/lib/annotation-detector-3.0.5.jar"/>
<zipfileset src="${core.dir}/lib/slf4j-api-1.7.30.jar"/> <zipfileset src="${core.dir}/lib/slf4j-api-1.7.30.jar"/>
<zipfileset src="${core.dir}/lib/logback-classic-1.2.11.jar"/> <zipfileset src="${core.dir}/lib/logback-classic-1.2.11.jar"/>

View File

@ -11,7 +11,6 @@ import javax.swing.JOptionPane;
import javax.swing.JTable; import javax.swing.JTable;
import net.sf.openrocket.util.StringUtils; import net.sf.openrocket.util.StringUtils;
import org.apache.commons.text.StringEscapeUtils;
import net.sf.openrocket.aerodynamics.Warning; import net.sf.openrocket.aerodynamics.Warning;
import net.sf.openrocket.aerodynamics.WarningSet; import net.sf.openrocket.aerodynamics.WarningSet;
@ -144,7 +143,7 @@ public class SimulationTableCSVExport {
} else { } else {
valueString = o.toString(); valueString = o.toString();
} }
rowColumnElement.add(StringEscapeUtils.escapeCsv(valueString)); rowColumnElement.add(StringUtils.escapeCSV(valueString));
} else { } else {
rowColumnElement.add(""); rowColumnElement.add("");
nullCnt++; nullCnt++;

View File

@ -81,9 +81,6 @@ import net.sf.openrocket.utils.TableRowTraversalPolicy;
@SuppressWarnings("serial") @SuppressWarnings("serial")
public class SimulationPanel extends JPanel { public class SimulationPanel extends JPanel {
private static final String SPACE = "SPACE";
private static final String TAB = "TAB";
private static final Logger log = LoggerFactory.getLogger(SimulationPanel.class); private static final Logger log = LoggerFactory.getLogger(SimulationPanel.class);
private static final Translator trans = Application.getTranslator(); private static final Translator trans = Application.getTranslator();