Skip to content

Commit

Permalink
root locale: Use Locale.ROOT where necessary when converting strings
Browse files Browse the repository at this point in the history
When we convert program information strings, like URI schemes, XML tags,
enum value names, to upper or lower case, we need to specify Locale.ROOT
as the locale to avoid unexpected conversion issues in some locales.

Signed-off-by: BJ Hargrave <bj@hargrave.dev>
  • Loading branch information
bjhargrave committed Feb 7, 2022
1 parent 5a3beb6 commit 70df4e1
Show file tree
Hide file tree
Showing 54 changed files with 156 additions and 105 deletions.
3 changes: 2 additions & 1 deletion aQute.libg/src/aQute/lib/converter/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Stack;
Expand Down Expand Up @@ -280,7 +281,7 @@ Object convertT(Type type, Object o) throws Exception {
try {
return Enum.valueOf((Class<Enum>) resultType, input);
} catch (Exception e) {
input = input.toUpperCase();
input = input.toUpperCase(Locale.ROOT);
return Enum.valueOf((Class<Enum>) resultType, input);
}
}
Expand Down
3 changes: 2 additions & 1 deletion aQute.libg/src/aQute/lib/getopt/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.Formatter;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
Expand Down Expand Up @@ -627,7 +628,7 @@ private String getTypeDescriptor(Type type) {
return ""; // Is a flag

return "<" + lastPart(clazz.getName()
.toLowerCase()) + ">";
.toLowerCase(Locale.ROOT)) + ">";
}

public Object getResult() {
Expand Down
3 changes: 2 additions & 1 deletion aQute.libg/src/aQute/lib/xpath/XPathParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.util.List;
import java.util.Locale;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
Expand Down Expand Up @@ -73,7 +74,7 @@ public <X> void parse(Node node, X dto) throws Exception {

if (f.getType()
.isAnnotation())
value = value.toUpperCase();
value = value.toUpperCase(Locale.ROOT);

Object o = Converter.cnv(f.getGenericType(), value);
try {
Expand Down
3 changes: 2 additions & 1 deletion aQute.libg/src/aQute/libg/classdump/ClassDumper.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.io.PrintStream;
import java.lang.reflect.Modifier;
import java.nio.file.Paths;
import java.util.Locale;

import aQute.lib.io.IO;

Expand Down Expand Up @@ -381,7 +382,7 @@ protected void printHex(byte[] code) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 16 && index < code.length; i++) {
String s = Integer.toHexString((0xFF & code[index++]))
.toUpperCase();
.toUpperCase(Locale.ROOT);
if (s.length() == 1)
sb.append("0");
sb.append(s);
Expand Down
5 changes: 3 additions & 2 deletions aQute.libg/src/aQute/libg/shacache/ShaCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;
import java.util.regex.Pattern;

import aQute.lib.io.IO;
Expand Down Expand Up @@ -81,7 +82,7 @@ public InputStream getStream(String sha, ShaSource... sources) throws Exception
// and copy it.
//

File tmp = IO.createTempFile(root, sha.toLowerCase(), ".shacache");
File tmp = IO.createTempFile(root, sha.toLowerCase(Locale.ROOT), ".shacache");
IO.copy(in, tmp);
String digest = SHA1.digest(tmp)
.asHex();
Expand Down Expand Up @@ -141,7 +142,7 @@ public File getFile(String sha, ShaSource... sources) throws Exception {
try {
InputStream in = s.get(sha);
if (in != null) {
File tmp = IO.createTempFile(root, sha.toLowerCase(), ".shacache");
File tmp = IO.createTempFile(root, sha.toLowerCase(Locale.ROOT), ".shacache");
IO.copy(in, tmp);
String digest = SHA1.digest(tmp)
.asHex();
Expand Down
3 changes: 2 additions & 1 deletion aQute.libg/test/aQute/lib/hex/HexTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Locale;

import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -50,7 +51,7 @@ public void testHexOddCount() {
public void testHex() {
byte[] bytes = Hex.toByteArray("b10a8db164e0754105b7a99be72e3fe5");
String s = Hex.toHexString(bytes);
assertEquals("b10a8db164e0754105b7a99be72e3fe5", s.toLowerCase());
assertEquals("b10a8db164e0754105b7a99be72e3fe5", s.toLowerCase(Locale.ROOT));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package aQute.bnd.exporter.subsystem;

import java.io.File;
import java.util.stream.Stream;
import java.util.Arrays;
import java.util.Locale;

import org.osgi.framework.Constants;
import org.osgi.service.subsystem.SubsystemConstants;;
Expand Down Expand Up @@ -111,9 +112,10 @@ public static ContainerType byFile(File file) {
return null;
}
String filename = file.getName()
.toLowerCase();
return Stream.of(ContainerType.values())
.filter(c -> filename.endsWith("." + c.getExtension()))
.toLowerCase(Locale.ROOT);
return Arrays.stream(ContainerType.values())
.filter(c -> filename.endsWith(".".concat(c.getExtension()
.toLowerCase(Locale.ROOT))))
.findFirst()
.orElse(null);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package aQute.bnd.exporter.subsystem;

import java.util.Locale;

public enum EsaArchiveType {
NONE,
CONTENT,
Expand All @@ -10,7 +12,7 @@ public static EsaArchiveType byParameter(String archiveContent) {
return CONTENT; // default
}
try {
return valueOf(archiveContent.toUpperCase());
return valueOf(archiveContent.toUpperCase(Locale.ROOT));
} catch (IllegalArgumentException e) {
return null;
}
Expand Down
3 changes: 2 additions & 1 deletion biz.aQute.bnd.runtime/src/aQute/bnd/runtime/gogo/Log.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;
import java.util.WeakHashMap;

import org.apache.felix.service.command.CommandSession;
Expand Down Expand Up @@ -118,7 +119,7 @@ public List<LogEntry> linfo(@Parameter(names = {
}

private List<LogEntry> log0(String level, int number) {
Level l = Level.valueOf(level.toUpperCase());
Level l = Level.valueOf(level.toUpperCase(Locale.ROOT));
List<LogEntry> result = new ArrayList<>();
for (int i = entries.size() - 1; i >= 0 && result.size() <= number; i--) {
LogEntry entry = entries.get(i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.time.format.DateTimeFormatter;
import java.util.Hashtable;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

Expand Down Expand Up @@ -156,7 +157,7 @@ private File flush(Map<String, Object> top, String name) throws IOException, Exc
if (className != null) {
int x = className.lastIndexOf('.');
className = className.substring(x + 1);
name = className.toLowerCase() + "-" + name;
name = className.toLowerCase(Locale.ROOT) + "-" + name;
}
name += ".json";
} else
Expand Down
6 changes: 4 additions & 2 deletions biz.aQute.bnd/src/aQute/bnd/main/BaselineCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
Expand Down Expand Up @@ -47,6 +48,7 @@
import aQute.bnd.service.diff.Delta;
import aQute.bnd.service.diff.Diff;
import aQute.bnd.service.diff.Tree;
import aQute.bnd.unmodifiable.Lists;
import aQute.bnd.version.Version;
import aQute.lib.collections.MultiMap;
import aQute.lib.collections.SortedList;
Expand All @@ -55,7 +57,6 @@
import aQute.lib.getopt.Options;
import aQute.lib.io.IO;
import aQute.lib.tag.Tag;
import aQute.bnd.unmodifiable.Lists;
import aQute.lib.xml.XML;

/**
Expand Down Expand Up @@ -245,7 +246,8 @@ protected void doPackageDiff(Diff diff) {
protected void doDiff(Diff diff, StringBuilder sb) {
String type = String.valueOf(diff.getType());

String output = String.format("%s%-5s %-10s %s", sb, getShortDelta(diff.getDelta()), type.toLowerCase(),
String output = String.format("%s%-5s %-10s %s", sb, getShortDelta(diff.getDelta()),
type.toLowerCase(Locale.ROOT),
diff.getName());

bnd.out.println(output);
Expand Down
5 changes: 3 additions & 2 deletions biz.aQute.bnd/src/aQute/bnd/main/bnd.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
Expand Down Expand Up @@ -690,7 +691,7 @@ public void _extract(extractOptions opts) throws Exception {
for (String path : selected) {
if (opts.verbose())
err.printf("%8s: %s\n", compression.toString()
.toLowerCase(), path);
.toLowerCase(Locale.ROOT), path);

File f = getFile(store, path);
File pf = f.getParentFile();
Expand Down Expand Up @@ -3746,7 +3747,7 @@ public void _copy(CopyOptions options) throws Exception {
.entrySet()) {
String header = e.getKey()
.toString()
.toLowerCase();
.toLowerCase(Locale.ROOT);
if (header.startsWith("bundle-"))
continue;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.net.Proxy.Type;
import java.net.URL;
import java.util.List;
import java.util.Locale;

import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -118,7 +119,7 @@ public void testProxies() throws Exception {
ProxyDTO p = settings.proxies.get(0);
assertEquals("http-proxy", p.id);
assertEquals(true, p.active);
assertEquals(Type.HTTP.name(), p.protocol.toUpperCase());
assertEquals(Type.HTTP.name(), p.protocol.toUpperCase(Locale.ROOT));
assertEquals("localhost", p.host);
assertEquals(80, p.port);
assertEquals(null, p.nonProxyHosts);
Expand All @@ -128,7 +129,7 @@ public void testProxies() throws Exception {
p = settings.proxies.get(1);
assertEquals("https-proxy", p.id);
assertEquals(true, p.active);
assertEquals("HTTPS", p.protocol.toUpperCase());
assertEquals("HTTPS", p.protocol.toUpperCase(Locale.ROOT));
assertEquals("localhost", p.host);
assertEquals(443, p.port);
assertEquals(null, p.nonProxyHosts);
Expand All @@ -143,7 +144,7 @@ public void testSocksAuth() throws Exception {
ProxyDTO p = settings.proxies.get(0);
assertEquals("myproxy", p.id);
assertEquals(true, p.active);
assertEquals(Type.SOCKS.name(), p.protocol.toUpperCase());
assertEquals(Type.SOCKS.name(), p.protocol.toUpperCase(Locale.ROOT));
assertEquals(1080, p.port);
assertEquals(null, p.nonProxyHosts);
assertEquals("proxyuser", p.username);
Expand All @@ -157,7 +158,7 @@ public void testSocksNoAuth() throws Exception {
ProxyDTO p = settings.proxies.get(0);
assertEquals("myproxy", p.id);
assertEquals(true, p.active);
assertEquals(Type.SOCKS.name(), p.protocol.toUpperCase());
assertEquals(Type.SOCKS.name(), p.protocol.toUpperCase(Locale.ROOT));
assertEquals(1080, p.port);
assertEquals(null, p.nonProxyHosts);
assertEquals(null, p.username);
Expand All @@ -171,7 +172,7 @@ public void testNonProxyHost() throws Exception {
ProxyDTO p = settings.proxies.get(0);
assertEquals("myproxy", p.id);
assertEquals(true, p.active);
assertEquals(Type.SOCKS.name(), p.protocol.toUpperCase());
assertEquals(Type.SOCKS.name(), p.protocol.toUpperCase(Locale.ROOT));
assertEquals(1080, p.port);
assertEquals("*.google.com|ibiblio.org", p.nonProxyHosts);
assertEquals(null, p.username);
Expand Down
10 changes: 5 additions & 5 deletions biz.aQute.bndlib.tests/test/test/lib/NanoHTTPD.java
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public static void main(String[] args) {
port = Integer.parseInt(args[i + 1]);
else if (args[i].equalsIgnoreCase("-d"))
wwwroot = new File(args[i + 1]).getAbsoluteFile();
else if (args[i].toLowerCase()
else if (args[i].toLowerCase(Locale.ROOT)
.endsWith("licence")) {
myOut.println(LICENCE + "\n");
break;
Expand Down Expand Up @@ -460,7 +460,7 @@ private void decodeHeader(BufferedReader in, Properties pre, Properties parms, P
if (p >= 0)
header.put(line.substring(0, p)
.trim()
.toLowerCase(),
.toLowerCase(Locale.ROOT),
line.substring(p + 1)
.trim());
line = in.readLine();
Expand Down Expand Up @@ -496,7 +496,7 @@ private void decodeMultipartData(String boundary, byte[] fbuf, BufferedReader in
if (p != -1)
item.put(mpline.substring(0, p)
.trim()
.toLowerCase(),
.toLowerCase(Locale.ROOT),
mpline.substring(p + 1)
.trim());
mpline = in.readLine();
Expand All @@ -515,7 +515,7 @@ private void decodeMultipartData(String boundary, byte[] fbuf, BufferedReader in
if (p != -1)
disposition.put(token.substring(0, p)
.trim()
.toLowerCase(),
.toLowerCase(Locale.ROOT),
token.substring(p + 1)
.trim());
}
Expand Down Expand Up @@ -869,7 +869,7 @@ else if (len < 1024 * 1024)
if (dot >= 0)
mime = theMimeTypes.get(f.getCanonicalPath()
.substring(dot + 1)
.toLowerCase());
.toLowerCase(Locale.ROOT));
if (mime == null)
mime = MIME_DEFAULT_BINARY;

Expand Down
3 changes: 2 additions & 1 deletion biz.aQute.bndlib/src/aQute/bnd/build/Project.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
Expand Down Expand Up @@ -980,7 +981,7 @@ public boolean getRunBuilds() {
String runBuildsStr = getProperty(Constants.RUNBUILDS);
if (runBuildsStr == null)
result = !getPropertiesFile().getName()
.toLowerCase()
.toLowerCase(Locale.ROOT)
.endsWith(Constants.DEFAULT_BNDRUN_EXTENSION);
else
result = Boolean.parseBoolean(runBuildsStr);
Expand Down
2 changes: 1 addition & 1 deletion biz.aQute.bndlib/src/aQute/bnd/build/Workspace.java
Original file line number Diff line number Diff line change
Expand Up @@ -1232,7 +1232,7 @@ public boolean addPlugin(Class<?> plugin, String alias, Map<String, String> para
alias = ann.name();
else {
alias = Strings.getLastSegment(plugin.getName())
.toLowerCase();
.toLowerCase(Locale.ROOT);
if (alias.endsWith("plugin")) {
alias = alias.substring(0, alias.length() - "plugin".length());
}
Expand Down
5 changes: 3 additions & 2 deletions biz.aQute.bndlib/src/aQute/bnd/build/model/BndEditModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
Expand Down Expand Up @@ -1007,9 +1008,9 @@ public void setEE(EE ee) {
}

public void setRunFramework(String clause) {
assert (Constants.RUNFRAMEWORK_SERVICES.equals(clause.toLowerCase()
assert (Constants.RUNFRAMEWORK_SERVICES.equals(clause.toLowerCase(Locale.ROOT)
.trim()) || Constants.RUNFRAMEWORK_NONE.equals(
clause.toLowerCase()
clause.toLowerCase(Locale.ROOT)
.trim()));
String oldValue = getRunFramework();
doSetObject(Constants.RUNFRAMEWORK, oldValue, clause, newlineEscapeFormatter);
Expand Down
Loading

0 comments on commit 70df4e1

Please sign in to comment.