KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > junit > viewer > client > BrowserInfo


1 /*
2  * Copyright 2007 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.junit.viewer.client;
17
18 /**
19  * Provides information about a browser (vendor,version,operating system,etc...)
20  * based on user agent and other easily accessible information.
21  *
22  * This is not meant to be a "detect script" to implement browser workarounds,
23  * but rather a "pretty printer" for the browser information.
24  *
25  * This code is a derivation of Browser Detect v2.1.6 documentation:
26  * http://www.dithered.com/javascript/browser_detect/index.html license:
27  * http://creativecommons.org/licenses/by/1.0/ code by Chris Nott
28  * (chris[at]dithered[dot]com)
29  *
30  * It has been transliterated from JavaScript to Java with additional changes
31  * along the way.
32  */

33 public class BrowserInfo {
34
35   /**
36    * Retrieves a "pretty" version of the browser version information.
37    *
38    * @param userAgent - The HTTP user agent string.
39    * @return A pretty-printed version of the browser including the a) vendor b)
40    * version c) and operating system
41    */

42   public static String JavaDoc getBrowser(String JavaDoc userAgent) {
43
44     userAgent = userAgent.toLowerCase();
45
46     // browser engine name
47
boolean isGecko = userAgent.indexOf("gecko") != -1
48         && userAgent.indexOf("safari") == -1;
49     boolean isAppleWebKit = userAgent.indexOf("applewebkit") != -1;
50
51     // browser name
52
boolean isKonqueror = userAgent.indexOf("konqueror") != -1;
53     boolean isSafari = userAgent.indexOf("safari") != -1;
54     boolean isOmniweb = userAgent.indexOf("omniweb") != -1;
55     boolean isOpera = userAgent.indexOf("opera") != -1;
56     boolean isIcab = userAgent.indexOf("icab") != -1;
57     boolean isAol = userAgent.indexOf("aol") != -1;
58     boolean isIE = userAgent.indexOf("msie") != -1 && !isOpera
59         && (userAgent.indexOf("webtv") == -1);
60     boolean isMozilla = isGecko
61         && userAgent.indexOf("gecko/") + 14 == userAgent.length();
62     boolean isFirefox = userAgent.indexOf("firefox/") != -1
63         || userAgent.indexOf("firebird/") != -1;
64     boolean isNS = isGecko ? userAgent.indexOf("netscape") != -1
65         : userAgent.indexOf("mozilla") != -1 && !isOpera && !isSafari
66             && userAgent.indexOf("spoofer") == -1
67             && userAgent.indexOf("compatible") == -1
68             && userAgent.indexOf("webtv") == -1
69             && userAgent.indexOf("hotjava") == -1;
70
71     // spoofing and compatible browsers
72
boolean isIECompatible = userAgent.indexOf("msie") != -1 && !isIE;
73     boolean isNSCompatible = userAgent.indexOf("mozilla") != -1 && !isNS
74         && !isMozilla;
75
76     // rendering engine versions
77
String JavaDoc geckoVersion = isGecko ? userAgent.substring(
78         userAgent.lastIndexOf("gecko/") + 6,
79         userAgent.lastIndexOf("gecko/") + 14) : "-1";
80     String JavaDoc equivalentMozilla = isGecko
81         ? userAgent.substring(userAgent.indexOf("rv:") + 3) : "-1";
82     String JavaDoc appleWebKitVersion = isAppleWebKit
83         ? userAgent.substring(userAgent.indexOf("applewebkit/") + 12) : "-1";
84
85     // float versionMinor = parseFloat(navigator.appVersion);
86
String JavaDoc versionMinor = "";
87
88     // correct version number
89
if (isGecko && !isMozilla) {
90       versionMinor = userAgent.substring(userAgent.indexOf("/",
91           userAgent.indexOf("gecko/") + 6) + 1);
92     } else if (isMozilla) {
93       versionMinor = userAgent.substring(userAgent.indexOf("rv:") + 3);
94     } else if (isIE) {
95       versionMinor = userAgent.substring(userAgent.indexOf("msie ") + 5);
96     } else if (isKonqueror) {
97       versionMinor = userAgent.substring(userAgent.indexOf("konqueror/") + 10);
98     } else if (isSafari) {
99       versionMinor = userAgent.substring(userAgent.lastIndexOf("safari/") + 7);
100     } else if (isOmniweb) {
101       versionMinor = userAgent.substring(userAgent.lastIndexOf("omniweb/") + 8);
102     } else if (isOpera) {
103       versionMinor = userAgent.substring(userAgent.indexOf("opera") + 6);
104     } else if (isIcab) {
105       versionMinor = userAgent.substring(userAgent.indexOf("icab") + 5);
106     }
107
108     String JavaDoc version = getVersion(versionMinor);
109
110     // dom support
111
// boolean isDOM1 = (document.getElementById);
112
// boolean isDOM2Event = (document.addEventListener &&
113
// document.removeEventListener);
114

115     // css compatibility mode
116
// this.mode = document.compatMode ? document.compatMode : "BackCompat";
117

118     // platform
119
boolean isWin = userAgent.indexOf("win") != -1;
120     boolean isWin32 = isWin && userAgent.indexOf("95") != -1
121         || userAgent.indexOf("98") != -1 || userAgent.indexOf("nt") != -1
122         || userAgent.indexOf("win32") != -1 || userAgent.indexOf("32bit") != -1
123         || userAgent.indexOf("xp") != -1;
124
125     boolean isMac = userAgent.indexOf("mac") != -1;
126     boolean isUnix = userAgent.indexOf("unix") != -1
127         || userAgent.indexOf("sunos") != -1 || userAgent.indexOf("bsd") != -1
128         || userAgent.indexOf("x11") != -1;
129
130     boolean isLinux = userAgent.indexOf("linux") != -1;
131
132     // specific browser shortcuts
133
/*
134      * this.isNS4x = (this.isNS && this.versionMajor == 4); this.isNS40x =
135      * (this.isNS4x && this.versionMinor < 4.5); this.isNS47x = (this.isNS4x &&
136      * this.versionMinor >= 4.7); this.isNS4up = (this.isNS && this.versionMinor >=
137      * 4); this.isNS6x = (this.isNS && this.versionMajor == 6); this.isNS6up =
138      * (this.isNS && this.versionMajor >= 6); this.isNS7x = (this.isNS &&
139      * this.versionMajor == 7); this.isNS7up = (this.isNS && this.versionMajor >=
140      * 7);
141      *
142      * this.isIE4x = (this.isIE && this.versionMajor == 4); this.isIE4up =
143      * (this.isIE && this.versionMajor >= 4); this.isIE5x = (this.isIE &&
144      * this.versionMajor == 5); this.isIE55 = (this.isIE && this.versionMinor ==
145      * 5.5); this.isIE5up = (this.isIE && this.versionMajor >= 5); this.isIE6x =
146      * (this.isIE && this.versionMajor == 6); this.isIE6up = (this.isIE &&
147      * this.versionMajor >= 6);
148      *
149      * this.isIE4xMac = (this.isIE4x && this.isMac);
150      */

151
152     String JavaDoc name = isGecko ? "Gecko" : isAppleWebKit ? "Apple WebKit"
153         : isKonqueror ? "Konqueror" : isSafari ? "Safari" : isOpera ? "Opera"
154             : isIE ? "IE" : isMozilla ? "Mozilla" : isFirefox ? "Firefox"
155                 : isNS ? "Netscape" : "";
156
157     name += " "
158         + version
159         + " on "
160         + (isWin ? "Windows" : isMac ? "Mac" : isUnix ? "Unix" : isLinux
161             ? "Linux" : "Unknown");
162
163     return name;
164   }
165
166   // Reads the version from a string which begins with a version number
167
// and contains additional character data
168
private static String JavaDoc getVersion(String JavaDoc versionPlusCruft) {
169     for (int index = 0; index < versionPlusCruft.length(); ++index) {
170       char c = versionPlusCruft.charAt(index);
171       if (c != '.' && !Character.isDigit(c)) {
172         return versionPlusCruft.substring(0, index);
173       }
174     }
175     return versionPlusCruft;
176   }
177 }
178
Popular Tags