View Javadoc

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      public static MagnoliaTemplatingUtilities getInstance(){
75          return Components.getSingleton(MagnoliaTemplatingUtilities.class);
76      }
77  
78      /**
79       * Returns an instance of SiblingsHelper for the given node.
80       */
81      public SiblingsHelper siblings(Content node) throws RepositoryException {
82          return SiblingsHelper.of(node);
83      }
84  
85      public void renderTemplate(Content content) throws RenderException, IOException {
86          renderTemplate(content, getWriter());
87      }
88  
89      public void renderTemplate(Content content, Writer out) throws RenderException, IOException {
90          renderingEngine.render(content, out);
91      }
92  
93      public void renderTemplate(Content content, Writer out, String templateName) throws RenderException, IOException {
94          renderingEngine.render(content, templateName, out);
95      }
96  
97      public void renderParagraph(Content paragraphNode) throws RenderException, IOException {
98          renderParagraph(paragraphNode, getWriter());
99      }
100 
101     public void renderParagraph(Content paragraphNode, Writer out) throws RenderException, IOException {
102         renderingEngine.render(paragraphNode, out);
103     }
104 
105     public void renderParagraph(Content paragraphNode, Writer out, String paragraphName) throws RenderException, IOException {
106         renderingEngine.render(paragraphNode, paragraphName, out);
107     }
108 
109     /**
110      * TODO each renderer should provide its own subclass.
111      */
112     protected Writer getWriter() {
113         final Environment env = Environment.getCurrentEnvironment();
114         return env.getOut();
115     }
116 
117     public Content inherit(Content node) {
118         return new InheritanceContentWrapper(node);
119     }
120 
121     public Content i18n(Content node) {
122         return new I18nContentWrapper(node);
123     }
124 
125     public boolean isEditMode(){
126         // TODO : see CmsFunctions.isEditMode, which checks a couple of other properties.
127         return isAuthorInstance() && !isPreviewMode();
128     }
129 
130     public boolean isPreviewMode(){
131         return MgnlContext.getAggregationState().isPreviewMode();
132     }
133 
134     public boolean isAuthorInstance(){
135         return ServerConfiguration.getInstance().isAdmin();
136     }
137 
138     public boolean isPublicInstance(){
139         return !isAuthorInstance();
140     }
141 
142     public String createLink(Content node) {
143         return LinkUtil.createLink(node);
144     }
145 
146     public String createLink(NodeData nd) {
147         try {
148             return LinkUtil.createLink(nd);
149         } catch (LinkException e) {
150             log.error("Can't resolve link defined in node {} because of {}.", nd.getHandle(), ExceptionUtils.getRootCauseMessage(e));
151             return null;
152         }
153     }
154 
155     public String createLink(String repositoryId, String uuid) {
156         try {
157             return LinkUtil.createLink(repositoryId, uuid);
158         } catch (RepositoryException e) {
159             log.error("Can't resolve link with UUID {} because of {}.", uuid , ExceptionUtils.getRootCauseMessage(e));
160             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     public String createAttribute(String name, String value){
169         value = StringUtils.trim(value);
170         if(StringUtils.isNotEmpty(value)){
171             return new StringBuffer().append(name).append("=\"").append(value).append("\"").toString();
172         }
173         return StringUtils.EMPTY;
174     }
175 
176     public Content getContent(String path){
177       return getContent(ContentRepository.WEBSITE, path);
178     }
179 
180     public Content getContent(String repository, String path){
181         return ContentUtil.getContent(repository, path);
182     }
183 
184     public Content getContentByUUID(String uuid){
185         return getContentByUUID(ContentRepository.WEBSITE, uuid);
186     }
187 
188     public Content getContentByUUID(String repository, String uuid){
189         return ContentUtil.getContentByUUID(repository, uuid);
190     }
191     
192     public static Content encode(Content content){
193         if(content != null){
194             return new HTMLEncodingContentWrapper(content, true);
195         }
196         return null;
197     }
198     
199     public static Content decode(Content content){
200         if(content instanceof HTMLEncodingContentWrapper){
201             return ((HTMLEncodingContentWrapper)content).getWrappedContent();
202         }
203         throw new IllegalStateException("The content to decode is not wrapped by a " + HTMLEncodingContentWrapper.class.getName());
204     }
205 
206 }