1 package org.apache.turbine.util; 2 3 18 19 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 56 private String userAgentString = ""; 57 58 59 private String browserName = ""; 60 61 65 private float browserVersion = (float) 1.0; 66 67 70 private String browserPlatform = "unknown"; 71 72 73 private boolean javascriptOK = false; 74 75 76 private boolean cssOK = false; 77 78 79 private boolean fileUploadOK = false; 80 81 86 public BrowserDetector(String userAgentString) 87 { 88 this.userAgentString = userAgentString; 89 parse(); 90 } 91 92 97 public BrowserDetector(RunData data) 98 { 99 this.userAgentString = data.getUserAgent(); 100 parse(); 101 } 102 103 108 public boolean isCssOK() 109 { 110 return cssOK; 111 } 112 113 118 public boolean isFileUploadOK() 119 { 120 return fileUploadOK; 121 } 122 123 128 public boolean isJavascriptOK() 129 { 130 return javascriptOK; 131 } 132 133 138 public String getBrowserName() 139 { 140 return browserName; 141 } 142 143 148 public String getBrowserPlatform() 149 { 150 return browserPlatform; 151 } 152 153 158 public float getBrowserVersion() 159 { 160 return browserVersion; 161 } 162 163 168 public String getUserAgentString() 169 { 170 return userAgentString; 171 } 172 173 176 private void parse() 177 { 178 int versionStartIndex = userAgentString.indexOf("/"); 179 int versionEndIndex = userAgentString.indexOf(" "); 180 181 browserName = userAgentString.substring(0, versionStartIndex); 183 try 184 { 185 String agentSubstring = null; 188 if (versionEndIndex < 0) 189 { 190 agentSubstring 191 = userAgentString.substring(versionStartIndex + 1); 192 } 193 else 194 { 195 agentSubstring = userAgentString 196 .substring(versionStartIndex + 1, versionEndIndex); 197 } 198 browserVersion = toFloat(agentSubstring); 199 } 200 catch (NumberFormatException e) 201 { 202 } 204 205 if (userAgentString.indexOf(MSIE) != -1) 207 { 208 versionStartIndex = (userAgentString.indexOf(MSIE) 210 + MSIE.length() + 1); 211 versionEndIndex = userAgentString.indexOf(";", versionStartIndex); 212 213 browserName = MSIE; 214 try 215 { 216 browserVersion = toFloat(userAgentString 217 .substring(versionStartIndex, versionEndIndex)); 218 } 219 catch (NumberFormatException e) 220 { 221 } 223 224 } 230 231 if (userAgentString.indexOf(OPERA) != -1) 234 { 235 versionStartIndex = (userAgentString.indexOf(OPERA) 237 + OPERA.length() + 1); 238 versionEndIndex = userAgentString.indexOf(" ", versionStartIndex); 239 240 browserName = OPERA; 241 try 242 { 243 browserVersion = toFloat(userAgentString 244 .substring(versionStartIndex, versionEndIndex)); 245 } 246 catch (NumberFormatException e) 247 { 248 } 250 251 } 257 258 259 if ((userAgentString.indexOf("Windows") != -1) 261 || (userAgentString.indexOf("WinNT") != -1) 262 || (userAgentString.indexOf("Win98") != -1) 263 || (userAgentString.indexOf("Win95") != -1)) 264 { 265 browserPlatform = WINDOWS; 266 } 267 268 if (userAgentString.indexOf("Mac") != -1) 269 { 270 browserPlatform = MACINTOSH; 271 } 272 273 if (userAgentString.indexOf("X11") != -1) 274 { 275 browserPlatform = UNIX; 276 } 277 278 if (browserPlatform == WINDOWS) 279 { 280 if (browserName.equals(MOZILLA)) 281 { 282 if (browserVersion >= 3.0) 283 { 284 javascriptOK = true; 285 fileUploadOK = true; 286 } 287 if (browserVersion >= 4.0) 288 { 289 cssOK = true; 290 } 291 } 292 else if (browserName == MSIE) 293 { 294 if (browserVersion >= 4.0) 295 { 296 javascriptOK = true; 297 fileUploadOK = true; 298 cssOK = true; 299 } 300 } 301 else if (browserName == OPERA) 302 { 303 if (browserVersion >= 3.0) 304 { 305 javascriptOK = true; 306 fileUploadOK = true; 307 cssOK = true; 308 } 309 } 310 } 311 else if (browserPlatform == MACINTOSH) 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 } 332 if (browserVersion > 4.0) 333 { 334 cssOK = true; 335 } 336 } 337 } 338 else if (browserPlatform == UNIX) 339 { 340 if (browserName.equals(MOZILLA)) 341 { 342 if (browserVersion >= 3.0) 343 { 344 javascriptOK = true; 345 fileUploadOK = true; 346 } 347 if (browserVersion >= 4.0) 348 { 349 cssOK = true; 350 } 351 } 352 } 353 } 354 355 361 private static final float toFloat(String s) 362 { 363 return Float.valueOf(s).floatValue(); 364 } 365 366 } 367 | Popular Tags |