Coverage report

  %line %branch
org.apache.turbine.util.BrowserDetector
0% 
0% 

 1  
 package org.apache.turbine.util;
 2  
 
 3  
 /*
 4  
  * Copyright 2001-2005 The Apache Software Foundation.
 5  
  *
 6  
  * Licensed under the Apache License, Version 2.0 (the "License")
 7  
  * you may not use this file except in compliance with the License.
 8  
  * You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 
 19  
 /**
 20  
  * This class parses the user agent string and sets javasciptOK and
 21  
  * cssOK following the rules described below.  If you want to check
 22  
  * for specific browsers/versions then use this class to parse the
 23  
  * user agent string and use the accessor methods in this class.
 24  
  *
 25  
  * JavaScriptOK means that the browser understands JavaScript on the
 26  
  * same level the Navigator 3 does.  Specifically, it can use named
 27  
  * images.  This allows easier rollovers.  If a browser doesn't do
 28  
  * this (Nav 2 or MSIE 3), then we just assume it can't do any
 29  
  * JavaScript.  Referencing images by load order is too hard to
 30  
  * maintain.
 31  
  *
 32  
  * CSSOK is kind of sketchy in that Nav 4 and MSIE work differently,
 33  
  * but they do seem to have most of the functionality.  MSIE 4 for the
 34  
  * Mac has buggy CSS support, so we let it do JavaScript, but no CSS.
 35  
  *
 36  
  * Ported from Leon's PHP code at
 37  
  * http://www.working-dogs.com/freetrade by Frank.
 38  
  *
 39  
  * @author <a href="mailto:frank.kim@clearink.com">Frank Y. Kim</a>
 40  
  * @author <a href="mailto:leon@clearink.com">Leon Atkisnon</a>
 41  
  * @author <a href="mailto:mospaw@polk-county.com">Chris Mospaw</a>
 42  
  * @author <a href="mailto:bgriffin@cddb.com">Benjamin Elijah Griffin</a>
 43  
  * @version $Id: BrowserDetector.java 264152 2005-08-29 14:50:22Z henning $
 44  
  */
 45  
 public class BrowserDetector
 46  
 {
 47  
     public static final String MSIE = "MSIE";
 48  
     public static final String OPERA = "Opera";
 49  
     public static final String MOZILLA = "Mozilla";
 50  
 
 51  
     public static final String WINDOWS = "Windows";
 52  
     public static final String UNIX = "Unix";
 53  
     public static final String MACINTOSH = "Macintosh";
 54  
 
 55  
     /** The user agent string. */
 56  0
     private String userAgentString = "";
 57  
 
 58  
     /** The browser name specified in the user agent string. */
 59  0
     private String browserName = "";
 60  
 
 61  
     /**
 62  
      * The browser version specified in the user agent string.  If we
 63  
      * can't parse the version just assume an old browser.
 64  
      */
 65  0
     private float browserVersion = (class="keyword">float) 1.0;
 66  
 
 67  
     /**
 68  
      * The browser platform specified in the user agent string.
 69  
      */
 70  0
     private String browserPlatform = "unknown";
 71  
 
 72  
     /** Whether or not javascript works in this browser. */
 73  0
     private boolean javascriptOK = false;
 74  
 
 75  
     /** Whether or not CSS works in this browser. */
 76  0
     private boolean cssOK = false;
 77  
 
 78  
     /** Whether or not file upload works in this browser. */
 79  0
     private boolean fileUploadOK = false;
 80  
 
 81  
     /**
 82  
      * Constructor used to initialize this class.
 83  
      *
 84  
      * @param userAgentString A String with the user agent field.
 85  
      */
 86  
     public BrowserDetector(String userAgentString)
 87  0
     {
 88  0
         this.userAgentString = userAgentString;
 89  0
         parse();
 90  0
     }
 91  
 
 92  
     /**
 93  
      * Constructor used to initialize this class.
 94  
      *
 95  
      * @param data The Turbine RunData object.
 96  
      */
 97  
     public BrowserDetector(RunData data)
 98  0
     {
 99  0
         this.userAgentString = data.getUserAgent();
 100  0
         parse();
 101  0
     }
 102  
 
 103  
     /**
 104  
      * Whether or not CSS works in this browser.
 105  
      *
 106  
      * @return True if CSS works in this browser.
 107  
      */
 108  
     public boolean isCssOK()
 109  
     {
 110  0
         return cssOK;
 111  
     }
 112  
 
 113  
     /**
 114  
      * Whether or not file upload works in this browser.
 115  
      *
 116  
      * @return True if file upload works in this browser.
 117  
      */
 118  
     public boolean isFileUploadOK()
 119  
     {
 120  0
         return fileUploadOK;
 121  
     }
 122  
 
 123  
     /**
 124  
      * Whether or not Javascript works in this browser.
 125  
      *
 126  
      * @return True if Javascript works in this browser.
 127  
      */
 128  
     public boolean isJavascriptOK()
 129  
     {
 130  0
         return javascriptOK;
 131  
     }
 132  
 
 133  
     /**
 134  
      * The browser name specified in the user agent string.
 135  
      *
 136  
      * @return A String with the browser name.
 137  
      */
 138  
     public String getBrowserName()
 139  
     {
 140  0
         return browserName;
 141  
     }
 142  
 
 143  
     /**
 144  
      * The browser platform specified in the user agent string.
 145  
      *
 146  
      * @return A String with the browser platform.
 147  
      */
 148  
     public String getBrowserPlatform()
 149  
     {
 150  0
         return browserPlatform;
 151  
     }
 152  
 
 153  
     /**
 154  
      * The browser version specified in the user agent string.
 155  
      *
 156  
      * @return A String with the browser version.
 157  
      */
 158  
     public float getBrowserVersion()
 159  
     {
 160  0
         return browserVersion;
 161  
     }
 162  
 
 163  
     /**
 164  
      * The user agent string for this class.
 165  
      *
 166  
      * @return A String with the user agent.
 167  
      */
 168  
     public String getUserAgentString()
 169  
     {
 170  0
         return userAgentString;
 171  
     }
 172  
 
 173  
     /**
 174  
      * Helper method to initialize this class.
 175  
      */
 176  
     private void parse()
 177  
     {
 178  0
         int versionStartIndex = userAgentString.indexOf("/");
 179  0
         int versionEndIndex = userAgentString.indexOf(" ");
 180  
 
 181  
         // Get the browser name and version.
 182  0
         browserName = userAgentString.substring(0, versionStartIndex);
 183  
         try
 184  
         {
 185  
             // Not all user agents will have a space in the reported
 186  
             // string.
 187  0
             String agentSubstring = null;
 188  0
             if (versionEndIndex < 0)
 189  
             {
 190  0
                 agentSubstring
 191  
                         = userAgentString.substring(versionStartIndex + 1);
 192  
             }
 193  
             else
 194  
             {
 195  0
                 agentSubstring = userAgentString
 196  
                         .substring(versionStartIndex + 1, versionEndIndex);
 197  
             }
 198  0
             browserVersion = toFloat(agentSubstring);
 199  
         }
 200  0
         catch (NumberFormatException e)
 201  
         {
 202  
             // Just use the default value.
 203  0
         }
 204  
 
 205  
         // MSIE lies about its name.  Of course...
 206  0
         if (userAgentString.indexOf(MSIE) != -1)
 207  
         {
 208  
             // Ex: Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)
 209  0
             versionStartIndex = (userAgentString.indexOf(MSIE)
 210  
                     + MSIE.length() + 1);
 211  0
             versionEndIndex = userAgentString.indexOf(";", versionStartIndex);
 212  
 
 213  0
             browserName = MSIE;
 214  
             try
 215  
             {
 216  0
                 browserVersion = toFloat(userAgentString
 217  
                         .substring(versionStartIndex, versionEndIndex));
 218  
             }
 219  0
             catch (NumberFormatException e)
 220  
             {
 221  
                 // Just use the default value.
 222  0
             }
 223  
 
 224  
             // PHP code
 225  
             // $Browser_Name = "MSIE";
 226  
             // $Browser_Version = strtok("MSIE");
 227  
             // $Browser_Version = strtok(" ");
 228  
             // $Browser_Version = strtok(";");
 229  
         }
 230  
 
 231  
         // Opera isn't completely honest, either...
 232  
         // Modificaton by Chris Mospaw <mospaw@polk-county.com>
 233  0
         if (userAgentString.indexOf(OPERA) != -1)
 234  
         {
 235  
             //Ex: Mozilla/4.0 (Windows NT 4.0;US) Opera 3.61  [en]
 236  0
             versionStartIndex = (userAgentString.indexOf(OPERA)
 237  
                     + OPERA.length() + 1);
 238  0
             versionEndIndex = userAgentString.indexOf(" ", versionStartIndex);
 239  
 
 240  0
             browserName = OPERA;
 241  
             try
 242  
             {
 243  0
                 browserVersion = toFloat(userAgentString
 244  
                         .substring(versionStartIndex, versionEndIndex));
 245  
             }
 246  0
             catch (NumberFormatException e)
 247  
             {
 248  
                 // Just use the default value.
 249  0
             }
 250  
 
 251  
             // PHP code
 252  
             // $Browser_Name = "Opera";
 253  
             // $Browser_Version = strtok("Opera");
 254  
             // $Browser_Version = strtok("/");
 255  
             // $Browser_Version = strtok(";");
 256  
         }
 257  
 
 258  
 
 259  
         // Try to figure out what platform.
 260  0
         if ((userAgentString.indexOf("Windows") != -1)
 261  
                 || (userAgentString.indexOf("WinNT") != -1)
 262  
                 || (userAgentString.indexOf("Win98") != -1)
 263  
                 || (userAgentString.indexOf("Win95") != -1))
 264  
         {
 265  0
             browserPlatform = WINDOWS;
 266  
         }
 267  
 
 268  0
         if (userAgentString.indexOf("Mac") != -1)
 269  
         {
 270  0
             browserPlatform = MACINTOSH;
 271  
         }
 272  
 
 273  0
         if (userAgentString.indexOf("X11") != -1)
 274  
         {
 275  0
             browserPlatform = UNIX;
 276  
         }
 277  
 
 278  0
         if (browserPlatform == WINDOWS)
 279  
         {
 280  0
             if (browserName.equals(MOZILLA))
 281  
             {
 282  0
                 if (browserVersion >= 3.0)
 283  
                 {
 284  0
                     javascriptOK = true;
 285  0
                     fileUploadOK = true;
 286  
                 }
 287  0
                 if (browserVersion >= 4.0)
 288  
                 {
 289  0
                     cssOK = true;
 290  
                 }
 291  
             }
 292  0
             else if (browserName == MSIE)
 293  
             {
 294  0
                 if (browserVersion >= 4.0)
 295  
                 {
 296  0
                     javascriptOK = true;
 297  0
                     fileUploadOK = true;
 298  0
                     cssOK = true;
 299  
                 }
 300  
             }
 301  0
             else if (browserName == OPERA)
 302  
             {
 303  0
                 if (browserVersion >= 3.0)
 304  
                 {
 305  0
                     javascriptOK = true;
 306  0
                     fileUploadOK = true;
 307  0
                     cssOK = true;
 308  
                 }
 309  
             }
 310  
         }
 311  0
         else if (browserPlatform == MACINTOSH)
 312  
         {
 313  0
             if (browserName.equals(MOZILLA))
 314  
             {
 315  0
                 if (browserVersion >= 3.0)
 316  
                 {
 317  0
                     javascriptOK = true;
 318  0
                     fileUploadOK = true;
 319  
                 }
 320  0
                 if (browserVersion >= 4.0)
 321  
                 {
 322  0
                     cssOK = true;
 323  
                 }
 324  
             }
 325  0
             else if (browserName == MSIE)
 326  
             {
 327  0
                 if (browserVersion >= 4.0)
 328  
                 {
 329  0
                     javascriptOK = true;
 330  0
                     fileUploadOK = true;
 331  
                 }
 332  0
                 if (browserVersion > 4.0)
 333  
                 {
 334  0
                     cssOK = true;
 335  
                 }
 336  
             }
 337  
         }
 338  0
         else if (browserPlatform == UNIX)
 339  
         {
 340  0
             if (browserName.equals(MOZILLA))
 341  
             {
 342  0
                 if (browserVersion >= 3.0)
 343  
                 {
 344  0
                     javascriptOK = true;
 345  0
                     fileUploadOK = true;
 346  
                 }
 347  0
                 if (browserVersion >= 4.0)
 348  
                 {
 349  0
                     cssOK = true;
 350  
                 }
 351  
             }
 352  
         }
 353  0
     }
 354  
 
 355  
     /**
 356  
      * Helper method to convert String to a float.
 357  
      *
 358  
      * @param s A String.
 359  
      * @return The String converted to float.
 360  
      */
 361  
     private static float toFloat(String s)
 362  
     {
 363  0
         return Float.valueOf(s).floatValue();
 364  
     }
 365  
 
 366  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.