001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.jsp.el;
031:
032: import com.caucho.el.Expr;
033: import com.caucho.vfs.WriteStream;
034:
035: import javax.el.ELContext;
036: import javax.el.ELException;
037: import javax.servlet.http.Cookie;
038: import javax.servlet.http.HttpServletRequest;
039: import javax.servlet.http.HttpSession;
040: import javax.servlet.jsp.JspContext;
041: import javax.servlet.jsp.PageContext;
042: import java.io.IOException;
043: import java.util.*;
044:
045: public class ImplicitFieldExpr extends Expr {
046: private int _index;
047:
048: private Expr _field;
049:
050: public ImplicitFieldExpr(int index, Expr field) {
051: _index = index;
052: _field = field;
053: }
054:
055: /**
056: * Evaluate the expr as an object.
057: *
058: * @param env the page context
059: */
060: @Override
061: public Object getValue(ELContext env) throws ELException {
062: PageContext page = (PageContext) env
063: .getContext(JspContext.class);
064:
065: if (page == null)
066: return null;
067:
068: Object fieldValue = _field.evalObject(env);
069:
070: if (fieldValue == null)
071: return null;
072:
073: String fieldString = toString(fieldValue, env);
074:
075: switch (_index) {
076: case ImplicitObjectExpr.APPLICATION_SCOPE:
077: return page.getServletContext().getAttribute(fieldString);
078:
079: case ImplicitObjectExpr.SESSION_SCOPE: {
080: HttpSession session = page.getSession();
081:
082: if (session != null) {
083: return session.getAttribute(fieldString);
084: } else
085: return null;
086: }
087:
088: case ImplicitObjectExpr.REQUEST_SCOPE: {
089: return page.getRequest().getAttribute(fieldString);
090: }
091:
092: case ImplicitObjectExpr.PAGE_SCOPE:
093: return page.getAttribute(fieldString);
094:
095: case ImplicitObjectExpr.PARAM:
096: return page.getRequest().getParameter(fieldString);
097:
098: case ImplicitObjectExpr.PARAM_VALUES:
099: return page.getRequest().getParameterValues(fieldString);
100:
101: case ImplicitObjectExpr.HEADER:
102: return ((HttpServletRequest) page.getRequest())
103: .getHeader(fieldString);
104:
105: case ImplicitObjectExpr.HEADER_VALUES: {
106: Enumeration e = ((HttpServletRequest) page.getRequest())
107: .getHeaders(fieldString);
108:
109: if (e == null)
110: return null;
111:
112: if (!e.hasMoreElements())
113: return null;
114:
115: ArrayList<String> list = new ArrayList<String>();
116:
117: while (e.hasMoreElements())
118: list.add((String) e.nextElement());
119:
120: return list.toArray(new String[list.size()]);
121: }
122:
123: case ImplicitObjectExpr.INIT_PARAM:
124: return page.getServletContext().getInitParameter(
125: fieldString);
126:
127: case ImplicitObjectExpr.COOKIE: {
128: Cookie[] cookies = ((HttpServletRequest) page.getRequest())
129: .getCookies();
130: if (cookies == null)
131: return null;
132:
133: for (int i = 0; i < cookies.length; i++) {
134: if (cookies[i].getName().equals(fieldString))
135: return cookies[i];
136: }
137:
138: return null;
139: }
140: }
141:
142: throw new UnsupportedOperationException();
143: }
144:
145: public String toString() {
146: return "implicit_" + _index + "[" + _field + "]";
147: }
148:
149: /**
150: * Prints the code to create an IdExpr.
151: */
152: @Override
153: public void printCreate(WriteStream os) throws IOException {
154: os.print("new com.caucho.jsp.el.ImplicitFieldExpr(");
155: os.print(_index);
156: os.print(", ");
157: _field.printCreate(os);
158: os.print(")");
159: }
160:
161: public static class AttributeMap extends AbstractMap {
162: private PageContext _pageContext;
163: private int _scope;
164:
165: AttributeMap(PageContext pageContext, int scope) {
166: _pageContext = pageContext;
167: _scope = scope;
168: }
169:
170: public Object get(Object key) {
171: return _pageContext.getAttribute((String) key, _scope);
172: }
173:
174: public Object put(Object key, Object value) {
175: _pageContext.setAttribute((String) key, value, _scope);
176:
177: return null;
178: }
179:
180: private EntrySet _entrySet;
181:
182: public Set entrySet() {
183: if (_entrySet == null)
184: _entrySet = new EntrySet();
185:
186: return _entrySet;
187: }
188:
189: public class EntrySet extends AbstractSet {
190: public int size() {
191: Enumeration e = _pageContext
192: .getAttributeNamesInScope(_scope);
193: int i = 0;
194: while (e.hasMoreElements()) {
195: e.nextElement();
196: i++;
197: }
198:
199: return i;
200: }
201:
202: public Iterator iterator() {
203: return new EntryIterator();
204: }
205: }
206:
207: public class EntryIterator implements Iterator, Map.Entry {
208: Enumeration _e;
209: String _name;
210: Object _value;
211:
212: EntryIterator() {
213: _e = _pageContext.getAttributeNamesInScope(_scope);
214: }
215:
216: public boolean hasNext() {
217: return _e.hasMoreElements();
218: }
219:
220: public Object next() {
221: _name = (String) _e.nextElement();
222: _value = _pageContext.getAttribute(_name, _scope);
223:
224: return this ;
225: }
226:
227: public void remove() {
228: throw new UnsupportedOperationException();
229: }
230:
231: public Object getKey() {
232: return _name;
233: }
234:
235: public Object getValue() {
236: return _value;
237: }
238:
239: public Object setValue(Object value) {
240: _pageContext.setAttribute(_name, value, _scope);
241:
242: Object oldValue = _value;
243: _value = value;
244:
245: return oldValue;
246: }
247:
248: public int hashCode() {
249: return _name.hashCode();
250: }
251:
252: public boolean equals(Object obj) {
253: if (!(obj instanceof EntryIterator))
254: return false;
255:
256: EntryIterator entry = (EntryIterator) obj;
257:
258: return (_name.equals(entry._name) && (_value == null
259: && entry._value == null || _value != null
260: && _value.equals(entry._value)));
261: }
262: }
263: }
264: }
|