Clover Coverage Report - magnolia-module-templating 4.4.5
Coverage timestamp: Mon Sep 12 2011 16:32:30 CEST
../../../../img/srcFileCovDistChart1.png 48% of files have more coverage
39   206   30   1.56
6   122   0.77   25
25     1.2  
1    
 
  MagnoliaTemplatingUtilities       Line # 68 39 0% 30 68 2.9% 0.028571429
 
  (5)
 
1    /**
2    * This file Copyright (c) 2003-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 freemarker.core.Environment;
37    import info.magnolia.cms.beans.config.ContentRepository;
38    import info.magnolia.cms.beans.config.ServerConfiguration;
39    import info.magnolia.cms.core.Content;
40    import info.magnolia.cms.core.NodeData;
41    import info.magnolia.cms.i18n.I18nContentWrapper;
42    import info.magnolia.cms.util.ContentUtil;
43    import info.magnolia.cms.util.InheritanceContentWrapper;
44    import info.magnolia.cms.util.SiblingsHelper;
45    import info.magnolia.context.MgnlContext;
46    import info.magnolia.link.LinkUtil;
47    import info.magnolia.link.LinkException;
48    import info.magnolia.module.templating.engine.RenderingEngine;
49    import info.magnolia.objectfactory.Components;
50   
51    import javax.jcr.RepositoryException;
52   
53    import org.apache.commons.lang.StringUtils;
54    import org.apache.commons.lang.exception.ExceptionUtils;
55    import org.slf4j.Logger;
56    import org.slf4j.LoggerFactory;
57   
58    import java.io.IOException;
59    import java.io.Writer;
60   
61    /**
62    * This is an object exposing a couple of methods useful for templates; it's exposed in
63    * templates as "mgnl".
64    *
65    * @author gjoseph
66    * @version $Revision: $ ($Author: $)
67    */
 
68    public class MagnoliaTemplatingUtilities {
69   
70    private static final Logger log = LoggerFactory.getLogger(MagnoliaTemplatingUtilities.class);
71   
72    protected RenderingEngine renderingEngine = Components.getSingleton(RenderingEngine.class);
73   
 
74  5 toggle public static MagnoliaTemplatingUtilities getInstance(){
75  5 return Components.getSingleton(MagnoliaTemplatingUtilities.class);
76    }
77   
78    /**
79    * Returns an instance of SiblingsHelper for the given node.
80    */
 
81  0 toggle public SiblingsHelper siblings(Content node) throws RepositoryException {
82  0 return SiblingsHelper.of(node);
83    }
84   
 
85  0 toggle public void renderTemplate(Content content) throws RenderException, IOException {
86  0 renderTemplate(content, getWriter());
87    }
88   
 
89  0 toggle public void renderTemplate(Content content, Writer out) throws RenderException, IOException {
90  0 renderingEngine.render(content, out);
91    }
92   
 
93  0 toggle public void renderTemplate(Content content, Writer out, String templateName) throws RenderException, IOException {
94  0 renderingEngine.render(content, templateName, out);
95    }
96   
 
97  0 toggle public void renderParagraph(Content paragraphNode) throws RenderException, IOException {
98  0 renderParagraph(paragraphNode, getWriter());
99    }
100   
 
101  0 toggle public void renderParagraph(Content paragraphNode, Writer out) throws RenderException, IOException {
102  0 renderingEngine.render(paragraphNode, out);
103    }
104   
 
105  0 toggle public void renderParagraph(Content paragraphNode, Writer out, String paragraphName) throws RenderException, IOException {
106  0 renderingEngine.render(paragraphNode, paragraphName, out);
107    }
108   
109    /**
110    * TODO each renderer should provide its own subclass.
111    */
 
112  0 toggle protected Writer getWriter() {
113  0 final Environment env = Environment.getCurrentEnvironment();
114  0 return env.getOut();
115    }
116   
 
117  0 toggle public Content inherit(Content node) {
118  0 return new InheritanceContentWrapper(node);
119    }
120   
 
121  0 toggle public Content i18n(Content node) {
122  0 return new I18nContentWrapper(node);
123    }
124   
 
125  0 toggle public boolean isEditMode(){
126    // TODO : see CmsFunctions.isEditMode, which checks a couple of other properties.
127  0 return isAuthorInstance() && !isPreviewMode();
128    }
129   
 
130  0 toggle public boolean isPreviewMode(){
131  0 return MgnlContext.getAggregationState().isPreviewMode();
132    }
133   
 
134  0 toggle public boolean isAuthorInstance(){
135  0 return ServerConfiguration.getInstance().isAdmin();
136    }
137   
 
138  0 toggle public boolean isPublicInstance(){
139  0 return !isAuthorInstance();
140    }
141   
 
142  0 toggle public String createLink(Content node) {
143  0 return LinkUtil.createLink(node);
144    }
145   
 
146  0 toggle public String createLink(NodeData nd) {
147  0 try {
148  0 return LinkUtil.createLink(nd);
149    } catch (LinkException e) {
150  0 log.error("Can't resolve link defined in node {} because of {}.", nd.getHandle(), ExceptionUtils.getRootCauseMessage(e));
151  0 return null;
152    }
153    }
154   
 
155  0 toggle public String createLink(String repositoryId, String uuid) {
156  0 try {
157  0 return LinkUtil.createLink(repositoryId, uuid);
158    } catch (RepositoryException e) {
159  0 log.error("Can't resolve link with UUID {} because of {}.", uuid , ExceptionUtils.getRootCauseMessage(e));
160  0 return null;
161    }
162    }
163   
164    /**
165    * Util method to create html attributes <code>name="value"</code>. If the value is empty an empty string will be returned.
166    * This is mainlly helpful to avoid empty attributes.
167    */
 
168  0 toggle public String createAttribute(String name, String value){
169  0 value = StringUtils.trim(value);
170  0 if(StringUtils.isNotEmpty(value)){
171  0 return new StringBuffer().append(name).append("=\"").append(value).append("\"").toString();
172    }
173  0 return StringUtils.EMPTY;
174    }
175   
 
176  0 toggle public Content getContent(String path){
177  0 return getContent(ContentRepository.WEBSITE, path);
178    }
179   
 
180  0 toggle public Content getContent(String repository, String path){
181  0 return ContentUtil.getContent(repository, path);
182    }
183   
 
184  0 toggle public Content getContentByUUID(String uuid){
185  0 return getContentByUUID(ContentRepository.WEBSITE, uuid);
186    }
187   
 
188  0 toggle public Content getContentByUUID(String repository, String uuid){
189  0 return ContentUtil.getContentByUUID(repository, uuid);
190    }
191   
 
192  0 toggle public static Content encode(Content content){
193  0 if(content != null){
194  0 return new HTMLEncodingContentWrapper(content, true);
195    }
196  0 return null;
197    }
198   
 
199  0 toggle public static Content decode(Content content){
200  0 if(content instanceof HTMLEncodingContentWrapper){
201  0 return ((HTMLEncodingContentWrapper)content).getWrappedContent();
202    }
203  0 throw new IllegalStateException("The content to decode is not wrapped by a " + HTMLEncodingContentWrapper.class.getName());
204    }
205   
206    }