Skip to content

Apache Lucene open-source search software

LUCENE-4199: fix more charset/locale usages

Robert Muir9df919c5baca
diff --git a/lucene/benchmark/src/java/org/apache/lucene/benchmark/byTask/feeds/DocMaker.java b/lucene/benchmark/src/java/org/apache/lucene/benchmark/byTask/feeds/DocMaker.java
index d0be4109b3b..af434cc8dab 100644
--- a/lucene/benchmark/src/java/org/apache/lucene/benchmark/byTask/feeds/DocMaker.java
+++ b/lucene/benchmark/src/java/org/apache/lucene/benchmark/byTask/feeds/DocMaker.java
@@ -29,6 +29,7 @@ import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.Random;
+import java.util.TimeZone;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.lucene.benchmark.byTask.utils.Config;
@@ -183,7 +184,7 @@ public class DocMaker implements Closeable {
private static class DateUtil {
public SimpleDateFormat parser = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss", Locale.ROOT);
- public Calendar cal = Calendar.getInstance();
+ public Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.ROOT);
public ParsePosition pos = new ParsePosition(0);
public DateUtil() {
parser.setLenient(true);
diff --git a/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/nodes/QueryNodeImpl.java b/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/nodes/QueryNodeImpl.java
index 2c049efb0a8..f2c16c89767 100644
--- a/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/nodes/QueryNodeImpl.java
+++ b/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/nodes/QueryNodeImpl.java
@@ -20,6 +20,7 @@ package org.apache.lucene.queryparser.flexible.core.nodes;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
+import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
@@ -151,20 +152,20 @@ public abstract class QueryNodeImpl implements QueryNode, Cloneable {
}
public void setTag(String tagName, Object value) {
- this.tags.put(tagName.toLowerCase(), value);
+ this.tags.put(tagName.toLowerCase(Locale.ROOT), value);
}
public void unsetTag(String tagName) {
- this.tags.remove(tagName.toLowerCase());
+ this.tags.remove(tagName.toLowerCase(Locale.ROOT));
}
/** verify if a node contains a tag */
public boolean containsTag(String tagName) {
- return this.tags.containsKey(tagName.toLowerCase());
+ return this.tags.containsKey(tagName.toLowerCase(Locale.ROOT));
}
public Object getTag(String tagName) {
- return this.tags.get(tagName.toString().toLowerCase());
+ return this.tags.get(tagName.toLowerCase(Locale.ROOT));
}
private QueryNode parent = null;
diff --git a/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/util/UnescapedCharSequence.java b/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/util/UnescapedCharSequence.java
index e3c649d28df..2527fbc95a2 100644
--- a/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/util/UnescapedCharSequence.java
+++ b/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/util/UnescapedCharSequence.java
@@ -1,5 +1,7 @@
package org.apache.lucene.queryparser.flexible.core.util;
+import java.util.Locale;
+
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
@@ -146,12 +148,12 @@ public final class UnescapedCharSequence implements CharSequence {
else return false;
}
- public static CharSequence toLowerCase(CharSequence text) {
+ public static CharSequence toLowerCase(CharSequence text, Locale locale) {
if (text instanceof UnescapedCharSequence) {
- char[] chars = text.toString().toLowerCase().toCharArray();
+ char[] chars = text.toString().toLowerCase(locale).toCharArray();
boolean[] wasEscaped = ((UnescapedCharSequence)text).wasEscaped;
return new UnescapedCharSequence(chars, wasEscaped, 0, chars.length);
} else
- return new UnescapedCharSequence(text.toString().toLowerCase());
+ return new UnescapedCharSequence(text.toString().toLowerCase(locale));
}
}
diff --git a/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/parser/EscapeQuerySyntaxImpl.java b/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/parser/EscapeQuerySyntaxImpl.java
index 5d36b3abca0..d3bb83ff86a 100644
--- a/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/parser/EscapeQuerySyntaxImpl.java
+++ b/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/parser/EscapeQuerySyntaxImpl.java
@@ -50,7 +50,7 @@ public class EscapeQuerySyntaxImpl implements EscapeQuerySyntax {
// regular escapable Char for terms
for (int i = 0; i < escapableTermChars.length; i++) {
- buffer = replaceIgnoreCase(buffer, escapableTermChars[i].toLowerCase(),
+ buffer = replaceIgnoreCase(buffer, escapableTermChars[i].toLowerCase(locale),
"\\", locale);
}
@@ -73,7 +73,7 @@ public class EscapeQuerySyntaxImpl implements EscapeQuerySyntax {
CharSequence buffer = str;
for (int i = 0; i < escapableQuotedChars.length; i++) {
- buffer = replaceIgnoreCase(buffer, escapableTermChars[i].toLowerCase(),
+ buffer = replaceIgnoreCase(buffer, escapableTermChars[i].toLowerCase(locale),
"\\", locale);
}
return buffer;
@@ -178,7 +178,7 @@ public class EscapeQuerySyntaxImpl implements EscapeQuerySyntax {
CharSequence buffer = str;
for (int i = 0; i < escapableWhiteChars.length; i++) {
- buffer = replaceIgnoreCase(buffer, escapableWhiteChars[i].toLowerCase(),
+ buffer = replaceIgnoreCase(buffer, escapableWhiteChars[i].toLowerCase(locale),
"\\", locale);
}
return buffer;
diff --git a/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/processors/LowercaseExpandedTermsQueryNodeProcessor.java b/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/processors/LowercaseExpandedTermsQueryNodeProcessor.java
index 80bedebb33c..fa6c0424c44 100644
--- a/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/processors/LowercaseExpandedTermsQueryNodeProcessor.java
+++ b/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/processors/LowercaseExpandedTermsQueryNodeProcessor.java
@@ -18,6 +18,7 @@ package org.apache.lucene.queryparser.flexible.standard.processors;
*/
import java.util.List;
+import java.util.Locale;
import org.apache.lucene.queryparser.flexible.core.QueryNodeException;
import org.apache.lucene.queryparser.flexible.core.config.QueryConfigHandler;
@@ -44,9 +45,10 @@ import org.apache.lucene.queryparser.flexible.standard.nodes.WildcardQueryNode;
*/
public class LowercaseExpandedTermsQueryNodeProcessor extends
QueryNodeProcessorImpl {
+ private final Locale locale;
- public LowercaseExpandedTermsQueryNodeProcessor() {
- // empty constructor
+ public LowercaseExpandedTermsQueryNodeProcessor(Locale locale) {
+ this.locale = locale;
}
@Override
@@ -71,7 +73,7 @@ public class LowercaseExpandedTermsQueryNodeProcessor extends
TextableQueryNode txtNode = (TextableQueryNode) node;
CharSequence text = txtNode.getText();
- txtNode.setText(text != null ? UnescapedCharSequence.toLowerCase(text) : null);
+ txtNode.setText(text != null ? UnescapedCharSequence.toLowerCase(text, locale) : null);
}
return node;
diff --git a/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/processors/StandardQueryNodeProcessorPipeline.java b/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/processors/StandardQueryNodeProcessorPipeline.java
index 7915f93ec5f..abc59e28ff8 100644
--- a/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/processors/StandardQueryNodeProcessorPipeline.java
+++ b/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/processors/StandardQueryNodeProcessorPipeline.java
@@ -17,12 +17,15 @@ package org.apache.lucene.queryparser.flexible.standard.processors;
* limitations under the License.
*/
+import java.util.Locale;
+
import org.apache.lucene.queryparser.flexible.core.config.QueryConfigHandler;
import org.apache.lucene.queryparser.flexible.core.processors.NoChildOptimizationQueryNodeProcessor;
import org.apache.lucene.queryparser.flexible.core.processors.QueryNodeProcessorPipeline;
import org.apache.lucene.queryparser.flexible.core.processors.RemoveDeletedQueryNodesProcessor;
import org.apache.lucene.queryparser.flexible.standard.builders.StandardQueryTreeBuilder;
import org.apache.lucene.queryparser.flexible.standard.config.StandardQueryConfigHandler;
+import org.apache.lucene.queryparser.flexible.standard.config.StandardQueryConfigHandler.ConfigurationKeys;
import org.apache.lucene.queryparser.flexible.standard.parser.StandardSyntaxParser;
import org.apache.lucene.search.Query;
@@ -55,7 +58,8 @@ public class StandardQueryNodeProcessorPipeline extends
add(new OpenRangeQueryNodeProcessor());
add(new NumericQueryNodeProcessor());
add(new NumericRangeQueryNodeProcessor());
- add(new LowercaseExpandedTermsQueryNodeProcessor());
+ // TODO: queryConfig is null here!
+ add(new LowercaseExpandedTermsQueryNodeProcessor(Locale.getDefault()));
add(new TermRangeQueryNodeProcessor());
add(new AllowLeadingWildcardProcessor());
add(new AnalyzerQueryNodeProcessor());
diff --git a/solr/contrib/extraction/src/java/org/apache/solr/handler/extraction/RegexRulesPasswordProvider.java b/solr/contrib/extraction/src/java/org/apache/solr/handler/extraction/RegexRulesPasswordProvider.java
index 1ab323b190a..76b8262b3b9 100644
--- a/solr/contrib/extraction/src/java/org/apache/solr/handler/extraction/RegexRulesPasswordProvider.java
+++ b/solr/contrib/extraction/src/java/org/apache/solr/handler/extraction/RegexRulesPasswordProvider.java
@@ -20,12 +20,12 @@ package org.apache.solr.handler.extraction;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
-import java.io.InputStreamReader;
import java.util.LinkedHashMap;
import java.util.Map.Entry;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
+import org.apache.lucene.util.IOUtils;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.metadata.TikaMetadataKeys;
import org.apache.tika.parser.PasswordProvider;
@@ -73,7 +73,7 @@ public class RegexRulesPasswordProvider implements PasswordProvider {
*/
public static LinkedHashMap<Pattern,String> parseRulesFile(InputStream is) {
LinkedHashMap<Pattern,String> rules = new LinkedHashMap<Pattern,String>();
- BufferedReader br = new BufferedReader(new InputStreamReader(is));
+ BufferedReader br = new BufferedReader(IOUtils.getDecodingReader(is, IOUtils.CHARSET_UTF_8));
String line;
try {
int linenum = 0;
diff --git a/solr/core/src/java/org/apache/solr/analysis/LegacyHTMLStripCharFilter.java b/solr/core/src/java/org/apache/solr/analysis/LegacyHTMLStripCharFilter.java
index b81c679a1c5..e0af2489e54 100644
--- a/solr/core/src/java/org/apache/solr/analysis/LegacyHTMLStripCharFilter.java
+++ b/solr/core/src/java/org/apache/solr/analysis/LegacyHTMLStripCharFilter.java
@@ -21,6 +21,7 @@ import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
+import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Set;
@@ -71,7 +72,7 @@ public class LegacyHTMLStripCharFilter extends BaseCharFilter {
public static void main(String[] args) throws IOException {
Reader in = new LegacyHTMLStripCharFilter(
- CharReader.get(new InputStreamReader(System.in)));
+ CharReader.get(new InputStreamReader(System.in, Charset.defaultCharset())));
int ch;
while ( (ch=in.read()) != -1 ) System.out.print((char)ch);
}
diff --git a/solr/core/src/java/org/apache/solr/search/Grouping.java b/solr/core/src/java/org/apache/solr/search/Grouping.java
index 0af470e2f12..2a41d438c32 100755
--- a/solr/core/src/java/org/apache/solr/search/Grouping.java
+++ b/solr/core/src/java/org/apache/solr/search/Grouping.java
@@ -376,7 +376,7 @@ public class Grouping {
cachedCollector.replay(secondPhaseCollectors);
} else {
signalCacheWarning = true;
- logger.warn(String.format("The grouping cache is active, but not used because it exceeded the max cache limit of %d percent", maxDocsPercentageToCache));
+ logger.warn(String.format(Locale.ROOT, "The grouping cache is active, but not used because it exceeded the max cache limit of %d percent", maxDocsPercentageToCache));
logger.warn("Please increase cache size or disable group caching.");
searchWithTimeLimiter(luceneFilter, secondPhaseCollectors);
}
diff --git a/solr/core/src/test/org/apache/solr/search/TestLFUCache.java b/solr/core/src/test/org/apache/solr/search/TestLFUCache.java
index e63e10193a5..3507389c7b5 100644
--- a/solr/core/src/test/org/apache/solr/search/TestLFUCache.java
+++ b/solr/core/src/test/org/apache/solr/search/TestLFUCache.java
@@ -26,6 +26,7 @@ import org.junit.Test;
import java.io.IOException;
import java.util.HashMap;
+import java.util.Locale;
import java.util.Map;
@@ -112,7 +113,7 @@ public class TestLFUCache extends SolrTestCaseJ4 {
private void assertCache(LFUCache cache, int... gets) {
for (int idx : gets) {
if (cache.get(idx) == null) {
- log.error(String.format("Expected entry %d not in cache", idx));
+ log.error(String.format(Locale.ROOT, "Expected entry %d not in cache", idx));
assertTrue(false);
}
}
@@ -120,7 +121,7 @@ public class TestLFUCache extends SolrTestCaseJ4 {
private void assertNotCache(LFUCache cache, int... gets) {
for (int idx : gets) {
if (cache.get(idx) != null) {
- log.error(String.format("Unexpected entry %d in cache", idx));
+ log.error(String.format(Locale.ROOT, "Unexpected entry %d in cache", idx));
assertTrue(false);
}
}
diff --git a/solr/core/src/test/org/apache/solr/search/TestSearchPerf.java b/solr/core/src/test/org/apache/solr/search/TestSearchPerf.java
index ddbde88fa02..ad19e3af460 100755
--- a/solr/core/src/test/org/apache/solr/search/TestSearchPerf.java
+++ b/solr/core/src/test/org/apache/solr/search/TestSearchPerf.java
@@ -50,7 +50,7 @@ public class TestSearchPerf extends AbstractSolrTestCase {
}
String t(int tnum) {
- return String.format("%08d", tnum);
+ return String.format(Locale.ROOT, "%08d", tnum);
}
Random r = new Random(0); // specific seed for reproducible perf testing
diff --git a/solr/solrj/src/java/org/apache/solr/common/params/FacetParams.java b/solr/solrj/src/java/org/apache/solr/common/params/FacetParams.java
index 897daa48735..135db223927 100644
--- a/solr/solrj/src/java/org/apache/solr/common/params/FacetParams.java
+++ b/solr/solrj/src/java/org/apache/solr/common/params/FacetParams.java
@@ -264,10 +264,10 @@ public interface FacetParams {
public enum FacetRangeOther {
BEFORE, AFTER, BETWEEN, ALL, NONE;
@Override
- public String toString() { return super.toString().toLowerCase(); }
+ public String toString() { return super.toString().toLowerCase(Locale.ROOT); }
public static FacetRangeOther get(String label) {
try {
- return valueOf(label.toUpperCase());
+ return valueOf(label.toUpperCase(Locale.ROOT));
} catch (IllegalArgumentException e) {
throw new SolrException
(SolrException.ErrorCode.BAD_REQUEST,
@@ -283,10 +283,10 @@ public interface FacetParams {
public enum FacetDateOther {
BEFORE, AFTER, BETWEEN, ALL, NONE;
@Override
- public String toString() { return super.toString().toLowerCase(); }
+ public String toString() { return super.toString().toLowerCase(Locale.ROOT); }
public static FacetDateOther get(String label) {
try {
- return valueOf(label.toUpperCase());
+ return valueOf(label.toUpperCase(Locale.ROOT));
} catch (IllegalArgumentException e) {
throw new SolrException
(SolrException.ErrorCode.BAD_REQUEST,
diff --git a/solr/solrj/src/test/org/apache/solr/common/util/ContentStreamTest.java b/solr/solrj/src/test/org/apache/solr/common/util/ContentStreamTest.java
index bae7dc9c291..a63d1bdd4cb 100755
--- a/solr/solrj/src/test/org/apache/solr/common/util/ContentStreamTest.java
+++ b/solr/solrj/src/test/org/apache/solr/common/util/ContentStreamTest.java
@@ -21,9 +21,9 @@ import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
-import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
+import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.ConnectException;
import java.net.HttpURLConnection;
@@ -60,7 +60,7 @@ public class ContentStreamTest extends LuceneTestCase
ContentStreamBase stream = new ContentStreamBase.FileStream( file );
assertEquals( file.length(), stream.getSize().intValue() );
assertTrue( IOUtils.contentEquals( new FileInputStream( file ), stream.getStream() ) );
- assertTrue( IOUtils.contentEquals( new FileReader( file ), stream.getReader() ) );
+ assertTrue( IOUtils.contentEquals( new InputStreamReader(new FileInputStream(file), "UTF-8"), stream.getReader() ) );
}