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