1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package net.fckeditor.tool;
22
23 import java.util.regex.Matcher;
24 import java.util.regex.Pattern;
25
26 import javax.servlet.http.HttpServletRequest;
27
28
29
30
31
32
33 public class Compatibility {
34
35
36
37
38
39
40
41
42 public static boolean check(final String userAgentString) {
43 if (Utils.isEmpty(userAgentString))
44 return false;
45
46 float version;
47
48
49
50 if (userAgentString.indexOf("Opera") < 0 && userAgentString.indexOf("mac") < 0) {
51 version = getBrowserVersion(userAgentString, ".*MSIE ([\\d]+.[\\d]+).*");
52 if (version != -1f && version >= 5.5f)
53 return true;
54 }
55
56
57 version = getBrowserVersion(userAgentString, ".*Gecko/([\\d]+).*");
58 if (version != -1f && version >= 20030210f)
59 return true;
60
61
62 version = getBrowserVersion(userAgentString, "Opera/([\\d]+.[\\d]+).*");
63 if (version != -1f && version >= 9.5f)
64 return true;
65 version = getBrowserVersion(userAgentString, ".*Opera ([\\d]+.[\\d]+)");
66 if (version != -1f && version >= 9.5f)
67 return true;
68
69
70 version = getBrowserVersion(userAgentString, ".*AppleWebKit/([\\d]+).*");
71 if (version != -1f && version >= 522f)
72 return true;
73
74 return false;
75 }
76
77
78
79
80
81
82 public static boolean isCompatibleBrowser(final HttpServletRequest request) {
83 return (request == null) ? false : check(request.getHeader("user-agent"));
84 }
85
86
87
88
89
90
91
92
93
94 private static float getBrowserVersion(final String userAgent, final String regex) {
95 Pattern pattern = Pattern.compile(regex);
96 Matcher matcher = pattern.matcher(userAgent);
97 if (matcher.matches()) {
98 try {
99 return Float.parseFloat(matcher.group(1));
100 } catch (NumberFormatException e) {
101 return -1f;
102 }
103 }
104 return -1f;
105 }
106
107 }