Coverage Report - net.fckeditor.tool.Compatibility
 
Classes in this File Line Coverage Branch Coverage Complexity
Compatibility
77%
17/22
85%
17/20
6
 
 1  
 /*
 2  
  * FCKeditor - The text editor for Internet - http://www.fckeditor.net
 3  
  * Copyright (C) 2003-2008 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  
 /**
 29  
  * Compatibility check.
 30  
  * 
 31  
  * @version $Id: Compatibility.java 2134 2008-06-25 08:36:32Z th-schwarz $
 32  
  */
 33  0
 public class Compatibility {
 34  
 
 35  
         /**
 36  
          * Checks if a browser's user agent string is compatible for the FCKeditor.
 37  
          * 
 38  
          * @param userAgentString
 39  
          * @return <code>true</code> if compatible otherwise <code>false</code>
 40  
          */
 41  
         public static boolean check(final String userAgentString) {
 42  18
                 if (Utils.isEmpty(userAgentString))
 43  0
                         return false;
 44  
                 
 45  18
                 String userAgentStr = userAgentString.toLowerCase();
 46  
 
 47  
                 // IE 5.5+, check special keys like 'Opera' and 'mac', because there are some
 48  
                 // other browsers, containing 'MSIE' in there agent string!
 49  18
                 if (userAgentStr.indexOf("opera") < 0 && userAgentStr.indexOf("mac") < 0) {
 50  9
                         if (getBrowserVersion(userAgentStr, ".*msie ([\\d]+.[\\d]+).*") >= 5.5f)
 51  4
                                 return true;
 52  
                 }
 53  
                 
 54  
                 // for all gecko based browsers
 55  14
                 if (getBrowserVersion(userAgentStr, ".*rv:([\\d]+.[\\d]+).*") > 1.7f)
 56  4
                         return true;
 57  
                 
 58  
                 // Opera 9.5+
 59  10
                 if (getBrowserVersion(userAgentStr, "opera/([\\d]+.[\\d]+).*") >= 9.5f
 60  
                                 || getBrowserVersion(userAgentStr, ".*opera ([\\d]+.[\\d]+)") >= 9.5f)
 61  2
                         return true;
 62  
 
 63  
                 // Safari 3+
 64  8
                 if (getBrowserVersion(userAgentStr, ".*applewebkit/([\\d]+).*") >= 522f)
 65  3
                         return true;
 66  
 
 67  5
                 return false;
 68  
         }
 69  
 
 70  
         /**
 71  
          * Just a wrapper to {@link #check(String)}.
 72  
          * 
 73  
          * @param request
 74  
          */
 75  
         public static boolean isCompatibleBrowser(final HttpServletRequest request) {
 76  0
                 return (request == null) ? false : check(request.getHeader("user-agent"));
 77  
         }
 78  
 
 79  
         /**
 80  
          * Helper method to get the browser version from 'userAgent' with regex. The
 81  
          * first matching group has to be the version number!
 82  
          * 
 83  
          * @param userAgent
 84  
          * @param regex
 85  
          * @return The browser version, or -1f if version can't be determined.
 86  
          */
 87  
         private static float getBrowserVersion(final String userAgent, final String regex) {
 88  50
                 Pattern pattern = Pattern.compile(regex);
 89  50
                 Matcher matcher = pattern.matcher(userAgent);
 90  50
                 if (matcher.matches()) {
 91  
                         try {
 92  17
                                 return Float.parseFloat(matcher.group(1));
 93  0
                         } catch (NumberFormatException e) {
 94  0
                                 return -1f;
 95  
                         }
 96  
                 }
 97  33
                 return -1f;
 98  
         }
 99  
 
 100  
 }