01: /*
02: * Copyright 2005 jWic group (http://www.jwic.de)
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: *
16: * de.jwic.renderer.jsp.JspRenderer
17: * Created on 04.12.2005
18: * $Id: JspRenderer.java,v 1.2 2006/03/14 13:05:55 lordsam Exp $
19: */
20: package de.jwic.renderer.jsp;
21:
22: import java.io.PrintWriter;
23:
24: import javax.servlet.RequestDispatcher;
25: import javax.servlet.http.HttpServletRequest;
26:
27: import org.apache.commons.logging.Log;
28: import org.apache.commons.logging.LogFactory;
29:
30: import de.jwic.base.Control;
31: import de.jwic.base.IControlContainer;
32: import de.jwic.base.IControlRenderer;
33: import de.jwic.base.RenderContext;
34: import de.jwic.renderer.util.ChildRenderer;
35: import de.jwic.renderer.util.JWicTools;
36:
37: /**
38: * Renders controls using a JSP page. The jsp pages must be stored in the
39: * directory /WEB-INF/jwic/jsp. The filename is the templatename with a
40: * ".jsp" extension.
41: *
42: * @author Florian Lippisch
43: * @version $Revision: 1.2 $
44: */
45: public class JspRenderer implements IControlRenderer {
46:
47: protected final Log log = LogFactory.getLog(getClass());
48:
49: /* (non-Javadoc)
50: * @see de.jwic.base.IControlRenderer#renderControl(de.jwic.base.Control, de.jwic.base.RenderContext)
51: */
52: public void renderControl(Control control, RenderContext context) {
53:
54: if (log.isDebugEnabled()) {
55: log.debug("Rendering control '" + control.getControlID()
56: + "' with template '" + control.getTemplateName()
57: + "'");
58: }
59:
60: String ctrlDivID = "ctrl_" + control.getControlID();//.replace('.', '_');
61: PrintWriter writer = context.getWriter();
62: if (!control.isVisible()) {
63: writer.print("<span><span id=\"" + ctrlDivID + "\">");
64: writer.print("</span></span>");
65: return;
66: }
67:
68: HttpServletRequest request = context.getRequest();
69: request.setAttribute("control", control);
70: request.setAttribute("jwic", new JWicTools(control
71: .getSessionContext().getLocale()));
72: if (control instanceof IControlContainer) {
73: request.setAttribute("insert", new ChildRenderer(
74: (IControlContainer) control, context));
75: }
76:
77: RequestDispatcher dispatcher = context.getRequest()
78: .getRequestDispatcher(
79: "/WEB-INF/jwic/jsp/"
80: + control.getTemplateName() + ".jsp");
81: try {
82: HttpServletResponseFacade res = new HttpServletResponseFacade(
83: context.getResponse(), context.getWriter());
84: writer.print("<span><span id=\"" + ctrlDivID + "\">");
85: dispatcher.include(context.getRequest(), res);
86: writer.print("</span></span>");
87:
88: } catch (Exception e) {
89: log.error("Error including jsp page", e);
90: context.getWriter().write(
91: "Error rendering Control (" + e + ")");
92: }
93:
94: }
95:
96: }
|