KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > components > render > AbstractRenderStrategy


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.components.render;
25
26 import java.io.IOException JavaDoc;
27 import java.io.PrintWriter JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36 import org.riotfamily.components.Component;
37 import org.riotfamily.components.ComponentList;
38 import org.riotfamily.components.ComponentRepository;
39 import org.riotfamily.components.ComponentVersion;
40 import org.riotfamily.components.Location;
41 import org.riotfamily.components.VersionContainer;
42 import org.riotfamily.components.component.AbstractComponent;
43 import org.riotfamily.components.config.ComponentListConfiguration;
44 import org.riotfamily.components.dao.ComponentDao;
45
46 public class AbstractRenderStrategy implements RenderStrategy {
47     
48     public static final String JavaDoc INHERTING_COMPONENT = "inherit";
49     
50     protected Log log = LogFactory.getLog(getClass());
51     
52     protected ComponentDao dao;
53     
54     protected ComponentRepository repository;
55                 
56     protected ComponentListConfiguration config;
57     
58     protected HttpServletRequest JavaDoc request;
59     
60     protected HttpServletResponse JavaDoc response;
61     
62     protected PrintWriter JavaDoc out;
63     
64     protected VersionContainer parent;
65     
66     public AbstractRenderStrategy(ComponentDao dao,
67             ComponentRepository repository, ComponentListConfiguration config,
68             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
69             throws IOException JavaDoc {
70         
71         this.dao = dao;
72         this.repository = repository;
73         this.config = config;
74         this.request = request;
75         this.response = response;
76         this.out = response.getWriter();
77         this.parent = (VersionContainer) request.getAttribute(
78                 AbstractComponent.CONTAINER);
79     }
80     
81     public final void render() throws IOException JavaDoc {
82         render(getLocation());
83     }
84     
85     protected void render(Location location) throws IOException JavaDoc {
86         ComponentList list = getComponentList(location);
87         if (list != null) {
88             renderComponentList(list);
89         }
90         else {
91             onListNotFound(location);
92         }
93     }
94     
95     public void render(ComponentList list) throws IOException JavaDoc {
96         renderComponentList(list);
97     }
98     
99     protected final Location getLocation() {
100         Location location = config.getLocator().getLocation(request);
101         log.debug("List location: " + location);
102         return location;
103     }
104     
105     protected void onListNotFound(Location location) throws IOException JavaDoc {
106         log.debug("No ComponentList found for " + location);
107     }
108     
109     /**
110      * Returns the ComponentList to be rendered. The default implementation
111      * uses the controller's ComponentDao to look up a list for the given
112      * location.
113      */

114     protected ComponentList getComponentList(Location location) {
115         if (parent != null) {
116             return dao.findComponentList(parent, location.getSlot());
117         }
118         return dao.findComponentList(location);
119     }
120     
121     /**
122      * Renders the given list. The default implementation calls
123      * {@link #getComponentsToRender(ComponentList)} and passes the result
124      * to {@link #renderComponents(List)}.
125      */

126     protected void renderComponentList(ComponentList list) throws IOException JavaDoc {
127         List JavaDoc components = getComponentsToRender(list);
128         renderComponents(components);
129     }
130     
131     /**
132      * Renders the given list. The default implementation iterates over the
133      * given list and calls {@link #renderContainer(VersionContainer, String)}
134      * for each item. If the list is empty or null,
135      * {@link #onEmptyComponentList()} is invoked.
136      */

137     protected final void renderComponents(List JavaDoc components) throws IOException JavaDoc {
138         if (components == null || components.isEmpty()) {
139             onEmptyComponentList();
140             return;
141         }
142         
143         int i = 0;
144         Iterator JavaDoc it = components.iterator();
145         while (it.hasNext()) {
146             VersionContainer container = (VersionContainer) it.next();
147             renderContainer(container, getPositionalClassName(i++, !it.hasNext()));
148         }
149     }
150     
151     protected void onEmptyComponentList() throws IOException JavaDoc {
152     }
153     
154     /**
155      * Returns a list of VersionContainers. The default implementation
156      * simply returns the list's live components.
157      */

158     protected List JavaDoc getComponentsToRender(ComponentList list) {
159         return list.getLiveContainers();
160     }
161     
162     /**
163      * Renders the given VersionContainer. The default implementation calls
164      * {@link #getVersionToRender(VersionContainer) getVersionToRender()} and
165      * passes the result to {@link #renderComponentVersion(ComponentVersion, String)
166      * renderComponentVersion()} (if not null).
167      */

168     protected void renderContainer(VersionContainer container,
169             String JavaDoc positionClassName) throws IOException JavaDoc {
170
171         ComponentVersion version = getVersionToRender(container);
172         if (version != null) {
173             renderComponentVersion(version, positionClassName);
174         }
175     }
176         
177     /**
178      * Returns the ComponentVersion to render. The default implementation
179      * simply returns the component's live version.
180      */

181     protected ComponentVersion getVersionToRender(VersionContainer container) {
182         return container.getLiveVersion();
183     }
184     
185     /**
186      * Renders the given ComponentVersion.
187      * @throws IOException
188      */

189     protected final void renderComponentVersion(ComponentVersion version,
190             String JavaDoc positionClassName) throws IOException JavaDoc {
191         
192         String JavaDoc type = version.getType();
193         if (INHERTING_COMPONENT.equals(type)) {
194             renderParentList(version.getContainer().getList());
195         }
196         else {
197             Component component = repository.getComponent(type);
198             renderComponent(component, version, positionClassName);
199         }
200     }
201     
202     protected final void renderParentList(ComponentList list)
203             throws IOException JavaDoc {
204         
205         Location location = list.getLocation();
206         Location parentLocation = config.getLocator().getParentLocation(location);
207         log.debug("Parent location: " + parentLocation);
208         
209         if (parentLocation != null) {
210             if (location.equals(parentLocation)) {
211                 log.warn("Parent location is the same");
212                 return;
213             }
214             ComponentList parentList = getComponentList(parentLocation);
215             if (parentList != null) {
216                 getStrategyForParentList().render(parentList);
217             }
218             else {
219                 onListNotFound(parentLocation);
220             }
221         }
222     }
223     
224     protected RenderStrategy getStrategyForParentList() throws IOException JavaDoc {
225         return this;
226     }
227     
228     protected void renderComponent(Component component,
229             ComponentVersion version, String JavaDoc positionClassName)
230             throws IOException JavaDoc {
231         
232         component.render(version, positionClassName, request, response);
233     }
234     
235     protected String JavaDoc getPositionalClassName(int position, boolean last) {
236         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("component-").append(position + 1);
237         if (last) {
238             sb.append(" last-component");
239         }
240         return sb.toString();
241     }
242
243 }
244
Popular Tags