001: /* ActionContextImpl.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Sat Sep 17 16:52: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.impl;
020:
021: import java.util.Map;
022: import java.io.Writer;
023: import java.io.IOException;
024:
025: import org.zkoss.web.servlet.Servlets;
026: import org.zkoss.web.servlet.BufferedResponse;
027: import org.zkoss.web.servlet.http.Encodes;
028: import org.zkoss.web.servlet.dsp.*;
029: import org.zkoss.web.servlet.dsp.action.Action;
030: import org.zkoss.web.servlet.dsp.action.ActionContext;
031:
032: /**
033: * An implementation of {@link ActionContext}.
034: *
035: * @author tomyeh
036: */
037: class ActionContextImpl implements ActionContext {
038: private final InterpretContext _ic;
039: private final Action _parent;
040: private final ActionNode _current;
041: private final int _nLines;
042:
043: ActionContextImpl(InterpretContext ic, Action parent,
044: ActionNode current, int nLines) {
045: _ic = ic;
046: _parent = parent;
047: _nLines = nLines;
048: _current = current;
049: }
050:
051: //-- ActionContext --//
052: public Object getAttribute(String name, int scope) {
053: return _ic.resolver.getAttributes(scope).get(name);
054: }
055:
056: public void setAttribute(String name, Object value, int scope) {
057: if (value == null) {
058: removeAttribute(name, scope);
059: return;
060: }
061: _ic.resolver.getAttributes(scope).put(name, value);
062: }
063:
064: public void removeAttribute(String name, int scope) {
065: _ic.resolver.getAttributes(scope).remove(name);
066: }
067:
068: public Object findAttribute(String name) {
069: Object o = getAttribute(name, PAGE_SCOPE);
070: if (o != null)
071: return o;
072: o = getAttribute(name, REQUEST_SCOPE);
073: if (o != null)
074: return o;
075: o = getAttribute(name, SESSION_SCOPE);
076: return o != null ? o : getAttribute(name, APPLICATION_SCOPE);
077: }
078:
079: public void setContentType(String ctype) {
080: _ic.dc.setContentType(ctype);
081: }
082:
083: public Writer getOut() throws IOException {
084: return _ic.dc.getOut();
085: }
086:
087: public Action getParent() {
088: return _parent;
089: }
090:
091: public void renderFragment(Writer out)
092: throws javax.servlet.ServletException, IOException {
093: if (out == null || out == _ic.dc.getOut()) {
094: _current.renderFragment(_ic);
095: } else {
096: final Writer old = _ic.dc.getOut();
097: _ic.dc.setOut(out);
098: try {
099: _current.renderFragment(_ic);
100: } finally {
101: _ic.dc.setOut(old);
102: }
103: }
104: }
105:
106: public void include(String uri, Map params)
107: throws javax.servlet.ServletException, IOException {
108: Servlets.include(_ic.dc.getServletContext(), _ic.dc
109: .getRequest(), BufferedResponse.getInstance(_ic.dc
110: .getResponse(), _ic.dc.getOut()), uri, params,
111: Servlets.PASS_THRU_ATTR);
112: }
113:
114: public String encodeURL(String uri)
115: throws javax.servlet.ServletException {
116: return Encodes.encodeURL(_ic.dc.getServletContext(), _ic.dc
117: .getRequest(), _ic.dc.getResponse(), uri);
118: }
119:
120: public int getLineNumber() {
121: return _nLines;
122: }
123: }
|