1 10 11 package org.mmbase.bridge.jsp.taglib.util; 12 13 import javax.servlet.jsp.PageContext ; 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 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 pageContext; 39 40 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 pc) { 48 pageContext = pc; 49 } 50 51 public void pushPageContext(PageContext pc) { 52 53 } 54 55 public void pullPageContext(PageContext pc) { 56 57 } 58 public PageContext getPageContext() { 59 return pageContext; 60 } 61 62 63 public void setJspVar(PageContext pc, String jspvar, int vartype, Object value) { 64 if (jspvar == null) return; 65 if (value == null) return; 66 jspvars.add(jspvar); 67 if (vartype == WriterHelper.TYPE_STRING) { 69 Object 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 name; 87 public Object next() { 88 if (nul == null) { 89 name = (String ) back.next(); 90 } else { 91 name = (String ) nul.next(); 92 } 93 return new Map.Entry() { 94 public Object getKey() { 95 return name; 96 } 97 public Object getValue() { 98 if (nul == null) { 99 return pageContext.findAttribute(name); 100 } else { 101 return null; 102 } 103 } 104 public Object setValue(Object value) { 105 Object 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 put(Object key, Object value) { 136 if (value == null) { 137 nulls.add(key); 138 } else { 139 String k = (String ) key; 140 Object 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 get(Object key) { 146 return pageContext.findAttribute((String ) key); 147 } 148 public Object getOriginal(Object key) { 149 Object value = unwrapped.get(key); 150 if (value != null) return value; 151 return pageContext.findAttribute((String ) key); 152 } 153 public boolean containsKey(Object key) { 154 return pageContext.findAttribute((String ) key) != null || nulls.contains(key); 155 } 156 157 public boolean containsOwnKey(Object 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 toString() { 168 return "PAGECONTEXT BACKING " + super.toString(); 169 } 170 171 } 172 | Popular Tags |