Clover Coverage Report - magnolia-module-templating 4.4.5
Coverage timestamp: Mon Sep 12 2011 16:32:30 CEST
../../../../../img/srcFileCovDistChart0.png 54% of files have more coverage
44   190   24   4
20   109   0.55   5.5
11     2.18  
2    
 
  DefaultRenderingEngine       Line # 63 29 0% 14 48 0% 0.0
  DefaultRenderingEngine.RenderingHelper       Line # 68 15 0% 10 27 0% 0.0
 
No Tests
 
1    /**
2    * This file Copyright (c) 2010-2011 Magnolia International
3    * Ltd. (http://www.magnolia.info). 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.info/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
32    *
33    */
34    package info.magnolia.module.templating.engine;
35   
36    import java.io.IOException;
37    import java.io.Writer;
38   
39    import info.magnolia.cms.core.AggregationState;
40    import info.magnolia.cms.core.Content;
41    import info.magnolia.cms.core.ItemType;
42    import info.magnolia.context.MgnlContext;
43    import info.magnolia.module.templating.Paragraph;
44    import info.magnolia.module.templating.ParagraphManager;
45    import info.magnolia.module.templating.ParagraphRenderer;
46    import info.magnolia.module.templating.ParagraphRendererManager;
47    import info.magnolia.module.templating.RenderException;
48    import info.magnolia.module.templating.RenderableDefinition;
49    import info.magnolia.module.templating.Template;
50    import info.magnolia.module.templating.TemplateManager;
51    import info.magnolia.module.templating.TemplateRenderer;
52    import info.magnolia.module.templating.TemplateRendererManager;
53   
54   
55    /**
56    * Default implementation which determines the definition (template/paragraph) from the content's
57    * meta data. Based on the node type (mgln:contentNode for paragraphs) a paragraph rendering or a
58    * template rendering is performed.
59    * @author pbaerfuss
60    * @version $Id$
61    *
62    */
 
63    public class DefaultRenderingEngine implements RenderingEngine {
64    /**
65    * A helper enumeration usable by subclasses.
66    *
67    */
 
68    protected enum RenderingHelper {
69    PARAGRAPH {
70   
 
71  0 toggle public RenderableDefinition getDefinition(String definitionName) {
72  0 return ParagraphManager.getInstance().getParagraphDefinition(definitionName);
73    }
74   
 
75  0 toggle public Object getRenderer(RenderableDefinition definition) {
76  0 return ParagraphRendererManager.getInstance().getRenderer(definition.getType());
77    }
78   
 
79  0 toggle void render(Content content, RenderableDefinition definition, Object renderer, Writer out) throws RenderException, IOException {
80  0 ((ParagraphRenderer) renderer).render(content, (Paragraph) definition, out);
81    }
82    },
83   
84    TEMPLATE {
85   
 
86  0 toggle public RenderableDefinition getDefinition(String definitionName) {
87  0 AggregationState state = getAggregationStateSafely();
88  0 String extension = null;
89  0 if (state != null) {
90  0 extension = state.getExtension();
91    }
92  0 Template template = TemplateManager.getInstance().getTemplateDefinition(definitionName);
93  0 if (template != null && extension != null) {
94  0 Template subTemplate = template.getSubTemplate(extension);
95  0 if (subTemplate != null) {
96  0 template = subTemplate;
97    }
98    }
99  0 return template;
100   
101    }
102   
 
103  0 toggle public Object getRenderer(RenderableDefinition definition) {
104  0 return TemplateRendererManager.getInstance().getRenderer(definition.getType());
105    }
106   
 
107  0 toggle public void render(Content content, RenderableDefinition definition, Object renderer, Writer out) throws RenderException, IOException {
108  0 ((TemplateRenderer) renderer).renderTemplate(content, (Template) definition, out);
109    }
110    };
111   
112    abstract RenderableDefinition getDefinition(String definitionName);
113   
114    abstract Object getRenderer(RenderableDefinition definition);
115   
116    abstract void render(Content content, RenderableDefinition definition, Object renderer, Writer out) throws RenderException, IOException;
117    }
118   
 
119  0 toggle public void render(Content content, Writer out) throws RenderException {
120  0 render(content, determineAssignedDefinitionName(content), out);
121    }
122   
 
123  0 toggle public void render(Content content, String definitionName, Writer out) throws RenderException {
124    // FIXME content can be null in case of a request to a node date having a template attribute set for the binary
125    // this is probably not used anymore and should not be supported
126  0 if (content != null && content.isNodeType(ItemType.CONTENTNODE.getSystemName())) {
127  0 render(content, definitionName, RenderingHelper.PARAGRAPH, out);
128    }
129    else {
130  0 render(content, definitionName, RenderingHelper.TEMPLATE, out);
131    }
132    }
133   
134    /**
135    * Reads the template name from the meta data.
136    */
 
137  0 toggle protected String determineAssignedDefinitionName(Content content) {
138  0 return content.getMetaData().getTemplate();
139    }
140   
141    /**
142    * Will update the aggregation state and perform the rendering by using the helper.
143    */
 
144  0 toggle protected void render(Content content, String definitionName, RenderingHelper helper, Writer out) throws RenderException {
145  0 Content orgMainContent = null;
146  0 Content orgCurrentContent = null;
147   
148  0 AggregationState state = getAggregationStateSafely();
149  0 if (state != null) {
150  0 orgMainContent = state.getMainContent();
151  0 orgCurrentContent = state.getCurrentContent();
152   
153  0 state.setCurrentContent(content);
154    // if not yet set the passed content is the entry point of the rendering
155  0 if (orgMainContent == null) {
156  0 state.setMainContent(content);
157    }
158    }
159   
160  0 RenderableDefinition definition = helper.getDefinition(definitionName);
161  0 if (definition == null) {
162  0 throw new RenderException("Can't find renderable definition " + definitionName + " for content: " + content.getHandle());
163    }
164   
165  0 Object renderer = helper.getRenderer(definition);
166  0 if (renderer == null) {
167  0 throw new RenderException("Can't find renderer for type " + definition.getType() + " of content: " + content.getHandle());
168    }
169   
170  0 try {
171  0 helper.render(content, definition, renderer, out);
172    }
173    catch (IOException e) {
174  0 throw new RenderException("Can't render " + content.getHandle(), e);
175    }
176   
177  0 if (state != null) {
178  0 state.setMainContent(orgMainContent);
179  0 state.setCurrentContent(orgCurrentContent);
180    }
181    }
182   
 
183  0 toggle protected static AggregationState getAggregationStateSafely() {
184  0 if (MgnlContext.isWebContext()) {
185  0 return MgnlContext.getAggregationState();
186    }
187  0 return null;
188    }
189   
190    }