001: /* *************************************************************************
002:
003: Millstone(TM)
004: Open Sourced User Interface Library for
005: Internet Development with Java
006:
007: Millstone is a registered trademark of IT Mill Ltd
008: Copyright (C) 2000-2005 IT Mill Ltd
009:
010: *************************************************************************
011:
012: This library is free software; you can redistribute it and/or
013: modify it under the terms of the GNU Lesser General Public
014: license version 2.1 as published by the Free Software Foundation.
015:
016: This library is distributed in the hope that it will be useful,
017: but WITHOUT ANY WARRANTY; without even the implied warranty of
018: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: Lesser General Public License for more details.
020:
021: You should have received a copy of the GNU Lesser General Public
022: License along with this library; if not, write to the Free Software
023: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024:
025: *************************************************************************
026:
027: For more information, contact:
028:
029: IT Mill Ltd phone: +358 2 4802 7180
030: Ruukinkatu 2-4 fax: +358 2 4802 7181
031: 20540, Turku email: info@itmill.com
032: Finland company www: www.itmill.com
033:
034: Primary source for MillStone information and releases: www.millstone.org
035:
036: ********************************************************************** */
037:
038: package org.millstone.webadapter;
039:
040: import org.millstone.base.terminal.Terminal;
041:
042: import java.util.ArrayList;
043: import java.util.Collection;
044: import java.util.Iterator;
045: import java.util.Locale;
046:
047: /** Web browser terminal type.
048: *
049: * This class implements web browser properties, which declare the features of
050: * the web browser.
051: *
052: * @author IT Mill Ltd.
053: * @version 3.1.1
054: * @since 3.0
055: */
056: public class WebBrowser implements Terminal {
057:
058: private static WebBrowser DEFAULT = new WebBrowser();
059:
060: /** Content type */
061: private String contentType = "text/html; charset=utf-8";
062:
063: /** Holds the collection of accepted locales */
064: private Collection locales = new ArrayList();
065:
066: /** Holds value of property browserApplication. */
067: private String browserApplication = null;
068:
069: /** Should the client side checkking be done. */
070: private boolean performClientCheck = true;
071:
072: /** Holds value for property isClientSideChecked. */
073: private boolean clientSideChecked = false;
074:
075: /** Holds value of property javaScriptVersion. */
076: private JavaScriptVersion javaScriptVersion = JAVASCRIPT_UNCHECKED;
077:
078: /** Holds value of property javaEnabled. */
079: private boolean javaEnabled = false;
080:
081: /** Holds value of property frameSupport. */
082: private boolean frameSupport = false;
083:
084: /** Holds value of property markup version. */
085: private MarkupVersion markupVersion = MARKUP_HTML_3_2;
086:
087: /** Pixel width of the terminal screen */
088: private int screenWidth = -1;
089:
090: /** Pixel height of the terminal screen */
091: private int screenHeight = -1;
092:
093: /** Constuctor with some autorecognition capabilities
094: * Retrieves all capability information reported in http request headers:
095: * <ul>
096: * <li>User web browser (User-Agent)</li>
097: * <li>Supported locale(s)</li>
098: * </ul>
099: */
100:
101: /**
102: * Constructor WebBrowserType.
103: * Creates default WebBrowserType instance.
104: */
105: public WebBrowser() {
106: }
107:
108: /** Get name of the default theme
109: * @return Name of the terminal window
110: */
111: public String getDefaultTheme() {
112: return "default";
113: }
114:
115: /** Get the name and version of the web browser application.
116: *
117: * This is the version string reported by the web-browser in http headers.
118: * @return Web browser application.
119: */
120: public String getBrowserApplication() {
121: return this .browserApplication;
122: }
123:
124: /** Get the version of the supported Java Script by the browser.
125: *
126: * Null if the Java Script is not supported.
127: * @return Version of the supported Java Script
128: */
129: public JavaScriptVersion getJavaScriptVersion() {
130: return this .javaScriptVersion;
131: }
132:
133: /** Does the browser support frames ?
134: * @return True if the browser supports frames, False if not
135: */
136: public boolean isFrameSupport() {
137: return this .frameSupport;
138: }
139:
140: /** Set the browser frame support
141: * @param frameSupport True if the browser supports frames, False if not
142: */
143: public void setFrameSupport(boolean frameSupport) {
144: this .frameSupport = frameSupport;
145: }
146:
147: /** Get the supported markup language.
148: *
149: * @return Supported markup language
150: */
151: public MarkupVersion getMarkupVersion() {
152: return this .markupVersion;
153: }
154:
155: /** Get height of the terminal window in pixels
156: * @return Height of the terminal window
157: */
158: public int getScreenHeight() {
159: return this .screenHeight;
160: }
161:
162: /** Get width of the terminal window in pixels
163: * @return Width of the terminal window
164: */
165: public int getScreenWidth() {
166: return this .screenWidth;
167: }
168:
169: /** Get the default locale requested by the browser.
170: * @return Default locale
171: */
172: public Locale getDefaultLocale() {
173: if (this .locales.isEmpty())
174: return null;
175: return (Locale) this .locales.iterator().next();
176: }
177:
178: /** Hash code composed of the properties of the web browser type */
179: public int hashCode() {
180: return toString().hashCode();
181: }
182:
183: /** Test the equality of the properties for two web browser types */
184: public boolean equals(Object obj) {
185: if (obj != null && obj instanceof WebBrowser) {
186: return toString().equals(obj.toString());
187: }
188: return false;
189: }
190:
191: /** Repsent the type of the web browser as string */
192: public String toString() {
193:
194: String localeString = "[";
195: for (Iterator i = this .locales.iterator(); i.hasNext(); localeString += ",") {
196: localeString += ((Locale) i.next()).toString();
197: }
198: localeString += "]";
199:
200: // Return catenation of the properties
201: return "Browser:" + this .browserApplication + ", " + "Locales:"
202: + localeString + ", " + "Frames:" + this .frameSupport
203: + ", " + "JavaScript:" + this .javaScriptVersion + ", "
204: + "Java: " + this .javaEnabled + ", " + "Markup:"
205: + this .markupVersion + ", " + "Height:"
206: + this .screenHeight + ", " + "Width:"
207: + this .screenWidth + ", ClientCheck:"
208: + this .performClientCheck + ", ClientCheckDone:"
209: + this .clientSideChecked;
210: }
211:
212: /** Get preferred content type */
213: public String getContentType() {
214: return contentType;
215: }
216:
217: /** Check if this type supports also given browser.
218: * @return true if this type matches the given browser.
219: */
220: public boolean supports(String browser) {
221: return this .getBrowserApplication().indexOf(browser) > 0;
222: }
223:
224: /** Check if this type supports given markup language version.
225: * @return true if this type supports the given markup version.
226: */
227: public boolean supports(MarkupVersion html) {
228: return this .getMarkupVersion().supports(html);
229: }
230:
231: /** Check if this type supports given javascript version.
232: * @param js The javascript version to check for.
233: * @return true if this type supports the given javascript version.
234: */
235: public boolean supports(JavaScriptVersion js) {
236: return this .getJavaScriptVersion().supports(js);
237: }
238:
239: /** Parse HTML version from string.
240: * @return HTMLVersion instance.
241: */
242: private MarkupVersion doParseHTMLVersion(String html) {
243: for (int i = 0; i < MARKUP_VERSIONS.length; i++) {
244: if (MARKUP_VERSIONS[i].name.equals(html))
245: return MARKUP_VERSIONS[i];
246: }
247: return MARKUP_UNKNOWN;
248: }
249:
250: /** Parse JavaScript version from string.
251: * @return HTMLVersion instance.
252: */
253: private JavaScriptVersion doParseJavaScriptVersion(String js) {
254: for (int i = 0; i < JAVASCRIPT_VERSIONS.length; i++) {
255: if (JAVASCRIPT_VERSIONS[i].name.toLowerCase().startsWith(
256: js.toLowerCase()))
257: return JAVASCRIPT_VERSIONS[i];
258: }
259: return JAVASCRIPT_NONE;
260: }
261:
262: /** Parse HTML version from string.
263: * @return HTMLVersion instance.
264: */
265: public static MarkupVersion parseHTMLVersion(String html) {
266: return DEFAULT.doParseHTMLVersion(html);
267: }
268:
269: /** Parse JavaScript version from string.
270: * @return HTMLVersion instance.
271: */
272: public static JavaScriptVersion parseJavaScriptVersion(String js) {
273: return DEFAULT.doParseJavaScriptVersion(js);
274: }
275:
276: /** Get the client side cheked property.
277: * Certain terminal features can only be detected at client side. This
278: * property indicates if the client side detections have been performed
279: * for this type.
280: * @return true if client has sent information about its properties. Default false
281: */
282: public boolean isClientSideChecked() {
283: return this .clientSideChecked;
284: }
285:
286: /** Set the client side checked property.
287: * Certain terminal features can only be detected at client side. This
288: * property indicates if the client side detections have been performed
289: * for this type.
290: * @param true if client has sent information about its properties, false otherweise.
291: */
292: public void setClientSideChecked(boolean value) {
293: this .clientSideChecked = value;
294: }
295:
296: /** Should the client features be checked using remote scripts.
297: * Should the client side terminal feature check be performed.
298: * @return true if client side checking should be performed for this terminal type. Default false.
299: */
300: public boolean performClientCheck() {
301: return this .performClientCheck;
302: }
303:
304: /** Should the client features be checked using remote scripts.
305: *
306: * @return true if client side checking should be performed for this terminal type. Default false.
307: */
308: public void performClientCheck(boolean value) {
309: this .performClientCheck = value;
310: }
311:
312: /** Check if web browser supports Java.
313: * @return boolean
314: */
315: public boolean isJavaEnabled() {
316: return javaEnabled;
317: }
318:
319: /** Returns the locales supported by the web browser.
320: * @return Collection
321: */
322: public Collection getLocales() {
323: return locales;
324: }
325:
326: /** Sets the browser application.
327: * This corresponds to User-Agent HTTP header.
328: * @param browserApplication The browserApplication to set
329: */
330: public void setBrowserApplication(String browserApplication) {
331: this .browserApplication = browserApplication;
332: }
333:
334: /** Sets the default content type.
335: * Default is <code>text/html</code>
336: * @param contentType The contentType to set
337: */
338: public void setContentType(String contentType) {
339: this .contentType = contentType;
340: }
341:
342: /** Sets the java enabled property.
343: * @param javaEnabled The javaEnabled to set
344: */
345: public void setJavaEnabled(boolean javaEnabled) {
346: this .javaEnabled = javaEnabled;
347: }
348:
349: /**Sets the JavaScript version.
350: * @param javaScriptVersion The JavaScript version to set
351: */
352: public void setJavaScriptVersion(JavaScriptVersion javaScriptVersion) {
353: this .javaScriptVersion = javaScriptVersion;
354: }
355:
356: /** Sets the markup language version.
357: * @param markupVersion ersion The markup language version to set
358: */
359: public void setMarkupVersion(MarkupVersion markupVersion) {
360: this .markupVersion = markupVersion;
361: }
362:
363: /** Sets the screen height.
364: * @param screenHeight The screen height to set in pixels.
365: */
366: public void setScreenHeight(int screenHeight) {
367: this .screenHeight = screenHeight;
368: }
369:
370: /** Sets the screen width.
371: * @param screenWidth The screenWidth to set in pixels.
372: */
373: public void setScreenWidth(int screenWidth) {
374: this .screenWidth = screenWidth;
375: }
376:
377: /*
378: * Consts defining the supported markup language versions
379: * @author IT Mill Ltd.
380: * @version 3.1.1
381: * @since 3.0
382: */
383: public class MarkupVersion {
384: private String name;
385: private int order;
386:
387: /**
388: * @see java.lang.Object#equals(Object)
389: */
390: public boolean equals(Object obj) {
391: if (obj != null && obj instanceof MarkupVersion)
392: return name.equals(((MarkupVersion) obj).name);
393: return false;
394: }
395:
396: /**
397: * @see java.lang.Object#toString()
398: */
399: public String toString() {
400: return name;
401: }
402:
403: private MarkupVersion(String name, int order) {
404: this .name = name;
405: this .order = order;
406: }
407:
408: /** Check compability with other HTML version.
409: * @return true if this is compatible with the other, false otherwise
410: */
411: public boolean supports(MarkupVersion other) {
412: return (this .order >= other.order);
413: }
414:
415: }
416:
417: public static final MarkupVersion MARKUP_UNKNOWN = DEFAULT.new MarkupVersion(
418: "HTML unknown", 0);
419: public static final MarkupVersion MARKUP_HTML_2_0 = DEFAULT.new MarkupVersion(
420: "HTML 2.0", 20);
421: public static final MarkupVersion MARKUP_HTML_3_2 = DEFAULT.new MarkupVersion(
422: "HTML 3.2", 32);
423: public static final MarkupVersion MARKUP_HTML_4_0 = DEFAULT.new MarkupVersion(
424: "HTML 4.0", 40);
425: public static final MarkupVersion MARKUP_XHTML_1_0 = DEFAULT.new MarkupVersion(
426: "XHTML 1.0", 110);
427: public static final MarkupVersion MARKUP_XHTML_2_0 = DEFAULT.new MarkupVersion(
428: "XHTML 2.0", 120);
429: public static final MarkupVersion MARKUP_WML_1_0 = DEFAULT.new MarkupVersion(
430: "WML 1.0", 10);
431: public static final MarkupVersion MARKUP_WML_1_1 = DEFAULT.new MarkupVersion(
432: "WML 1.1", 11);
433: public static final MarkupVersion MARKUP_WML_1_2 = DEFAULT.new MarkupVersion(
434: "WML 1.2", 12);
435:
436: public static final MarkupVersion[] MARKUP_VERSIONS = new MarkupVersion[] {
437: MARKUP_UNKNOWN, MARKUP_HTML_2_0, MARKUP_HTML_3_2,
438: MARKUP_HTML_4_0, MARKUP_XHTML_1_0, MARKUP_XHTML_2_0,
439: MARKUP_WML_1_0, MARKUP_WML_1_1, MARKUP_WML_1_2 };
440:
441: /*
442: * Consts defining the supported JavaScript versions
443: * @author IT Mill Ltd.
444: * @version 3.1.1
445: * @since 3.0
446: */
447: public class JavaScriptVersion {
448: private String name;
449: private int order;
450:
451: /**
452: * @see java.lang.Object#equals(Object)
453: */
454: public boolean equals(Object obj) {
455: if (obj != null && obj instanceof JavaScriptVersion)
456: return name.equals(((JavaScriptVersion) obj).name);
457: return false;
458: }
459:
460: /**
461: * @see java.lang.Object#toString()
462: */
463: public String toString() {
464: return name;
465: }
466:
467: private JavaScriptVersion(String name, int order) {
468: this .name = name;
469: this .order = order;
470: }
471:
472: /** Check compability with other JavaScript version.
473: * Use this like:
474: * <code>boolean isEcma = someVersion.supports(ECMA_262);</code>
475: * @return true if this supports the other, false otherwise
476: */
477: public boolean supports(JavaScriptVersion other) {
478:
479: // ECMA-262 support compare
480: if (other.equals(ECMA_262)) {
481:
482: // JScript over 5.0 support ECMA-262
483: if (this .order >= 100) {
484: return (this .order >= JSCRIPT_5_0.order);
485: } else {
486: return (this .order >= JAVASCRIPT_1_3.order);
487: }
488: }
489:
490: // JavaScript version compare
491: else if (this .order < 100 && other.order < 100) {
492: return (this .order >= other.order);
493: }
494:
495: // JScript version compare
496: else if (this .order >= 100 && other.order >= 100) {
497: return (this .order >= other.order);
498: }
499:
500: return false;
501:
502: }
503:
504: }
505:
506: public static final JavaScriptVersion JAVASCRIPT_UNCHECKED = DEFAULT.new JavaScriptVersion(
507: "JavaScript unchecked", -1);
508: public static final JavaScriptVersion JAVASCRIPT_NONE = DEFAULT.new JavaScriptVersion(
509: "JavaScript none", -1);
510: public static final JavaScriptVersion JAVASCRIPT_1_0 = DEFAULT.new JavaScriptVersion(
511: "JavaScript 1.0", 10);
512: public static final JavaScriptVersion JAVASCRIPT_1_1 = DEFAULT.new JavaScriptVersion(
513: "JavaScript 1.1", 11);
514: public static final JavaScriptVersion JAVASCRIPT_1_2 = DEFAULT.new JavaScriptVersion(
515: "JavaScript 1.2", 12);
516: public static final JavaScriptVersion JAVASCRIPT_1_3 = DEFAULT.new JavaScriptVersion(
517: "JavaScript 1.3", 13);
518: public static final JavaScriptVersion JAVASCRIPT_1_4 = DEFAULT.new JavaScriptVersion(
519: "JavaScript 1.4", 14);
520: public static final JavaScriptVersion JAVASCRIPT_1_5 = DEFAULT.new JavaScriptVersion(
521: "JavaScript 1.5", 15);
522: public static final JavaScriptVersion JSCRIPT_1_0 = DEFAULT.new JavaScriptVersion(
523: "JScript 1.0", 110);
524: public static final JavaScriptVersion JSCRIPT_3_0 = DEFAULT.new JavaScriptVersion(
525: "JScript 3.0", 130);
526: public static final JavaScriptVersion JSCRIPT_4_0 = DEFAULT.new JavaScriptVersion(
527: "JScript 4.0", 140);
528: public static final JavaScriptVersion JSCRIPT_5_0 = DEFAULT.new JavaScriptVersion(
529: "JScript 5.0", 150);
530: public static final JavaScriptVersion JSCRIPT_5_1 = DEFAULT.new JavaScriptVersion(
531: "JScript 5.1", 151);
532: public static final JavaScriptVersion JSCRIPT_5_5 = DEFAULT.new JavaScriptVersion(
533: "JScript 5.5", 155);
534: public static final JavaScriptVersion JSCRIPT_5_6 = DEFAULT.new JavaScriptVersion(
535: "JScript 5.6", 156);
536: public static final JavaScriptVersion ECMA_262 = DEFAULT.new JavaScriptVersion(
537: "ECMA-262", 262);
538:
539: public static final JavaScriptVersion[] JAVASCRIPT_VERSIONS = new JavaScriptVersion[] {
540: JAVASCRIPT_UNCHECKED, JAVASCRIPT_NONE, JAVASCRIPT_1_0,
541: JAVASCRIPT_1_1, JAVASCRIPT_1_2, JAVASCRIPT_1_3,
542: JAVASCRIPT_1_4, JAVASCRIPT_1_5, JSCRIPT_1_0, JSCRIPT_3_0,
543: JSCRIPT_4_0, JSCRIPT_5_0, JSCRIPT_5_1, JSCRIPT_5_5,
544: JSCRIPT_5_6, ECMA_262 };
545: }
|