Apache Solr open-source search software
SOLR-8778: Deprecate CSVStrategy's setters, and make its pre-configured strategies immutable
Steve Rowea079ff2528a3
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex cb3030eea35..a96282b39db 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -380,6 +380,8 @@ Other Changes* SOLR-8780: Remove unused OverseerCollectionMessageHandler#getClusterStatus method. (Varun Thacker)+* SOLR-8778: Deprecate CSVStrategy's setters, and make its pre-configured strategies immutable. (Steve Rowe)+================== 5.5.1 ==================Bug Fixesdiff --git a/solr/core/src/java/org/apache/solr/handler/loader/CSVLoaderBase.java b/solr/core/src/java/org/apache/solr/handler/loader/CSVLoaderBase.javaindex 12e41c517e7..84c82d74679 100644--- a/solr/core/src/java/org/apache/solr/handler/loader/CSVLoaderBase.java+++ b/solr/core/src/java/org/apache/solr/handler/loader/CSVLoaderBase.java@@ -165,7 +165,7 @@ abstract class CSVLoaderBase extends ContentStreamLoader {templateAdd.overwrite=params.getBool(OVERWRITE,true);templateAdd.commitWithin = params.getInt(UpdateParams.COMMIT_WITHIN, -1);- strategy = new CSVStrategy(',', '"', CSVStrategy.COMMENTS_DISABLED, CSVStrategy.ESCAPE_DISABLED, false, false, false, true);+ strategy = new CSVStrategy(',', '"', CSVStrategy.COMMENTS_DISABLED, CSVStrategy.ESCAPE_DISABLED, false, false, false, true, "\n");String sep = params.get(SEPARATOR);if (sep!=null) {if (sep.length()!=1) throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,"Invalid separator:'"+sep+"'");@@ -274,7 +274,8 @@ abstract class CSVLoaderBase extends ContentStreamLoader {String escStr = params.getFieldParam(fname,ESCAPE);char fesc = escStr==null || escStr.length()==0 ? CSVStrategy.ESCAPE_DISABLED : escStr.charAt(0);- CSVStrategy fstrat = new CSVStrategy(fsep,fenc,CSVStrategy.COMMENTS_DISABLED,fesc, false, false, false, false);+ CSVStrategy fstrat = new CSVStrategy+ (fsep, fenc, CSVStrategy.COMMENTS_DISABLED, fesc, false, false, false, false, "\n");adders[i] = new CSVLoaderBase.FieldSplitter(fstrat, adders[i]);}}diff --git a/solr/core/src/java/org/apache/solr/internal/csv/CSVStrategy.java b/solr/core/src/java/org/apache/solr/internal/csv/CSVStrategy.javaindex e27c9c37261..930bbe06377 100644--- a/solr/core/src/java/org/apache/solr/internal/csv/CSVStrategy.java+++ b/solr/core/src/java/org/apache/solr/internal/csv/CSVStrategy.java@@ -20,119 +20,226 @@ import java.io.Serializable;/*** CSVStrategy- *+ ** Represents the strategy for a CSV.*/public class CSVStrategy implements Cloneable, Serializable {- private char delimiter;- private char encapsulator;- private char commentStart;- private char escape;- private boolean ignoreLeadingWhitespaces;- private boolean ignoreTrailingWhitespaces;- private boolean interpretUnicodeEscapes;- private boolean ignoreEmptyLines;-- // controls for output- private String printerNewline = "\n";-- // -2 is used to signal disabled, because it won't be confused with- // an EOF signal (-1), and because \ufffe in UTF-16 would be- // encoded as two chars (using surrogates) and thus there should never- // be a collision with a real text char.- public static char COMMENTS_DISABLED = (char)-2;- public static char ESCAPE_DISABLED = (char)-2;- public static char ENCAPSULATOR_DISABLED = (char)-2;-- public static CSVStrategy DEFAULT_STRATEGY = new CSVStrategy(',', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, true,- true, false, true);- public static CSVStrategy EXCEL_STRATEGY = new CSVStrategy(',', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, false,- false, false, false);- public static CSVStrategy TDF_STRATEGY = new CSVStrategy('\t', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, true,- true, false, true);--- public CSVStrategy(char delimiter, char encapsulator, char commentStart) {- this(delimiter, encapsulator, commentStart, ESCAPE_DISABLED, true, true, false, true);- }-- /**- * Customized CSV strategy setter.- *- * @param delimiter a Char used for value separation- * @param encapsulator a Char used as value encapsulation marker- * @param commentStart a Char used for comment identification- * @param ignoreLeadingWhitespace TRUE when leading whitespaces should be- * ignored- * @param interpretUnicodeEscapes TRUE when unicode escapes should be- * interpreted- * @param ignoreEmptyLines TRUE when the parser should skip emtpy lines- */- public CSVStrategy(- char delimiter,- char encapsulator,- char commentStart,- char escape,- boolean ignoreLeadingWhitespace,- boolean ignoreTrailingWhitespace,- boolean interpretUnicodeEscapes,- boolean ignoreEmptyLines)- {- setDelimiter(delimiter);- setEncapsulator(encapsulator);- setCommentStart(commentStart);- setEscape(escape);- setIgnoreLeadingWhitespaces(ignoreLeadingWhitespace);- setIgnoreTrailingWhitespaces(ignoreTrailingWhitespace);- setUnicodeEscapeInterpretation(interpretUnicodeEscapes);- setIgnoreEmptyLines(ignoreEmptyLines);- }-- public void setDelimiter(char delimiter) { this.delimiter = delimiter; }- public char getDelimiter() { return this.delimiter; }-- public void setEncapsulator(char encapsulator) { this.encapsulator = encapsulator; }- public char getEncapsulator() { return this.encapsulator; }-- public void setCommentStart(char commentStart) { this.commentStart = commentStart; }- public char getCommentStart() { return this.commentStart; }- public boolean isCommentingDisabled() { return this.commentStart == COMMENTS_DISABLED; }-- public void setEscape(char escape) { this.escape = escape; }- public char getEscape() { return this.escape; }-- public void setIgnoreLeadingWhitespaces(boolean ignoreLeadingWhitespaces) {- this.ignoreLeadingWhitespaces = ignoreLeadingWhitespaces;- }- public boolean getIgnoreLeadingWhitespaces() { return this.ignoreLeadingWhitespaces; }-- public void setIgnoreTrailingWhitespaces(boolean ignoreTrailingWhitespaces) {- this.ignoreTrailingWhitespaces = ignoreTrailingWhitespaces;- }- public boolean getIgnoreTrailingWhitespaces() { return this.ignoreTrailingWhitespaces; }-- public void setUnicodeEscapeInterpretation(boolean interpretUnicodeEscapes) {- this.interpretUnicodeEscapes = interpretUnicodeEscapes;- }- public boolean getUnicodeEscapeInterpretation() { return this.interpretUnicodeEscapes; }-- public void setIgnoreEmptyLines(boolean ignoreEmptyLines) { this.ignoreEmptyLines = ignoreEmptyLines; }- public boolean getIgnoreEmptyLines() { return this.ignoreEmptyLines; }-- public void setPrinterNewline(String newline) {- this.printerNewline = newline;- }- public String getPrinterNewline() {- return this.printerNewline;+ private char delimiter;+ private char encapsulator;+ private char commentStart;+ private char escape;+ private boolean ignoreLeadingWhitespaces;+ private boolean ignoreTrailingWhitespaces;+ private boolean interpretUnicodeEscapes;+ private boolean ignoreEmptyLines;++ // controls for output+ private String printerNewline;++ // -2 is used to signal disabled, because it won't be confused with+ // an EOF signal (-1), and because \ufffe in UTF-16 would be+ // encoded as two chars (using surrogates) and thus there should never+ // be a collision with a real text char.+ public static char COMMENTS_DISABLED = (char)-2;+ public static char ESCAPE_DISABLED = (char)-2;+ public static char ENCAPSULATOR_DISABLED = (char)-2;+ public static String DEFAULT_PRINTER_NEWLINE = "\n";++ public static final CSVStrategy DEFAULT_STRATEGY = new ImmutableCSVStrategy+ (',', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, true, true, false, true, DEFAULT_PRINTER_NEWLINE);+ public static final CSVStrategy EXCEL_STRATEGY = new ImmutableCSVStrategy+ (',', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, false, false, false, false, DEFAULT_PRINTER_NEWLINE);+ public static final CSVStrategy TDF_STRATEGY = new ImmutableCSVStrategy+ ('\t', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, true, true, false, true, DEFAULT_PRINTER_NEWLINE);++ public CSVStrategy(char delimiter, char encapsulator, char commentStart) {+ this(delimiter, encapsulator, commentStart, ESCAPE_DISABLED, true, true, false, true, DEFAULT_PRINTER_NEWLINE);+ }++ /**+ * Customized CSV strategy setter.+ *+ * @param delimiter a Char used for value separation+ * @param encapsulator a Char used as value encapsulation marker+ * @param commentStart a Char used for comment identification+ * @param escape a Char used for escaping+ * @param ignoreTrailingWhitespaces TRUE when trailing whitespaces should be+ * ignored+ * @param ignoreLeadingWhitespaces TRUE when leading whitespaces should be+ * ignored+ * @param interpretUnicodeEscapes TRUE when unicode escapes should be+ * interpreted+ * @param ignoreEmptyLines TRUE when the parser should skip emtpy lines+ * @param printerNewline The string to use when printing a newline+ */+ public CSVStrategy(char delimiter, char encapsulator, char commentStart, char escape,+ boolean ignoreLeadingWhitespaces, boolean ignoreTrailingWhitespaces,+ boolean interpretUnicodeEscapes, boolean ignoreEmptyLines,+ String printerNewline) {+ this.delimiter = delimiter;+ this.encapsulator = encapsulator;+ this.commentStart = commentStart;+ this.escape = escape;+ this.ignoreLeadingWhitespaces = ignoreLeadingWhitespaces;+ this.ignoreTrailingWhitespaces = ignoreTrailingWhitespaces;+ this.interpretUnicodeEscapes = interpretUnicodeEscapes;+ this.ignoreEmptyLines = ignoreEmptyLines;+ this.printerNewline = printerNewline;+ }++ /**+ * Customized CSV strategy setter.+ *+ * @param delimiter a Char used for value separation+ * @param encapsulator a Char used as value encapsulation marker+ * @param commentStart a Char used for comment identification+ * @param escape a Char used for escaping+ * @param ignoreTrailingWhitespaces TRUE when trailing whitespaces should be+ * ignored+ * @param ignoreLeadingWhitespaces TRUE when leading whitespaces should be+ * ignored+ * @param interpretUnicodeEscapes TRUE when unicode escapes should be+ * interpreted+ * @param ignoreEmptyLines TRUE when the parser should skip emtpy lines+ * @deprecated Use the ctor that also takes printerNewline. This ctor will be removed in Solr 7.+ */+ @Deprecated+ public CSVStrategy(char delimiter, char encapsulator, char commentStart, char escape,+ boolean ignoreLeadingWhitespaces, boolean ignoreTrailingWhitespaces,+ boolean interpretUnicodeEscapes, boolean ignoreEmptyLines) {+ this(delimiter, encapsulator, commentStart, escape,+ ignoreLeadingWhitespaces, ignoreTrailingWhitespaces,+ interpretUnicodeEscapes, ignoreEmptyLines,+ DEFAULT_PRINTER_NEWLINE);+ }++ /** @deprecated will be removed in Solr 7 */+ @Deprecated+ public void setDelimiter(char delimiter) { this.delimiter = delimiter; }+ public char getDelimiter() { return this.delimiter; }++ /** @deprecated will be removed in Solr 7 */+ @Deprecated+ public void setEncapsulator(char encapsulator) { this.encapsulator = encapsulator; }+ public char getEncapsulator() { return this.encapsulator; }++ /** @deprecated will be removed in Solr 7 */+ @Deprecated+ public void setCommentStart(char commentStart) { this.commentStart = commentStart; }+ public char getCommentStart() { return this.commentStart; }+ public boolean isCommentingDisabled() { return this.commentStart == COMMENTS_DISABLED; }++ /** @deprecated will be removed in Solr 7 */+ @Deprecated+ public void setEscape(char escape) { this.escape = escape; }+ public char getEscape() { return this.escape; }++ /** @deprecated will be removed in Solr 7 */+ @Deprecated+ public void setIgnoreLeadingWhitespaces(boolean ignoreLeadingWhitespaces) {+ this.ignoreLeadingWhitespaces = ignoreLeadingWhitespaces;+ }+ public boolean getIgnoreLeadingWhitespaces() { return this.ignoreLeadingWhitespaces; }++ /** @deprecated will be removed in Solr 7 */+ @Deprecated+ public void setIgnoreTrailingWhitespaces(boolean ignoreTrailingWhitespaces) {+ this.ignoreTrailingWhitespaces = ignoreTrailingWhitespaces;+ }+ public boolean getIgnoreTrailingWhitespaces() { return this.ignoreTrailingWhitespaces; }++ /** @deprecated will be removed in Solr 7 */+ @Deprecated+ public void setUnicodeEscapeInterpretation(boolean interpretUnicodeEscapes) {+ this.interpretUnicodeEscapes = interpretUnicodeEscapes;+ }+ public boolean getUnicodeEscapeInterpretation() { return this.interpretUnicodeEscapes; }++ /** @deprecated will be removed in Solr 7 */+ @Deprecated+ public void setIgnoreEmptyLines(boolean ignoreEmptyLines) { this.ignoreEmptyLines = ignoreEmptyLines; }+ public boolean getIgnoreEmptyLines() { return this.ignoreEmptyLines; }++ /** @deprecated will be removed in Solr 7 */+ @Deprecated+ public void setPrinterNewline(String newline) {+ this.printerNewline = newline;+ }+ public String getPrinterNewline() {+ return this.printerNewline;+ }++ /** @deprecated will be removed in Solr 7 */+ @Deprecated+ @Override+ public Object clone() {+ try {+ return super.clone();+ } catch (CloneNotSupportedException e) {+ throw new RuntimeException(e); // impossible}+ }+}- @Override- public Object clone() {- try {- return super.clone();- } catch (CloneNotSupportedException e) {- throw new RuntimeException(e); // impossible- }- }+/**+ * @deprecated will be removed in Solr 7+ * @lucene.internal+ */+@Deprecated+class ImmutableCSVStrategy extends CSVStrategy {+ ImmutableCSVStrategy(char delimiter, char encapsulator, char commentStart, char escape,+ boolean ignoreLeadingWhitespaces, boolean ignoreTrailingWhitespaces,+ boolean interpretUnicodeEscapes, boolean ignoreEmptyLines,+ String printerNewline) {+ super(delimiter, encapsulator, commentStart, escape,+ ignoreLeadingWhitespaces, ignoreTrailingWhitespaces,+ interpretUnicodeEscapes, ignoreEmptyLines,+ printerNewline);+ }+ @Override+ public void setDelimiter(char delimiter) {+ throw new UnsupportedOperationException();+ }+ @Override+ public void setEncapsulator(char encapsulator) {+ throw new UnsupportedOperationException();+ }+ @Override+ public void setCommentStart(char commentStart) {+ throw new UnsupportedOperationException();+ }+ @Override+ public void setEscape(char escape) {+ throw new UnsupportedOperationException();+ }+ @Override+ public void setIgnoreLeadingWhitespaces(boolean ignoreLeadingWhitespaces) {+ throw new UnsupportedOperationException();+ }+ @Override+ public void setIgnoreTrailingWhitespaces(boolean ignoreTrailingWhitespaces) {+ throw new UnsupportedOperationException();+ }+ @Override+ public void setUnicodeEscapeInterpretation(boolean interpretUnicodeEscapes) {+ throw new UnsupportedOperationException();+ }+ @Override+ public void setIgnoreEmptyLines(boolean ignoreEmptyLines) {+ throw new UnsupportedOperationException();+ }+ @Override+ public void setPrinterNewline(String newline) {+ throw new UnsupportedOperationException();+ }++ /** Returns a mutable clone */+ @Override+ public Object clone() {+ return new CSVStrategy(getDelimiter(), getEncapsulator(), getCommentStart(), getEscape(),+ getIgnoreLeadingWhitespaces(), getIgnoreTrailingWhitespaces(),+ getUnicodeEscapeInterpretation(), getIgnoreEmptyLines(),+ getPrinterNewline());+ }}diff --git a/solr/core/src/java/org/apache/solr/response/CSVResponseWriter.java b/solr/core/src/java/org/apache/solr/response/CSVResponseWriter.javaindex 816e0e038f0..ba77821badb 100644--- a/solr/core/src/java/org/apache/solr/response/CSVResponseWriter.java+++ b/solr/core/src/java/org/apache/solr/response/CSVResponseWriter.java@@ -179,7 +179,8 @@ class CSVWriter extends TextResponseWriter {public void writeResponse() throws IOException {SolrParams params = req.getParams();- strategy = new CSVStrategy(',', '"', CSVStrategy.COMMENTS_DISABLED, CSVStrategy.ESCAPE_DISABLED, false, false, false, true);+ strategy = new CSVStrategy+ (',', '"', CSVStrategy.COMMENTS_DISABLED, CSVStrategy.ESCAPE_DISABLED, false, false, false, true, "\n");CSVStrategy strat = strategy;String sep = params.get(CSV_SEPARATOR);@@ -218,7 +219,8 @@ class CSVWriter extends TextResponseWriter {printer = new CSVPrinter(writer, strategy);- CSVStrategy mvStrategy = new CSVStrategy(strategy.getDelimiter(), CSVStrategy.ENCAPSULATOR_DISABLED, CSVStrategy.COMMENTS_DISABLED, '\\', false, false, false, false);+ CSVStrategy mvStrategy = new CSVStrategy(strategy.getDelimiter(), CSVStrategy.ENCAPSULATOR_DISABLED,+ CSVStrategy.COMMENTS_DISABLED, '\\', false, false, false, false, "\n");strat = mvStrategy;sep = params.get(MV_SEPARATOR);diff --git a/solr/core/src/test/org/apache/solr/internal/csv/CSVParserTest.java b/solr/core/src/test/org/apache/solr/internal/csv/CSVParserTest.javaindex 54b0c614bf6..0688c5257d8 100644--- a/solr/core/src/test/org/apache/solr/internal/csv/CSVParserTest.java+++ b/solr/core/src/test/org/apache/solr/internal/csv/CSVParserTest.java@@ -446,7 +446,7 @@ public class CSVParserTest extends TestCase {};- CSVStrategy strategy = new CSVStrategy(',','\'',CSVStrategy.COMMENTS_DISABLED,'/',false,false,true,true);+ CSVStrategy strategy = new CSVStrategy(',','\'',CSVStrategy.COMMENTS_DISABLED,'/',false,false,true,true,"\n");CSVParser parser = new CSVParser(new StringReader(code), strategy);String[][] tmp = parser.getAllValues();@@ -474,7 +474,8 @@ public class CSVParserTest extends TestCase {};- CSVStrategy strategy = new CSVStrategy(',',CSVStrategy.ENCAPSULATOR_DISABLED,CSVStrategy.COMMENTS_DISABLED,'/',false,false,true,true);+ CSVStrategy strategy = new CSVStrategy+ (',', CSVStrategy.ENCAPSULATOR_DISABLED, CSVStrategy.COMMENTS_DISABLED, '/', false, false, true, true, "\n");CSVParser parser = new CSVParser(new StringReader(code), strategy);String[][] tmp = parser.getAllValues();@@ -529,8 +530,9 @@ public class CSVParserTest extends TestCase {public void testUnicodeEscape() throws IOException {String code = "abc,\\u0070\\u0075\\u0062\\u006C\\u0069\\u0063";- CSVParser parser = new CSVParser(new StringReader(code));- parser.getStrategy().setUnicodeEscapeInterpretation(true);+ CSVStrategy strategy = (CSVStrategy)CSVStrategy.DEFAULT_STRATEGY.clone();+ strategy.setUnicodeEscapeInterpretation(true);+ CSVParser parser = new CSVParser(new StringReader(code), strategy);String[] data = parser.getLine();assertEquals(2, data.length);assertEquals("abc", data[0]);diff --git a/solr/core/src/test/org/apache/solr/internal/csv/CSVPrinterTest.java b/solr/core/src/test/org/apache/solr/internal/csv/CSVPrinterTest.javaindex 6a9a8192e98..a141efb2448 100644--- a/solr/core/src/test/org/apache/solr/internal/csv/CSVPrinterTest.java+++ b/solr/core/src/test/org/apache/solr/internal/csv/CSVPrinterTest.java@@ -83,7 +83,8 @@ public class CSVPrinterTest extends TestCase {doRandom(iter);// Strategy for MySQL- strategy = new CSVStrategy('\t', CSVStrategy.ENCAPSULATOR_DISABLED, CSVStrategy.COMMENTS_DISABLED,'\\',false, false, false, false);+ strategy = new CSVStrategy+ ('\t', CSVStrategy.ENCAPSULATOR_DISABLED, CSVStrategy.COMMENTS_DISABLED,'\\',false, false, false, false, "\n");doRandom(iter);}