KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > util > BrowserDetector


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

18
19 /**
20  * This class parses the user agent string and sets javasciptOK and
21  * cssOK following the rules described below. If you want to check
22  * for specific browsers/versions then use this class to parse the
23  * user agent string and use the accessor methods in this class.
24  *
25  * JavaScriptOK means that the browser understands JavaScript on the
26  * same level the Navigator 3 does. Specifically, it can use named
27  * images. This allows easier rollovers. If a browser doesn't do
28  * this (Nav 2 or MSIE 3), then we just assume it can't do any
29  * JavaScript. Referencing images by load order is too hard to
30  * maintain.
31  *
32  * CSSOK is kind of sketchy in that Nav 4 and MSIE work differently,
33  * but they do seem to have most of the functionality. MSIE 4 for the
34  * Mac has buggy CSS support, so we let it do JavaScript, but no CSS.
35  *
36  * Ported from Leon's PHP code at
37  * http://www.working-dogs.com/freetrade by Frank.
38  *
39  * @author <a HREF="mailto:frank.kim@clearink.com">Frank Y. Kim</a>
40  * @author <a HREF="mailto:leon@clearink.com">Leon Atkisnon</a>
41  * @author <a HREF="mailto:mospaw@polk-county.com">Chris Mospaw</a>
42  * @author <a HREF="mailto:bgriffin@cddb.com">Benjamin Elijah Griffin</a>
43  * @version $Id: BrowserDetector.java,v 1.4.2.3 2004/08/03 05:16:15 seade Exp $
44  */

45 public class BrowserDetector
46 {
47     public static final String JavaDoc MSIE = "MSIE";
48     public static final String JavaDoc OPERA = "Opera";
49     public static final String JavaDoc MOZILLA = "Mozilla";
50
51     public static final String JavaDoc WINDOWS = "Windows";
52     public static final String JavaDoc UNIX = "Unix";
53     public static final String JavaDoc MACINTOSH = "Macintosh";
54
55     /** The user agent string. */
56     private String JavaDoc userAgentString = "";
57
58     /** The browser name specified in the user agent string. */
59     private String JavaDoc browserName = "";
60
61     /**
62      * The browser version specified in the user agent string. If we
63      * can't parse the version just assume an old browser.
64      */

65     private float browserVersion = (float) 1.0;
66
67     /**
68      * The browser platform specified in the user agent string.
69      */

70     private String JavaDoc browserPlatform = "unknown";
71
72     /** Whether or not javascript works in this browser. */
73     private boolean javascriptOK = false;
74
75     /** Whether or not CSS works in this browser. */
76     private boolean cssOK = false;
77
78     /** Whether or not file upload works in this browser. */
79     private boolean fileUploadOK = false;
80
81     /**
82      * Constructor used to initialize this class.
83      *
84      * @param userAgentString A String with the user agent field.
85      */

86     public BrowserDetector(String JavaDoc userAgentString)
87     {
88         this.userAgentString = userAgentString;
89         parse();
90     }
91
92     /**
93      * Constructor used to initialize this class.
94      *
95      * @param data The Turbine RunData object.
96      */

97     public BrowserDetector(RunData data)
98     {
99         this.userAgentString = data.getUserAgent();
100         parse();
101     }
102
103     /**
104      * Whether or not CSS works in this browser.
105      *
106      * @return True if CSS works in this browser.
107      */

108     public boolean isCssOK()
109     {
110         return cssOK;
111     }
112
113     /**
114      * Whether or not file upload works in this browser.
115      *
116      * @return True if file upload works in this browser.
117      */

118     public boolean isFileUploadOK()
119     {
120         return fileUploadOK;
121     }
122
123     /**
124      * Whether or not Javascript works in this browser.
125      *
126      * @return True if Javascript works in this browser.
127      */

128     public boolean isJavascriptOK()
129     {
130         return javascriptOK;
131     }
132
133     /**
134      * The browser name specified in the user agent string.
135      *
136      * @return A String with the browser name.
137      */

138     public String JavaDoc getBrowserName()
139     {
140         return browserName;
141     }
142
143     /**
144      * The browser platform specified in the user agent string.
145      *
146      * @return A String with the browser platform.
147      */

148     public String JavaDoc getBrowserPlatform()
149     {
150         return browserPlatform;
151     }
152
153     /**
154      * The browser version specified in the user agent string.
155      *
156      * @return A String with the browser version.
157      */

158     public float getBrowserVersion()
159     {
160         return browserVersion;
161     }
162
163     /**
164      * The user agent string for this class.
165      *
166      * @return A String with the user agent.
167      */

168     public String JavaDoc getUserAgentString()
169     {
170         return userAgentString;
171     }
172
173     /**
174      * Helper method to initialize this class.
175      */

176     private void parse()
177     {
178         int versionStartIndex = userAgentString.indexOf("/");
179         int versionEndIndex = userAgentString.indexOf(" ");
180
181         // Get the browser name and version.
182
browserName = userAgentString.substring(0, versionStartIndex);
183         try
184         {
185             // Not all user agents will have a space in the reported
186
// string.
187
String JavaDoc agentSubstring = null;
188             if (versionEndIndex < 0)
189             {
190                 agentSubstring
191                         = userAgentString.substring(versionStartIndex + 1);
192             }
193             else
194             {
195                 agentSubstring = userAgentString
196                         .substring(versionStartIndex + 1, versionEndIndex);
197             }
198             browserVersion = toFloat(agentSubstring);
199         }
200         catch (NumberFormatException JavaDoc e)
201         {
202             // Just use the default value.
203
}
204
205         // MSIE lies about its name. Of course...
206
if (userAgentString.indexOf(MSIE) != -1)
207         {
208             // Ex: Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)
209
versionStartIndex = (userAgentString.indexOf(MSIE)
210                     + MSIE.length() + 1);
211             versionEndIndex = userAgentString.indexOf(";", versionStartIndex);
212
213             browserName = MSIE;
214             try
215             {
216                 browserVersion = toFloat(userAgentString
217                         .substring(versionStartIndex, versionEndIndex));
218             }
219             catch (NumberFormatException JavaDoc e)
220             {
221                 // Just use the default value.
222
}
223
224             // PHP code
225
// $Browser_Name = "MSIE";
226
// $Browser_Version = strtok("MSIE");
227
// $Browser_Version = strtok(" ");
228
// $Browser_Version = strtok(";");
229
}
230
231         // Opera isn't completely honest, either...
232
// Modificaton by Chris Mospaw <mospaw@polk-county.com>
233
if (userAgentString.indexOf(OPERA) != -1)
234         {
235             //Ex: Mozilla/4.0 (Windows NT 4.0;US) Opera 3.61 [en]
236
versionStartIndex = (userAgentString.indexOf(OPERA)
237                     + OPERA.length() + 1);
238             versionEndIndex = userAgentString.indexOf(" ", versionStartIndex);
239
240             browserName = OPERA;
241             try
242             {
243                 browserVersion = toFloat(userAgentString
244                         .substring(versionStartIndex, versionEndIndex));
245             }
246             catch (NumberFormatException JavaDoc e)
247             {
248                 // Just use the default value.
249
}
250
251             // PHP code
252
// $Browser_Name = "Opera";
253
// $Browser_Version = strtok("Opera");
254
// $Browser_Version = strtok("/");
255
// $Browser_Version = strtok(";");
256
}
257
258
259         // Try to figure out what platform.
260
if ((userAgentString.indexOf("Windows") != -1)
261                 || (userAgentString.indexOf("WinNT") != -1)
262                 || (userAgentString.indexOf("Win98") != -1)
263                 || (userAgentString.indexOf("Win95") != -1))
264         {
265             browserPlatform = WINDOWS;
266         }
267
268         if (userAgentString.indexOf("Mac") != -1)
269         {
270             browserPlatform = MACINTOSH;
271         }
272
273         if (userAgentString.indexOf("X11") != -1)
274         {
275             browserPlatform = UNIX;
276         }
277
278         if (browserPlatform == WINDOWS)
279         {
280             if (browserName.equals(MOZILLA))
281             {
282                 if (browserVersion >= 3.0)
283                 {
284                     javascriptOK = true;
285                     fileUploadOK = true;
286                 }
287                 if (browserVersion >= 4.0)
288                 {
289                     cssOK = true;
290                 }
291             }
292             else if (browserName == MSIE)
293             {
294                 if (browserVersion >= 4.0)
295                 {
296                     javascriptOK = true;
297                     fileUploadOK = true;
298                     cssOK = true;
299                 }
300             }
301             else if (browserName == OPERA)
302             {
303                 if (browserVersion >= 3.0)
304                 {
305                     javascriptOK = true;
306                     fileUploadOK = true;
307                     cssOK = true;
308                 }
309             }
310         }
311         else if (browserPlatform == MACINTOSH)
312         {
313             if (browserName.equals(MOZILLA))
314             {
315                 if (browserVersion >= 3.0)
316                 {
317                     javascriptOK = true;
318                     fileUploadOK = true;
319                 }
320                 if (browserVersion >= 4.0)
321                 {
322                     cssOK = true;
323                 }
324             }
325             else if (browserName == MSIE)
326             {
327                 if (browserVersion >= 4.0)
328                 {
329                     javascriptOK = true;
330                     fileUploadOK = true;
331                 }
332                 if (browserVersion > 4.0)
333                 {
334                     cssOK = true;
335                 }
336             }
337         }
338         else if (browserPlatform == UNIX)
339         {
340             if (browserName.equals(MOZILLA))
341             {
342                 if (browserVersion >= 3.0)
343                 {
344                     javascriptOK = true;
345                     fileUploadOK = true;
346                 }
347                 if (browserVersion >= 4.0)
348                 {
349                     cssOK = true;
350                 }
351             }
352         }
353     }
354
355     /**
356      * Helper method to convert String to a float.
357      *
358      * @param s A String.
359      * @return The String converted to float.
360      */

361     private static final float toFloat(String JavaDoc s)
362     {
363         return Float.valueOf(s).floatValue();
364     }
365
366 }
367
Popular Tags