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
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public class BrowserDetector
49 {
50
51 public static final String MSIE = "MSIE";
52
53 public static final String OPERA = "Opera";
54
55 public static final String MOZILLA = "Mozilla";
56
57
58 public static final String WINDOWS = "Windows";
59
60 public static final String UNIX = "Unix";
61
62 public static final String MACINTOSH = "Macintosh";
63
64
65 private String userAgentString = "";
66
67
68 private String browserName = "";
69
70
71
72
73
74 private float browserVersion = (float) 1.0;
75
76
77
78
79 private String browserPlatform = "unknown";
80
81
82 private boolean javascriptOK = false;
83
84
85 private boolean cssOK = false;
86
87
88 private boolean fileUploadOK = false;
89
90
91
92
93
94
95 public BrowserDetector(String userAgentString)
96 {
97 this.userAgentString = userAgentString;
98 parse();
99 }
100
101
102
103
104
105
106 public BrowserDetector(RunData data)
107 {
108 this.userAgentString = data.getUserAgent();
109 parse();
110 }
111
112
113
114
115
116
117 public boolean isCssOK()
118 {
119 return cssOK;
120 }
121
122
123
124
125
126
127 public boolean isFileUploadOK()
128 {
129 return fileUploadOK;
130 }
131
132
133
134
135
136
137 public boolean isJavascriptOK()
138 {
139 return javascriptOK;
140 }
141
142
143
144
145
146
147 public String getBrowserName()
148 {
149 return browserName;
150 }
151
152
153
154
155
156
157 public String getBrowserPlatform()
158 {
159 return browserPlatform;
160 }
161
162
163
164
165
166
167 public float getBrowserVersion()
168 {
169 return browserVersion;
170 }
171
172
173
174
175
176
177 public String getUserAgentString()
178 {
179 return userAgentString;
180 }
181
182
183
184
185 private void parse()
186 {
187 int versionStartIndex = userAgentString.indexOf("/");
188 int versionEndIndex = userAgentString.indexOf(" ");
189
190
191 browserName = userAgentString.substring(0, versionStartIndex);
192 try
193 {
194
195
196 String agentSubstring = null;
197 if (versionEndIndex < 0)
198 {
199 agentSubstring
200 = userAgentString.substring(versionStartIndex + 1);
201 }
202 else
203 {
204 agentSubstring = userAgentString
205 .substring(versionStartIndex + 1, versionEndIndex);
206 }
207 browserVersion = toFloat(agentSubstring);
208 }
209 catch (NumberFormatException e)
210 {
211
212 }
213
214
215 if (userAgentString.indexOf(MSIE) != -1)
216 {
217
218 versionStartIndex = (userAgentString.indexOf(MSIE)
219 + MSIE.length() + 1);
220 versionEndIndex = userAgentString.indexOf(";", versionStartIndex);
221
222 browserName = MSIE;
223 try
224 {
225 browserVersion = toFloat(userAgentString
226 .substring(versionStartIndex, versionEndIndex));
227 }
228 catch (NumberFormatException e)
229 {
230
231 }
232
233
234
235
236
237
238 }
239
240
241
242 if (userAgentString.indexOf(OPERA) != -1)
243 {
244
245
246 versionStartIndex = (userAgentString.indexOf(OPERA)
247 + OPERA.length() + 1);
248 versionEndIndex = userAgentString.indexOf(" ", versionStartIndex);
249 if (versionEndIndex == -1)
250 {
251 versionEndIndex = userAgentString.length();
252 }
253
254 browserName = OPERA;
255 try
256 {
257 browserVersion = toFloat(userAgentString
258 .substring(versionStartIndex, versionEndIndex));
259 }
260 catch (NumberFormatException e)
261 {
262
263 }
264
265
266
267
268
269
270 }
271
272
273
274 if ((userAgentString.indexOf("Windows") != -1)
275 || (userAgentString.indexOf("WinNT") != -1)
276 || (userAgentString.indexOf("Win98") != -1)
277 || (userAgentString.indexOf("Win95") != -1))
278 {
279 browserPlatform = WINDOWS;
280 }
281
282 if (userAgentString.indexOf("Mac") != -1)
283 {
284 browserPlatform = MACINTOSH;
285 }
286
287 if (userAgentString.indexOf("X11") != -1)
288 {
289 browserPlatform = UNIX;
290 }
291
292 if (browserPlatform == WINDOWS)
293 {
294 if (browserName.equals(MOZILLA))
295 {
296 if (browserVersion >= 3.0)
297 {
298 javascriptOK = true;
299 fileUploadOK = true;
300 }
301 if (browserVersion >= 4.0)
302 {
303 cssOK = true;
304 }
305 }
306 else if (browserName == MSIE)
307 {
308 if (browserVersion >= 4.0)
309 {
310 javascriptOK = true;
311 fileUploadOK = true;
312 cssOK = true;
313 }
314 }
315 else if (browserName == OPERA)
316 {
317 if (browserVersion >= 3.0)
318 {
319 javascriptOK = true;
320 fileUploadOK = true;
321 cssOK = true;
322 }
323 }
324 }
325 else if (browserPlatform == MACINTOSH)
326 {
327 if (browserName.equals(MOZILLA))
328 {
329 if (browserVersion >= 3.0)
330 {
331 javascriptOK = true;
332 fileUploadOK = true;
333 }
334 if (browserVersion >= 4.0)
335 {
336 cssOK = true;
337 }
338 }
339 else if (browserName == MSIE)
340 {
341 if (browserVersion >= 4.0)
342 {
343 javascriptOK = true;
344 fileUploadOK = true;
345 }
346 if (browserVersion > 4.0)
347 {
348 cssOK = true;
349 }
350 }
351 }
352 else if (browserPlatform == UNIX)
353 {
354 if (browserName.equals(MOZILLA))
355 {
356 if (browserVersion >= 3.0)
357 {
358 javascriptOK = true;
359 fileUploadOK = true;
360 }
361 if (browserVersion >= 4.0)
362 {
363 cssOK = true;
364 }
365 }
366 }
367 }
368
369
370
371
372
373
374
375 private static final float toFloat(String s)
376 {
377 return Float.valueOf(s).floatValue();
378 }
379
380 }