KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > web > Browser


1 /*
2 Copyright (c) 2003 eInnovation Inc. All rights reserved
3
4 This library is free software; you can redistribute it and/or modify it under the terms
5 of the GNU Lesser General Public License as published by the Free Software Foundation;
6 either version 2.1 of the License, or (at your option) any later version.
7
8 This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the GNU Lesser General Public License for more details.
11 */

12
13 package com.openedit.web;
14
15 import java.util.Locale JavaDoc;
16 import java.util.StringTokenizer JavaDoc;
17
18
19 /**
20  * This interface represents a particular type of browser.
21  *
22  * @author cburkey
23  */

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; // percentage width of edit iframe in IE
37
public static final int MOZILLA_EDIT_WIDTH = 100; // percentage width of edit iframe in Mozilla
38
protected String JavaDoc fieldMajorVersion = "";
39     protected String JavaDoc fieldMinorVersion = "";
40     protected String JavaDoc fieldUserAgent;
41     protected String JavaDoc fieldVersion = "";
42     protected int fieldBrowserType = UNKNOWN_BROWSER;
43     protected int fieldPlatformType = UNKNOWN_PLATFORM;
44     protected Locale JavaDoc fieldLocale;
45     
46     public Browser(String JavaDoc inUserAgent)
47     {
48         fieldUserAgent = inUserAgent;
49         parseUserAgent();
50     }
51
52     /**
53      * Retrieve the browser type.
54      *
55      * @return One of the <code>BROWSER</code> constants
56      */

57     public int getBrowserType()
58     {
59         return fieldBrowserType;
60     }
61
62     /**
63      * Determine if the browser is a Microsoft browser.
64      *
65      * @return
66      */

67     public boolean isMSIE()
68     {
69         return (getBrowserType() == MSIE_BROWSER);
70     }
71
72     /**
73      * Retrieve the major version (if one could be found).
74      *
75      * @return
76      */

77     public String JavaDoc getMajorVersion()
78     {
79         return fieldMajorVersion;
80     }
81
82     /**
83      * Retrieve the minor version (if one could be found).
84      *
85      * @return
86      */

87     public String JavaDoc getMinorVersion()
88     {
89         return fieldMinorVersion;
90     }
91
92     /**
93      * Determine whether the browser is a Mozilla derivative (including Netscape 6 and 7).
94      *
95      * @return
96      */

97     public boolean isMozilla()
98     {
99         return (getBrowserType() == MOZILLA_BROWSER);
100     }
101
102     /**
103      * Retrieve this browser's nickname.
104      *
105      * @return This browser's nickname
106      */

107     public String JavaDoc 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     /**
126      * Retrieve the platform type.
127      *
128      * @return One of the <code>PLATFORM</code> constants
129      */

130     public int getPlatformType()
131     {
132         return fieldPlatformType;
133     }
134
135     /**
136      * Retrieve the user agent string that we used to determine the information in this browser.
137      *
138      * @return
139      */

140     public String JavaDoc getUserAgent()
141     {
142         return fieldUserAgent;
143     }
144
145     /**
146      * Retrieve the browser version.
147      *
148      * @return
149      */

150     public String JavaDoc getVersion()
151     {
152         return fieldVersion;
153     }
154
155     /**
156      * FIXME: This is a temporary method needed because Velocity can't handle static fields (it
157      * would seem). This should disappear when we fix bug WSP35.
158      *
159      * @return
160      */

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 JavaDoc inMajorVersion)
174     {
175         fieldMajorVersion = inMajorVersion;
176     }
177
178     protected void setMinorVersion(String JavaDoc inMinorVersion)
179     {
180         fieldMinorVersion = inMinorVersion;
181     }
182
183     protected void setPlatformType(int inPlatformType)
184     {
185         fieldPlatformType = inPlatformType;
186     }
187
188     protected void setVersion(String JavaDoc inVersion)
189     {
190         fieldVersion = inVersion;
191     }
192
193     protected void parseUserAgent()
194     {
195         // NOTE: This code was adapted from the WebMonkey code at
196
// http://hotwired.lycos.com/webmonkey/reference/javascript_code_library/rb_browser_detect/
197
// Split into stuff before parens and stuff in parens.
198
String JavaDoc preparens = "";
199         String JavaDoc parenthesized = "";
200         String JavaDoc 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         // First assume browser and version are in preparens.
230
// We will override them later if we find them in the parenthesized stuff.
231
String JavaDoc browVer = preparens;
232         String JavaDoc leftover = "";
233
234         // Mozilla and Netscape 6+ have "Gecko/20020530" (or whatever) after
235
// the closing parenthesis on the user agent string.
236
if (postparens.indexOf("Gecko") >= 0)
237         {
238             browVer = "Gecko";
239         }
240
241         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(parenthesized, ";");
242
243         while (tokenizer.hasMoreTokens())
244         {
245             String JavaDoc token = tokenizer.nextToken().trim();
246
247             // Now go through parenthesized tokens.
248
if (token == "compatible")
249             {
250                 // compatible - might want to reset from Netscape
251
// One might want to reset browVer to a null string
252
// here, but instead, we'll assume that if we don't
253
// find out otherwise, then it really is Mozilla
254
// (or whatever showed up before the parens).
255
}
256
257             // browser - try for Opera or IE
258
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                 // Set browVer to something like "Gecko 1.0.0", for parsing
269
// down below.
270
browVer = browVer + " " + token.substring("rv:".length());
271             }
272
273             // platform - try for X11, SunOS, Win, Mac, PPC
274
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                 // We didn't get a version name (e.g. Galeon), so assume it's
304
// 1.0.0.
305
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             // FIXME: What about Mozilla (as opposed to Netscape)?
315
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         // # Try to get version info out of leftover stuff
342
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 JavaDoc getLocale()
388     {
389         return fieldLocale;
390     }
391
392     public void setLocale(Locale JavaDoc inLocale)
393     {
394         fieldLocale = inLocale;
395     }
396 }
397
Popular Tags