1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
5 *
6 * Portions Copyright Apache Software Foundation.
7 *
8 * The contents of this file are subject to the terms of either the GNU
9 * General Public License Version 2 only ("GPL") or the Common Development
10 * and Distribution License("CDDL") (collectively, the "License"). You
11 * may not use this file except in compliance with the License. You can obtain
12 * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
13 * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
14 * language governing permissions and limitations under the License.
15 *
16 * When distributing the software, include this License Header Notice in each
17 * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
18 * Sun designates this particular file as subject to the "Classpath" exception
19 * as provided by Sun in the GPL Version 2 section of the License file that
20 * accompanied this code. If applicable, add the following below the License
21 * Header, with the fields enclosed by brackets [] replaced by your own
22 * identifying information: "Portions Copyrighted [year]
23 * [name of copyright owner]"
24 *
25 * Contributor(s):
26 *
27 * If you wish your version of this file to be governed by only the CDDL or
28 * only the GPL Version 2, indicate your decision by adding "[Contributor]
29 * elects to include this software in this distribution under the [CDDL or GPL
30 * Version 2] license." If you don't indicate a single choice of license, a
31 * recipient has the option to distribute your version of this file under
32 * either the CDDL, the GPL Version 2 or to extend the choice of license to
33 * its licensees as provided above. However, if you add GPL Version 2 code
34 * and therefore, elected the GPL Version 2 license, then the option applies
35 * only if the new code is made subject to such option by the copyright
36 * holder.
37 */
38
39 package javax.servlet.jsp;
40
41 import java.util.Enumeration;
42
43 import javax.servlet.jsp.el.ExpressionEvaluator;
44 import javax.servlet.jsp.el.VariableResolver;
45
46 /**
47 * <p>
48 * <code>JspContext</code> serves as the base class for the
49 * PageContext class and abstracts all information that is not specific
50 * to servlets. This allows for Simple Tag Extensions to be used
51 * outside of the context of a request/response Servlet.
52 * <p>
53 * The JspContext provides a number of facilities to the
54 * page/component author and page implementor, including:
55 * <ul>
56 * <li>a single API to manage the various scoped namespaces
57 * <li>a mechanism to obtain the JspWriter for output
58 * <li>a mechanism to expose page directive attributes to the
59 * scripting environment
60 * </ul>
61 *
62 * <p><B>Methods Intended for Container Generated Code</B>
63 * <p>
64 * The following methods enable the <B>management of nested</B> JspWriter
65 * streams to implement Tag Extensions: <code>pushBody()</code> and
66 * <code>popBody()</code>
67 *
68 * <p><B>Methods Intended for JSP authors</B>
69 * <p>
70 * Some methods provide <B>uniform access</B> to the diverse objects
71 * representing scopes.
72 * The implementation must use the underlying machinery
73 * corresponding to that scope, so information can be passed back and
74 * forth between the underlying environment (e.g. Servlets) and JSP pages.
75 * The methods are:
76 * <code>setAttribute()</code>, <code>getAttribute()</code>,
77 * <code>findAttribute()</code>, <code>removeAttribute()</code>,
78 * <code>getAttributesScope()</code> and
79 * <code>getAttributeNamesInScope()</code>.
80 *
81 * <p>
82 * The following methods provide <B>convenient access</B> to implicit objects:
83 * <code>getOut()</code>
84 *
85 * <p>
86 * The following methods provide <B>programmatic access</b> to the
87 * Expression Language evaluator:
88 * <code>getExpressionEvaluator()</code>, <code>getVariableResolver()</code>
89 *
90 * @since 2.0
91 */
92
93 public abstract class JspContext {
94
95 /**
96 * Sole constructor. (For invocation by subclass constructors,
97 * typically implicit.)
98 */
99 public JspContext() {
100 }
101
102 /**
103 * Register the name and value specified with page scope semantics.
104 * If the value passed in is <code>null</code>, this has the same
105 * effect as calling
106 * <code>removeAttribute( name, PageContext.PAGE_SCOPE )</code>.
107 *
108 * @param name the name of the attribute to set
109 * @param value the value to associate with the name, or null if the
110 * attribute is to be removed from the page scope.
111 * @throws NullPointerException if the name is null
112 */
113
114 abstract public void setAttribute(String name, Object value);
115
116 /**
117 * Register the name and value specified with appropriate
118 * scope semantics. If the value passed in is <code>null</code>,
119 * this has the same effect as calling
120 * <code>removeAttribute( name, scope )</code>.
121 *
122 * @param name the name of the attribute to set
123 * @param value the object to associate with the name, or null if
124 * the attribute is to be removed from the specified scope.
125 * @param scope the scope with which to associate the name/object
126 *
127 * @throws NullPointerException if the name is null
128 * @throws IllegalArgumentException if the scope is invalid
129 * @throws IllegalStateException if the scope is
130 * PageContext.SESSION_SCOPE but the page that was requested
131 * does not participate in a session or the session has been
132 * invalidated.
133 */
134
135 abstract public void setAttribute(String name, Object value, int scope);
136
137 /**
138 * Returns the object associated with the name in the page scope or null
139 * if not found.
140 *
141 * @param name the name of the attribute to get
142 * @return the object associated with the name in the page scope
143 * or null if not found.
144 *
145 * @throws NullPointerException if the name is null
146 */
147
148 abstract public Object getAttribute(String name);
149
150 /**
151 * Return the object associated with the name in the specified
152 * scope or null if not found.
153 *
154 * @param name the name of the attribute to set
155 * @param scope the scope with which to associate the name/object
156 * @return the object associated with the name in the specified
157 * scope or null if not found.
158 *
159 * @throws NullPointerException if the name is null
160 * @throws IllegalArgumentException if the scope is invalid
161 * @throws IllegalStateException if the scope is
162 * PageContext.SESSION_SCOPE but the page that was requested
163 * does not participate in a session or the session has been
164 * invalidated.
165 */
166
167 abstract public Object getAttribute(String name, int scope);
168
169 /**
170 * Searches for the named attribute in page, request, session (if valid),
171 * and application scope(s) in order and returns the value associated or
172 * null.
173 *
174 * @param name the name of the attribute to search for
175 * @return the value associated or null
176 * @throws NullPointerException if the name is null
177 */
178
179 abstract public Object findAttribute(String name);
180
181 /**
182 * Remove the object reference associated with the given name
183 * from all scopes. Does nothing if there is no such object.
184 *
185 * @param name The name of the object to remove.
186 * @throws NullPointerException if the name is null
187 */
188
189 abstract public void removeAttribute(String name);
190
191 /**
192 * Remove the object reference associated with the specified name
193 * in the given scope. Does nothing if there is no such object.
194 *
195 * @param name The name of the object to remove.
196 * @param scope The scope where to look.
197 * @throws IllegalArgumentException if the scope is invalid
198 * @throws IllegalStateException if the scope is
199 * PageContext.SESSION_SCOPE but the page that was requested
200 * does not participate in a session or the session has been
201 * invalidated.
202 * @throws NullPointerException if the name is null
203 */
204
205 abstract public void removeAttribute(String name, int scope);
206
207 /**
208 * Get the scope where a given attribute is defined.
209 *
210 * @param name the name of the attribute to return the scope for
211 * @return the scope of the object associated with the name specified or 0
212 * @throws NullPointerException if the name is null
213 */
214
215 abstract public int getAttributesScope(String name);
216
217 /**
218 * Enumerate all the attributes in a given scope.
219 *
220 * @param scope the scope to enumerate all the attributes for
221 * @return an enumeration of names (java.lang.String) of all the
222 * attributes the specified scope
223 * @throws IllegalArgumentException if the scope is invalid
224 * @throws IllegalStateException if the scope is
225 * PageContext.SESSION_SCOPE but the page that was requested
226 * does not participate in a session or the session has been
227 * invalidated.
228 */
229
230 abstract public Enumeration getAttributeNamesInScope(int scope);
231
232 /**
233 * The current value of the out object (a JspWriter).
234 *
235 * @return the current JspWriter stream being used for client response
236 */
237 abstract public JspWriter getOut();
238
239 /**
240 * Provides programmatic access to the ExpressionEvaluator.
241 * The JSP Container must return a valid instance of an
242 * ExpressionEvaluator that can parse EL expressions.
243 *
244 * @return A valid instance of an ExpressionEvaluator.
245 * @since 2.0
246 */
247 public abstract ExpressionEvaluator getExpressionEvaluator();
248
249 /**
250 * Returns an instance of a VariableResolver that provides access to the
251 * implicit objects specified in the JSP specification using this JspContext
252 * as the context object.
253 *
254 * @return A valid instance of a VariableResolver.
255 * @since 2.0
256 */
257 public abstract VariableResolver getVariableResolver();
258
259 /**
260 * Return a new JspWriter object that sends output to the
261 * provided Writer. Saves the current "out" JspWriter,
262 * and updates the value of the "out" attribute in the
263 * page scope attribute namespace of the JspContext.
264 * <p>The returned JspWriter must implement all methods and
265 * behave as though it were unbuffered. More specifically:
266 * <ul>
267 * <li>clear() must throw an IOException</li>
268 * <li>clearBuffer() does nothing</li>
269 * <li>getBufferSize() always returns 0</li>
270 * <li>getRemaining() always returns 0</li>
271 * </ul>
272 * </p>
273 *
274 * @param writer The Writer for the returned JspWriter to send
275 * output to.
276 * @return a new JspWriter that writes to the given Writer.
277 * @since 2.0
278 */
279 public JspWriter pushBody( java.io.Writer writer ) {
280 return null; // XXX to implement
281 }
282
283 /**
284 * Return the previous JspWriter "out" saved by the matching
285 * pushBody(), and update the value of the "out" attribute in
286 * the page scope attribute namespace of the JspContext.
287 *
288 * @return the saved JspWriter.
289 */
290 public JspWriter popBody() {
291 return null; // XXX to implement
292 }
293 }