KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > core > view > ViewUtil


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: ViewUtil.java,v 1.8 2004/02/01 05:16:28 christianc Exp $
19  */

20 package org.enhydra.barracuda.core.view;
21
22 import java.util.*;
23 import javax.servlet.*;
24 import javax.servlet.http.*;
25
26 import org.enhydra.barracuda.core.helper.servlet.*;
27
28 /**
29  * <p>This class provides utility functions for determining the ViewCapabilities.
30  *
31  * <p>Note that currently only the HTML related logic is fully implemented. If
32  * you need support for specific versions of WML, CHTML, etc, you may have to
33  * add some code here to examine the appropriate headers and map it to the
34  * proper types. If you have expertise working with these other flavors of
35  * markup and would like to help flesh out support for these languages, email
36  * the list and we'll be glad to give you pointers...
37  */

38 public class ViewUtil {
39
40     //base data types
41
private static int HTML = 0;
42     private static int CHTML = 1;
43     private static int XML = 2;
44     private static int VXML = 3;
45     private static int WML = 4;
46     private static int XHTML_BASIC = 5;
47     private static int XHTML_STANDARD = 6;
48
49     /**
50      * Determine the format type by looking at the Accept header in
51      * the request
52      *
53      * @param req an HttpServletRequest object
54      * @return the appropriate client type (defaults to FormatType.UNKNOWN_FORMAT
55      * if we are unable to make a positive match)
56      */

57     public static FormatType getFormatType(HttpServletRequest req) {
58         FormatType ft = FormatType.UNKNOWN_FORMAT;
59         int baseType = getBaseType(req);
60
61         //figure out what we're dealing with
62
//...HTML
63
if (baseType==HTML) {
64             ft = FormatType.HTML_4_0;
65         //...CHTML
66
} else if (baseType==CHTML) {
67             ft = FormatType.CHTML_1_0;
68         //...XML
69
} else if (baseType==XML) {
70             ft = FormatType.XML_1_0;
71         //...VXML
72
} else if (baseType==VXML) {
73             ft = FormatType.VXML_1_0;
74         //...WML
75
} else if (baseType==WML) {
76             ft = FormatType.WML_1_0;
77         //...XHTML_BASIC
78
} else if (baseType==XHTML_BASIC) {
79             ft = FormatType.XHTML_BASIC_1_0;
80         //...XHTML_STANDARD
81
} else if (baseType==XHTML_STANDARD) {
82             ft = FormatType.XHTML_STANDARD_1_0;
83         }
84         
85         return ft;
86     }
87
88     /**
89      * Determine the what type of scripting is supported by figuring
90      * out what kind of browser we're dealing with. Note that this method
91      * does NOT determine whether or not the browser actually has scripting
92      * enabled.
93      *
94      * @param req an HttpServletRequest object
95      * @return the appropriate client type (defaults to ScriptingType.NONE
96      * if we are unable to make a positive match)
97      */

98     public static ScriptingType getScriptingType(HttpServletRequest req) {
99         ScriptingType st = ScriptingType.NONE;
100         if (req!=null) {
101         
102             //csc_102201.1
103
//first and foremost see if the request includes a scripting tag,
104
//which would be a direct indication from the client of what it
105
//supports. If it comes back false, we can immediately return;
106
//otherwise, we still try to figure out the specific level of support
107
//by looking at the client type (except we can now safely default to
108
//Javascript 1.0)
109
Boolean JavaDoc enabled = ScriptDetector.scriptingEnabled(req);
110             if (enabled!=null) {
111                 if (enabled.booleanValue()==false) return ScriptingType.NONE;
112                 else st = ScriptingType.JAVASCRIPT_1_0;
113             }
114         
115             //first get the client type
116
ClientType ct = getClientType(req);
117             
118             //now map accordingly (note that we are not actually determining
119
//whether or not the scripting is enabled). I got these mappings from
120
//http://www.digitalroom.net/index2.html
121
//http://www.javascripter.net/faq/javascr3.htm
122
if (ct instanceof ClientType.HtmlBrowser) {
123                 if (ct instanceof ClientType.IE5x) st = ScriptingType.JAVASCRIPT_1_3;
124                 else if (ct instanceof ClientType.IE4x) st = ScriptingType.JAVASCRIPT_1_2; //note: the first link asserts (above) IE4x actually supports 1.3
125
else if (ct instanceof ClientType.IE3x) st = ScriptingType.JAVASCRIPT_1_0;
126                 else if (ct instanceof ClientType.NN6x) st = ScriptingType.JAVASCRIPT_1_5;
127                 else if (ct instanceof ClientType.NN4x) st = ScriptingType.JAVASCRIPT_1_3;
128                 else if (ct instanceof ClientType.NN3x) st = ScriptingType.JAVASCRIPT_1_1; //by 4x we're assuming NN 4.5 or higher
129
else if (ct instanceof ClientType.Opera4x) st = ScriptingType.JAVASCRIPT_1_3;
130                 else if (ct instanceof ClientType.Html32Browser) st = ScriptingType.JAVASCRIPT_1_0;
131             } else if (ct instanceof ClientType.WmlBrowser) {
132                 st = ScriptingType.WMLSCRIPT_1x;
133             }
134         }
135         return st;
136     }
137
138     /**
139      * Determine the client type by looking at the User-Agent header in
140      * the request
141      *
142      * @param req an HttpServletRequest object
143      * @return the appropriate client type (defaults to ClientType.UNKNOWN_BROWSER
144      * if we are unable to make a positive match)
145      */

146     public static ClientType getClientType(HttpServletRequest req) {
147         ClientType ct = ClientType.UNKNOWN_BROWSER;
148         chk: if (req!=null) {
149             //get the user agent string
150
String JavaDoc uas = req.getHeader("User-Agent");
151             if (uas==null) uas = req.getHeader("user-agent");
152             if (uas==null) break chk;
153             uas = uas.toLowerCase();
154
155             int baseType = getBaseType(req);
156
157             //figure out what we're dealing with
158
//...HTML
159
if (baseType==HTML) {
160                 //these here are going to cover the most common situations
161
//...Opera (we check for opera first since it masquerades as both IE and NN)
162
if (uas.indexOf("opera")!=-1) {
163                     if (uas.indexOf("opera/5")!=-1) ct = ClientType.OPERA_5x;
164                     else if (uas.indexOf("opera/4")!=-1) ct = ClientType.OPERA_4x;
165                     else if (uas.indexOf("opera 5")!=-1) ct = ClientType.OPERA_5x;
166                     else if (uas.indexOf("opera 4")!=-1) ct = ClientType.OPERA_4x;
167                     else if (uas.indexOf("opera/2")!=-1) ct = ClientType.HTML_BROWSER;
168                     else if (uas.indexOf("opera 2")!=-1) ct = ClientType.HTML_BROWSER;
169                     else ct = ClientType.HTML_3_2_BROWSER;
170                 //...IE (next we check for IE, since it also looks like NN)
171
} else if (uas.indexOf("msie ")!=-1) {
172                     if (uas.indexOf("msie 6")!=-1) ct = ClientType.IE_6x;
173                     else if (uas.indexOf("msie 5")!=-1) ct = ClientType.IE_5x;
174                     else if (uas.indexOf("msie 4")!=-1) ct = ClientType.IE_4x;
175                     else if (uas.indexOf("msie 3")!=-1) ct = ClientType.IE_3x;
176                     else if (uas.indexOf("msie 2")!=-1) ct = ClientType.HTML_BROWSER;
177                     else ct = ClientType.HTML_3_2_BROWSER;
178                 //...NN (it its none of the above, its probably NN)
179
} else if (uas.indexOf("mozilla")!=-1) {
180                     if (uas.indexOf("netscape6")!=-1) ct = ClientType.NN_6x;
181                     else if (uas.indexOf("gecko")!=-1) ct = ClientType.NN_6x;
182                     else if (uas.indexOf("mozilla/5")!=-1) ct = ClientType.HTML_4_0_BROWSER;
183                     else if (uas.indexOf("mozilla/4")!=-1) ct = ClientType.NN_4x;
184                     else if (uas.indexOf("mozilla/3")!=-1) ct = ClientType.NN_3x;
185                     else if (uas.indexOf("mozilla 5")!=-1) ct = ClientType.HTML_4_0_BROWSER;
186                     else if (uas.indexOf("mozilla 4")!=-1) ct = ClientType.NN_4x;
187                     else if (uas.indexOf("mozilla 3")!=-1) ct = ClientType.NN_3x;
188                     else ct = ClientType.HTML_BROWSER;
189                 }
190                 if (ct!=ClientType.UNKNOWN_BROWSER) break chk;
191             
192             
193                 //if we're still looking at an unknown browser, now we can get
194
//more specific (we do these checks down here for performance
195
//purposes...we only want to check these when we have to)
196
if (uas.indexOf("lynx")!=-1) ct = ClientType.HTML_3_2_BROWSER;
197                 else if (uas.indexOf("konqueror")!=-1) ct = ClientType.HTML_BROWSER;
198                 else if (uas.indexOf("mozilla")!=-1) ct = ClientType.HTML_BROWSER;
199             //...CHTML
200
} else if (baseType==CHTML) {
201                 //TODO: we still need better granularity for this
202
ct = ClientType.CHTML_BROWSER;
203             //...XML
204
} else if (baseType==XML) {
205                 //TODO: we still need better granularity for this
206
ct = ClientType.XML_BROWSER;
207             //...VXML
208
} else if (baseType==VXML) {
209                 //TODO: we still need better granularity for this
210
ct = ClientType.VXML_BROWSER;
211             //...WML
212
} else if (baseType==WML) {
213                 //TODO: we still need better granularity for this. A good place
214
//to look for a whole slew of sample headers is
215
//http://amaro.g-art.nl/useragent/
216
ct = ClientType.WML_BROWSER;
217             //...XHTML_BASIC
218
} else if (baseType==XHTML_BASIC) {
219                 //TODO: we still need better granularity for this
220
ct = ClientType.XHTML_BROWSER;
221             //...XHTML_STANDARD
222
} else if (baseType==XHTML_STANDARD) {
223                 //TODO: we still need better granularity for this
224
ct = ClientType.XHTML_BROWSER;
225             }
226         }
227         return ct;
228     }
229
230     //look in the accept header to determine the basic content type
231
private static int getBaseType(HttpServletRequest req) {
232         int baseType = HTML; //default
233
chk: if (req!=null) {
234             //get the accept string
235
String JavaDoc acc = req.getHeader("Accept");
236             if (acc==null) acc = req.getHeader("accept");
237             if (acc==null) break chk;
238             acc = acc.toLowerCase();
239
240             //now figure out what we're dealing with
241
if (acc.indexOf("text/html")!=-1) baseType = HTML;
242             else if (acc.indexOf("text/vnd.wap.wml")!=-1) baseType = WML;
243             else if (acc.indexOf("text/chtml")!=-1) baseType = CHTML;
244             else if (acc.indexOf("text/xml")!=-1) baseType = XML;
245             //TODO: identify VXML
246
//TODO: identify XHTML_BASIC
247
//TODO: identify XHTML_STANDARD
248
}
249         return baseType;
250     }
251 }
Popular Tags