CE-881 fix how booleans are written; after profiling, replace regex for cleansing test w/ indexOf calls

This commit is contained in:
2024-04-02 19:30:52 -05:00
parent 01b7b3fe63
commit ad3d9cc6d1

View File

@ -24,7 +24,6 @@ package com.kingsrook.qqq.backend.core.actions.reporting.excel.poi;
import java.io.IOException; import java.io.IOException;
import java.io.Writer; import java.io.Writer;
import java.util.regex.Pattern;
import org.apache.poi.ss.util.CellReference; import org.apache.poi.ss.util.CellReference;
@ -34,8 +33,6 @@ import org.apache.poi.ss.util.CellReference;
*******************************************************************************/ *******************************************************************************/
public class StreamedPoiSheetWriter public class StreamedPoiSheetWriter
{ {
private static Pattern xmlSpecialChars = Pattern.compile(".*[&<>'\"].*");
private final Writer writer; private final Writer writer;
private int rowNo; private int rowNo;
@ -111,8 +108,11 @@ public class StreamedPoiSheetWriter
{ {
writer.write(" s=\"" + styleIndex + "\""); writer.write(" s=\"" + styleIndex + "\"");
} }
String cleanValue = cleanseValue(value);
writer.write(">"); writer.write(">");
writer.write("<is><t>" + cleanseValue(value) + "</t></is>"); writer.write("<is><t>" + cleanValue + "</t></is>");
writer.write("</c>"); writer.write("</c>");
} }
@ -123,8 +123,9 @@ public class StreamedPoiSheetWriter
*******************************************************************************/ *******************************************************************************/
public static String cleanseValue(String value) public static String cleanseValue(String value)
{ {
// todo - profile... if(value != null)
if(xmlSpecialChars.matcher(value).find()) {
if(value.indexOf('&') > -1 || value.indexOf('<') > -1 || value.indexOf('>') > -1 || value.indexOf('\'') > -1 || value.indexOf('"') > -1)
{ {
value = value.replace("&", "&amp;"); value = value.replace("&", "&amp;");
value = value.replace("<", "&lt;"); value = value.replace("<", "&lt;");
@ -132,6 +133,8 @@ public class StreamedPoiSheetWriter
value = value.replace("'", "&apos;"); value = value.replace("'", "&apos;");
value = value.replace("\"", "&quot;"); value = value.replace("\"", "&quot;");
} }
}
return (value); return (value);
} }
@ -191,13 +194,16 @@ public class StreamedPoiSheetWriter
public void createCell(int columnIndex, Boolean value, int styleIndex) throws IOException public void createCell(int columnIndex, Boolean value, int styleIndex) throws IOException
{ {
String ref = new CellReference(rowNo, columnIndex).formatAsString(); String ref = new CellReference(rowNo, columnIndex).formatAsString();
writer.write("<c r=\"" + ref + "\" t=\"n\""); writer.write("<c r=\"" + ref + "\" t=\"b\"");
if(styleIndex != -1) if(styleIndex != -1)
{ {
writer.write(" s=\"" + styleIndex + "\""); writer.write(" s=\"" + styleIndex + "\"");
} }
writer.write(">"); writer.write(">");
writer.write("<v>" + value + "</v>"); if(value != null)
{
writer.write("<v>" + (value ? 1 : 0) + "</v>");
}
writer.write("</c>"); writer.write("</c>");
} }