KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jsp > el > ImplicitObjectExpr


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jsp.el;
30
31 import com.caucho.el.Expr;
32 import com.caucho.jsp.PageContextImpl;
33 import com.caucho.vfs.WriteStream;
34
35 import javax.el.ELContext;
36 import javax.el.ELException;
37 import javax.servlet.ServletContext JavaDoc;
38 import javax.servlet.http.Cookie JavaDoc;
39 import javax.servlet.http.HttpServletRequest JavaDoc;
40 import javax.servlet.jsp.PageContext JavaDoc;
41 import java.io.IOException JavaDoc;
42 import java.util.*;
43
44 public class ImplicitObjectExpr extends Expr {
45   final static int PAGE_CONTEXT = 1;
46   
47   final static int APPLICATION_SCOPE = PAGE_CONTEXT + 1;
48   final static int SESSION_SCOPE = APPLICATION_SCOPE + 1;
49   final static int REQUEST_SCOPE = SESSION_SCOPE + 1;
50   final static int PAGE_SCOPE = REQUEST_SCOPE + 1;
51   
52   final static int PARAM = PAGE_SCOPE + 1;
53   final static int PARAM_VALUES = PARAM + 1;
54   
55   final static int INIT_PARAM = PARAM_VALUES + 1;
56   
57   final static int HEADER = INIT_PARAM + 1;
58   final static int HEADER_VALUES = HEADER + 1;
59   
60   final static int COOKIE = HEADER_VALUES + 1;
61
62   private String JavaDoc _id;
63   private int _index;
64
65   public ImplicitObjectExpr(String JavaDoc id)
66   {
67     _id = id;
68
69     if ("pageContext".equals(id))
70       _index = PAGE_CONTEXT;
71     else if ("applicationScope".equals(id))
72       _index = APPLICATION_SCOPE;
73     else if ("sessionScope".equals(id))
74       _index = SESSION_SCOPE;
75     else if ("requestScope".equals(id))
76       _index = REQUEST_SCOPE;
77     else if ("pageScope".equals(id))
78       _index = PAGE_SCOPE;
79     else if ("param".equals(id))
80       _index = PARAM;
81     else if ("paramValues".equals(id))
82       _index = PARAM_VALUES;
83     else if ("initParam".equals(id))
84       _index = INIT_PARAM;
85     else if ("header".equals(id))
86       _index = HEADER;
87     else if ("headerValues".equals(id))
88       _index = HEADER_VALUES;
89     else if ("cookie".equals(id))
90       _index = COOKIE;
91     else
92       throw new IllegalArgumentException JavaDoc();
93   }
94
95   public Expr createField(Expr field)
96   {
97     switch (_index) {
98     case APPLICATION_SCOPE:
99     case SESSION_SCOPE:
100     case REQUEST_SCOPE:
101     case PAGE_SCOPE:
102     case PARAM:
103     case PARAM_VALUES:
104     case HEADER:
105     case HEADER_VALUES:
106     case COOKIE:
107     case INIT_PARAM:
108       return new ImplicitFieldExpr(_index, field);
109       
110     default:
111       return super.createField(field);
112     }
113   }
114
115   /**
116    * Evaluate the expr as an object.
117    *
118    * @param env the page context
119    */

120   @Override JavaDoc
121   public Object JavaDoc getValue(ELContext env)
122     throws ELException
123   {
124     if (! (env instanceof PageContextImpl.PageELContext))
125       return null;
126     
127     PageContextImpl page
128       = ((PageContextImpl.PageELContext) env).getPageContext();
129     
130     switch (_index) {
131     case PAGE_CONTEXT:
132       return page;
133
134     case APPLICATION_SCOPE:
135       return new AttributeMap(page, PageContext.APPLICATION_SCOPE);
136
137     case SESSION_SCOPE:
138       return new AttributeMap(page, PageContext.SESSION_SCOPE);
139
140     case REQUEST_SCOPE:
141       return new AttributeMap(page, PageContext.REQUEST_SCOPE);
142
143     case PAGE_SCOPE:
144       return new AttributeMap(page, PageContext.PAGE_SCOPE);
145
146     case PARAM_VALUES:
147       return page.getRequest().getParameterMap();
148
149     case PARAM: {
150       HashMap<String JavaDoc,String JavaDoc> map = new HashMap<String JavaDoc,String JavaDoc>();
151       Map pMap = page.getRequest().getParameterMap();
152       Iterator iter = pMap.entrySet().iterator();
153       
154       while (iter.hasNext()) {
155         Map.Entry entry = (Map.Entry) iter.next();
156         String JavaDoc key = (String JavaDoc) entry.getKey();
157         String JavaDoc []value = (String JavaDoc []) entry.getValue();
158         map.put(key, value[0]);
159       }
160       
161       return map;
162     }
163
164     case INIT_PARAM:
165     {
166       ServletContext JavaDoc app = page.getServletContext();
167       HashMap<String JavaDoc,String JavaDoc> map = new HashMap<String JavaDoc,String JavaDoc>();
168       Enumeration e = app.getInitParameterNames();
169
170       while (e.hasMoreElements()) {
171         String JavaDoc name = (String JavaDoc) e.nextElement();
172
173         map.put(name, app.getInitParameter(name));
174       }
175       
176       return map;
177     }
178
179     case HEADER:
180     {
181       HttpServletRequest JavaDoc req = (HttpServletRequest JavaDoc) page.getRequest();
182       HashMap<String JavaDoc,String JavaDoc> map = new HashMap<String JavaDoc,String JavaDoc>();
183       Enumeration e = req.getHeaderNames();
184
185       while (e.hasMoreElements()) {
186         String JavaDoc name = (String JavaDoc) e.nextElement();
187
188         map.put(name, req.getHeader(name));
189       }
190       
191       return map;
192     }
193
194     case HEADER_VALUES:
195     {
196       HttpServletRequest JavaDoc req = (HttpServletRequest JavaDoc) page.getRequest();
197       HashMap<String JavaDoc,String JavaDoc[]> map = new HashMap<String JavaDoc,String JavaDoc[]>();
198       Enumeration e = req.getHeaderNames();
199
200       while (e.hasMoreElements()) {
201         String JavaDoc name = (String JavaDoc) e.nextElement();
202         Enumeration values = req.getHeaders(name);
203       
204         ArrayList<String JavaDoc> list = new ArrayList<String JavaDoc>();
205
206         while (values.hasMoreElements())
207           list.add((String JavaDoc) values.nextElement());
208
209         map.put(name, list.toArray(new String JavaDoc[list.size()]));
210       }
211       
212       return map;
213     }
214
215     case COOKIE:
216     {
217       HashMap<String JavaDoc,Object JavaDoc> map = new HashMap<String JavaDoc,Object JavaDoc>();
218       Cookie JavaDoc []cookies = ((HttpServletRequest JavaDoc) page.getRequest()).getCookies();
219
220       for (int i = 0; cookies != null && i < cookies.length; i++) {
221         if (map.get(cookies[i].getName()) == null)
222           map.put(cookies[i].getName(), cookies[i]);
223       }
224       
225       return map;
226     }
227     }
228
229     throw new UnsupportedOperationException JavaDoc();
230   }
231
232   public String JavaDoc toString()
233   {
234     return _id;
235   }
236
237   /**
238    * Prints the code to create an IdExpr.
239    */

240   public void printCreate(WriteStream os)
241     throws IOException JavaDoc
242   {
243     os.print("new com.caucho.jsp.el.ImplicitObjectExpr(\"");
244     printEscapedString(os, _id);
245     os.print("\")");
246   }
247
248   public static class AttributeMap extends AbstractMap {
249     private PageContext JavaDoc _pageContext;
250     private int _scope;
251     
252     AttributeMap(PageContext JavaDoc pageContext, int scope)
253     {
254       _pageContext = pageContext;
255       _scope = scope;
256     }
257
258     public Object JavaDoc get(Object JavaDoc key)
259     {
260       return _pageContext.getAttribute((String JavaDoc) key, _scope);
261     }
262
263     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value)
264     {
265       _pageContext.setAttribute((String JavaDoc) key, value, _scope);
266
267       return null;
268     }
269
270     private EntrySet _entrySet;
271     
272     public Set entrySet()
273     {
274       if (_entrySet == null)
275         _entrySet = new EntrySet();
276       
277       return _entrySet;
278     }
279
280     public class EntrySet extends AbstractSet {
281       public int size()
282       {
283         Enumeration e = _pageContext.getAttributeNamesInScope(_scope);
284         int i = 0;
285         while (e.hasMoreElements()) {
286           e.nextElement();
287           i++;
288         }
289
290         return i;
291       }
292       
293       public Iterator iterator()
294       {
295         return new EntryIterator();
296       }
297     }
298
299     public class EntryIterator implements Iterator, Map.Entry {
300       Enumeration _e;
301       String JavaDoc _name;
302       Object JavaDoc _value;
303
304       EntryIterator()
305       {
306         _e = _pageContext.getAttributeNamesInScope(_scope);
307       }
308
309       public boolean hasNext()
310       {
311         return _e.hasMoreElements();
312       }
313
314       public Object JavaDoc next()
315       {
316         _name = (String JavaDoc) _e.nextElement();
317         _value = _pageContext.getAttribute(_name, _scope);
318
319         return this;
320       }
321
322       public void remove()
323       {
324         throw new UnsupportedOperationException JavaDoc();
325       }
326
327       public Object JavaDoc getKey()
328       {
329         return _name;
330       }
331
332       public Object JavaDoc getValue()
333       {
334         return _value;
335       }
336
337       public Object JavaDoc setValue(Object JavaDoc value)
338       {
339         _pageContext.setAttribute(_name, value, _scope);
340         
341         Object JavaDoc oldValue = _value;
342         _value = value;
343         
344         return oldValue;
345       }
346
347       public int hashCode()
348       {
349         return _name.hashCode();
350       }
351
352       public boolean equals(Object JavaDoc obj)
353       {
354         if (! (obj instanceof EntryIterator))
355           return false;
356         
357         EntryIterator entry = (EntryIterator) obj;
358
359         return (_name.equals(entry._name) &&
360                 (_value == null && entry._value == null ||
361                  _value != null && _value.equals(entry._value)));
362       }
363     }
364   }
365 }
366
Popular Tags