001: /*
002: * Copyright (c) 2002-2008 Gargoyle Software Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * 1. Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: * 2. Redistributions in binary form must reproduce the above copyright notice,
010: * this list of conditions and the following disclaimer in the documentation
011: * and/or other materials provided with the distribution.
012: * 3. The end-user documentation included with the redistribution, if any, must
013: * include the following acknowledgment:
014: *
015: * "This product includes software developed by Gargoyle Software Inc.
016: * (http://www.GargoyleSoftware.com/)."
017: *
018: * Alternately, this acknowledgment may appear in the software itself, if
019: * and wherever such third-party acknowledgments normally appear.
020: * 4. The name "Gargoyle Software" must not be used to endorse or promote
021: * products derived from this software without prior written permission.
022: * For written permission, please contact info@GargoyleSoftware.com.
023: * 5. Products derived from this software may not be called "HtmlUnit", nor may
024: * "HtmlUnit" appear in their name, without prior written permission of
025: * Gargoyle Software Inc.
026: *
027: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
028: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
029: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
030: * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
031: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
032: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
033: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
034: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
035: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
036: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
037: */
038: package com.gargoylesoftware.htmlunit;
039:
040: import java.io.Serializable;
041: import java.util.HashSet;
042: import java.util.Set;
043:
044: import org.apache.commons.lang.builder.EqualsBuilder;
045: import org.apache.commons.lang.builder.HashCodeBuilder;
046:
047: /**
048: * Objects of this class represent one specific version of a given browser. Predefined
049: * constants are provided for common browser versions.
050: *
051: * If you wish to create a BrowserVersion for a browser that doesn't have a constant defined
052: * but aren't sure what values to pass into the constructor then point your browser at
053: * <a href="http://htmlunit.sourceforge.net/cgi-bin/browserVersion">
054: * http://htmlunit.sourceforge.net/cgi-bin/browserVersion</a>
055: * and the code will be generated for you.
056: *
057: * @version $Revision: 2142 $
058: * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
059: * @author Daniel Gredler
060: * @author Marc Guillemot
061: * @author Chris Erskine
062: * @author Ahmed Ashour
063: */
064: public class BrowserVersion implements Serializable {
065:
066: private static final long serialVersionUID = 594005988985654117L;
067:
068: private String applicationCodeName_ = APP_CODE_NAME;
069: private String applicationMinorVersion_ = "0";
070: private String applicationName_;
071: private String applicationVersion_;
072: private String browserLanguage_ = LANGUAGE_ENGLISH_US;
073: private String cpuClass_ = CPU_CLASS_X86;
074: private boolean onLine_ = true;
075: private String platform_ = PLATFORM_WIN32;
076: private String systemLanguage_ = LANGUAGE_ENGLISH_US;
077: private String userAgent_;
078: private String userLanguage_ = LANGUAGE_ENGLISH_US;
079: private String javaScriptVersion_;
080: private float javaScriptVersionNumeric_;
081: private float browserVersionNumeric_;
082: private Set plugins_ = new HashSet();
083:
084: /** Application code name for both Microsoft Internet Explorer and Netscape series */
085: public static final String APP_CODE_NAME = "Mozilla";
086:
087: /** Application name for the Microsoft Internet Explorer series of browsers */
088: public static final String INTERNET_EXPLORER = "Microsoft Internet Explorer";
089:
090: /** Application name the Netscape navigator series of browsers */
091: public static final String NETSCAPE = "Netscape";
092:
093: /** United States English language identifier. */
094: public static final String LANGUAGE_ENGLISH_US = "en-us";
095:
096: /** The X86 CPU class. */
097: public static final String CPU_CLASS_X86 = "x86";
098:
099: /** The WIN32 platform. */
100: public static final String PLATFORM_WIN32 = "Win32";
101:
102: /**
103: * A fake browser that supports all the new features. This constant is used whenever
104: * you don't care which browser is being simulated.
105: * @deprecated please use INTERNET_EXPLORER_7_0
106: */
107: public static final BrowserVersion FULL_FEATURED_BROWSER = new BrowserVersion(
108: INTERNET_EXPLORER,
109: "4.0 (compatible; MSIE 6.0b; Windows 98)",
110: "Mozilla/4.0 (compatible; MSIE 6.0b; Windows 98)", "1.2", 6);
111:
112: /**
113: * Mozilla 1.0
114: * @deprecated please use FIREFOX_2
115: */
116: public static final BrowserVersion MOZILLA_1_0 = new BrowserVersion(
117: NETSCAPE,
118: "5.0 (Windows; en-US)",
119: "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.0.0) Gecko/20020530",
120: "1.2", 6);
121:
122: /**
123: * Netscape 4.79
124: * @deprecated please use FIREFOX_2
125: */
126: public static final BrowserVersion NETSCAPE_4_7_9 = new BrowserVersion(
127: NETSCAPE, "4.79 [en] (Windows NT 5.0; U)",
128: "Mozilla/4.79 [en] (Windows NT 5.0; U)", "1.2", 4.79f);
129:
130: /**
131: * Netscape 6.2.3
132: * @deprecated please use FIREFOX_2
133: */
134: public static final BrowserVersion NETSCAPE_6_2_3 = new BrowserVersion(
135: NETSCAPE,
136: "5.0 (Windows; en-US)",
137: "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US;rv:0.9.4.1) Gecko/20020508 Netscape6/6.2.3",
138: "1.2", 6);
139:
140: /** Firefox 2 */
141: public static final BrowserVersion FIREFOX_2 = new BrowserVersion(
142: NETSCAPE,
143: "5.0 (Windows; en-US)",
144: "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4",
145: "1.2", 6);
146:
147: static {
148: final PluginConfiguration pluginFlash = new PluginConfiguration(
149: "Shockwave Flash", "Shockwave Flash 9.0 r31",
150: "libflashplayer.so");
151: pluginFlash.getMimeTypes().add(
152: new PluginConfiguration.MimeType(
153: "application/x-shockwave-flash",
154: "Shockwave Flash", "swf"));
155: FIREFOX_2.getPlugins().add(pluginFlash);
156: }
157:
158: /** Internet Explorer 6 */
159: public static final BrowserVersion INTERNET_EXPLORER_6_0 = new BrowserVersion(
160: INTERNET_EXPLORER,
161: "4.0 (compatible; MSIE 6.0b; Windows 98)",
162: "4.0 (compatible; MSIE 6.0; Windows 98)", "1.2", 6);
163:
164: /** Internet Explorer 7 */
165: public static final BrowserVersion INTERNET_EXPLORER_7_0 = new BrowserVersion(
166: INTERNET_EXPLORER,
167: "4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322)",
168: "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322)",
169: "1.2", 7);
170:
171: /** The default browser version. */
172: private static BrowserVersion DefaultBrowserVersion_ = INTERNET_EXPLORER_6_0;
173:
174: /**
175: * Instantiate one.
176: *
177: * @param applicationName The name of the application
178: * @param applicationVersion The version string of the application
179: * @param userAgent The user agent string that will be sent to the server
180: * @param javaScriptVersion The version of JavaScript
181: * @param browserVersionNumeric The floating number version of the browser
182: */
183: public BrowserVersion(final String applicationName,
184: final String applicationVersion, final String userAgent,
185: final String javaScriptVersion,
186: final float browserVersionNumeric) {
187:
188: applicationName_ = applicationName;
189: setApplicationVersion(applicationVersion);
190: userAgent_ = userAgent;
191: setJavaScriptVersion(javaScriptVersion);
192: browserVersionNumeric_ = browserVersionNumeric;
193: }
194:
195: /**
196: * Returns the default browser version that is used whenever a specific version isn't specified.
197: * Defaults to {@link #INTERNET_EXPLORER_6_0}.
198: * @return The default browser version.
199: */
200: public static BrowserVersion getDefault() {
201: return DefaultBrowserVersion_;
202: }
203:
204: /**
205: * Sets the default browser version that is used whenever a specific version isn't specified.
206: * @param newBrowserVersion The new default browser version.
207: */
208: public static void setDefault(final BrowserVersion newBrowserVersion) {
209: Assert.notNull("newBrowserVersion", newBrowserVersion);
210: DefaultBrowserVersion_ = newBrowserVersion;
211: }
212:
213: /**
214: * Returns <tt>true</tt> if this <tt>BrowserVersion</tt> instance represents some
215: * version of Microsoft Internet Explorer.
216: * @return Whether or not this version is a version of IE.
217: */
218: public final boolean isIE() {
219: return INTERNET_EXPLORER.equals(getApplicationName());
220: }
221:
222: /**
223: * Returns <tt>true</tt> if this <tt>BrowserVersion</tt> instance represents some
224: * version of a Netscape browser, including Mozilla and Firefox.
225: * @return Whether or not this version is a version of a Netscape browser.
226: */
227: public final boolean isNetscape() {
228: return NETSCAPE.equals(getApplicationName());
229: }
230:
231: /**
232: * Return the application code name, for example "Mozilla".
233: * Default value is {@link #APP_CODE_NAME} if not explicitly configured.
234: * @return The application code name.
235: * @see <a href="http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/appcodename.asp">
236: * MSDN documentation</a>
237: */
238: public String getApplicationCodeName() {
239: return applicationCodeName_;
240: }
241:
242: /**
243: * Return the application minor version, for example "0".
244: * Default value is "0" if not explicitly configured.
245: * @return The application minor version.
246: * @see <a href="http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/appminorversion.asp">
247: * MSDN documentation</a>
248: */
249: public String getApplicationMinorVersion() {
250: return applicationMinorVersion_;
251: }
252:
253: /**
254: * Return the application name, for example "Microsoft Internet Explorer".
255: * @return The application name.
256: * @see <a href="http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/appname.asp">
257: * MSDN documentation</a>
258: */
259: public String getApplicationName() {
260: return applicationName_;
261: }
262:
263: /**
264: * Return the application version, for example "4.0 (compatible; MSIE 6.0b; Windows 98)".
265: * @return The application version.
266: * @see <a href="http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/appversion.asp">
267: * MSDN documentation</a>
268: */
269: public String getApplicationVersion() {
270: return applicationVersion_;
271: }
272:
273: /**
274: * Return the browser application language, for example "en-us".
275: * Default value is {@link #LANGUAGE_ENGLISH_US} if not explicitly configured.
276: * @return The browser application language.
277: * @see <a href="http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/browserlanguage.asp">
278: * MSDN documentation</a>
279: */
280: public String getBrowserLanguage() {
281: return browserLanguage_;
282: }
283:
284: /**
285: * Return the type of CPU in the machine, for example "x86".
286: * Default value is {@link #CPU_CLASS_X86} if not explicitly configured.
287: * @return The type of CPU in the machine.
288: * @see <a href="http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/cpuclass.asp">
289: * MSDN documentation</a>
290: */
291: public String getCpuClass() {
292: return cpuClass_;
293: }
294:
295: /**
296: * Return <tt>true</tt> if the browser is currently online.
297: * Default value is <code>true</code> if not explicitly configured.
298: * @return <tt>true</tt> if the browser is currently online.
299: * @see <a href="http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/online.asp">
300: * MSDN documentation</a>
301: */
302: public boolean isOnLine() {
303: return onLine_;
304: }
305:
306: /**
307: * Return the platform on which the application is running, for example "Win32".
308: * Default value is {@link #PLATFORM_WIN32} if not explicitly configured.
309: * @return the platform on which the application is running.
310: * @see <a href="http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/platform.asp">
311: * MSDN documentation</a>
312: */
313: public String getPlatform() {
314: return platform_;
315: }
316:
317: /**
318: * Return the system language, for example "en-us".
319: * Default value is {@link #LANGUAGE_ENGLISH_US} if not explicitly configured.
320: * @return The system language.
321: * @see <a href="http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/systemlanguage.asp">
322: * MSDN documentation</a>
323: */
324: public String getSystemLanguage() {
325: return systemLanguage_;
326: }
327:
328: /**
329: * Return the user agent string, for example "Mozilla/4.0 (compatible; MSIE 6.0b; Windows 98)".
330: * @return The user agent string.
331: */
332: public String getUserAgent() {
333: return userAgent_;
334: }
335:
336: /**
337: * Return the user language, for example "en-us".
338: * Default value is {@link #LANGUAGE_ENGLISH_US} if not explicitly configured.
339: * @return The user language.
340: * @see <a href="http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/userlanguage.asp">
341: * MSDN documentation</a>
342: */
343: public String getUserLanguage() {
344: return userLanguage_;
345: }
346:
347: /**
348: * Return the version of javascript used by the browser, for example "1.2".
349: * @return the version of javascript used by the browser.
350: */
351: public String getJavaScriptVersion() {
352: return javaScriptVersion_;
353: }
354:
355: /**
356: * @param applicationCodeName The applicationCodeName to set.
357: */
358: public void setApplicationCodeName(final String applicationCodeName) {
359: applicationCodeName_ = applicationCodeName;
360: }
361:
362: /**
363: * @param applicationMinorVersion The applicationMinorVersion to set.
364: */
365: public void setApplicationMinorVersion(
366: final String applicationMinorVersion) {
367: applicationMinorVersion_ = applicationMinorVersion;
368: }
369:
370: /**
371: * @param applicationName The applicationName to set.
372: */
373: public void setApplicationName(final String applicationName) {
374: applicationName_ = applicationName;
375: }
376:
377: /**
378: * @param applicationVersion The applicationVersion to set.
379: */
380: public void setApplicationVersion(final String applicationVersion) {
381: applicationVersion_ = applicationVersion;
382: }
383:
384: /**
385: * @param browserLanguage The browserLanguage to set.
386: */
387: public void setBrowserLanguage(final String browserLanguage) {
388: browserLanguage_ = browserLanguage;
389: }
390:
391: /**
392: * @param cpuClass The cpuClass to set.
393: */
394: public void setCpuClass(final String cpuClass) {
395: cpuClass_ = cpuClass;
396: }
397:
398: /**
399: * @param javaScriptVersion The javaScriptVersion to set.
400: */
401: public void setJavaScriptVersion(final String javaScriptVersion) {
402: javaScriptVersion_ = javaScriptVersion;
403: javaScriptVersionNumeric_ = Float.parseFloat(javaScriptVersion);
404: }
405:
406: /**
407: * @param onLine The onLine to set.
408: */
409: public void setOnLine(final boolean onLine) {
410: onLine_ = onLine;
411: }
412:
413: /**
414: * @param platform The platform to set.
415: */
416: public void setPlatform(final String platform) {
417: platform_ = platform;
418: }
419:
420: /**
421: * @param systemLanguage The systemLanguage to set.
422: */
423: public void setSystemLanguage(final String systemLanguage) {
424: systemLanguage_ = systemLanguage;
425: }
426:
427: /**
428: * @param userAgent The userAgent to set.
429: */
430: public void setUserAgent(final String userAgent) {
431: userAgent_ = userAgent;
432: }
433:
434: /**
435: * @param userLanguage The userLanguage to set.
436: */
437: public void setUserLanguage(final String userLanguage) {
438: userLanguage_ = userLanguage;
439: }
440:
441: /**
442: * @param browserVersion The browserVersion to set.
443: */
444: public void setBrowserVersion(final float browserVersion) {
445: browserVersionNumeric_ = browserVersion;
446: }
447:
448: /**
449: * @return Returns the browserVersionNumeric.
450: */
451: public float getBrowserVersionNumeric() {
452: return browserVersionNumeric_;
453: }
454:
455: /**
456: * @return Returns the javaScriptVersionNumeric.
457: */
458: public float getJavaScriptVersionNumeric() {
459: return javaScriptVersionNumeric_;
460: }
461:
462: /**
463: * {@inheritDoc}
464: */
465: public boolean equals(final Object o) {
466: return EqualsBuilder.reflectionEquals(this , o);
467: }
468:
469: /**
470: * {@inheritDoc}
471: */
472: public int hashCode() {
473: return HashCodeBuilder.reflectionHashCode(this );
474: }
475:
476: /**
477: * Gets the configured plugins. This makes only sense for Firefox as only this browser makes this kind
478: * of information available through javascript
479: * @return the available plugins
480: */
481: public Set getPlugins() {
482: return plugins_;
483: }
484: }
|