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