KickJava   Java API By Example, From Geeks To Geeks.

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


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  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.jsp.el;
31
32 import com.caucho.el.Expr;
33 import com.caucho.vfs.WriteStream;
34
35 import javax.el.ELContext;
36 import javax.el.ELException;
37 import javax.servlet.http.Cookie JavaDoc;
38 import javax.servlet.http.HttpServletRequest JavaDoc;
39 import javax.servlet.http.HttpSession JavaDoc;
40 import javax.servlet.jsp.JspContext JavaDoc;
41 import javax.servlet.jsp.PageContext JavaDoc;
42 import java.io.IOException JavaDoc;
43 import java.util.*;
44
45 public class ImplicitFieldExpr extends Expr {
46   private int _index;
47
48   private Expr _field;
49
50   public ImplicitFieldExpr(int index, Expr field)
51   {
52     _index = index;
53     _field = field;
54   }
55
56   /**
57    * Evaluate the expr as an object.
58    *
59    * @param env the page context
60    */

61   @Override JavaDoc
62   public Object JavaDoc getValue(ELContext env)
63     throws ELException
64   {
65     PageContext JavaDoc page = (PageContext JavaDoc) env.getContext(JspContext JavaDoc.class);
66
67     Object JavaDoc fieldValue = _field.evalObject(env);
68
69     if (fieldValue == null)
70       return null;
71
72     String JavaDoc fieldString = toString(fieldValue, env);
73
74     switch (_index) {
75     case ImplicitObjectExpr.APPLICATION_SCOPE:
76       return page.getServletContext().getAttribute(fieldString);
77
78     case ImplicitObjectExpr.SESSION_SCOPE:
79       {
80     HttpSession JavaDoc session = page.getSession();
81
82     if (session != null) {
83       return session.getAttribute(fieldString);
84     }
85     else
86       return null;
87       }
88
89     case ImplicitObjectExpr.REQUEST_SCOPE:
90       {
91     return page.getRequest().getAttribute(fieldString);
92       }
93
94     case ImplicitObjectExpr.PAGE_SCOPE:
95       return page.getAttribute(fieldString);
96       
97     case ImplicitObjectExpr.PARAM:
98       return page.getRequest().getParameter(fieldString);
99
100     case ImplicitObjectExpr.PARAM_VALUES:
101       return page.getRequest().getParameterValues(fieldString);
102       
103     case ImplicitObjectExpr.HEADER:
104       return ((HttpServletRequest JavaDoc) page.getRequest()).getHeader(fieldString);
105
106     case ImplicitObjectExpr.HEADER_VALUES: {
107       Enumeration e =
108         ((HttpServletRequest JavaDoc) page.getRequest()).getHeaders(fieldString);
109
110       if (e == null)
111         return null;
112
113       if (! e.hasMoreElements())
114         return null;
115       
116       ArrayList<String JavaDoc> list = new ArrayList<String JavaDoc>();
117
118       while (e.hasMoreElements())
119         list.add((String JavaDoc) e.nextElement());
120       
121       return list.toArray(new String JavaDoc[list.size()]);
122     }
123       
124     case ImplicitObjectExpr.INIT_PARAM:
125       return page.getServletContext().getInitParameter(fieldString);
126
127     case ImplicitObjectExpr.COOKIE: {
128       Cookie JavaDoc []cookies = ((HttpServletRequest JavaDoc) page.getRequest()).getCookies();
129       if (cookies == null)
130         return null;
131
132       for (int i = 0; i < cookies.length; i++) {
133         if (cookies[i].getName().equals(fieldString))
134           return cookies[i];
135       }
136
137       return null;
138     }
139     }
140       
141     throw new UnsupportedOperationException JavaDoc();
142   }
143
144   public String JavaDoc toString()
145   {
146     return "implicit_" + _index + "[" + _field + "]";
147   }
148   
149   /**
150    * Prints the code to create an IdExpr.
151    */

152   @Override JavaDoc
153   public void printCreate(WriteStream os)
154     throws IOException JavaDoc
155   {
156     os.print("new com.caucho.jsp.el.ImplicitFieldExpr(");
157     os.print(_index);
158     os.print(", ");
159     _field.printCreate(os);
160     os.print(")");
161   }
162
163   public static class AttributeMap extends AbstractMap {
164     private PageContext JavaDoc _pageContext;
165     private int _scope;
166     
167     AttributeMap(PageContext JavaDoc pageContext, int scope)
168     {
169       _pageContext = pageContext;
170       _scope = scope;
171     }
172
173     public Object JavaDoc get(Object JavaDoc key)
174     {
175       return _pageContext.getAttribute((String JavaDoc) key, _scope);
176     }
177
178     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value)
179     {
180       _pageContext.setAttribute((String JavaDoc) key, value, _scope);
181
182       return null;
183     }
184
185     private EntrySet _entrySet;
186     
187     public Set entrySet()
188     {
189       if (_entrySet == null)
190         _entrySet = new EntrySet();
191       
192       return _entrySet;
193     }
194
195     public class EntrySet extends AbstractSet {
196       public int size()
197       {
198         Enumeration e = _pageContext.getAttributeNamesInScope(_scope);
199         int i = 0;
200         while (e.hasMoreElements()) {
201           e.nextElement();
202           i++;
203         }
204
205         return i;
206       }
207       
208       public Iterator iterator()
209       {
210         return new EntryIterator();
211       }
212     }
213
214     public class EntryIterator implements Iterator, Map.Entry {
215       Enumeration _e;
216       String JavaDoc _name;
217       Object JavaDoc _value;
218
219       EntryIterator()
220       {
221         _e = _pageContext.getAttributeNamesInScope(_scope);
222       }
223
224       public boolean hasNext()
225       {
226         return _e.hasMoreElements();
227       }
228
229       public Object JavaDoc next()
230       {
231         _name = (String JavaDoc) _e.nextElement();
232         _value = _pageContext.getAttribute(_name, _scope);
233
234         return this;
235       }
236
237       public void remove()
238       {
239         throw new UnsupportedOperationException JavaDoc();
240       }
241
242       public Object JavaDoc getKey()
243       {
244         return _name;
245       }
246
247       public Object JavaDoc getValue()
248       {
249         return _value;
250       }
251
252       public Object JavaDoc setValue(Object JavaDoc value)
253       {
254         _pageContext.setAttribute(_name, value, _scope);
255         
256         Object JavaDoc oldValue = _value;
257         _value = value;
258         
259         return oldValue;
260       }
261
262       public int hashCode()
263       {
264         return _name.hashCode();
265       }
266
267       public boolean equals(Object JavaDoc obj)
268       {
269         if (! (obj instanceof EntryIterator))
270           return false;
271         
272         EntryIterator entry = (EntryIterator) obj;
273
274         return (_name.equals(entry._name) &&
275                 (_value == null && entry._value == null ||
276                  _value != null && _value.equals(entry._value)));
277       }
278     }
279   }
280 }
281
Popular Tags