001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.junit.viewer.client;
017:
018: /**
019: * Provides information about a browser (vendor,version,operating system,etc...)
020: * based on user agent and other easily accessible information.
021: *
022: * This is not meant to be a "detect script" to implement browser workarounds,
023: * but rather a "pretty printer" for the browser information.
024: *
025: * This code is a derivation of Browser Detect v2.1.6 documentation:
026: * http://www.dithered.com/javascript/browser_detect/index.html license:
027: * http://creativecommons.org/licenses/by/1.0/ code by Chris Nott
028: * (chris[at]dithered[dot]com)
029: *
030: * It has been transliterated from JavaScript to Java with additional changes
031: * along the way.
032: */
033: public class BrowserInfo {
034:
035: /**
036: * Retrieves a "pretty" version of the browser version information.
037: *
038: * @param userAgent - The HTTP user agent string.
039: * @return A pretty-printed version of the browser including the a) vendor b)
040: * version c) and operating system
041: */
042: public static String getBrowser(String userAgent) {
043:
044: userAgent = userAgent.toLowerCase();
045:
046: // browser engine name
047: boolean isGecko = userAgent.indexOf("gecko") != -1
048: && userAgent.indexOf("safari") == -1;
049: boolean isAppleWebKit = userAgent.indexOf("applewebkit") != -1;
050:
051: // browser name
052: boolean isKonqueror = userAgent.indexOf("konqueror") != -1;
053: boolean isSafari = userAgent.indexOf("safari") != -1;
054: boolean isOmniweb = userAgent.indexOf("omniweb") != -1;
055: boolean isOpera = userAgent.indexOf("opera") != -1;
056: boolean isIcab = userAgent.indexOf("icab") != -1;
057: boolean isAol = userAgent.indexOf("aol") != -1;
058: boolean isIE = userAgent.indexOf("msie") != -1 && !isOpera
059: && (userAgent.indexOf("webtv") == -1);
060: boolean isMozilla = isGecko
061: && userAgent.indexOf("gecko/") + 14 == userAgent
062: .length();
063: boolean isFirefox = userAgent.indexOf("firefox/") != -1
064: || userAgent.indexOf("firebird/") != -1;
065: boolean isNS = isGecko ? userAgent.indexOf("netscape") != -1
066: : userAgent.indexOf("mozilla") != -1 && !isOpera
067: && !isSafari
068: && userAgent.indexOf("spoofer") == -1
069: && userAgent.indexOf("compatible") == -1
070: && userAgent.indexOf("webtv") == -1
071: && userAgent.indexOf("hotjava") == -1;
072:
073: // spoofing and compatible browsers
074: boolean isIECompatible = userAgent.indexOf("msie") != -1
075: && !isIE;
076: boolean isNSCompatible = userAgent.indexOf("mozilla") != -1
077: && !isNS && !isMozilla;
078:
079: // rendering engine versions
080: String geckoVersion = isGecko ? userAgent.substring(userAgent
081: .lastIndexOf("gecko/") + 6, userAgent
082: .lastIndexOf("gecko/") + 14) : "-1";
083: String equivalentMozilla = isGecko ? userAgent
084: .substring(userAgent.indexOf("rv:") + 3) : "-1";
085: String appleWebKitVersion = isAppleWebKit ? userAgent
086: .substring(userAgent.indexOf("applewebkit/") + 12)
087: : "-1";
088:
089: // float versionMinor = parseFloat(navigator.appVersion);
090: String versionMinor = "";
091:
092: // correct version number
093: if (isGecko && !isMozilla) {
094: versionMinor = userAgent.substring(userAgent.indexOf("/",
095: userAgent.indexOf("gecko/") + 6) + 1);
096: } else if (isMozilla) {
097: versionMinor = userAgent
098: .substring(userAgent.indexOf("rv:") + 3);
099: } else if (isIE) {
100: versionMinor = userAgent.substring(userAgent
101: .indexOf("msie ") + 5);
102: } else if (isKonqueror) {
103: versionMinor = userAgent.substring(userAgent
104: .indexOf("konqueror/") + 10);
105: } else if (isSafari) {
106: versionMinor = userAgent.substring(userAgent
107: .lastIndexOf("safari/") + 7);
108: } else if (isOmniweb) {
109: versionMinor = userAgent.substring(userAgent
110: .lastIndexOf("omniweb/") + 8);
111: } else if (isOpera) {
112: versionMinor = userAgent.substring(userAgent
113: .indexOf("opera") + 6);
114: } else if (isIcab) {
115: versionMinor = userAgent.substring(userAgent
116: .indexOf("icab") + 5);
117: }
118:
119: String version = getVersion(versionMinor);
120:
121: // dom support
122: // boolean isDOM1 = (document.getElementById);
123: // boolean isDOM2Event = (document.addEventListener &&
124: // document.removeEventListener);
125:
126: // css compatibility mode
127: // this.mode = document.compatMode ? document.compatMode : "BackCompat";
128:
129: // platform
130: boolean isWin = userAgent.indexOf("win") != -1;
131: boolean isWin32 = isWin && userAgent.indexOf("95") != -1
132: || userAgent.indexOf("98") != -1
133: || userAgent.indexOf("nt") != -1
134: || userAgent.indexOf("win32") != -1
135: || userAgent.indexOf("32bit") != -1
136: || userAgent.indexOf("xp") != -1;
137:
138: boolean isMac = userAgent.indexOf("mac") != -1;
139: boolean isUnix = userAgent.indexOf("unix") != -1
140: || userAgent.indexOf("sunos") != -1
141: || userAgent.indexOf("bsd") != -1
142: || userAgent.indexOf("x11") != -1;
143:
144: boolean isLinux = userAgent.indexOf("linux") != -1;
145:
146: // specific browser shortcuts
147: /*
148: * this.isNS4x = (this.isNS && this.versionMajor == 4); this.isNS40x =
149: * (this.isNS4x && this.versionMinor < 4.5); this.isNS47x = (this.isNS4x &&
150: * this.versionMinor >= 4.7); this.isNS4up = (this.isNS && this.versionMinor >=
151: * 4); this.isNS6x = (this.isNS && this.versionMajor == 6); this.isNS6up =
152: * (this.isNS && this.versionMajor >= 6); this.isNS7x = (this.isNS &&
153: * this.versionMajor == 7); this.isNS7up = (this.isNS && this.versionMajor >=
154: * 7);
155: *
156: * this.isIE4x = (this.isIE && this.versionMajor == 4); this.isIE4up =
157: * (this.isIE && this.versionMajor >= 4); this.isIE5x = (this.isIE &&
158: * this.versionMajor == 5); this.isIE55 = (this.isIE && this.versionMinor ==
159: * 5.5); this.isIE5up = (this.isIE && this.versionMajor >= 5); this.isIE6x =
160: * (this.isIE && this.versionMajor == 6); this.isIE6up = (this.isIE &&
161: * this.versionMajor >= 6);
162: *
163: * this.isIE4xMac = (this.isIE4x && this.isMac);
164: */
165:
166: String name = isGecko ? "Gecko"
167: : isAppleWebKit ? "Apple WebKit"
168: : isKonqueror ? "Konqueror"
169: : isSafari ? "Safari"
170: : isOpera ? "Opera"
171: : isIE ? "IE"
172: : isMozilla ? "Mozilla"
173: : isFirefox ? "Firefox"
174: : isNS ? "Netscape"
175: : "";
176:
177: name += " "
178: + version
179: + " on "
180: + (isWin ? "Windows" : isMac ? "Mac" : isUnix ? "Unix"
181: : isLinux ? "Linux" : "Unknown");
182:
183: return name;
184: }
185:
186: // Reads the version from a string which begins with a version number
187: // and contains additional character data
188: private static String getVersion(String versionPlusCruft) {
189: for (int index = 0; index < versionPlusCruft.length(); ++index) {
190: char c = versionPlusCruft.charAt(index);
191: if (c != '.' && !Character.isDigit(c)) {
192: return versionPlusCruft.substring(0, index);
193: }
194: }
195: return versionPlusCruft;
196: }
197: }
|