Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Compatibility |
|
| 7.0;7 |
1 | /* | |
2 | * FCKeditor - The text editor for Internet - http://www.fckeditor.net | |
3 | * Copyright (C) 2004-2009 Frederico Caldeira Knabben | |
4 | * | |
5 | * == BEGIN LICENSE == | |
6 | * | |
7 | * Licensed under the terms of any of the following licenses at your | |
8 | * choice: | |
9 | * | |
10 | * - GNU General Public License Version 2 or later (the "GPL") | |
11 | * http://www.gnu.org/licenses/gpl.html | |
12 | * | |
13 | * - GNU Lesser General Public License Version 2.1 or later (the "LGPL") | |
14 | * http://www.gnu.org/licenses/lgpl.html | |
15 | * | |
16 | * - Mozilla Public License Version 1.1 or later (the "MPL") | |
17 | * http://www.mozilla.org/MPL/MPL-1.1.html | |
18 | * | |
19 | * == END LICENSE == | |
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 | import net.fckeditor.FCKeditor; | |
29 | ||
30 | /** | |
31 | * Checks browser compatibility with the {@link FCKeditor editor}. | |
32 | * | |
33 | * @version $Id: Compatibility.java 3595 2009-06-01 20:02:02Z mosipov $ | |
34 | */ | |
35 | 0 | public class Compatibility { |
36 | ||
37 | /** | |
38 | * Checks if a browser's user agent string is compatible with the FCKeditor. | |
39 | * | |
40 | * @param userAgentString | |
41 | * the user agent string of a browser | |
42 | * @return <code>true</code> if compatible, else <code>false</code> | |
43 | */ | |
44 | public static boolean check(final String userAgentString) { | |
45 | 18 | if (Utils.isEmpty(userAgentString)) |
46 | 0 | return false; |
47 | ||
48 | 18 | String userAgentStr = userAgentString.toLowerCase(); |
49 | ||
50 | // IE 5.5+, check special keys like 'Opera' and 'mac', because there are | |
51 | // some | |
52 | // other browsers, containing 'MSIE' in there agent string! | |
53 | 18 | if (userAgentStr.indexOf("opera") < 0 |
54 | && userAgentStr.indexOf("mac") < 0 | |
55 | && getBrowserVersion(userAgentStr, ".*msie ([\\d]+.[\\d]+).*") >= 5.5f) | |
56 | 4 | return true; |
57 | ||
58 | // for all gecko based browsers | |
59 | 14 | if (getBrowserVersion(userAgentStr, ".*rv:([\\d]+.[\\d]+).*") > 1.7f) |
60 | 4 | return true; |
61 | ||
62 | // Opera 9.5+ | |
63 | 10 | if (getBrowserVersion(userAgentStr, "opera/([\\d]+.[\\d]+).*") >= 9.5f |
64 | || getBrowserVersion(userAgentStr, ".*opera ([\\d]+.[\\d]+)") >= 9.5f) | |
65 | 2 | return true; |
66 | ||
67 | // Safari 3+ | |
68 | 8 | if (getBrowserVersion(userAgentStr, ".*applewebkit/([\\d]+).*") >= 522f) |
69 | 3 | return true; |
70 | ||
71 | 5 | return false; |
72 | } | |
73 | ||
74 | /** | |
75 | * Returns <code>true</code> if a browser is compatible by its request | |
76 | * user-agent header. | |
77 | * | |
78 | * @see #check(String) | |
79 | * @param request | |
80 | * current user request instance | |
81 | * @return <code>true</code> if a browser is compatible, else | |
82 | * <code>false</code> | |
83 | */ | |
84 | public static boolean isCompatibleBrowser(final HttpServletRequest request) { | |
85 | 0 | return (request == null) ? false : check(request |
86 | .getHeader("user-agent")); | |
87 | } | |
88 | ||
89 | /** | |
90 | * Helper method to get the browser version from 'userAgent' with regex. The | |
91 | * first matching group has to be the version number! | |
92 | * | |
93 | * @param userAgentString | |
94 | * the user agent string of a browser | |
95 | * @param regex | |
96 | * the pattern to retrieve the browser version | |
97 | * @return the browser version, or -1f if version can't be determined | |
98 | */ | |
99 | private static float getBrowserVersion(final String userAgentString, | |
100 | final String regex) { | |
101 | 50 | Pattern pattern = Pattern.compile(regex); |
102 | 50 | Matcher matcher = pattern.matcher(userAgentString); |
103 | 50 | if (matcher.matches()) { |
104 | try { | |
105 | 17 | return Float.parseFloat(matcher.group(1)); |
106 | 0 | } catch (NumberFormatException e) { |
107 | 0 | return -1f; |
108 | } | |
109 | } | |
110 | 33 | return -1f; |
111 | } | |
112 | ||
113 | } |