1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
64
65
66
67
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
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
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
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
172
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 }