diff --git a/src/main/java/ua_parser/UserAgent.java b/src/main/java/ua_parser/UserAgent.java index 2523c22..c2f22a0 100644 --- a/src/main/java/ua_parser/UserAgent.java +++ b/src/main/java/ua_parser/UserAgent.java @@ -27,17 +27,26 @@ public class UserAgent { public static final UserAgent OTHER = new UserAgent("Other", null, null, null); - public final String family, major, minor, patch; + public final String family, major, minor, patch, patchMinor; + + public UserAgent(String family, String major, String minor, String patch, String patchMinor) { + this.family = family; + this.major = major; + this.minor = minor; + this.patch = patch; + this.patchMinor = patchMinor; + } public UserAgent(String family, String major, String minor, String patch) { this.family = family; this.major = major; this.minor = minor; this.patch = patch; + this.patchMinor = null; } public static UserAgent fromMap(Map m) { - return new UserAgent(m.get("family"), m.get("major"), m.get("minor"), m.get("patch")); + return new UserAgent(m.get("family"), m.get("major"), m.get("minor"), m.get("patch"), m.get("patch_minor")); } @Override @@ -63,11 +72,12 @@ public int hashCode() { @Override public String toString() { - return String.format("{\"family\": %s, \"major\": %s, \"minor\": %s, \"patch\": %s}", + return String.format("{\"family\": %s, \"major\": %s, \"minor\": %s, \"patch\": %s, \"patch_minor\": %s}", family == null ? Constants.EMPTY_STRING : '"' + family + '"', major == null ? Constants.EMPTY_STRING : '"' + major + '"', minor == null ? Constants.EMPTY_STRING : '"' + minor + '"', - patch == null ? Constants.EMPTY_STRING : '"' + patch + '"'); + patch == null ? Constants.EMPTY_STRING : '"' + patch + '"', + patchMinor == null ? Constants.EMPTY_STRING : '"' + patchMinor + '"'); } } \ No newline at end of file diff --git a/src/main/java/ua_parser/UserAgentParser.java b/src/main/java/ua_parser/UserAgentParser.java index 9a3655f..6120675 100644 --- a/src/main/java/ua_parser/UserAgentParser.java +++ b/src/main/java/ua_parser/UserAgentParser.java @@ -88,7 +88,7 @@ public UAPattern(Pattern pattern, String familyReplacement, String v1Replacement } public UserAgent match(String agentString) { - String family = null, v1 = null, v2 = null, v3 = null; + String family = null, v1 = null, v2 = null, v3 = null, v4 = null; Matcher matcher = pattern.matcher(agentString); if (!matcher.find()) { @@ -129,8 +129,14 @@ public UserAgent match(String agentString) { v3 = group4; } } + if (groupCount >= 5) { + String group5 = matcher.group(5); + if (!isBlank(group5)) { + v4 = group5; + } + } } - return family == null ? null : new UserAgent(family, v1, v2, v3); + return family == null ? null : new UserAgent(family, v1, v2, v3, v4); } private boolean isBlank(String value) { diff --git a/src/test/java/ua_parser/ParserTest.java b/src/test/java/ua_parser/ParserTest.java index 206487d..f084b0b 100644 --- a/src/test/java/ua_parser/ParserTest.java +++ b/src/test/java/ua_parser/ParserTest.java @@ -78,6 +78,7 @@ public void testParsePGTS() { public void testParseAll() { String agentString1 = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; fr; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5,gzip(gfe),gzip(gfe)"; String agentString2 = "Mozilla/5.0 (iPhone; CPU iPhone OS 5_1_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B206 Safari/7534.48.3"; + String agentString3 = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36"; Client expected1 = new Client(new UserAgent("Firefox", "3", "5", "5"), new OS("Mac OS X", "10", "4", null, null), @@ -85,9 +86,13 @@ public void testParseAll() { Client expected2 = new Client(new UserAgent("Mobile Safari", "5", "1", null), new OS("iOS", "5", "1", "1", null), new Device("iPhone")); + Client expected3 = new Client(new UserAgent("Chrome", "86", "0", "4240", "111"), + new OS("Mac OS X", "10", "15", "6", null), + new Device("Mac")); assertThat(parser.parse(agentString1), is(expected1)); assertThat(parser.parse(agentString2), is(expected2)); + assertThat(parser.parse(agentString3), is(expected3)); } @Test