Skip to content

Commit 561a93b

Browse files
[java] Use static Patterns for regex-matching
1 parent 05ab017 commit 561a93b

File tree

7 files changed

+37
-38
lines changed

7 files changed

+37
-38
lines changed

java/src/org/openqa/selenium/By.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ public String toString() {
296296

297297
public static class ByClassName extends PreW3CLocator {
298298

299+
private static final Pattern AT_LEAST_ONE_WHITESPACE = Pattern.compile(".*\\s.*");
299300
private final String className;
300301

301302
public ByClassName(String className) {
@@ -305,7 +306,7 @@ public ByClassName(String className) {
305306
.nonNull("Cannot find elements when the class name expression is null."),
306307
".%s");
307308

308-
if (className.matches(".*\\s.*")) {
309+
if (AT_LEAST_ONE_WHITESPACE.matcher(className).matches()) {
309310
throw new InvalidSelectorException("Compound class names not permitted");
310311
}
311312

java/src/org/openqa/selenium/PersistentCapabilities.java

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@
1717

1818
package org.openqa.selenium;
1919

20-
import java.util.Collections;
2120
import java.util.Map;
2221
import java.util.Set;
2322
import java.util.function.Function;
24-
import java.util.stream.Collector;
2523
import java.util.stream.Collectors;
2624
import java.util.stream.Stream;
2725
import org.openqa.selenium.internal.Require;
@@ -58,7 +56,7 @@ public PersistentCapabilities setCapability(String name, Object value) {
5856
@Override
5957
public Map<String, Object> asMap() {
6058
return getCapabilityNames().stream()
61-
.collect(toUnmodifiableMap(Function.identity(), this::getCapability));
59+
.collect(Collectors.toUnmodifiableMap(Function.identity(), this::getCapability));
6260
}
6361

6462
@Override
@@ -81,19 +79,7 @@ public Capabilities merge(Capabilities other) {
8179
public Set<String> getCapabilityNames() {
8280
return Stream.concat(
8381
caps.getCapabilityNames().stream(), overrides.getCapabilityNames().stream())
84-
.collect(toUnmodifiableSet());
85-
}
86-
87-
// Needed, since we're dependent on Java 8 as a minimum version
88-
private <T, K, U> Collector<T, ?, Map<K, U>> toUnmodifiableMap(
89-
Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper) {
90-
return Collectors.collectingAndThen(
91-
Collectors.toMap(keyMapper, valueMapper), Collections::unmodifiableMap);
92-
}
93-
94-
// Needed, since we're dependent on Java 8 as a minimum version
95-
private <T> Collector<T, ?, Set<T>> toUnmodifiableSet() {
96-
return Collectors.collectingAndThen(Collectors.toSet(), Collections::unmodifiableSet);
82+
.collect(Collectors.toUnmodifiableSet());
9783
}
9884

9985
@Override

java/src/org/openqa/selenium/Platform.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ public String toString() {
369369
}
370370
};
371371

372+
private static final Pattern VERSION_PATTERN = Pattern.compile("^(\\d+)\\.(\\d+).*");
372373
private static @Nullable Platform current;
373374
private final String[] partOfOsName;
374375
private int minorVersion = 0;
@@ -389,21 +390,20 @@ public static Platform getCurrent() {
389390

390391
String version = System.getProperty("os.version", "0.0.0");
391392
int major = 0;
392-
int min = 0;
393+
int minor = 0;
393394

394-
Pattern pattern = Pattern.compile("^(\\d+)\\.(\\d+).*");
395-
Matcher matcher = pattern.matcher(version);
395+
final Matcher matcher = VERSION_PATTERN.matcher(version);
396396
if (matcher.matches()) {
397397
try {
398398
major = Integer.parseInt(matcher.group(1));
399-
min = Integer.parseInt(matcher.group(2));
399+
minor = Integer.parseInt(matcher.group(2));
400400
} catch (NumberFormatException e) {
401401
// These things happen
402402
}
403403
}
404404

405405
current.majorVersion = major;
406-
current.minorVersion = min;
406+
current.minorVersion = minor;
407407
}
408408
return current;
409409
}

java/src/org/openqa/selenium/grid/commands/InfoCommand.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.nio.charset.StandardCharsets;
3232
import java.util.Collections;
3333
import java.util.Set;
34+
import java.util.regex.Pattern;
3435
import org.openqa.selenium.cli.CliCommand;
3536
import org.openqa.selenium.cli.WrappedPrintWriter;
3637
import org.openqa.selenium.grid.config.Role;
@@ -39,6 +40,8 @@
3940
@AutoService(CliCommand.class)
4041
public class InfoCommand implements CliCommand {
4142

43+
private static final Pattern CODE_OR_LIST_PATTERN = Pattern.compile("^\\s*(\\*|\\d+\\.).*");
44+
4245
public String getName() {
4346
return "info";
4447
}
@@ -109,7 +112,7 @@ public Executable configure(PrintStream out, PrintStream err, String... args) {
109112
break;
110113
}
111114

112-
String path = getClass().getPackage().getName().replaceAll("\\.", "/") + "/" + toDisplay;
115+
String path = getClass().getPackage().getName().replace('.', '/') + "/" + toDisplay;
113116
String content;
114117
try {
115118
content = readContent(path);
@@ -143,11 +146,11 @@ private String readContent(String path) throws IOException {
143146
} else if ("```".equals(line)) {
144147
inCode = !inCode;
145148
} else {
146-
if (line.startsWith("=")) {
149+
if (line.charAt(0) == '=') {
147150
formattedText.append("\n");
148151
}
149152
formattedText.append(line);
150-
if (inCode || line.matches("^\\s*\\*.*") || line.matches("^\\s*\\d+\\..*")) {
153+
if (inCode || CODE_OR_LIST_PATTERN.matcher(line).matches()) {
151154
formattedText.append("\n");
152155
} else {
153156
formattedText.append(" ");

java/src/org/openqa/selenium/grid/node/docker/DockerOptions.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ public class DockerOptions {
6868
private static final String DEFAULT_DOCKER_NETWORK = "bridge";
6969
private static final Logger LOG = Logger.getLogger(DockerOptions.class.getName());
7070
private static final Json JSON = new Json();
71+
private static final Pattern LINUX_DEVICE_MAPPING_WITH_DEFAULT_PERMISSIONS =
72+
Pattern.compile("^([\\w/-]+):([\\w/-]+)$");
73+
private static final Pattern LINUX_DEVICE_MAPPING_WITH_PERMISSIONS =
74+
Pattern.compile("^([\\w/-]+):([\\w/-]+):(\\w+)$");
7175
private final Config config;
7276

7377
public DockerOptions(Config config) {
@@ -208,23 +212,17 @@ public Map<Capabilities, Collection<SessionFactory>> getDockerSessionFactories(
208212
}
209213

210214
protected List<Device> getDevicesMapping() {
211-
Pattern linuxDeviceMappingWithDefaultPermissionsPattern =
212-
Pattern.compile("^([\\w\\/-]+):([\\w\\/-]+)$");
213-
Pattern linuxDeviceMappingWithPermissionsPattern =
214-
Pattern.compile("^([\\w\\/-]+):([\\w\\/-]+):([\\w]+)$");
215-
216-
List<String> devices =
217-
config.getAll(DOCKER_SECTION, "devices").orElseGet(Collections::emptyList);
215+
List<String> devices = config.getAll(DOCKER_SECTION, "devices")
216+
.orElseGet(Collections::emptyList);
218217

219218
List<Device> deviceMapping = new ArrayList<>();
220219
for (String device : devices) {
221220
String deviceMappingDefined = device.trim();
222-
Matcher matcher =
223-
linuxDeviceMappingWithDefaultPermissionsPattern.matcher(deviceMappingDefined);
221+
Matcher matcher = LINUX_DEVICE_MAPPING_WITH_DEFAULT_PERMISSIONS.matcher(deviceMappingDefined);
224222

225223
if (matcher.matches()) {
226224
deviceMapping.add(device(matcher.group(1), matcher.group(2), null));
227-
} else if ((matcher = linuxDeviceMappingWithPermissionsPattern.matcher(deviceMappingDefined))
225+
} else if ((matcher = LINUX_DEVICE_MAPPING_WITH_PERMISSIONS.matcher(deviceMappingDefined))
228226
.matches()) {
229227
deviceMapping.add(device(matcher.group(1), matcher.group(2), matcher.group(3)));
230228
}

java/src/org/openqa/selenium/net/Urls.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public static URI from(String rawUri) {
8888
return createHttpUri(rawUri);
8989
}
9090

91-
if (Pattern.matches("\\d+", rawUri.substring(0, colonIndex))) {
91+
if (isAllDigits(rawUri.substring(0, colonIndex))) {
9292
return createHttpUri(rawUri);
9393
}
9494
}
@@ -101,6 +101,16 @@ public static URI from(String rawUri) {
101101
}
102102
}
103103

104+
private static boolean isAllDigits(final String input) {
105+
for (int i = 0; i < input.length(); i++) {
106+
if (!Character.isDigit(input.charAt(i))) {
107+
return false;
108+
}
109+
}
110+
111+
return !input.isEmpty();
112+
}
113+
104114
private static URI createHttpUri(String rawHost) {
105115
int slashIndex = rawHost.indexOf('/');
106116
String host = slashIndex == -1 ? rawHost : rawHost.substring(0, slashIndex);

java/src/org/openqa/selenium/remote/codec/w3c/W3CHttpCommandCodec.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ public class W3CHttpCommandCodec extends AbstractHttpCommandCodec {
100100

101101
private static final ConcurrentHashMap<String, String> ATOM_SCRIPTS = new ConcurrentHashMap<>();
102102
private static final Pattern CSS_ESCAPE =
103-
Pattern.compile("([\\s'\"\\\\#.:;,!?+<>=~*^$|%&@`{}\\-\\/\\[\\]\\(\\)])");
103+
Pattern.compile("([\\s'\"\\\\#.:;,!?+<>=~*^$|%&@`{}\\-/\\[\\]()])");
104+
private static final Pattern AT_LEAST_ONE_WHITESPACE = Pattern.compile(".*\\s.*");
104105

105106
public W3CHttpCommandCodec() {
106107
String sessionId = "/session/:sessionId";
@@ -181,7 +182,7 @@ public W3CHttpCommandCodec() {
181182
String stringValue = (String) value;
182183
switch (using) {
183184
case "class name":
184-
if (stringValue.matches(".*\\s.*")) {
185+
if (AT_LEAST_ONE_WHITESPACE.matcher(stringValue).matches()) {
185186
throw new InvalidSelectorException("Compound class names not permitted");
186187
}
187188
return amendLocatorToCssSelector(parameters, "." + cssEscape(stringValue));

0 commit comments

Comments
 (0)