diff --git a/java/src/org/openqa/selenium/By.java b/java/src/org/openqa/selenium/By.java index 5ed367d994a09..ff2cac31b4a62 100644 --- a/java/src/org/openqa/selenium/By.java +++ b/java/src/org/openqa/selenium/By.java @@ -296,6 +296,7 @@ public String toString() { public static class ByClassName extends PreW3CLocator { + private static final Pattern AT_LEAST_ONE_WHITESPACE = Pattern.compile(".*\\s.*"); private final String className; public ByClassName(String className) { @@ -305,7 +306,7 @@ public ByClassName(String className) { .nonNull("Cannot find elements when the class name expression is null."), ".%s"); - if (className.matches(".*\\s.*")) { + if (AT_LEAST_ONE_WHITESPACE.matcher(className).matches()) { throw new InvalidSelectorException("Compound class names not permitted"); } diff --git a/java/src/org/openqa/selenium/Platform.java b/java/src/org/openqa/selenium/Platform.java index e4bc941cdfd4e..927989c0bfd97 100644 --- a/java/src/org/openqa/selenium/Platform.java +++ b/java/src/org/openqa/selenium/Platform.java @@ -369,6 +369,7 @@ public String toString() { } }; + private static final Pattern VERSION_PATTERN = Pattern.compile("^(\\d+)\\.(\\d+).*"); private static @Nullable Platform current; private final String[] partOfOsName; private int minorVersion = 0; @@ -389,21 +390,20 @@ public static Platform getCurrent() { String version = System.getProperty("os.version", "0.0.0"); int major = 0; - int min = 0; + int minor = 0; - Pattern pattern = Pattern.compile("^(\\d+)\\.(\\d+).*"); - Matcher matcher = pattern.matcher(version); + final Matcher matcher = VERSION_PATTERN.matcher(version); if (matcher.matches()) { try { major = Integer.parseInt(matcher.group(1)); - min = Integer.parseInt(matcher.group(2)); + minor = Integer.parseInt(matcher.group(2)); } catch (NumberFormatException e) { // These things happen } } current.majorVersion = major; - current.minorVersion = min; + current.minorVersion = minor; } return current; } diff --git a/java/src/org/openqa/selenium/grid/commands/InfoCommand.java b/java/src/org/openqa/selenium/grid/commands/InfoCommand.java index ed83ae3668c6d..6ad582fe33d06 100644 --- a/java/src/org/openqa/selenium/grid/commands/InfoCommand.java +++ b/java/src/org/openqa/selenium/grid/commands/InfoCommand.java @@ -31,6 +31,7 @@ import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.Set; +import java.util.regex.Pattern; import org.openqa.selenium.cli.CliCommand; import org.openqa.selenium.cli.WrappedPrintWriter; import org.openqa.selenium.grid.config.Role; @@ -39,6 +40,8 @@ @AutoService(CliCommand.class) public class InfoCommand implements CliCommand { + private static final Pattern CODE_OR_LIST_PATTERN = Pattern.compile("^\\s*(\\*|\\d+\\.).*"); + public String getName() { return "info"; } @@ -109,7 +112,7 @@ public Executable configure(PrintStream out, PrintStream err, String... args) { break; } - String path = getClass().getPackage().getName().replaceAll("\\.", "/") + "/" + toDisplay; + String path = getClass().getPackage().getName().replace('.', '/') + "/" + toDisplay; String content; try { content = readContent(path); @@ -143,11 +146,11 @@ private String readContent(String path) throws IOException { } else if ("```".equals(line)) { inCode = !inCode; } else { - if (line.startsWith("=")) { + if (line.charAt(0) == '=') { formattedText.append("\n"); } formattedText.append(line); - if (inCode || line.matches("^\\s*\\*.*") || line.matches("^\\s*\\d+\\..*")) { + if (inCode || CODE_OR_LIST_PATTERN.matcher(line).matches()) { formattedText.append("\n"); } else { formattedText.append(" "); diff --git a/java/src/org/openqa/selenium/grid/node/docker/DockerOptions.java b/java/src/org/openqa/selenium/grid/node/docker/DockerOptions.java index 7082c518342d9..2ec4040428ec2 100644 --- a/java/src/org/openqa/selenium/grid/node/docker/DockerOptions.java +++ b/java/src/org/openqa/selenium/grid/node/docker/DockerOptions.java @@ -68,6 +68,10 @@ public class DockerOptions { private static final String DEFAULT_DOCKER_NETWORK = "bridge"; private static final Logger LOG = Logger.getLogger(DockerOptions.class.getName()); private static final Json JSON = new Json(); + private static final Pattern LINUX_DEVICE_MAPPING_WITH_DEFAULT_PERMISSIONS = + Pattern.compile("^([\\w/-]+):([\\w/-]+)$"); + private static final Pattern LINUX_DEVICE_MAPPING_WITH_PERMISSIONS = + Pattern.compile("^([\\w/-]+):([\\w/-]+):(\\w+)$"); private final Config config; public DockerOptions(Config config) { @@ -209,23 +213,17 @@ public Map> getDockerSessionFactories( } protected List getDevicesMapping() { - Pattern linuxDeviceMappingWithDefaultPermissionsPattern = - Pattern.compile("^([\\w\\/-]+):([\\w\\/-]+)$"); - Pattern linuxDeviceMappingWithPermissionsPattern = - Pattern.compile("^([\\w\\/-]+):([\\w\\/-]+):([\\w]+)$"); - List devices = config.getAll(DOCKER_SECTION, "devices").orElseGet(Collections::emptyList); List deviceMapping = new ArrayList<>(); for (String device : devices) { String deviceMappingDefined = device.trim(); - Matcher matcher = - linuxDeviceMappingWithDefaultPermissionsPattern.matcher(deviceMappingDefined); + Matcher matcher = LINUX_DEVICE_MAPPING_WITH_DEFAULT_PERMISSIONS.matcher(deviceMappingDefined); if (matcher.matches()) { deviceMapping.add(device(matcher.group(1), matcher.group(2), null)); - } else if ((matcher = linuxDeviceMappingWithPermissionsPattern.matcher(deviceMappingDefined)) + } else if ((matcher = LINUX_DEVICE_MAPPING_WITH_PERMISSIONS.matcher(deviceMappingDefined)) .matches()) { deviceMapping.add(device(matcher.group(1), matcher.group(2), matcher.group(3))); } diff --git a/java/src/org/openqa/selenium/net/Urls.java b/java/src/org/openqa/selenium/net/Urls.java index 3981aa4dbdb98..4ee4fe959843c 100644 --- a/java/src/org/openqa/selenium/net/Urls.java +++ b/java/src/org/openqa/selenium/net/Urls.java @@ -25,7 +25,6 @@ import java.net.URL; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; -import java.util.regex.Pattern; import org.openqa.selenium.internal.Require; public class Urls { @@ -88,7 +87,7 @@ public static URI from(String rawUri) { return createHttpUri(rawUri); } - if (Pattern.matches("\\d+", rawUri.substring(0, colonIndex))) { + if (isAllDigits(rawUri.substring(0, colonIndex))) { return createHttpUri(rawUri); } } @@ -101,6 +100,16 @@ public static URI from(String rawUri) { } } + private static boolean isAllDigits(final String input) { + for (int i = 0; i < input.length(); i++) { + if (!Character.isDigit(input.charAt(i))) { + return false; + } + } + + return !input.isEmpty(); + } + private static URI createHttpUri(String rawHost) { int slashIndex = rawHost.indexOf('/'); String host = slashIndex == -1 ? rawHost : rawHost.substring(0, slashIndex); diff --git a/java/src/org/openqa/selenium/remote/codec/w3c/W3CHttpCommandCodec.java b/java/src/org/openqa/selenium/remote/codec/w3c/W3CHttpCommandCodec.java index 7df6fdca72a70..970cd2035c6e4 100644 --- a/java/src/org/openqa/selenium/remote/codec/w3c/W3CHttpCommandCodec.java +++ b/java/src/org/openqa/selenium/remote/codec/w3c/W3CHttpCommandCodec.java @@ -100,7 +100,8 @@ public class W3CHttpCommandCodec extends AbstractHttpCommandCodec { private static final ConcurrentHashMap ATOM_SCRIPTS = new ConcurrentHashMap<>(); private static final Pattern CSS_ESCAPE = - Pattern.compile("([\\s'\"\\\\#.:;,!?+<>=~*^$|%&@`{}\\-\\/\\[\\]\\(\\)])"); + Pattern.compile("([\\s'\"\\\\#.:;,!?+<>=~*^$|%&@`{}\\-/\\[\\]()])"); + private static final Pattern AT_LEAST_ONE_WHITESPACE = Pattern.compile(".*\\s.*"); public W3CHttpCommandCodec() { String sessionId = "/session/:sessionId"; @@ -181,7 +182,7 @@ public W3CHttpCommandCodec() { String stringValue = (String) value; switch (using) { case "class name": - if (stringValue.matches(".*\\s.*")) { + if (AT_LEAST_ONE_WHITESPACE.matcher(stringValue).matches()) { throw new InvalidSelectorException("Compound class names not permitted"); } return amendLocatorToCssSelector(parameters, "." + cssEscape(stringValue));