1 package org.apache.turbine.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 264152 2005-08-29 14:50:22Z henning $
44 */
45 public class BrowserDetector
46 {
47 public static final String MSIE = "MSIE";
48 public static final String OPERA = "Opera";
49 public static final String MOZILLA = "Mozilla";
50
51 public static final String WINDOWS = "Windows";
52 public static final String UNIX = "Unix";
53 public static final String MACINTOSH = "Macintosh";
54
55 /*** The user agent string. */
56 private String userAgentString = "";
57
58 /*** The browser name specified in the user agent string. */
59 private String 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 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 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 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 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 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
182 browserName = userAgentString.substring(0, versionStartIndex);
183 try
184 {
185
186
187 String 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 e)
201 {
202
203 }
204
205
206 if (userAgentString.indexOf(MSIE) != -1)
207 {
208
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 e)
220 {
221
222 }
223
224
225
226
227
228
229 }
230
231
232
233 if (userAgentString.indexOf(OPERA) != -1)
234 {
235
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 e)
247 {
248
249 }
250
251
252
253
254
255
256 }
257
258
259
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 float toFloat(String s)
362 {
363 return Float.valueOf(s).floatValue();
364 }
365
366 }