Skip to content

Commit f65c6a7

Browse files
author
Florian Hirsch
committed
ua-parser#6 updated DeviceParser to work with new core regexes
1 parent 5b71dfd commit f65c6a7

File tree

1 file changed

+35
-17
lines changed

1 file changed

+35
-17
lines changed

src/main/java/ua_parser/DeviceParser.java

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -62,39 +62,57 @@ protected static DevicePattern patternFromMap(Map<String, String> configMap) {
6262
String regex = configMap.get("regex");
6363
if (regex == null) {
6464
throw new IllegalArgumentException("Device is missing regex");
65-
}
66-
return new DevicePattern(Pattern.compile(regex),
67-
configMap.get("device_replacement"));
65+
}
66+
Pattern pattern = "i".equals(configMap.get("regex_flag")) // no ohter flags used (by now)
67+
? Pattern.compile(regex, Pattern.CASE_INSENSITIVE) : Pattern.compile(regex);
68+
return new DevicePattern(pattern, configMap.get("device_replacement"));
6869
}
6970

7071
protected static class DevicePattern {
72+
private static final Pattern SUBSTITUTIONS_PATTERN = Pattern.compile("\\$\\d");
7173
private final Pattern pattern;
72-
private final String familyReplacement;
74+
private final String deviceReplacement;
7375

74-
public DevicePattern(Pattern pattern, String familyReplacement) {
76+
public DevicePattern(Pattern pattern, String deviceReplacement) {
7577
this.pattern = pattern;
76-
this.familyReplacement = familyReplacement;
78+
this.deviceReplacement = deviceReplacement;
7779
}
7880

7981
public String match(String agentString) {
8082
Matcher matcher = pattern.matcher(agentString);
81-
8283
if (!matcher.find()) {
8384
return null;
8485
}
85-
86-
String family = null;
87-
if (familyReplacement != null) {
88-
if (familyReplacement.contains("$1") && matcher.groupCount() >= 1 && matcher.group(1) != null) {
89-
family = familyReplacement.replaceFirst("\\$1", Matcher.quoteReplacement(matcher.group(1)));
90-
} else {
91-
family = familyReplacement;
92-
}
86+
String device = null;
87+
if (deviceReplacement != null) {
88+
if (deviceReplacement.contains("$")) {
89+
device = deviceReplacement;
90+
for (String substitution : getSubstitutions(deviceReplacement)) {
91+
int i = Integer.valueOf(substitution.substring(1));
92+
String replacement = matcher.groupCount() >= i && matcher.group(i) != null
93+
? Matcher.quoteReplacement(matcher.group(i)) : "";
94+
device = device.replaceFirst("\\" + substitution, replacement);
95+
}
96+
device = device.trim();
97+
} else {
98+
device = deviceReplacement;
99+
}
93100
} else if (matcher.groupCount() >= 1) {
94-
family = matcher.group(1);
101+
device = matcher.group(1);
102+
}
103+
104+
return device;
105+
}
106+
107+
private List<String> getSubstitutions(String deviceReplacement) {
108+
Matcher matcher = SUBSTITUTIONS_PATTERN.matcher(deviceReplacement);
109+
List<String> substitutions = new ArrayList<String>();
110+
while (matcher.find()) {
111+
substitutions.add(matcher.group());
95112
}
96-
return family;
113+
return substitutions;
97114
}
115+
98116
}
99117

100118
}

0 commit comments

Comments
 (0)