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.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
63
64
65
66
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
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
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
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
166
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 }