View Javadoc

1   /**
2    * This file Copyright (c) 2008-2011 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.templating;
35  
36  import java.io.Writer;
37  import java.util.HashMap;
38  import java.util.Iterator;
39  import java.util.Map;
40  
41  import org.apache.commons.lang.exception.ExceptionUtils;
42  
43  import info.magnolia.cms.core.Content;
44  import info.magnolia.cms.core.AggregationState;
45  import info.magnolia.cms.i18n.I18nContentWrapper;
46  import info.magnolia.context.MgnlContext;
47  
48  
49  /**
50   * Abstract renderer which can be used to implement paragraph or template renderers.
51   * Sets up the context by providing the following objects: content, aggregationState, page, model, actionResult, mgnl
52   *
53   * @author pbracher
54   * @version $Id: AbstractRenderer.java 41223 2011-01-12 10:46:25Z tmattsson $
55   *
56   */
57  public abstract class AbstractRenderer implements RenderingModelBasedRenderer {
58  
59      private static final String MODEL_ATTRIBUTE = RenderingModel.class.getName();
60  
61      public AbstractRenderer() {
62      }
63  
64      protected void render(Content content, RenderableDefinition definition, Writer out) throws RenderException {
65  
66          final RenderingModel parentModel = (RenderingModel) MgnlContext.getAttribute(MODEL_ATTRIBUTE);
67  
68          RenderingModel model;
69          String actionResult;
70  
71          model = (RenderingModel) MgnlContext.getAttribute(ModelExecutionFilter.MODEL_ATTRIBUTE_PREFIX + content.getUUID());
72  
73          if (model == null) {
74  
75              model = newModel(content, definition, parentModel);
76  
77              actionResult = model.execute();
78  
79              if (RenderingModel.SKIP_RENDERING.equals(actionResult)) {
80                  return;
81              }
82          } else {
83              actionResult = (String) MgnlContext.getAttribute(ModelExecutionFilter.ACTION_RESULT_ATTRIBUTE_PREFIX + content.getUUID());
84              if (model instanceof EarlyExecutionAware)
85                  ((EarlyExecutionAware)model).setParent(parentModel);
86          }
87  
88          String templatePath = determineTemplatePath(content, definition, model, actionResult);
89  
90          final Map ctx = newContext();
91          final Map savedContextState = saveContextState(ctx);
92          setupContext(ctx, content, definition, model, actionResult);
93          MgnlContext.setAttribute(MODEL_ATTRIBUTE, model);
94          onRender(content, definition, out, ctx, templatePath);
95          MgnlContext.setAttribute(MODEL_ATTRIBUTE, parentModel);
96  
97          restoreContext(ctx, savedContextState);
98      }
99  
100     protected String determineTemplatePath(Content content, RenderableDefinition definition, RenderingModel model, final String actionResult) {
101         String templatePath = definition.determineTemplatePath(actionResult, model);
102 
103         if (templatePath == null) {
104             throw new IllegalStateException("Unable to render " + definition.getClass().getName() + " " + definition.getName() + " in page " + content.getHandle() + ": templatePath not set.");
105         }
106         return templatePath;
107     }
108 
109     /**
110      * Creates the model for this rendering process. Will set the properties
111      */
112     public RenderingModel newModel(Content content, RenderableDefinition definition, RenderingModel parentModel) throws RenderException {
113         try {
114             final Content wrappedContent = wrapNodeForModel(content, getMainContentSafely(content));
115             return definition.newModel(wrappedContent, definition, parentModel);
116         } catch (Exception e) {
117             throw new RenderException("Can't create rendering model: " + ExceptionUtils.getRootCauseMessage(e), e);
118         }
119     }
120 
121     protected Map saveContextState(final Map ctx) {
122         Map state = new HashMap();
123         // save former values
124         saveAttribute(ctx, state, "content");
125         saveAttribute(ctx, state, "def");
126         saveAttribute(ctx, state, "state");
127         saveAttribute(ctx, state, "mgnl");
128         saveAttribute(ctx, state, "model");
129         saveAttribute(ctx, state, "actionResult");
130 
131         saveAttribute(ctx, state, getPageAttributeName());
132         return state;
133     }
134 
135     protected void saveAttribute(final Map ctx, Map state, String name) {
136         state.put(name, ctx.get(name));
137     }
138 
139     protected void restoreContext(final Map ctx, Map state) {
140         for (Iterator iterator = state.keySet().iterator(); iterator.hasNext();) {
141             String name = (String) iterator.next();
142             setContextAttribute(ctx, name, state.get(name));
143         }
144     }
145 
146     protected void setupContext(final Map ctx, Content content, RenderableDefinition definition, RenderingModel model, Object actionResult){
147         final Content mainContent = getMainContentSafely(content);
148 
149         setContextAttribute(ctx, getPageAttributeName(), wrapNodeForTemplate(mainContent, mainContent));
150         setContextAttribute(ctx, "content", wrapNodeForTemplate(content, mainContent));
151         setContextAttribute(ctx, "def", definition);
152         setContextAttribute(ctx, "state", getAggregationStateSafely());
153         setContextAttribute(ctx, "mgnl", getMagnoliaTemplatingUtilities());
154         setContextAttribute(ctx, "model", model);
155         setContextAttribute(ctx, "actionResult", actionResult);
156     }
157 
158     /**
159      * Gets the current main contain and treats the situation where the context is not a web context nicely by using the current content instead.
160      */
161     protected Content getMainContentSafely(Content current) {
162         AggregationState state = getAggregationStateSafely();
163         if(state != null){
164             return state.getMainContent();
165         }
166         return current;
167     }
168 
169     /**
170      * @deprecated since 4.3 - typo, use getAggregationStateSafely()
171      */
172     protected AggregationState getAggrigationStateSafely() {
173         return getAggregationStateSafely();
174     }
175 
176     /**
177      * This gets the aggregation state without throwing an exception if the current context is not a WebContext.
178      */
179     protected AggregationState getAggregationStateSafely() {
180         if(MgnlContext.isWebContext()){
181             return MgnlContext.getAggregationState();
182         }
183         return null;
184     }
185 
186     protected MagnoliaTemplatingUtilities getMagnoliaTemplatingUtilities() {
187         return MagnoliaTemplatingUtilities.getInstance();
188     }
189 
190     /**
191      * Wraps the current content node before passing it to the model.
192      * @param currentContent the actual content
193      * @param mainContent the current "main content" or "page", which might be needed in certain wrapping situations
194      */
195     protected Content wrapNodeForModel(Content currentContent, Content mainContent) {
196         return new I18nContentWrapper(currentContent);
197     }
198 
199     /**
200      * Wraps the current content node before exposing it to the template renderer.
201      * @param currentContent the actual content being exposed to the template
202      * @param mainContent the current "main content" or "page", which might be needed in certain wrapping situations
203      * @see info.magnolia.module.templating.paragraphs.JspParagraphRenderer
204      * TODO : return an Object instance instead - more flexibility for the template engine ?
205      */
206     protected Content wrapNodeForTemplate(Content currentContent, Content mainContent) {
207         return new I18nContentWrapper(currentContent);
208     }
209 
210     protected Object setContextAttribute(final Map ctx, final String name, Object value) {
211         return ctx.put(name, value);
212     }
213 
214     /**
215      * Used to give JSP implementations to give the chance to use on other name than page which is a reserved name in JSPs.
216      */
217     protected String getPageAttributeName() {
218         return "page";
219     }
220 
221     /**
222      * Create a new context object which is a map.
223      */
224     protected abstract Map newContext();
225 
226     /**
227      * Finally execute the rendering.
228      * @param content TODO
229      */
230     protected abstract void onRender(Content content, RenderableDefinition definition, Writer out, Map ctx, String templatePath) throws RenderException;
231 
232 }