1 14 package org.wings.session; 15 16 import org.apache.regexp.RE; 17 18 import java.io.FileReader ; 19 import java.io.LineNumberReader ; 20 import java.util.Locale ; 21 import java.util.StringTokenizer ; 22 23 24 30 public class Browser { 31 protected String agent; 32 private int majorVersion; 33 private double minorVersion; 34 private String release; 35 private String os; 36 private OSType osType = OSType.UNKNOWN; 37 private String osVersion; 38 private String browserName; 39 private Locale browserLocale; 40 private BrowserType browserType = BrowserType.UNKNOWN; 41 42 48 public Browser(String agent) 49 throws org.apache.regexp.RESyntaxException { 50 this.agent = agent; 51 detect(); 52 } 53 54 57 public String getBrowserName() { 58 return browserName; 59 } 60 61 64 public BrowserType getBrowserType() { 65 return browserType; 66 } 67 68 74 public int getMajorVersion() { 75 return majorVersion; 76 } 77 78 85 public double getMinorVersion() { 86 return minorVersion; 87 } 88 89 95 public String getRelease() { 96 return release; 97 } 98 99 104 public String getOs() { 105 return os; 106 } 107 108 113 public String getOsVersion() { 114 return osVersion; 115 } 116 117 122 public OSType getOsType() { 123 return osType; 124 } 125 126 132 public Locale getClientLocale() { 133 return browserLocale; 134 } 135 136 137 protected final RE RE_START = new RE("^([a-zA-Z0-9_\\-]+)(/([0-9])\\.([0-9]+))?"); 138 protected final RE RE_MSIE = new RE("MSIE ([0-9])\\.([0-9]+)([a-z])?"); 139 protected final RE RE_MSIE_WIN_LANG_OS = new RE("[wW]in(dows)? ([A-Z0-9]+) ?([0-9]\\.[0-9])?"); 140 protected final RE RE_MSIE_MAC_LANG_OS = new RE("Mac_PowerPC"); 141 protected final RE RE_NS_LANG_OS = new RE("\\[([a-z-]+)\\][ a-zA-Z0-9-]*\\(([a-zA-Z\\-]+)/?([0-9]* ?[.a-zA-Z0-9 ]*);"); 142 protected final RE RE_NS_X11_LANG_OS = new RE("\\(X11; U; ([a-zA-Z-]+) ([0-9\\.]+)[^\\);]+\\)"); 143 protected final RE RE_NS6_LANG_OS = new RE("\\(([a-zA-Z0-9]+); [a-zA-Z]+; ([a-zA-Z0-9_]+)( ([a-zA-Z0-9]+))?; ([_a-zA-Z-]+);"); 144 protected final RE RE_LANG = new RE("\\[([_a-zA-Z-]+)\\]"); 145 protected final RE RE_OPERA = new RE("((; )|\\()([a-zA-Z0-9\\-]+)[ ]+([a-zA-Z0-9\\.]+)([^;\\)]*)(; U)?\\) RE_OPERA ([0-9]+)\\.([0-9]+)[ ]+\\[([_a-zA-Z-]+)\\]"); 146 protected final RE RE_OPERA_LANG_OS = new RE("\\(([a-zA-Z0-9\\-]+) ([0-9\\.]+)[^)]+\\)[ \t]*\\[([a-z_]+)\\]"); 147 protected final RE RE_KONQUEROR_OS = new RE("Konqueror/([0-9\\.]+); ([a-zA-Z0-9\\-]+)"); 148 protected final RE RE_GALEON_OS = new RE("\\(([a-zA-Z0-9]+); U; Galeon; ([0-9]+)\\.([0-9]+);"); 149 protected final RE Gecko_Engine = new RE("Gecko/[0-9]*( ([a-zA-Z]+)+[0-9]*/([0-9]+)\\.([0-9]+)([a-zA-Z0-9]*))?"); 150 151 154 protected void detect() 155 throws org.apache.regexp.RESyntaxException { 156 if (agent == null || agent.length() == 0) 157 return; 158 String mav, miv, lang = null; 159 160 if (RE_START.match(agent)) { 161 browserName = RE_START.getParen(1); 162 mav = RE_START.getParen(3); 163 miv = RE_START.getParen(4); 164 165 168 if (RE_MSIE.match(agent)) { 169 browserName = "MSIE"; 170 browserType = BrowserType.IE; 171 mav = RE_MSIE.getParen(1); 172 miv = RE_MSIE.getParen(2); 173 release = RE_MSIE.getParen(3); 174 175 if (RE_MSIE_WIN_LANG_OS.match(agent)) { 176 osType = OSType.WINDOWS; 177 os = "Windows"; 178 osVersion = RE_MSIE_WIN_LANG_OS.getParen(2) + 179 (RE_MSIE_WIN_LANG_OS.getParen(3) == null ? 180 "" : 181 " " + RE_MSIE_WIN_LANG_OS.getParen(3)); 182 } else if (RE_MSIE_MAC_LANG_OS.match(agent)) { 183 os = "MacOS"; 184 osType = OSType.MACOS; 185 } 186 } 187 190 else if (browserName.equals("Mozilla") || browserName == null) { 191 browserName = "Mozilla"; 192 browserType = BrowserType.MOZILLA; 193 194 195 if (RE_NS_LANG_OS.match(agent)) { 196 lang = RE_NS_LANG_OS.getParen(1); 197 os = RE_NS_LANG_OS.getParen(2); 198 osVersion = RE_NS_LANG_OS.getParen(3); 199 200 if (os.equals("X")) { 201 if (RE_NS_X11_LANG_OS.match(agent)) { 202 os = RE_NS_X11_LANG_OS.getParen(1); 203 osVersion = RE_NS_X11_LANG_OS.getParen(2); 204 osType = OSType.UNIX; 205 } 206 } 207 } 208 209 else if (RE_GALEON_OS.match(agent)) { 210 browserName = "Galeon"; 211 browserType = BrowserType.GECKO; 212 os = RE_GALEON_OS.getParen(1); 213 if (os.equals("X11")) { 214 os = "Unix"; 215 osType = OSType.UNIX; 216 } 217 mav = RE_GALEON_OS.getParen(2); 218 miv = RE_GALEON_OS.getParen(3); 219 } else if (RE_NS6_LANG_OS.match(agent)) { 220 os = RE_NS6_LANG_OS.getParen(2); 221 lang = RE_NS6_LANG_OS.getParen(5); 222 } 223 224 else if (RE_MSIE_WIN_LANG_OS.match(agent)) { 225 os = "Windows"; 226 osType = OSType.WINDOWS; 227 osVersion = RE_MSIE_WIN_LANG_OS.getParen(2) + 228 (RE_MSIE_WIN_LANG_OS.getParen(3) == null ? 229 "" : 230 " " + RE_MSIE_WIN_LANG_OS.getParen(3)); 231 } 232 233 else if (RE_KONQUEROR_OS.match(agent)) { 234 browserName = "Konqueror"; 235 browserType = BrowserType.KONQUEROR; 236 StringTokenizer strtok = new StringTokenizer (RE_KONQUEROR_OS.getParen(1), "."); 237 mav = strtok.nextToken(); 238 if (strtok.hasMoreTokens()) 239 miv = strtok.nextToken(); 240 if (strtok.hasMoreTokens()) 241 release = strtok.nextToken(); 242 os = RE_KONQUEROR_OS.getParen(2); 243 } 244 245 else { 246 browserName = "Mozilla"; 247 browserType = BrowserType.MOZILLA; 248 } 249 250 251 if (os != null && os.startsWith("Win") && 252 (osVersion == null || osVersion.length() == 0) 253 ) { 254 osVersion = os.substring(3, os.length()); 255 os = "Windows"; 256 osType = OSType.WINDOWS; 257 } 258 259 if (os != null && os.equals("Win")) { 260 os = "Windows"; 261 osType = OSType.WINDOWS; 262 } 263 } 264 265 else if (browserName.equals("Opera")) { 266 browserType = BrowserType.OPERA; 267 if (RE_MSIE_WIN_LANG_OS.match(agent)) { 268 os = "Windows"; 269 osType = OSType.WINDOWS; 270 osVersion = RE_MSIE_WIN_LANG_OS.getParen(2) + 271 (RE_MSIE_WIN_LANG_OS.getParen(3) == null ? 272 "" : 273 " " + RE_MSIE_WIN_LANG_OS.getParen(3)); 274 } else if (RE_OPERA_LANG_OS.match(agent)) { 275 os = RE_OPERA_LANG_OS.getParen(1); 276 osVersion = RE_OPERA_LANG_OS.getParen(2); 277 lang = RE_OPERA_LANG_OS.getParen(3); 278 } 279 } 280 281 282 if (RE_OPERA.match(agent)) { 283 browserName = "Opera"; 284 browserType = BrowserType.OPERA; 285 os = RE_OPERA.getParen(3); 286 osVersion = RE_OPERA.getParen(4); 287 mav = RE_OPERA.getParen(7); 288 miv = RE_OPERA.getParen(8); 289 lang = RE_OPERA.getParen(10); 290 } 291 292 293 if (Gecko_Engine.match(agent)) { 294 browserType = BrowserType.GECKO; 295 if (Gecko_Engine.getParen(2) != null) 296 browserName = Gecko_Engine.getParen(2); 297 if (Gecko_Engine.getParen(3) != null) 298 mav = Gecko_Engine.getParen(3); 299 if (Gecko_Engine.getParen(4) != null) 300 miv = Gecko_Engine.getParen(4); 301 if (Gecko_Engine.getParen(5) != null) 302 release = Gecko_Engine.getParen(5); 303 } 304 305 306 if (lang == null) { 307 if (RE_LANG.match(agent)) { 308 lang = RE_LANG.getParen(1); 309 } 310 } 311 312 try { 313 majorVersion = new Integer (mav).intValue(); 314 } catch (NumberFormatException ex) { 315 majorVersion = 0; 316 } 317 318 try { 319 minorVersion = new Double ("0." + miv).doubleValue(); 320 } catch (NumberFormatException ex) { 321 minorVersion = 0f; 322 } 323 324 if (lang == null) 325 browserLocale = Locale.getDefault(); 326 else { 327 328 lang = lang.replace('-', '_'); 329 330 331 StringTokenizer strtok = new StringTokenizer (lang, "_"); 332 String l = strtok.nextToken(); 333 if (strtok.hasMoreElements()) 334 browserLocale = new Locale (l, strtok.nextToken()); 335 else 336 browserLocale = new Locale (l, ""); 337 } 338 339 if (osType == OSType.UNKNOWN && os != null) { 340 if (os.equals("Windows")) 341 osType = OSType.WINDOWS; 342 else if (os.equals("MacOS")) 343 osType = OSType.MACOS; 344 else if ( 345 os.equals("Linux") || 346 os.equals("AIX") || 347 os.equals("SunOS") || 348 os.equals("HP-UX") || 349 os.equals("Solaris") || 350 os.equals("BSD") 351 ) { 352 osType = OSType.UNIX; 353 } else if (os.equals("os")) { 354 osType = OSType.IBMOS; 355 } 356 } 357 } 358 } 359 360 363 public boolean supportsCssChildSelector() { 364 return browserType != BrowserType.IE; 365 } 366 367 370 public static void main(String [] args) { 371 try { 372 if (args.length != 1) { 373 System.err.println("Usage: java " + new Browser("").getClass().getName() + " <agents file>"); 374 return; 375 } 376 FileReader fi = new FileReader (args[0]); 377 LineNumberReader lnr = new LineNumberReader (fi); 378 String line; 379 while ((line = lnr.readLine()) != null) { 380 System.out.println(line); 381 System.out.println("\t" + new Browser(line).toString()); 382 } 383 fi.close(); 384 } catch (Exception ex) { 385 ex.printStackTrace(); 386 } 387 } 388 389 392 public String toString() { 393 return browserName + " v" + (majorVersion + minorVersion) + (release == null ? "" : "-" + release) + 394 "["+browserType+"], " + browserLocale + ", " + osType.getName() + ": " + os + " " + osVersion; 395 } 396 397 398 } 399 | Popular Tags |