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: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.jsp.el;
030:
031: import com.caucho.el.Expr;
032: import com.caucho.jsp.PageContextImpl;
033: import com.caucho.vfs.WriteStream;
034:
035: import javax.el.ELContext;
036: import javax.el.ELException;
037: import javax.servlet.ServletContext;
038: import javax.servlet.http.Cookie;
039: import javax.servlet.http.HttpServletRequest;
040: import javax.servlet.jsp.PageContext;
041: import java.io.IOException;
042: import java.util.*;
043:
044: public class PageContextAttributeMap extends AbstractMap {
045: private PageContext _pageContext;
046: private int _scope;
047:
048: public PageContextAttributeMap(PageContext pageContext, int scope) {
049: _pageContext = pageContext;
050: _scope = scope;
051: }
052:
053: public Object get(Object key) {
054: return _pageContext.getAttribute((String) key, _scope);
055: }
056:
057: public Object put(Object key, Object value) {
058: _pageContext.setAttribute((String) key, value, _scope);
059:
060: return null;
061: }
062:
063: private EntrySet _entrySet;
064:
065: public Set entrySet() {
066: if (_entrySet == null)
067: _entrySet = new EntrySet();
068:
069: return _entrySet;
070: }
071:
072: public class EntrySet extends AbstractSet {
073: public int size() {
074: Enumeration e = _pageContext
075: .getAttributeNamesInScope(_scope);
076: int i = 0;
077: while (e.hasMoreElements()) {
078: e.nextElement();
079: i++;
080: }
081:
082: return i;
083: }
084:
085: public Iterator iterator() {
086: return new EntryIterator();
087: }
088: }
089:
090: public class EntryIterator implements Iterator, Map.Entry {
091: Enumeration _e;
092: String _name;
093: Object _value;
094:
095: EntryIterator() {
096: _e = _pageContext.getAttributeNamesInScope(_scope);
097: }
098:
099: public boolean hasNext() {
100: return _e.hasMoreElements();
101: }
102:
103: public Object next() {
104: _name = (String) _e.nextElement();
105: _value = _pageContext.getAttribute(_name, _scope);
106:
107: return this ;
108: }
109:
110: public void remove() {
111: throw new UnsupportedOperationException();
112: }
113:
114: public Object getKey() {
115: return _name;
116: }
117:
118: public Object getValue() {
119: return _value;
120: }
121:
122: public Object setValue(Object value) {
123: _pageContext.setAttribute(_name, value, _scope);
124:
125: Object oldValue = _value;
126: _value = value;
127:
128: return oldValue;
129: }
130:
131: public int hashCode() {
132: return _name.hashCode();
133: }
134:
135: public boolean equals(Object obj) {
136: if (!(obj instanceof EntryIterator))
137: return false;
138:
139: EntryIterator entry = (EntryIterator) obj;
140:
141: return (_name.equals(entry._name) && (_value == null
142: && entry._value == null || _value != null
143: && _value.equals(entry._value)));
144: }
145: }
146:
147: public String toString() {
148: StringBuilder sb = new StringBuilder();
149:
150: sb.append("[");
151:
152: boolean isFirst = true;
153: Iterator iter = entrySet().iterator();
154: while (iter.hasNext()) {
155: Map.Entry entry = (Map.Entry) iter.next();
156:
157: if (!isFirst)
158: sb.append(", ");
159: isFirst = false;
160:
161: sb.append("{");
162: sb.append(entry.getKey());
163: sb.append(", ");
164: sb.append(entry.getValue());
165: sb.append("}");
166: }
167:
168: sb.append("]");
169:
170: return sb.toString();
171: }
172: }
|