1 12 13 package com.openedit.web; 14 15 import java.util.Locale ; 16 import java.util.StringTokenizer ; 17 18 19 24 public class Browser 25 { 26 public static final int UNKNOWN_BROWSER = 0; 27 public static final int MSIE_BROWSER = 1; 28 public static final int NETSCAPE_BROWSER = 2; 29 public static final int MOZILLA_BROWSER = 3; 30 public static final int OPERA_BROWSER = 4; 31 public static final int LYNX_BROWSER = 5; 32 public static final int UNKNOWN_PLATFORM = 0; 33 public static final int WINDOWS_PLATFORM = 1; 34 public static final int UNIX_PLATFORM = 2; 35 public static final int MAC_PLATFORM = 3; 36 public static final int IE_EDIT_WIDTH = 83; public static final int MOZILLA_EDIT_WIDTH = 100; protected String fieldMajorVersion = ""; 39 protected String fieldMinorVersion = ""; 40 protected String fieldUserAgent; 41 protected String fieldVersion = ""; 42 protected int fieldBrowserType = UNKNOWN_BROWSER; 43 protected int fieldPlatformType = UNKNOWN_PLATFORM; 44 protected Locale fieldLocale; 45 46 public Browser(String inUserAgent) 47 { 48 fieldUserAgent = inUserAgent; 49 parseUserAgent(); 50 } 51 52 57 public int getBrowserType() 58 { 59 return fieldBrowserType; 60 } 61 62 67 public boolean isMSIE() 68 { 69 return (getBrowserType() == MSIE_BROWSER); 70 } 71 72 77 public String getMajorVersion() 78 { 79 return fieldMajorVersion; 80 } 81 82 87 public String getMinorVersion() 88 { 89 return fieldMinorVersion; 90 } 91 92 97 public boolean isMozilla() 98 { 99 return (getBrowserType() == MOZILLA_BROWSER); 100 } 101 102 107 public String getNickName() 108 { 109 switch (getBrowserType()) 110 { 111 case MSIE_BROWSER: 112 return "MSIE"; 113 114 case NETSCAPE_BROWSER: 115 return "Navigator"; 116 117 case MOZILLA_BROWSER: 118 return "Mozilla"; 119 120 default: 121 return "Unknown"; 122 } 123 } 124 125 130 public int getPlatformType() 131 { 132 return fieldPlatformType; 133 } 134 135 140 public String getUserAgent() 141 { 142 return fieldUserAgent; 143 } 144 145 150 public String getVersion() 151 { 152 return fieldVersion; 153 } 154 155 161 public boolean isXMLEditorCompatible() 162 { 163 return (getBrowserType() == MOZILLA_BROWSER) || 164 ((getBrowserType() == MSIE_BROWSER) && (getPlatformType() == WINDOWS_PLATFORM) && 165 (getVersion().compareTo("5.5") >= 0)); 166 } 167 168 protected void setBrowserType(int inBrowserType) 169 { 170 fieldBrowserType = inBrowserType; 171 } 172 173 protected void setMajorVersion(String inMajorVersion) 174 { 175 fieldMajorVersion = inMajorVersion; 176 } 177 178 protected void setMinorVersion(String inMinorVersion) 179 { 180 fieldMinorVersion = inMinorVersion; 181 } 182 183 protected void setPlatformType(int inPlatformType) 184 { 185 fieldPlatformType = inPlatformType; 186 } 187 188 protected void setVersion(String inVersion) 189 { 190 fieldVersion = inVersion; 191 } 192 193 protected void parseUserAgent() 194 { 195 String preparens = ""; 199 String parenthesized = ""; 200 String postparens = ""; 201 202 if (getUserAgent() == null) 203 { 204 setBrowserType(UNKNOWN_BROWSER); 205 206 return; 207 } 208 209 int i = getUserAgent().indexOf("("); 210 211 if (i >= 0) 212 { 213 preparens = getUserAgent().substring(0, i).trim(); 214 parenthesized = getUserAgent().substring(i + 1, getUserAgent().length()); 215 216 int j = parenthesized.indexOf(")"); 217 218 if (j >= 0) 219 { 220 postparens = parenthesized.substring(j + 1); 221 parenthesized = parenthesized.substring(0, j); 222 } 223 } 224 else 225 { 226 preparens = getUserAgent(); 227 } 228 229 String browVer = preparens; 232 String leftover = ""; 233 234 if (postparens.indexOf("Gecko") >= 0) 237 { 238 browVer = "Gecko"; 239 } 240 241 StringTokenizer tokenizer = new StringTokenizer (parenthesized, ";"); 242 243 while (tokenizer.hasMoreTokens()) 244 { 245 String token = tokenizer.nextToken().trim(); 246 247 if (token == "compatible") 249 { 250 } 256 257 else if (token.indexOf("MSIE") >= 0) 259 { 260 browVer = token; 261 } 262 else if (token.indexOf("Opera") >= 0) 263 { 264 browVer = token; 265 } 266 else if (token.startsWith("rv:") && browVer.equals("Gecko")) 267 { 268 browVer = browVer + " " + token.substring("rv:".length()); 271 } 272 273 else if ( 275 (token.indexOf("X11") >= 0) || (token.indexOf("SunOS") >= 0) || 276 (token.indexOf("Linux") >= 0)) 277 { 278 setPlatformType(UNIX_PLATFORM); 279 } 280 else if (token.indexOf("Win") >= 0) 281 { 282 setPlatformType(WINDOWS_PLATFORM); 283 } 284 else if ((token.indexOf("Mac") >= 0) || (token.indexOf("PPC") >= 0)) 285 { 286 setPlatformType(MAC_PLATFORM); 287 } 288 } 289 290 int msieIndex = browVer.indexOf("MSIE"); 291 292 if (msieIndex >= 0) 293 { 294 browVer = browVer.substring(msieIndex, browVer.length()); 295 } 296 297 if (browVer.startsWith("Gecko")) 298 { 299 setBrowserType(MOZILLA_BROWSER); 300 301 if (browVer.equals("Gecko")) 302 { 303 leftover = "1.0.0"; 306 } 307 else 308 { 309 leftover = browVer.substring("Gecko".length() + 1); 310 } 311 } 312 else if (browVer.startsWith("Mozilla")) 313 { 314 setBrowserType(NETSCAPE_BROWSER); 316 leftover = browVer.substring("Mozilla".length() + 1); 317 } 318 else if (browVer.startsWith("Lynx")) 319 { 320 setBrowserType(LYNX_BROWSER); 321 leftover = browVer.substring("Lynx".length() + 1); 322 } 323 else if (browVer.startsWith("MSIE")) 324 { 325 setBrowserType(MSIE_BROWSER); 326 leftover = browVer.substring("MSIE".length() + 1); 327 } 328 else if (browVer.startsWith("Microsoft Internet Explorer")) 329 { 330 setBrowserType(MSIE_BROWSER); 331 leftover = browVer.substring("Microsoft Internet Explorer".length() + 1); 332 } 333 else if (browVer.startsWith("Opera")) 334 { 335 setBrowserType(OPERA_BROWSER); 336 leftover = browVer.substring("Opera".length() + 1); 337 } 338 339 leftover = leftover.trim(); 340 341 i = leftover.indexOf(" "); 343 344 if (i >= 0) 345 { 346 setVersion(leftover.substring(0, i)); 347 } 348 else 349 { 350 setVersion(leftover); 351 } 352 353 int j = getVersion().indexOf("."); 354 355 if (j >= 0) 356 { 357 setMajorVersion(getVersion().substring(0, j)); 358 359 int k = getVersion().indexOf(".", j + 1); 360 361 if (k < 0) 362 { 363 k = getVersion().length(); 364 } 365 366 setMinorVersion(getVersion().substring(j + 1, k)); 367 } 368 else 369 { 370 setMajorVersion(getVersion()); 371 setMinorVersion("0"); 372 } 373 } 374 375 public int getEditWidth() 376 { 377 if ( getBrowserType() == MSIE_BROWSER ) 378 { 379 return IE_EDIT_WIDTH; 380 } 381 else 382 { 383 return MOZILLA_EDIT_WIDTH; 384 } 385 } 386 387 public Locale getLocale() 388 { 389 return fieldLocale; 390 } 391 392 public void setLocale(Locale inLocale) 393 { 394 fieldLocale = inLocale; 395 } 396 } 397 | Popular Tags |