KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > jsp > taglib > util > PageContextBacking


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10
11 package org.mmbase.bridge.jsp.taglib.util;
12
13 import javax.servlet.jsp.PageContext JavaDoc;
14 import java.util.*;
15 import org.mmbase.util.Casting;
16 import org.mmbase.util.transformers.CharTransformer;
17 import org.mmbase.bridge.jsp.taglib.ContentTag;
18 import org.mmbase.bridge.jsp.taglib.WriterHelper;
19
20 import org.mmbase.util.logging.Logger;
21 import org.mmbase.util.logging.Logging;
22
23 /**
24  * A basic implementation for the backing, using the PageContext itself. It can also store nulls, in
25  * contradiction to PageContext.
26
27  * @author Michiel Meeuwissen
28  * @since MMBase-1.8
29  * @version $Id: PageContextBacking.java,v 1.11.2.1 2006/11/21 20:39:56 michiel Exp $
30  */

31
32 public class PageContextBacking extends AbstractMap implements Backing {
33
34     private static final Logger log = Logging.getLoggerInstance(PageContextBacking.class);
35
36     private static final int SCOPE = PageContext.PAGE_SCOPE;
37
38     private final PageContext JavaDoc pageContext;
39
40     // We also want to store null, pageContext cannot contain those.
41
private final Set nulls = new HashSet();
42
43     private final Set jspvars = new HashSet();
44
45     private final Map unwrapped = new HashMap();
46
47     public PageContextBacking(PageContext JavaDoc pc) {
48         pageContext = pc;
49     }
50
51     public void pushPageContext(PageContext JavaDoc pc) {
52
53     }
54
55     public void pullPageContext(PageContext JavaDoc pc) {
56
57     }
58     public PageContext JavaDoc getPageContext() {
59         return pageContext;
60     }
61
62
63     public void setJspVar(PageContext JavaDoc pc, String JavaDoc jspvar, int vartype, Object JavaDoc value) {
64         if (jspvar == null) return;
65         if (value == null) return;
66         jspvars.add(jspvar);
67         // When it doesn't, it goes ok. (at least I think that this is the difference between orion and tomcat)
68
if (vartype == WriterHelper.TYPE_STRING) {
69             // string is final, the wrapped version cannot be string..
70
Object JavaDoc v = Casting.unWrap(value);
71             if (v == null) return;
72             pc.setAttribute(jspvar, v);
73         } else {
74             pc.setAttribute(jspvar, value);
75         }
76
77     }
78
79     public Set entrySet() {
80         return new AbstractSet() {
81                 Collection names = unwrapped.keySet();
82                 public Iterator iterator() {
83                     return new Iterator() {
84                             Iterator back = names.iterator();
85                             Iterator nul = null;
86                             String JavaDoc name;
87                             public Object JavaDoc next() {
88                                 if (nul == null) {
89                                     name = (String JavaDoc) back.next();
90                                 } else {
91                                     name = (String JavaDoc) nul.next();
92                                 }
93                                 return new Map.Entry() {
94                                         public Object JavaDoc getKey() {
95                                             return name;
96                                         }
97                                         public Object JavaDoc getValue() {
98                                             if (nul == null) {
99                                                 return pageContext.findAttribute(name);
100                                             } else {
101                                                 return null;
102                                             }
103                                         }
104                                         public Object JavaDoc setValue(Object JavaDoc value) {
105                                             Object JavaDoc was = pageContext.findAttribute(name);
106                                             if (value != null) {
107                                                 pageContext.setAttribute(name, jspvars.contains(name) ? value : Casting.wrap(value, (CharTransformer) pageContext.findAttribute(ContentTag.ESCAPER_KEY)), SCOPE);
108                                             } else {
109                                                 pageContext.removeAttribute(name, SCOPE);
110                                                 nulls.add(name);
111                                             }
112                                             return was;
113                                         }
114                                     };
115                             }
116                             public boolean hasNext() {
117                                 if (back.hasNext()) return true;
118                                 if (nul == null) nul = nulls.iterator();
119                                 return nul.hasNext();
120                             }
121                             public void remove() {
122                                 pageContext.removeAttribute(name, SCOPE);
123                                 nulls.remove(name);
124                                 unwrapped.remove(name);
125                             }
126                         };
127                 }
128                 public int size() {
129                     return names.size();
130                 }
131             };
132     }
133
134
135     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
136         if (value == null) {
137             nulls.add(key);
138         } else {
139             String JavaDoc k = (String JavaDoc) key;
140             Object JavaDoc v = jspvars.contains(key) ? value : Casting.wrap(value, (CharTransformer) pageContext.findAttribute(ContentTag.ESCAPER_KEY));
141             pageContext.setAttribute(k, v, SCOPE);
142         }
143         return unwrapped.put(key, value);
144     }
145     public Object JavaDoc get(Object JavaDoc key) {
146         return pageContext.findAttribute((String JavaDoc) key);
147     }
148     public Object JavaDoc getOriginal(Object JavaDoc key) {
149         Object JavaDoc value = unwrapped.get(key);
150         if (value != null) return value;
151         return pageContext.findAttribute((String JavaDoc) key);
152     }
153     public boolean containsKey(Object JavaDoc key) {
154         return pageContext.findAttribute((String JavaDoc) key) != null || nulls.contains(key);
155     }
156
157     public boolean containsOwnKey(Object JavaDoc key) {
158         return unwrapped.containsKey(key);
159     }
160
161     void release() {
162         nulls.clear();
163         unwrapped.clear();
164         jspvars.clear();
165     }
166
167     public String JavaDoc toString() {
168         return "PAGECONTEXT BACKING " + super.toString();
169     }
170
171 }
172
Popular Tags