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.jsf.el;
030:
031: import com.caucho.el.Expr;
032: import com.caucho.vfs.WriteStream;
033:
034: import javax.el.ELContext;
035: import javax.el.ELException;
036: import javax.faces.context.*;
037: import javax.servlet.ServletContext;
038: import javax.servlet.http.Cookie;
039: import javax.servlet.http.HttpServletRequest;
040: import java.io.IOException;
041: import java.util.*;
042:
043: public class ImplicitObjectExpr extends Expr {
044: private static final HashMap<String, ImplicitObjectExpr> _exprMap = new HashMap<String, ImplicitObjectExpr>();
045:
046: private String _id;
047: private ImplicitEnum _code;
048:
049: private ImplicitObjectExpr(String id, ImplicitEnum code) {
050: _id = id;
051: _code = code;
052: }
053:
054: static ImplicitObjectExpr create(String name) {
055: return _exprMap.get(name);
056: }
057:
058: /**
059: * Evaluate the expr as an object.
060: *
061: * @param env the page context
062: */
063: @Override
064: public Object getValue(ELContext env) throws ELException {
065: FacesContext context = (FacesContext) env
066: .getContext(FacesContext.class);
067:
068: if (context == null)
069: return null;
070:
071: switch (_code) {
072: case APPLICATION:
073: return context.getExternalContext().getContext();
074: case APPLICATION_SCOPE:
075: return context.getExternalContext().getApplicationMap();
076: case COOKIE:
077: return context.getExternalContext().getRequestCookieMap();
078: case FACES_CONTEXT:
079: return context;
080: case HEADER:
081: return context.getExternalContext().getRequestHeaderMap();
082: case HEADER_VALUES:
083: return context.getExternalContext()
084: .getRequestHeaderValuesMap();
085: case INIT_PARAM:
086: return context.getExternalContext().getInitParameterMap();
087: case PARAM:
088: return context.getExternalContext()
089: .getRequestParameterMap();
090: case PARAM_VALUES:
091: return context.getExternalContext()
092: .getRequestParameterValuesMap();
093: case REQUEST:
094: return context.getExternalContext().getRequest();
095: case REQUEST_SCOPE:
096: return context.getExternalContext().getRequestMap();
097: case SESSION:
098: return context.getExternalContext().getSession(true);
099: case SESSION_SCOPE:
100: return context.getExternalContext().getSessionMap();
101: case VIEW:
102: return context.getViewRoot();
103: }
104:
105: throw new UnsupportedOperationException();
106: }
107:
108: public String toString() {
109: return _id;
110: }
111:
112: /**
113: * Prints the code to create an IdExpr.
114: */
115: public void printCreate(WriteStream os) throws IOException {
116: os.print("com.caucho.jsf.el.ImplicitObjectExpr.create(\"");
117: printEscapedString(os, _id);
118: os.print("\")");
119: }
120:
121: enum ImplicitEnum {
122: APPLICATION, APPLICATION_SCOPE, COOKIE, FACES_CONTEXT, HEADER, HEADER_VALUES, INIT_PARAM, PARAM, PARAM_VALUES, REQUEST, REQUEST_SCOPE, SESSION, SESSION_SCOPE, VIEW,
123: };
124:
125: static {
126: _exprMap.put("application", new ImplicitObjectExpr(
127: "application", ImplicitEnum.APPLICATION));
128: _exprMap.put("applicationScope", new ImplicitObjectExpr(
129: "applicationScope", ImplicitEnum.APPLICATION_SCOPE));
130:
131: _exprMap.put("cookie", new ImplicitObjectExpr("cookie",
132: ImplicitEnum.COOKIE));
133: _exprMap.put("facesContext", new ImplicitObjectExpr(
134: "facesContext", ImplicitEnum.FACES_CONTEXT));
135: _exprMap.put("header", new ImplicitObjectExpr("header",
136: ImplicitEnum.HEADER));
137: _exprMap.put("headerValues", new ImplicitObjectExpr(
138: "headerValues", ImplicitEnum.HEADER_VALUES));
139: _exprMap.put("initParam", new ImplicitObjectExpr("initParam",
140: ImplicitEnum.INIT_PARAM));
141: _exprMap.put("param", new ImplicitObjectExpr("param",
142: ImplicitEnum.PARAM));
143: _exprMap.put("paramValues", new ImplicitObjectExpr(
144: "paramValues", ImplicitEnum.PARAM_VALUES));
145: _exprMap.put("request", new ImplicitObjectExpr("request",
146: ImplicitEnum.REQUEST));
147: _exprMap.put("requestScope", new ImplicitObjectExpr(
148: "requestScope", ImplicitEnum.REQUEST_SCOPE));
149: _exprMap.put("session", new ImplicitObjectExpr("session",
150: ImplicitEnum.SESSION));
151: _exprMap.put("sessionScope", new ImplicitObjectExpr(
152: "sessionScope", ImplicitEnum.SESSION_SCOPE));
153: _exprMap.put("view", new ImplicitObjectExpr("view",
154: ImplicitEnum.VIEW));
155: }
156: }
|