1
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74 package com.ivata.mask.web.browser;
75 import com.ivata.mask.util.StringHandling;
76 /***
77 * <p>
78 * This class identifies the capabilities of the web browser the client is
79 * using, from the user agent string.
80 * </p>
81 *
82 * <p>
83 * <code>LoginTag</code> creates an instance of this class in the http
84 * session, storing it under the attribute name <i>"browser" </i>.
85 * </p>
86 *
87 * @since ivata masks 0.4 (2002-09-12)
88 * @author Colin MacLeod
89 * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
90 * @version $Revision: 1.2 $
91 */
92 public class Browser {
93 /***
94 * Refer to {@link #getJavaScriptVersion()}.
95 */
96 private String javaScriptVersion = null;
97 /***
98 * Refer to {@link #getType}.
99 */
100 private Integer type;
101 /***
102 * Refer to {@link #getUserAgent}.
103 */
104 private String userAgent = null;
105 /***
106 * Refer to {@link #getVersion}.
107 */
108 private String version = null;
109 /***
110 * <p>
111 * Construct an instance of the browser, using the user agent string
112 * provided to detect the browser's capabilities.
113 * </p>
114 *
115 * @param userAgentParam
116 * Refer to {@link #getUserAgent()}.
117 * @param javaScriptVersionParam
118 * Refer to {@link #getJavaScriptVersion()}.
119 */
120 public Browser(final String userAgentParam,
121 final String javaScriptVersionParam) {
122 this.javaScriptVersion = javaScriptVersionParam;
123
124 setUserAgent(userAgentParam);
125 }
126 /***
127 * <p>
128 * Detect whether or not the browser can display frames.
129 * </p>
130 *
131 * @return <code>true</code> if the browser can display frames, otherwise
132 * <code>false</code>.
133 */
134 public final boolean canDisplayFrames() {
135 if (type.equals(BrowserConstants.TYPE_GALEON)
136 || type.equals(BrowserConstants.TYPE_KONQUEROR)
137 || type.equals(BrowserConstants.TYPE_LYNX)
138 || type.equals(BrowserConstants.TYPE_MOZILLA)
139 || type.equals(BrowserConstants.TYPE_OPERA)) {
140 return true;
141 } else if (type.equals(BrowserConstants.TYPE_INTERNET_EXPLORER)) {
142 return (version.compareTo("3") >= 0);
143 } else if (type.equals(BrowserConstants.TYPE_NETSCAPE)) {
144 return (version.compareTo("2") >= 0);
145 } else {
146 return false;
147 }
148 }
149 /***
150 * <p>
151 * Detect whether or not the browser can display <code><iframe&bt;</code>
152 * tags.
153 * </p>
154 *
155 * @return <code>true</code> if the browser can display iframes, otherwise
156 * <code>false</code>.
157 */
158 public final boolean canDisplayIFrames() {
159 if (type.equals(BrowserConstants.TYPE_GALEON)
160 || type.equals(BrowserConstants.TYPE_MOZILLA)) {
161 return true;
162 } else if (type.equals(BrowserConstants.TYPE_OPERA)) {
163 return (version.compareTo("5") >= 0);
164 } else if (type.equals(BrowserConstants.TYPE_INTERNET_EXPLORER)) {
165 return (version.compareTo("3") >= 0);
166 } else if (type.equals(BrowserConstants.TYPE_NETSCAPE)) {
167 return (version.compareTo("6") >= 0);
168 } else {
169 return false;
170 }
171 }
172 /***
173 * <p>
174 * Detect whether or not the browser can display
175 * <code><marquee&bt;</code> tags.
176 * </p>
177 *
178 * @return <code>true</code> if the browser can display marquee, otherwise
179 * <code>false</code>.
180 */
181 public final boolean canDisplayMarquee() {
182 if (type.equals(BrowserConstants.TYPE_GALEON)
183 || type.equals(BrowserConstants.TYPE_MOZILLA)) {
184 return true;
185 } else if (type.equals(BrowserConstants.TYPE_INTERNET_EXPLORER)) {
186 return (version.compareTo("3") >= 0);
187 } else if (type.equals(BrowserConstants.TYPE_NETSCAPE)) {
188 return (version.compareTo("6") >= 0);
189 } else {
190 return false;
191 }
192 }
193 /***
194 * <p>
195 * A String depicting the version number of the JavaScript this browser
196 * supports if supported, otherwise <code>null</code> if the browser
197 * doesn't support JavaScript.
198 * </p>
199 *
200 * @return the current value of javaScriptVersion.
201 */
202 public String getJavaScriptVersion() {
203 return javaScriptVersion;
204 }
205 /***
206 * <p>
207 * Identifies the make, or type of browser used (such as Mozilla, IE, etc.).
208 * Equates to one of the constants in {@linkBrowserConstants.
209 * </p>
210 *
211 * @return the current value of type.
212 */
213 public Integer getType() {
214 return type;
215 }
216 /***
217 * <p>
218 * Initialized by the constructor, this is the user agent string from the
219 * <code>request.getHeader("User-Agent")</code> value.
220 * </p>
221 *
222 * @return the current value of userAgent.
223 */
224 public String getUserAgent() {
225 return userAgent;
226 }
227 /***
228 * <p>
229 * A string depiction of the browser version number if available and known,
230 * otherwise <code>null</code> if the browser manufacturer could not be
231 * identified.
232 * </p>
233 *
234 * @return the current value of version.
235 */
236 public String getVersion() {
237 return version;
238 }
239 /***
240 * <p>
241 * Detect whether or not this browser is capable of displaying JavaScript,
242 * and has JavaScript enabled.
243 * </p>
244 *
245 * @return <code>true</code> if the browser can display JavaScript,
246 * otherwise <code>false</code>.
247 */
248 public boolean isJavaScriptEnabled() {
249 return (javaScriptVersion != null);
250 }
251 /***
252 * Refer to {@link #getJavaScriptVersion}.
253 *
254 * @param javaScriptVersionParam
255 * Refer to {@link #getJavaScriptVersion}.
256 */
257 public final void setJavaScriptVersion(
258 final String javaScriptVersionParam) {
259 this.javaScriptVersion = javaScriptVersionParam;
260 }
261 /***
262 * Refer to {@link #getType}.
263 *
264 * @param typeParam
265 * Refer to {@link #getType}.
266 */
267 public final void setType(final Integer typeParam) {
268 this.type = typeParam;
269 }
270 /***
271 * Refer to {@link #getUserAgent}.
272 *
273 * @param userAgentParam
274 * Refer to {@link #getUserAgent}.
275 */
276 public final void setUserAgent(final String userAgentParam) {
277
278 if (StringHandling.isNullOrEmpty(userAgentParam)) {
279 this.userAgent = "";
280 } else {
281 this.userAgent = userAgentParam;
282 }
283 this.version = null;
284
285 int position;
286
287 int start;
288
289 int end;
290 if ((position = userAgent.indexOf("Opera")) != -1) {
291 this.type = BrowserConstants.TYPE_OPERA;
292
293
294 start = position + "Opera".length() + 1;
295 end = userAgent.indexOf(" ", start);
296 if ((start != -1) && (end != -1)) {
297 this.version = userAgent.substring(start, end);
298 }
299 } else if ((position = userAgent.indexOf("Galeon")) != -1) {
300 this.type = BrowserConstants.TYPE_GALEON;
301 start = userAgent.indexOf("/", position);
302 end = userAgent.indexOf(" ", start);
303 if ((start != -1) && (end != -1)) {
304 this.version = userAgent.substring(++start, end);
305 }
306 } else if (userAgent.indexOf("Gecko") != -1) {
307
308 if ((position = userAgent.indexOf("Netscape")) != -1) {
309 this.type = BrowserConstants.TYPE_NETSCAPE;
310 start = userAgent.indexOf("/", position);
311 end = userAgent.indexOf(" ", start);
312 if ((start != -1) && (end != -1)) {
313 this.version = userAgent.substring(++start, end);
314 }
315 } else {
316
317 this.type = BrowserConstants.TYPE_MOZILLA;
318 String rv = "rv:";
319 start = userAgent.indexOf(rv, position);
320 end = userAgent.indexOf(")", start);
321 if ((start != -1) && (end != -1)) {
322 start += rv.length();
323 this.version = userAgent.substring(start, end);
324 } else {
325
326 this.version = "0";
327 }
328 }
329 } else if ((userAgent.indexOf("Nav") != -1)
330 || (userAgent.indexOf("Gold") != -1)
331 || (userAgent.indexOf("Netscape") != -1)) {
332
333 this.type = BrowserConstants.TYPE_NETSCAPE;
334 } else if ((position = userAgent.indexOf("MSIE")) != -1) {
335
336 this.type = BrowserConstants.TYPE_INTERNET_EXPLORER;
337 start = userAgent.indexOf(" ", position);
338 end = userAgent.indexOf(";", start);
339 if ((start != -1) && (end != -1)) {
340 this.version = userAgent.substring(++start, end);
341 } else {
342
343
344 this.version = "3.0";
345 }
346 } else if (userAgent.indexOf("Konq") != -1) {
347 this.type = BrowserConstants.TYPE_KONQUEROR;
348 } else if (userAgent.indexOf("Lynx") != -1) {
349 this.type = BrowserConstants.TYPE_LYNX;
350 } else if ((userAgent.indexOf("bot") != 1)
351 || (userAgent.indexOf("Google") != 1)
352 || (userAgent.indexOf("Slurp") != 1)
353 || (userAgent.indexOf("Scooter") != 1)
354 || (userAgent.indexOf("Spider") != 1)
355 || (userAgent.indexOf("Infoseek") != 1)) {
356 this.type = BrowserConstants.TYPE_ROBOT;
357 } else {
358 this.type = BrowserConstants.TYPE_UNKNOWN;
359 }
360
361
362 if (this.version == null) {
363 start = userAgent.indexOf("/", position);
364 end = userAgent.indexOf(" ", start);
365 if ((start != -1) && (end != -1)) {
366 this.version = userAgent.substring(++start, end);
367 } else {
368
369
370 this.version = "0";
371 }
372 }
373 }
374 /***
375 * Refer to {@link #getVersion()}.
376 *
377 * @param versionParam
378 * Refer to {@link #getVersion()}.
379 */
380 public final void setVersion(final String versionParam) {
381 this.version = versionParam;
382 }
383 }