Coverage Report - net.fckeditor.tool.Compatibility
 
Classes in this File Line Coverage Branch Coverage Complexity
Compatibility
82%
23/28
83%
25/30
6.667
 
 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 1763 2008-03-21 14:26:06Z th-schwarz $
 32  
  */
 33  0
 public class Compatibility {
 34  
 
 35  
         /**
 36  
          * Checks, if a browser's user agent string is compatible for the FCKeditor. <br>
 37  
          * Adapted from: http://dev.fckeditor.net/browser/FCKeditor/releases/stable/fckeditor.php
 38  
          * 
 39  
          * @param userAgentString
 40  
          * @return true, if compatible, otherwise false
 41  
          */
 42  
         public static boolean check(final String userAgentString) {
 43  16
                 if (Utils.isEmpty(userAgentString))
 44  0
                         return false;
 45  
 
 46  
                 float version;
 47  
 
 48  
                 // IE 5.5+, check special keys like 'Opera' and 'mac', because there are some
 49  
                 // other browsers, containing 'MSIE' in there agent string!
 50  16
                 if (userAgentString.indexOf("Opera") < 0 && userAgentString.indexOf("mac") < 0) {
 51  12
                         version = getBrowserVersion(userAgentString, ".*MSIE ([\\d]+.[\\d]+).*");
 52  12
                         if (version != -1f && version >= 5.5f)
 53  4
                                 return true;
 54  
                 }
 55  
 
 56  
                 // for mozilla only, because all firefox versions are supported
 57  12
                 version = getBrowserVersion(userAgentString, ".*Gecko/([\\d]+).*");
 58  12
                 if (version != -1f && version >= 20030210f)
 59  2
                         return true;
 60  
 
 61  
                 // Opera 9.5+
 62  10
                 version = getBrowserVersion(userAgentString, "Opera/([\\d]+.[\\d]+).*");
 63  10
                 if (version != -1f && version >= 9.5f)
 64  1
                         return true;
 65  9
                 version = getBrowserVersion(userAgentString, ".*Opera ([\\d]+.[\\d]+)");
 66  9
                 if (version != -1f && version >= 9.5f)
 67  1
                         return true;
 68  
 
 69  
                 // Safari 3+
 70  8
                 version = getBrowserVersion(userAgentString, ".*AppleWebKit/([\\d]+).*");
 71  8
                 if (version != -1f && version >= 522f)
 72  3
                         return true;
 73  
 
 74  5
                 return false;
 75  
         }
 76  
 
 77  
         /**
 78  
          * Just a wrapper to {@link #check(String)}.
 79  
          * 
 80  
          * @param request
 81  
          */
 82  
         public static boolean isCompatibleBrowser(final HttpServletRequest request) {
 83  0
                 return (request == null) ? false : check(request.getHeader("user-agent"));
 84  
         }
 85  
 
 86  
         /**
 87  
          * Helper method to get the the browser version from 'userAgent' with the regular expression
 88  
          * 'regex'. The first group of the matches has to be the version number!
 89  
          * 
 90  
          * @param userAgent
 91  
          * @param regex
 92  
          * @return The browser version, or -1f, if version con't find out.
 93  
          */
 94  
         private static float getBrowserVersion(final String userAgent, final String regex) {
 95  51
                 Pattern pattern = Pattern.compile(regex);
 96  51
                 Matcher matcher = pattern.matcher(userAgent);
 97  51
                 if (matcher.matches()) {
 98  
                         try {
 99  16
                                 return Float.parseFloat(matcher.group(1));
 100  0
                         } catch (NumberFormatException e) {
 101  0
                                 return -1f;
 102  
                         }
 103  
                 }
 104  35
                 return -1f;
 105  
         }
 106  
 
 107  
 }