1 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 ; 38 import javax.servlet.http.HttpServletRequest ; 39 import javax.servlet.http.HttpSession ; 40 import javax.servlet.jsp.JspContext ; 41 import javax.servlet.jsp.PageContext ; 42 import java.io.IOException ; 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 61 @Override 62 public Object getValue(ELContext env) 63 throws ELException 64 { 65 PageContext page = (PageContext ) env.getContext(JspContext .class); 66 67 Object fieldValue = _field.evalObject(env); 68 69 if (fieldValue == null) 70 return null; 71 72 String 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 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 ) page.getRequest()).getHeader(fieldString); 105 106 case ImplicitObjectExpr.HEADER_VALUES: { 107 Enumeration e = 108 ((HttpServletRequest ) page.getRequest()).getHeaders(fieldString); 109 110 if (e == null) 111 return null; 112 113 if (! e.hasMoreElements()) 114 return null; 115 116 ArrayList<String > list = new ArrayList<String >(); 117 118 while (e.hasMoreElements()) 119 list.add((String ) e.nextElement()); 120 121 return list.toArray(new String [list.size()]); 122 } 123 124 case ImplicitObjectExpr.INIT_PARAM: 125 return page.getServletContext().getInitParameter(fieldString); 126 127 case ImplicitObjectExpr.COOKIE: { 128 Cookie []cookies = ((HttpServletRequest ) 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 (); 142 } 143 144 public String toString() 145 { 146 return "implicit_" + _index + "[" + _field + "]"; 147 } 148 149 152 @Override 153 public void printCreate(WriteStream os) 154 throws IOException 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 _pageContext; 165 private int _scope; 166 167 AttributeMap(PageContext pageContext, int scope) 168 { 169 _pageContext = pageContext; 170 _scope = scope; 171 } 172 173 public Object get(Object key) 174 { 175 return _pageContext.getAttribute((String ) key, _scope); 176 } 177 178 public Object put(Object key, Object value) 179 { 180 _pageContext.setAttribute((String ) 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 _name; 217 Object _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 next() 230 { 231 _name = (String ) _e.nextElement(); 232 _value = _pageContext.getAttribute(_name, _scope); 233 234 return this; 235 } 236 237 public void remove() 238 { 239 throw new UnsupportedOperationException (); 240 } 241 242 public Object getKey() 243 { 244 return _name; 245 } 246 247 public Object getValue() 248 { 249 return _value; 250 } 251 252 public Object setValue(Object value) 253 { 254 _pageContext.setAttribute(_name, value, _scope); 255 256 Object 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 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 |