Skip to content

Commit 4abfeee

Browse files
zodacpujaganidiemol
authored
[java] Use static Patterns for regex-matching (#15499)
* [java] Use static Patterns for regex-matching * [java] Running formatter * [java] Undoing removal of Java 8 code Will submit separate PR * [java] Fixing formatting --------- Co-authored-by: Puja Jagani <[email protected]> Co-authored-by: Diego Molina <[email protected]>
1 parent a23bc9f commit 4abfeee

File tree

6 files changed

+33
-21
lines changed

6 files changed

+33
-21
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/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: 6 additions & 8 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) {
@@ -209,23 +213,17 @@ public Map<Capabilities, Collection<SessionFactory>> getDockerSessionFactories(
209213
}
210214

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

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

226224
if (matcher.matches()) {
227225
deviceMapping.add(device(matcher.group(1), matcher.group(2), null));
228-
} else if ((matcher = linuxDeviceMappingWithPermissionsPattern.matcher(deviceMappingDefined))
226+
} else if ((matcher = LINUX_DEVICE_MAPPING_WITH_PERMISSIONS.matcher(deviceMappingDefined))
229227
.matches()) {
230228
deviceMapping.add(device(matcher.group(1), matcher.group(2), matcher.group(3)));
231229
}

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.net.URL;
2626
import java.net.URLEncoder;
2727
import java.nio.charset.StandardCharsets;
28-
import java.util.regex.Pattern;
2928
import org.openqa.selenium.internal.Require;
3029

3130
public class Urls {
@@ -88,7 +87,7 @@ public static URI from(String rawUri) {
8887
return createHttpUri(rawUri);
8988
}
9089

91-
if (Pattern.matches("\\d+", rawUri.substring(0, colonIndex))) {
90+
if (isAllDigits(rawUri.substring(0, colonIndex))) {
9291
return createHttpUri(rawUri);
9392
}
9493
}
@@ -101,6 +100,16 @@ public static URI from(String rawUri) {
101100
}
102101
}
103102

103+
private static boolean isAllDigits(final String input) {
104+
for (int i = 0; i < input.length(); i++) {
105+
if (!Character.isDigit(input.charAt(i))) {
106+
return false;
107+
}
108+
}
109+
110+
return !input.isEmpty();
111+
}
112+
104113
private static URI createHttpUri(String rawHost) {
105114
int slashIndex = rawHost.indexOf('/');
106115
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)