KickJava   Java API By Example, From Geeks To Geeks.

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


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: ViewCapabilities.java,v 1.12 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.plankton.l10n.*;
27
28 /**
29  * This class defines the the cpabilities & preferences of the
30  * client view. Specifically, it provides the component with
31  * 4 useful pieces of information:
32  *
33  * <ol>
34  * <li>FormatType - what format is the output expected to be in (HTML, WML,
35  * XMLC, etc.)</li>
36  * <li>ScriptingType - what scripting capabilities does the client
37  * support (JavaScript, VBScript, WMLScript, etc)</li>
38  * <li>ClientType - what browser is the client using (HTML_3x, HTML_4x,
39  * WML_1_2, etc)</li>
40  * <li>ClientLocale - what Locale does the client prefer content in?</li>
41  * </ol>
42  *
43  * <p>Note that it's up to the components to actually respond to this. Just because
44  * a client supports HTML 4.0 doesn't mean the component can't render in HTML 3.2
45  * (although the component does have an implicit responsibility to be well behaved
46  * and send back meaningful content that will work in the clients configuration)
47  *
48  * <p>It should also be noted that the FormatType, ScriptingType, and ClientType
49  * classes are all defined hierarchically. This is particularly important because
50  * it allows components to support a given range of functionality...if a component
51  * supports HTML 4x and HTML 4.1 comes out, the component should still work just fine.
52  *
53  * <p>Note that we don't determine the various types from the source until they are
54  * actually requested (unless of course they are manually specified).
55  */

56 public class ViewCapabilities {
57
58     protected HttpServletRequest req = null;
59     protected HttpServletResponse resp = null;
60
61     protected FormatType formatType = null;
62     protected ScriptingType scriptingType = null;
63     protected ClientType clientType = null;
64     protected Locale clientLocale = null;
65     
66     /**
67      * Create an empty ViewCapabilities object. In order to use this
68      * class, you will either need to manually set the req/resp source,
69      * or manually specify the various type properties.
70      */

71     public ViewCapabilities() {
72         this(null, null, null, null);
73     }
74     
75     /**
76      * This constructor creates a ViewCapabilities object from the servlet
77      * response and request objects.
78      *
79      * @param req the servlet request
80      * @param resp the servlet response
81      */

82     public ViewCapabilities(HttpServletRequest req, HttpServletResponse resp) {
83         setSource(req, resp);
84     }
85     
86     /**
87      * This constructor allows you to manually specify the various
88      * types for a ViewCapabilities object.
89      *
90      * @param formatType the format type for the view
91      * @param clientType the actual client view type
92      * @param scriptingType the scripting type supported by the view
93      * @param clientLocale the target client locale
94      */

95     public ViewCapabilities(FormatType formatType, ClientType clientType, ScriptingType scriptingType, Locale clientLocale) {
96         setFormatType(formatType);
97         setClientType(clientType);
98         setScriptingType(scriptingType);
99         setClientLocale(clientLocale);
100     }
101     
102     /**
103      * This convenience method allows you to specify the servlet req/resp
104      * source objects (from which the types will be automagically determined)
105      *
106      * @param ireq the servlet request
107      * @param iresp the servlet response
108      */

109     public void setSource(HttpServletRequest ireq, HttpServletResponse iresp) {
110         req = ireq;
111         resp = iresp;
112     }
113     
114     /**
115      * Manually specify a format type
116      *
117      * @param iformatType the format type for the view
118      */

119     public void setFormatType(FormatType iformatType) {
120         formatType = iformatType;
121     }
122     
123     /**
124      * Get the current format type
125      *
126      * @return the current format type
127      */

128     public FormatType getFormatType() {
129         if (formatType==null) {
130             formatType = ViewUtil.getFormatType(req);
131         }
132         return formatType;
133     }
134
135     /**
136      * Manually specify a client type
137      *
138      * @param iclientType the actual client view type
139      */

140     public void setClientType(ClientType iclientType) {
141         clientType = iclientType;
142     }
143     
144     /**
145      * Get the current client type
146      *
147      * @return the current client type
148      */

149     public ClientType getClientType() {
150         if (clientType==null) {
151             clientType = ViewUtil.getClientType(req);
152         }
153         return clientType;
154     }
155
156     /**
157      * Manually specify a scripting type
158      *
159      * @param iscriptingType the scripting type supported by the view
160      */

161     public void setScriptingType(ScriptingType iscriptingType) {
162         scriptingType = iscriptingType;
163     }
164     
165     /**
166      * Get the current scripting type
167      *
168      * @return the current scripting type
169      */

170     public ScriptingType getScriptingType() {
171         if (scriptingType==null) {
172             scriptingType = ViewUtil.getScriptingType(req);
173         }
174         return scriptingType;
175     }
176
177     /**
178      * Manually specify a target locale
179      *
180      * @param iclientLocale the target client locale
181      */

182     public void setClientLocale(Locale iclientLocale) {
183         clientLocale = iclientLocale;
184     }
185     
186     /**
187      * Get the current target locale
188      *
189      * @return the current target locale
190      */

191     public Locale getClientLocale() {
192         if (clientLocale==null) {
193             clientLocale = Locales.getClientLocale(req, resp);
194         }
195         return clientLocale;
196     }
197     
198     /**
199      * get a string representation of the ViewCapabilities
200      *
201      * @return a string representation of the ViewCapabilities
202      */

203     public String JavaDoc toString() {
204         return super.toString()+" {"+
205            "ft="+getFormatType()+", "+
206            "ct="+getClientType()+", "+
207            "st="+getScriptingType()+", "+
208            "loc="+(clientLocale==null ? "default" : getClientLocale().toString())+"}";
209     }
210 }
Popular Tags