View Javadoc
1 package org.apache.turbine.util; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Turbine" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * "Apache Turbine", nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>;. 55 */ 56 57 /*** 58 * This class parses the user agent string and sets javasciptOK and 59 * cssOK following the rules described below. If you want to check 60 * for specific browsers/versions then use this class to parse the 61 * user agent string and use the accessor methods in this class. 62 * 63 * JavaScriptOK means that the browser understands JavaScript on the 64 * same level the Navigator 3 does. Specifically, it can use named 65 * images. This allows easier rollovers. If a browser doesn't do 66 * this (Nav 2 or MSIE 3), then we just assume it can't do any 67 * JavaScript. Referencing images by load order is too hard to 68 * maintain. 69 * 70 * CSSOK is kind of sketchy in that Nav 4 and MSIE work differently, 71 * but they do seem to have most of the functionality. MSIE 4 for the 72 * Mac has buggy CSS support, so we let it do JavaScript, but no CSS. 73 * 74 * Ported from Leon's PHP code at 75 * http://www.working-dogs.com/freetrade by Frank. 76 * 77 * @author <a href="mailto:frank.kim@clearink.com">Frank Y. Kim</a> 78 * @author <a href="mailto:leon@clearink.com">Leon Atkisnon</a> 79 * @author <a href="mailto:mospaw@polk-county.com">Chris Mospaw</a> 80 * @author <a href="mailto:bgriffin@cddb.com">Benjamin Elijah Griffin</a> 81 * @version $Id: BrowserDetector.java,v 1.1.1.1 2001/08/16 05:09:37 jvanzyl Exp $ 82 */ 83 public class BrowserDetector 84 { 85 /*** The user agent string. */ 86 private String userAgentString = ""; 87 88 /*** The browser name specified in the user agent string. */ 89 private String browserName = ""; 90 91 /*** 92 * The browser version specified in the user agent string. If we 93 * can't parse the version just assume an old browser. 94 */ 95 private float browserVersion = (float)1.0; 96 97 /*** 98 * The browser platform specified in the user agent string. 99 */ 100 private String browserPlatform = "unknown"; 101 102 /*** Whether or not javascript works in this browser. */ 103 private boolean javascriptOK = false; 104 105 /*** Whether or not CSS works in this browser. */ 106 private boolean cssOK = false; 107 108 /*** Whether or not file upload works in this browser. */ 109 private boolean fileUploadOK = false; 110 111 /*** Constants used by this class. */ 112 public static final String MSIE = "MSIE"; 113 public static final String OPERA = "Opera"; 114 public static final String MOZILLA = "Mozilla"; 115 public static final String WINDOWS = "Windows"; 116 public static final String UNIX = "Unix"; 117 public static final String MACINTOSH = "Macintosh"; 118 119 /*** 120 * Constructor used to initialize this class. 121 * 122 * @param userAgentString A String with the user agent field. 123 */ 124 public BrowserDetector(String userAgentString) 125 { 126 this.userAgentString = userAgentString; 127 parse(); 128 } 129 130 /*** 131 * Constructor used to initialize this class. 132 * 133 * @param data The Turbine RunData object. 134 */ 135 public BrowserDetector(RunData data) 136 { 137 this.userAgentString = 138 data.getRequest().getHeader("User-Agent"); 139 parse(); 140 } 141 142 /*** 143 * Whether or not CSS works in this browser. 144 * 145 * @return True if CSS works in this browser. 146 */ 147 public boolean isCssOK() 148 { 149 return cssOK; 150 } 151 152 /*** 153 * Whether or not file upload works in this browser. 154 * 155 * @return True if file upload works in this browser. 156 */ 157 public boolean isFileUploadOK() 158 { 159 return fileUploadOK; 160 } 161 162 /*** 163 * Whether or not Javascript works in this browser. 164 * 165 * @return True if Javascript works in this browser. 166 */ 167 public boolean isJavascriptOK() 168 { 169 return javascriptOK; 170 } 171 172 /*** 173 * The browser name specified in the user agent string. 174 * 175 * @return A String with the browser name. 176 */ 177 public String getBrowserName() 178 { 179 return browserName; 180 } 181 182 /*** 183 * The browser platform specified in the user agent string. 184 * 185 * @return A String with the browser platform. 186 */ 187 public String getBrowserPlatform() 188 { 189 return browserPlatform; 190 } 191 192 /*** 193 * The browser version specified in the user agent string. 194 * 195 * @return A String with the browser version. 196 */ 197 public float getBrowserVersion() 198 { 199 return browserVersion; 200 } 201 202 /*** 203 * The user agent string for this class. 204 * 205 * @return A String with the user agent. 206 */ 207 public String getUserAgentString() 208 { 209 return userAgentString; 210 } 211 212 /*** 213 * Helper method to initialize this class. 214 */ 215 private void parse() 216 { 217 int versionStartIndex = userAgentString.indexOf("/"); 218 int versionEndIndex = userAgentString.indexOf(" "); 219 220 // Get the browser name and version. 221 browserName = userAgentString.substring( 0, versionStartIndex ); 222 try 223 { 224 // Not all user agents will have a space in the reported 225 // string. 226 String agentSubstring = null; 227 if ( versionEndIndex < 0 ) 228 agentSubstring = userAgentString.substring( versionStartIndex+1 ); 229 else 230 agentSubstring = userAgentString.substring( versionStartIndex+1, versionEndIndex ); 231 browserVersion = toFloat( agentSubstring ); 232 } 233 catch (NumberFormatException e) 234 { 235 // Just use the default value. 236 } 237 238 // MSIE lies about its name. Of course... 239 if ( userAgentString.indexOf(MSIE) != -1 ) 240 { 241 // Ex: Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt) 242 versionStartIndex = (userAgentString.indexOf(MSIE) + 243 MSIE.length() + 1); 244 versionEndIndex = userAgentString.indexOf( ";", 245 versionStartIndex ); 246 247 browserName = MSIE; 248 try 249 { 250 browserVersion = toFloat( userAgentString.substring(versionStartIndex, versionEndIndex) ); 251 } 252 catch (NumberFormatException e) 253 { 254 // Just use the default value. 255 } 256 257 // PHP code 258 // $Browser_Name = "MSIE"; 259 // $Browser_Version = strtok("MSIE"); 260 // $Browser_Version = strtok(" "); 261 // $Browser_Version = strtok(";"); 262 } 263 264 // Opera isn't completely honest, either... 265 // Modificaton by Chris Mospaw <mospaw@polk-county.com> 266 if ( userAgentString.indexOf(OPERA) != -1 ) 267 { 268 //Ex: Mozilla/4.0 (Windows NT 4.0;US) Opera 3.61 [en] 269 versionStartIndex = (userAgentString.indexOf(OPERA) + 270 OPERA.length() + 1); 271 versionEndIndex = userAgentString.indexOf( " ", 272 versionStartIndex ); 273 274 browserName = OPERA; 275 try 276 { 277 browserVersion = toFloat( userAgentString.substring(versionStartIndex, versionEndIndex) ); 278 } 279 catch (NumberFormatException e) 280 { 281 // Just use the default value. 282 } 283 284 // PHP code 285 // $Browser_Name = "Opera"; 286 // $Browser_Version = strtok("Opera"); 287 // $Browser_Version = strtok("/"); 288 // $Browser_Version = strtok(";"); 289 } 290 291 292 // Try to figure out what platform. 293 if ( (userAgentString.indexOf("Windows") != -1) || 294 (userAgentString.indexOf("WinNT") != -1) || 295 (userAgentString.indexOf("Win98") != -1) || 296 (userAgentString.indexOf("Win95") != -1) ) 297 { 298 browserPlatform = WINDOWS; 299 } 300 301 if ( userAgentString.indexOf("Mac") != -1 ) 302 { 303 browserPlatform = MACINTOSH; 304 } 305 306 if ( userAgentString.indexOf("X11") != -1 ) 307 { 308 browserPlatform = UNIX; 309 } 310 311 if (browserPlatform == WINDOWS) 312 { 313 if (browserName.equals(MOZILLA)) 314 { 315 if (browserVersion >= 3.0) 316 { 317 javascriptOK = true; 318 fileUploadOK = true; 319 } 320 if (browserVersion >= 4.0) 321 { 322 cssOK = true; 323 } 324 } 325 else if (browserName == MSIE) 326 { 327 if (browserVersion >= 4.0) 328 { 329 javascriptOK = true; 330 fileUploadOK = true; 331 cssOK = true; 332 } 333 } 334 else if (browserName == OPERA) 335 { 336 if (browserVersion >= 3.0) 337 { 338 javascriptOK = true; 339 fileUploadOK = true; 340 cssOK = true; 341 } 342 } 343 } 344 else if (browserPlatform == MACINTOSH) 345 { 346 if (browserName.equals(MOZILLA)) 347 { 348 if (browserVersion >= 3.0) 349 { 350 javascriptOK = true; 351 fileUploadOK = true; 352 } 353 if( browserVersion >= 4.0) 354 { 355 cssOK = true; 356 } 357 } 358 else if (browserName == MSIE) 359 { 360 if (browserVersion >= 4.0) 361 { 362 javascriptOK = true ; 363 fileUploadOK = true; 364 } 365 if (browserVersion > 4.0) 366 { 367 cssOK = true; 368 } 369 } 370 } 371 else if (browserPlatform == UNIX) 372 { 373 if (browserName.equals(MOZILLA)) 374 { 375 if (browserVersion >= 3.0) 376 { 377 javascriptOK = true; 378 fileUploadOK = true; 379 } 380 if (browserVersion >= 4.0) 381 { 382 cssOK = true; 383 } 384 } 385 } 386 } 387 388 /*** 389 * Helper method to conver String to a float. 390 * 391 * @param s A String. 392 * @return The String converted to float. 393 */ 394 private float toFloat(String s) 395 { 396 return Float.valueOf(s).floatValue(); 397 } 398 }

This page was automatically generated by Maven