summaryrefslogtreecommitdiffstats
path: root/src/com/itmill/toolkit/terminal/web/WebBrowser.java
blob: 7b194ac3e8bf73799413e16170cfdd76fc8b5b48 (plain)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/* *************************************************************************
 
                               IT Mill Toolkit 

               Development of Browser User Intarfaces Made Easy

                    Copyright (C) 2000-2006 IT Mill Ltd
                     
   *************************************************************************

   This product is distributed under commercial license that can be found
   from the product package on license/license.txt. Use of this product might 
   require purchasing a commercial license from IT Mill Ltd. For guidelines 
   on usage, see license/licensing-guidelines.html

   *************************************************************************
   
   For more information, contact:
   
   IT Mill Ltd                           phone: +358 2 4802 7180
   Ruukinkatu 2-4                        fax:   +358 2 4802 7181
   20540, Turku                          email:  info@itmill.com
   Finland                               company www: www.itmill.com
   
   Primary source for information and releases: www.itmill.com

   ********************************************************************** */

package com.itmill.toolkit.terminal.web;

import com.itmill.toolkit.terminal.Terminal;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Locale;

/** Web browser terminal type.
 *
 * This class implements web browser properties, which declare the features of
 * the web browser.
 *
 * @author IT Mill Ltd.
 * @version @VERSION@
 * @since 3.0
 */
public class WebBrowser implements Terminal {

	private static WebBrowser DEFAULT = new WebBrowser();

	/** Content type */
	private String contentType = "text/html; charset=utf-8";

	/** Holds the collection of accepted locales */
	private Collection locales = new ArrayList();

	/** Holds value of property browserApplication. */
	private String browserApplication = null;

	/** Should the client side checkking be done. */
	private boolean performClientCheck = true;

	/** Holds value for property isClientSideChecked. */
	private boolean clientSideChecked = false;

	/** Holds value of property javaScriptVersion. */
	private JavaScriptVersion javaScriptVersion = JAVASCRIPT_UNCHECKED;

	/** Holds value of property javaEnabled. */
	private boolean javaEnabled = false;

	/** Holds value of property frameSupport. */
	private boolean frameSupport = false;

	/** Holds value of property markup version. */
	private MarkupVersion markupVersion = MARKUP_HTML_3_2;

	/** Pixel width of the terminal screen */
	private int screenWidth = -1;

	/** Pixel height of the terminal screen */
	private int screenHeight = -1;

	/** Constuctor with some autorecognition capabilities 
	 *  Retrieves all capability information reported in http request headers:
	 * <ul>
	 *  <li>User web browser (User-Agent)</li>
	 *  <li>Supported locale(s)</li>
	 * </ul>
	 */

	/**
	 * Constructor WebBrowserType.
	 * Creates default WebBrowserType instance.
	 */
	public WebBrowser() {
	}

	/** Get name of the default theme
	 * @return Name of the terminal window
	 */
	public String getDefaultTheme() {
		return ApplicationServlet.DEFAULT_THEME;
	}

	/** Get the name and version of the web browser application.
	 *
	 * This is the version string reported by the web-browser in http headers.
	 * @return Web browser application.
	 */
	public String getBrowserApplication() {
		return this.browserApplication;
	}

	/** Get the version of the supported Java Script by the browser.
	 *
	 * Null if the Java Script is not supported.
	 * @return Version of the supported Java Script
	 */
	public JavaScriptVersion getJavaScriptVersion() {
		return this.javaScriptVersion;
	}

	/** Does the browser support frames ?
	 * @return True if the browser supports frames, False if not
	 */
	public boolean isFrameSupport() {
		return this.frameSupport;
	}

	/** Set the browser frame support
	 * @param frameSupport True if the browser supports frames, False if not
	 */
	public void setFrameSupport(boolean frameSupport) {
		this.frameSupport = frameSupport;
	}

	/** Get the supported markup language.
	 *
	 * @return Supported markup language
	 */
	public MarkupVersion getMarkupVersion() {
		return this.markupVersion;
	}

	/** Get height of the terminal window in pixels
	 * @return Height of the terminal window
	 */
	public int getScreenHeight() {
		return this.screenHeight;
	}

	/** Get width of the terminal window in pixels
	 * @return Width of the terminal window
	 */
	public int getScreenWidth() {
		return this.screenWidth;
	}

	/** Get the default locale requested by the browser.
	 * @return Default locale
	 */
	public Locale getDefaultLocale() {
		if (this.locales.isEmpty())
			return null;
		return (Locale) this.locales.iterator().next();
	}

	/** Hash code composed of the properties of the web browser type */
	public int hashCode() {
		return toString().hashCode();
	}

	/** Test the equality of the properties for two web browser types */
	public boolean equals(Object obj) {
		if (obj != null && obj instanceof WebBrowser) {
			return toString().equals(obj.toString());
		}
		return false;
	}

	/** Repsent the type of the web browser as string */
	public String toString() {

		String localeString = "[";
		for (Iterator i = this.locales.iterator();
			i.hasNext();
			localeString += ",") {
			localeString += ((Locale) i.next()).toString();
		}
		localeString += "]";

		// Return catenation of the properties
		return "Browser:"
			+ this.browserApplication
			+ ", "
			+ "Locales:"
			+ localeString
			+ ", "
			+ "Frames:"
			+ this.frameSupport
			+ ", "
			+ "JavaScript:"
			+ this.javaScriptVersion
			+ ", "
			+ "Java: "
			+ this.javaEnabled
			+ ", "
			+ "Markup:"
			+ this.markupVersion
			+ ", "
			+ "Height:"
			+ this.screenHeight
			+ ", "
			+ "Width:"
			+ this.screenWidth
			+ ", ClientCheck:"
			+ this.performClientCheck
			+ ", ClientCheckDone:"
			+ this.clientSideChecked;
	}

	/** Get preferred content type */
	public String getContentType() {
		return contentType;
	}

	/** Check if this type supports also given browser.
	 *  @return true if this type matches the given browser.
	 */
	public boolean supports(String browser) {
		return this.getBrowserApplication().indexOf(browser) > 0;
	}

	/** Check if this type supports given markup language version.
	 *  @return true if this type supports the given markup version.
	 */
	public boolean supports(MarkupVersion html) {
		return this.getMarkupVersion().supports(html);
	}

	/** Check if this type supports given javascript version.
	 *  @param js The javascript version to check for.
	 *  @return true if this type supports the given javascript version.
	 */
	public boolean supports(JavaScriptVersion js) {
		return this.getJavaScriptVersion().supports(js);
	}

	/** Parse HTML version from string.
	 * @return HTMLVersion instance.
	 */
	private MarkupVersion doParseHTMLVersion(String html) {
		for (int i = 0; i < MARKUP_VERSIONS.length; i++) {
			if (MARKUP_VERSIONS[i].name.equals(html))
				return MARKUP_VERSIONS[i];
		}
		return MARKUP_UNKNOWN;
	}

	/** Parse JavaScript version from string.
		 * @return HTMLVersion instance.
	 */
	private JavaScriptVersion doParseJavaScriptVersion(String js) {
		for (int i = 0; i < JAVASCRIPT_VERSIONS.length; i++) {
			if (JAVASCRIPT_VERSIONS[i]
				.name
				.toLowerCase()
				.startsWith(js.toLowerCase()))
				return JAVASCRIPT_VERSIONS[i];
		}
		return JAVASCRIPT_NONE;
	}

	/** Parse HTML version from string.
	 * @return HTMLVersion instance.
	 */
	public static MarkupVersion parseHTMLVersion(String html) {
		return DEFAULT.doParseHTMLVersion(html);
	}

	/** Parse JavaScript version from string.
	 * @return HTMLVersion instance.
	 */
	public static JavaScriptVersion parseJavaScriptVersion(String js) {
		return DEFAULT.doParseJavaScriptVersion(js);
	}

	/** Get the client side cheked property.
	 *  Certain terminal features can only be detected at client side. This
	 *  property indicates if the client side detections have been performed
	 *  for this type.
	 * @return true if client has sent information about its properties. Default false
	 */
	public boolean isClientSideChecked() {
		return this.clientSideChecked;
	}

	/** Set the client side checked property.
	 *  Certain terminal features can only be detected at client side. This
	 *  property indicates if the client side detections have been performed
	 *  for this type.
	 * @param true if client has sent information about its properties, false otherweise.
	 */
	public void setClientSideChecked(boolean value) {
		this.clientSideChecked = value;
	}

	/** Should the client features be checked using remote scripts.
	 *  Should the client side terminal feature check be performed.
	 * @return true if client side checking should be performed for this terminal type. Default false.
	 */
	public boolean performClientCheck() {
		return this.performClientCheck;
	}

	/** Should the  client features be checked using remote scripts.
	 * 
	 * @return true if client side checking should be performed for this terminal type. Default false.
	 */
	public void performClientCheck(boolean value) {
		this.performClientCheck = value;
	}

	/** Check if web browser supports Java.
	 * @return boolean
	 */
	public boolean isJavaEnabled() {
		return javaEnabled;
	}

	/** Returns the locales supported by the web browser.
	 * @return Collection
	 */
	public Collection getLocales() {
		return locales;
	}

	/** Sets the browser application.
	 *  This corresponds to User-Agent HTTP header.
	 * @param browserApplication The browserApplication to set
	 */
	public void setBrowserApplication(String browserApplication) {
		this.browserApplication = browserApplication;
	}

	/** Sets the default content type.
	 *  Default is <code>text/html</code>
	 * @param contentType The contentType to set
	 */
	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	/** Sets the java enabled property.
	 * @param javaEnabled The javaEnabled to set
	 */
	public void setJavaEnabled(boolean javaEnabled) {
		this.javaEnabled = javaEnabled;
	}

	/**Sets the JavaScript version.
	 * @param javaScriptVersion The JavaScript version to set
	 */
	public void setJavaScriptVersion(JavaScriptVersion javaScriptVersion) {
		this.javaScriptVersion = javaScriptVersion;
	}

	/** Sets the markup language version.
	 * @param markupVersion ersion The markup language version to set
	 */
	public void setMarkupVersion(MarkupVersion markupVersion) {
		this.markupVersion = markupVersion;
	}

	/** Sets the screen height.
	 * @param screenHeight The screen height to set in pixels.
	 */
	public void setScreenHeight(int screenHeight) {
		this.screenHeight = screenHeight;
	}

	/** Sets the screen width.
	 * @param screenWidth The screenWidth to set in pixels.
	 */
	public void setScreenWidth(int screenWidth) {
		this.screenWidth = screenWidth;
	}

	/*
	 * Consts defining the supported markup language versions 
	 * @author IT Mill Ltd.
	 * @version @VERSION@
	 * @since 3.0
	 */
	public class MarkupVersion {
		private String name;
		private int order;

		/**
		 * @see java.lang.Object#equals(Object)
		 */
		public boolean equals(Object obj) {
			if (obj != null && obj instanceof MarkupVersion)
				return name.equals(((MarkupVersion) obj).name);
			return false;
		}

		/**
		 * @see java.lang.Object#toString()
		 */
		public String toString() {
			return name;
		}

		private MarkupVersion(String name, int order) {
			this.name = name;
			this.order = order;
		}

		/** Check compability with other HTML version.
		 *  @return true if this is compatible with the other, false otherwise
		 */
		public boolean supports(MarkupVersion other) {
			return (this.order >= other.order);
		}

	}

	public static final MarkupVersion MARKUP_UNKNOWN =
		DEFAULT.new MarkupVersion("HTML unknown", 0);
	public static final MarkupVersion MARKUP_HTML_2_0 =
		DEFAULT.new MarkupVersion("HTML 2.0", 20);
	public static final MarkupVersion MARKUP_HTML_3_2 =
		DEFAULT.new MarkupVersion("HTML 3.2", 32);
	public static final MarkupVersion MARKUP_HTML_4_0 =
		DEFAULT.new MarkupVersion("HTML 4.0", 40);
	public static final MarkupVersion MARKUP_XHTML_1_0 =
		DEFAULT.new MarkupVersion("XHTML 1.0", 110);
	public static final MarkupVersion MARKUP_XHTML_2_0 =
		DEFAULT.new MarkupVersion("XHTML 2.0", 120);
	public static final MarkupVersion MARKUP_WML_1_0 =
		DEFAULT.new MarkupVersion("WML 1.0", 10);
	public static final MarkupVersion MARKUP_WML_1_1 =
		DEFAULT.new MarkupVersion("WML 1.1", 11);
	public static final MarkupVersion MARKUP_WML_1_2 =
		DEFAULT.new MarkupVersion("WML 1.2", 12);

	public static final MarkupVersion[] MARKUP_VERSIONS =
		new MarkupVersion[] {
			MARKUP_UNKNOWN,
			MARKUP_HTML_2_0,
			MARKUP_HTML_3_2,
			MARKUP_HTML_4_0,
			MARKUP_XHTML_1_0,
			MARKUP_XHTML_2_0,
			MARKUP_WML_1_0,
			MARKUP_WML_1_1,
			MARKUP_WML_1_2 };
	/*
	 * Consts defining the supported JavaScript versions 
	 * @author IT Mill Ltd.
	 * @version @VERSION@
	 * @since 3.0
	 */
	public class JavaScriptVersion {
		private String name;
		private int order;

		/**
		 * @see java.lang.Object#equals(Object)
		 */
		public boolean equals(Object obj) {
			if (obj != null && obj instanceof JavaScriptVersion)
				return name.equals(((JavaScriptVersion) obj).name);
			return false;
		}

		/**
		 * @see java.lang.Object#toString()
		 */
		public String toString() {
			return name;
		}

		private JavaScriptVersion(String name, int order) {
			this.name = name;
			this.order = order;
		}

		/** Check compability with other JavaScript version.
		 *  Use this like:
		 *  <code>boolean isEcma = someVersion.supports(ECMA_262);</code>
		 *  @return true if this supports the other, false otherwise
		 */
		public boolean supports(JavaScriptVersion other) {

			// ECMA-262 support compare
			if (other.equals(ECMA_262)) {

				// JScript over 5.0 support ECMA-262
				if (this.order >= 100) {
					return (this.order >= JSCRIPT_5_0.order);
				} else {
					return (this.order >= JAVASCRIPT_1_3.order);
				}
			}

			// JavaScript version compare
			else if (this.order < 100 && other.order < 100) {
				return (this.order >= other.order);
			}

			// JScript version compare
			else if (this.order >= 100 && other.order >= 100) {
				return (this.order >= other.order);
			}

			return false;

		}

	}
	public static final JavaScriptVersion JAVASCRIPT_UNCHECKED =
		DEFAULT.new JavaScriptVersion("JavaScript unchecked", -1);
	public static final JavaScriptVersion JAVASCRIPT_NONE =
		DEFAULT.new JavaScriptVersion("JavaScript none", -1);
	public static final JavaScriptVersion JAVASCRIPT_1_0 =
		DEFAULT.new JavaScriptVersion("JavaScript 1.0", 10);
	public static final JavaScriptVersion JAVASCRIPT_1_1 =
		DEFAULT.new JavaScriptVersion("JavaScript 1.1", 11);
	public static final JavaScriptVersion JAVASCRIPT_1_2 =
		DEFAULT.new JavaScriptVersion("JavaScript 1.2", 12);
	public static final JavaScriptVersion JAVASCRIPT_1_3 =
		DEFAULT.new JavaScriptVersion("JavaScript 1.3", 13);
	public static final JavaScriptVersion JAVASCRIPT_1_4 =
		DEFAULT.new JavaScriptVersion("JavaScript 1.4", 14);
	public static final JavaScriptVersion JAVASCRIPT_1_5 =
		DEFAULT.new JavaScriptVersion("JavaScript 1.5", 15);
	public static final JavaScriptVersion JSCRIPT_1_0 =
		DEFAULT.new JavaScriptVersion("JScript 1.0", 110);
	public static final JavaScriptVersion JSCRIPT_3_0 =
		DEFAULT.new JavaScriptVersion("JScript 3.0", 130);
	public static final JavaScriptVersion JSCRIPT_4_0 =
		DEFAULT.new JavaScriptVersion("JScript 4.0", 140);
	public static final JavaScriptVersion JSCRIPT_5_0 =
		DEFAULT.new JavaScriptVersion("JScript 5.0", 150);
	public static final JavaScriptVersion JSCRIPT_5_1 =
		DEFAULT.new JavaScriptVersion("JScript 5.1", 151);
	public static final JavaScriptVersion JSCRIPT_5_5 =
		DEFAULT.new JavaScriptVersion("JScript 5.5", 155);
	public static final JavaScriptVersion JSCRIPT_5_6 =
		DEFAULT.new JavaScriptVersion("JScript 5.6", 156);
	public static final JavaScriptVersion JSCRIPT_5_7 =
		DEFAULT.new JavaScriptVersion("JScript 5.7", 157);	
	public static final JavaScriptVersion ECMA_262 =
		DEFAULT.new JavaScriptVersion("ECMA-262", 262);

	public static final JavaScriptVersion[] JAVASCRIPT_VERSIONS =
		new JavaScriptVersion[] {
			JAVASCRIPT_UNCHECKED,
			JAVASCRIPT_NONE,
			JAVASCRIPT_1_0,
			JAVASCRIPT_1_1,
			JAVASCRIPT_1_2,
			JAVASCRIPT_1_3,
			JAVASCRIPT_1_4,
			JAVASCRIPT_1_5,
			JSCRIPT_1_0,
			JSCRIPT_3_0,
			JSCRIPT_4_0,
			JSCRIPT_5_0,
			JSCRIPT_5_1,
			JSCRIPT_5_5,
			JSCRIPT_5_6,
			JSCRIPT_5_7,
			ECMA_262 };
}