001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.theme.render;
023:
024: import org.jboss.portal.common.util.MarkupInfo;
025: import org.jboss.portal.common.util.MediaType;
026: import org.jboss.portal.theme.render.renderer.DecorationRenderer;
027: import org.jboss.portal.theme.render.renderer.DecorationRendererContext;
028: import org.jboss.portal.theme.render.renderer.PageRenderer;
029: import org.jboss.portal.theme.render.renderer.PageRendererContext;
030: import org.jboss.portal.theme.render.renderer.PortletRenderer;
031: import org.jboss.portal.theme.render.renderer.PortletRendererContext;
032: import org.jboss.portal.theme.render.renderer.RegionRenderer;
033: import org.jboss.portal.theme.render.renderer.RegionRendererContext;
034: import org.jboss.portal.theme.render.renderer.WindowRenderer;
035: import org.jboss.portal.theme.render.renderer.WindowRendererContext;
036: import org.jboss.portal.web.ServletContextDispatcher;
037:
038: import java.io.PrintWriter;
039: import java.util.ArrayList;
040: import java.util.HashMap;
041: import java.util.Map;
042:
043: /**
044: * A render context to render a context on a page. <p>A render context is scoped to a MarkupContainer (a region, or a
045: * window). A render context that is not scoped to a MarkupContainer is ment to be a page level template to allow easier
046: * creation of the region and window render contexts via the <code>RenderContext.getContext()</code> methods.</p>
047: *
048: * @author <a href="mailto:mholzner@novell.com>Martin Holzner</a>
049: * @version $LastChangedRevision: 8784 $, $LastChangedDate: 2007-10-27 19:01:46 -0400 (Sat, 27 Oct 2007) $
050: */
051: public abstract class RendererContext {
052:
053: private final ThemeContext themeContext;
054:
055: /** . */
056: private final RendererFactory rendererFactory;
057:
058: /** . */
059: private final ServletContextDispatcher dispatcher;
060:
061: /** . */
062: private final MarkupInfo markupInfo;
063:
064: /** . */
065: private final ArrayList stack;
066:
067: /** . */
068: private final Map attributes;
069:
070: /**
071: * Create a new render context for the provided result. <p>The result contains information about the markup container
072: * (page, region , window context) to render.</p>
073: */
074: public RendererContext(ThemeContext themeContext,
075: RendererFactory rendererFactory,
076: ServletContextDispatcher dispatcher, MarkupInfo markupInfo) {
077: if (themeContext == null) {
078: throw new IllegalArgumentException(
079: "no theme factory provided");
080: }
081: if (rendererFactory == null) {
082: throw new IllegalArgumentException(
083: "no renderer factory provided");
084: }
085: if (dispatcher == null) {
086: throw new IllegalArgumentException(
087: "no server invocation provided");
088: }
089: if (markupInfo == null) {
090: throw new IllegalArgumentException(
091: "no stream info provided");
092: }
093:
094: this .themeContext = themeContext;
095: this .rendererFactory = rendererFactory;
096: this .dispatcher = dispatcher;
097: this .markupInfo = markupInfo;
098: this .stack = new ArrayList(6);
099: this .attributes = new HashMap();
100: }
101:
102: public abstract PrintWriter getWriter();
103:
104: public ThemeContext getThemeContext() {
105: return themeContext;
106: }
107:
108: public ServletContextDispatcher getDispatcher() {
109: return dispatcher;
110: }
111:
112: public MarkupInfo getMarkupInfo() {
113: return markupInfo;
114: }
115:
116: public MediaType getContentType() {
117: return markupInfo.getContentType();
118: }
119:
120: public String getCharset() {
121: return markupInfo.getCharset();
122: }
123:
124: public Object getAttribute(String attrName) {
125: if (attrName == null) {
126: throw new IllegalArgumentException("No null attribute name");
127: }
128: return attributes.get(attrName);
129: }
130:
131: public void setAttribute(String attrName, Object attrValue) {
132: if (attrName == null) {
133: throw new IllegalArgumentException("No null attribute name");
134: }
135: if (attrValue != null) {
136: attributes.put(attrName, attrValue);
137: } else {
138: attributes.remove(attrName);
139: }
140: }
141:
142: public String getProperty(String propertyName) {
143: return getProperty(propertyName, stack.size() - 1);
144: }
145:
146: public String getProperty(String propertyName, PropertyFetch fetch) {
147: return getProperty(propertyName, stack.size()
148: - (fetch.getScope() == PropertyFetch.ALL_SCOPE ? 1 : 2));
149: }
150:
151: private String getProperty(String propertyName, int from) {
152: for (int i = from; i >= 0; i--) {
153: ObjectRendererContext ctx = (ObjectRendererContext) stack
154: .get(i);
155: String propertyValue = ctx.getProperty(propertyName);
156: if (propertyValue != null) {
157: return propertyValue;
158: }
159: }
160: return null;
161: }
162:
163: // public int getObjectRendererContextSize()
164: // {
165: // return stack.size();
166: // }
167: //
168: // public ObjectRendererContext getObjectRendererContext(int index)
169: // {
170: // return (ObjectRendererContext)stack.get(index);
171: // }
172:
173: public void pushObjectRenderContext(
174: ObjectRendererContext objectRendererContext) {
175: stack.add(objectRendererContext);
176: ObjectRenderer renderer = rendererFactory.getRenderer(this ,
177: objectRendererContext);
178: renderer.startContext(this , objectRendererContext);
179: }
180:
181: public ObjectRendererContext popObjectRenderContext() {
182: ObjectRendererContext objectRendererContext = (ObjectRendererContext) stack
183: .remove(stack.size() - 1);
184: ObjectRenderer renderer = rendererFactory.getRenderer(this ,
185: objectRendererContext);
186: renderer.endContext(this , objectRendererContext);
187: return objectRendererContext;
188: }
189:
190: public void render(ObjectRendererContext ctx)
191: throws RenderException, IllegalStateException {
192: pushObjectRenderContext(ctx);
193:
194: //
195: try {
196: if (ctx instanceof PageRendererContext) {
197: PageRendererContext prc = (PageRendererContext) ctx;
198: PageRenderer renderer = (PageRenderer) rendererFactory
199: .getRenderer(this , prc);
200: renderer.render(this , prc);
201: }
202: if (ctx instanceof WindowRendererContext) {
203: WindowRendererContext wrc = (WindowRendererContext) ctx;
204: WindowRenderer renderer = (WindowRenderer) rendererFactory
205: .getRenderer(this , wrc);
206: renderer.render(this , wrc);
207: } else if (ctx instanceof RegionRendererContext) {
208: RegionRendererContext rrc = (RegionRendererContext) ctx;
209: RegionRenderer renderer = (RegionRenderer) rendererFactory
210: .getRenderer(this , rrc);
211:
212: //
213: renderer.renderHeader(this , rrc);
214: renderer.renderBody(this , rrc);
215: renderer.renderFooter(this , rrc);
216: } else if (ctx instanceof PortletRendererContext) {
217: PortletRendererContext prc = (PortletRendererContext) ctx;
218: PortletRenderer renderer = (PortletRenderer) rendererFactory
219: .getRenderer(this , prc);
220: renderer.render(this , prc);
221: } else if (ctx instanceof DecorationRendererContext) {
222: DecorationRendererContext drc = (DecorationRendererContext) ctx;
223: DecorationRenderer renderer = (DecorationRenderer) rendererFactory
224: .getRenderer(this , drc);
225:
226: //
227: renderer.render(this, drc);
228: }
229: } finally {
230: popObjectRenderContext();
231: }
232: }
233: }
|