001: /* ServletDspContext.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Sat Sep 17 19:27:35 2005, Created by tomyeh
010: }}IS_NOTE
011:
012: Copyright (C) 2004 Potix Corporation. All Rights Reserved.
013:
014: {{IS_RIGHT
015: This program is distributed under GPL Version 2.0 in the hope that
016: it will be useful, but WITHOUT ANY WARRANTY.
017: }}IS_RIGHT
018: */
019: package org.zkoss.web.servlet.dsp;
020:
021: import java.io.Writer;
022: import java.io.IOException;
023:
024: import javax.servlet.ServletContext;
025: import javax.servlet.ServletRequest;
026: import javax.servlet.ServletResponse;
027: import javax.servlet.http.HttpServletRequest;
028: import javax.servlet.http.HttpServletResponse;
029:
030: import org.zkoss.util.resource.Locator;
031: import org.zkoss.xel.VariableResolver;
032: import org.zkoss.xel.FunctionMapper;
033: import org.zkoss.xel.ExpressionFactory;
034: import org.zkoss.xel.Expressions;
035:
036: import org.zkoss.web.util.resource.ServletContextLocator;
037: import org.zkoss.web.servlet.xel.RequestXelResolver;
038:
039: /**
040: * A DSP context based on HTTP servlet request and response.
041: *
042: * @author tomyeh
043: */
044: public class ServletDspContext implements DspContext {
045: private final Locator _locator;
046: private final ServletContext _ctx;
047: private final HttpServletRequest _request;
048: private final HttpServletResponse _response;
049: private Writer _out;
050: private VariableResolver _resolver;
051: private ExpressionFactory _expf;
052:
053: /**
054: * Constructor.
055: *
056: * @param locator used to locate resources, such as taglib.
057: * If null is specified, the locator for the specified servlet context
058: * is used. (In other words, we use ServletContextLocator(ctx), if locator is null).
059: */
060: public ServletDspContext(ServletContext ctx,
061: HttpServletRequest request, HttpServletResponse response,
062: Locator locator) {
063: this (ctx, request, response, null, locator);
064: }
065:
066: /**
067: * Constructor with the specified writer.
068: *
069: * @param locator used to locate resources, such as taglib.
070: * If null is specified, the locator for the specified servlet context
071: * is used. (In other words, we use ServletContextLocator(ctx), if locator is null).
072: * @param out the output to generate the result.
073: * If null, it is the same as {@link #ServletDspContext(ServletContext,HttpServletRequest,HttpServletResponse,Locator)}
074: * In other words, response.getWriter() is used.
075: * @since 2.4.1
076: */
077: public ServletDspContext(ServletContext ctx,
078: HttpServletRequest request, HttpServletResponse response,
079: Writer out, Locator locator) {
080: _locator = locator != null ? locator
081: : new ServletContextLocator(ctx);
082: _ctx = ctx;
083: _request = request;
084: _response = response;
085: _out = out;
086: }
087:
088: //-- DspContext --//
089: public Locator getLocator() {
090: return _locator;
091: }
092:
093: public void setContentType(String ctype) {
094: _response.setContentType(ctype);
095: }
096:
097: public void setOut(Writer out) {
098: _out = out;
099: }
100:
101: //-- XelContext --//
102: public Writer getOut() throws IOException {
103: return _out != null ? _out : _response.getWriter();
104: }
105:
106: public ServletRequest getRequest() {
107: return _request;
108: }
109:
110: public ServletResponse getResponse() {
111: return _response;
112: }
113:
114: public ServletContext getServletContext() {
115: return _ctx;
116: }
117:
118: public VariableResolver getVariableResolver() {
119: if (_resolver == null)
120: _resolver = new RequestXelResolver(_ctx, _request,
121: _response) {
122: public ExpressionFactory getExpressionFactory() {
123: return ServletDspContext.this
124: .getExpressionFactory();
125: }
126: };
127: return _resolver;
128: }
129:
130: private ExpressionFactory getExpressionFactory() {
131: if (_expf == null)
132: _expf = Expressions.newExpressionFactory(); //TODO: expfcls
133: return _expf;
134: }
135:
136: public FunctionMapper getFunctionMapper() {
137: return null;
138: }
139: }
|