View Javadoc
1   /**
2    * This file Copyright (c) 2010-2014 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.templating.jsp.cmsfn;
35  
36  import info.magnolia.cms.util.SiblingsHelper;
37  import info.magnolia.jcr.util.ContentMap;
38  import info.magnolia.objectfactory.Components;
39  import info.magnolia.rendering.template.configured.ConfiguredInheritance;
40  import info.magnolia.templating.functions.TemplatingFunctions;
41  
42  import java.util.Collection;
43  import java.util.List;
44  
45  import javax.jcr.Node;
46  import javax.jcr.Property;
47  import javax.jcr.RepositoryException;
48  
49  import org.apache.commons.lang3.StringUtils;
50  import org.tldgen.annotations.Function;
51  
52  /**
53   * Static wrapper of the Templating function class.
54   * This wrapper gives us the ability to expose these functions as Tags in the JSP's.
55   */
56  public class JspTemplatingFunction {
57  
58      private static TemplatingFunctions templatingFunctions;
59  
60      /**
61       * Convenience method.
62       * Currently, TemplatingFunction (singleton) is accessed once, but in case of we need to change
63       * this and to access it each time, this could be done here (discussed with Greg and Philipp).
64       * Initial Impl:
65       * private static TemplatingFunctions templatingFunctions = Components.getComponent(TemplatingFunctions.class);
66       */
67      private static TemplatingFunctions getTemplatingFunctions() {
68          if (templatingFunctions == null) {
69              templatingFunctions = Components.getComponent(TemplatingFunctions.class);
70          }
71          return templatingFunctions;
72      }
73  
74      @Function
75      public static Node asJCRNode(ContentMap contentMap) {
76          return getTemplatingFunctions().asJCRNode(contentMap);
77      }
78  
79      @Function
80      public static ContentMap asContentMap(Node content) {
81          return getTemplatingFunctions().asContentMap(content);
82      }
83  
84      @Function
85      public static List<ContentMap> children(ContentMap content, String nodeTypeName) throws RepositoryException {
86          if (!nodeTypeName.isEmpty()) {
87              return getTemplatingFunctions().children(content, nodeTypeName);
88          }
89          return getTemplatingFunctions().children(content);
90      }
91  
92      @Function()
93      public static ContentMap root(ContentMap contentMap, String nodeTypeName) throws RepositoryException {
94          if (!nodeTypeName.isEmpty()) {
95              ContentMap root = getTemplatingFunctions().root(contentMap, nodeTypeName);
96              return root;
97          }
98          return getTemplatingFunctions().root(contentMap);
99      }
100 
101     @Function
102     public static ContentMap parent(ContentMap contentMap, String nodeTypeName) throws RepositoryException {
103         if (!nodeTypeName.isEmpty()) {
104             return getTemplatingFunctions().parent(contentMap, nodeTypeName);
105         }
106         return getTemplatingFunctions().parent(contentMap);
107     }
108 
109     /**
110      * Returns the page's {@link ContentMap} of the passed {@link ContentMap}. If the passed {@link ContentMap} represents a page, the passed {@link ContentMap} will be returned.
111      * If the passed {@link ContentMap} has no parent page at all, null is returned.
112      * 
113      * @param content the {@link ContentMap} to get the page's {@link ContentMap} from.
114      * @return returns the page {@link ContentMap} of the passed content {@link ContentMap}.
115      */
116     @Function
117     public static ContentMap page(ContentMap content) throws RepositoryException {
118         return getTemplatingFunctions().page(content);
119     }
120 
121     @Function
122     public static List<ContentMap> ancestors(ContentMap contentMap, String nodeTypeName) throws RepositoryException {
123         if (nodeTypeName.isEmpty()) {
124             return getTemplatingFunctions().ancestors(contentMap);
125         }
126         return getTemplatingFunctions().ancestors(contentMap, nodeTypeName);
127     }
128 
129     @Function
130     public static ContentMap inherit(ContentMap content, String relPath) throws RepositoryException {
131         return JspTemplatingFunction.inherit(content, relPath, null, ConfiguredInheritance.COMPONENTS_FILTERED, ConfiguredInheritance.PROPERTIES_ALL);
132     }
133 
134     @Function
135     public static ContentMap inherit(ContentMap content, String relPath, String nodeTypes, String nodeInheritance, String propertyInheritance) throws RepositoryException {
136         return getTemplatingFunctions().inherit(content, relPath, nodeTypes, nodeInheritance, propertyInheritance);
137     }
138 
139     @Function
140     public static Property inheritProperty(ContentMap content, String relPath) throws RepositoryException {
141         return getTemplatingFunctions().inheritProperty(content, relPath);
142     }
143 
144     @Function
145     public static List<ContentMap> inheritList(ContentMap content, String relPath) throws RepositoryException {
146         return getTemplatingFunctions().inheritList(content, relPath);
147     }
148 
149     @Function
150     public static boolean isInherited(ContentMap content) {
151         return getTemplatingFunctions().isInherited(content);
152     }
153 
154     @Function
155     public static boolean isFromCurrentPage(ContentMap content) {
156         return getTemplatingFunctions().isFromCurrentPage(content);
157     }
158 
159     /**
160      * Create link for the Node identified by nodeIdentifier in the specified workspace.
161      */
162     @Function
163     public static String linkForWorkspace(String workspace, String nodeIdentifier) {
164         return getTemplatingFunctions().link(workspace, nodeIdentifier);
165     }
166 
167     /**
168      * FIXME Add a LinkUtil.createLink(Property property).... Dirty Hack.
169      * FIXME: Should be changed when a decision is made on SCRUM-525.
170      */
171     @Function
172     public static String linkForProperty(Property property) {
173         return getTemplatingFunctions().link(property);
174     }
175 
176     @Function
177     public static String link(ContentMap contentMap) throws RepositoryException {
178         return getTemplatingFunctions().link(contentMap);
179     }
180 
181     @Function
182     public static String link(ContentMap contentMap, String propertyName) throws RepositoryException {
183         return getTemplatingFunctions().link(contentMap);
184     }
185 
186     /**
187      * Get the language used currently.
188      * 
189      * @return The language as a String.
190      */
191     @Function
192     public static String language() {
193         return getTemplatingFunctions().language();
194     }
195 
196     /**
197      * Returns an external link prepended with <code>http://</code> in case the protocol is missing or an empty String
198      * if the link does not exist.
199      * 
200      * @param content The node's map representation where the link property is stored on.
201      * @param linkPropertyName The property where the link value is stored in.
202      * @return The link prepended with <code>http://</code>;
203      */
204     @Function
205     public static String externalLink(ContentMap content, String linkPropertyName) {
206         return getTemplatingFunctions().externalLink(content, linkPropertyName);
207     }
208 
209     /**
210      * Return a link title based on the @param linkTitlePropertyName. When property @param linkTitlePropertyName is
211      * empty or null, the link itself is provided as the linkTitle (prepended with <code>http://</code>).
212      * 
213      * @param content The node where the link property is stored on.
214      * @param linkPropertyName The property where the link value is stored in.
215      * @param linkTitlePropertyName The property where the link title value is stored
216      * @return the resolved link title value
217      */
218     @Function
219     public static String externalLinkTitle(ContentMap content, String linkPropertyName, String linkTitlePropertyName) {
220         return getTemplatingFunctions().externalLinkTitle(content, linkPropertyName, linkTitlePropertyName);
221     }
222 
223     @Function
224     public static boolean isEditMode() {
225         return getTemplatingFunctions().isEditMode();
226     }
227 
228     @Function
229     public static boolean isPreviewMode() {
230         return getTemplatingFunctions().isPreviewMode();
231     }
232 
233     @Function
234     public static boolean isAuthorInstance() {
235         return getTemplatingFunctions().isAuthorInstance();
236     }
237 
238     @Function
239     public static boolean isPublicInstance() {
240         return getTemplatingFunctions().isPublicInstance();
241     }
242 
243     /**
244      * Util method to create html attributes <code>name="value"</code>. If the value is empty an empty string will be returned.
245      * This is mainly helpful to avoid empty attributes.
246      */
247     @Function
248     public static String createHtmlAttribute(String name, String value) {
249         return getTemplatingFunctions().createHtmlAttribute(name, value);
250     }
251 
252     /**
253      * Returns an instance of SiblingsHelper for the given contentMap.
254      */
255     @Function
256     public static SiblingsHelper siblings(ContentMap node) throws RepositoryException {
257         return getTemplatingFunctions().siblings(node);
258     }
259 
260     /**
261      * Return the Node for the Given Path
262      * from the given repository.
263      * If the repository is empty, take the default (website).
264      */
265     @Function
266     public static Node content(String path, String repository) {
267         if (StringUtils.isBlank(repository)) {
268             return getTemplatingFunctions().content(path);
269         }
270         return getTemplatingFunctions().content(repository, path);
271     }
272 
273     /**
274      * Return the Node for the given identifier
275      * from the given repository.
276      * If the repository is empty, take the default (website).
277      */
278     @Function
279     public static Node contentByIdentifier(String id, String repository) {
280         if (StringUtils.isBlank(repository)) {
281             return getTemplatingFunctions().contentByIdentifier(id);
282         }
283         return getTemplatingFunctions().contentByIdentifier(repository, id);
284     }
285 
286     @Function
287     public static List<ContentMap> asContentMapList(Collection<Node> nodeList) {
288         return getTemplatingFunctions().asContentMapList(nodeList);
289     }
290 
291     @Function
292     public static List<Node> asNodeList(Collection<ContentMap> contentMapList) {
293         return getTemplatingFunctions().asNodeList(contentMapList);
294     }
295 
296     /**
297      * Removes escaping of HTML on properties.
298      */
299     @Function
300     public static ContentMap decode(ContentMap content) {
301         return getTemplatingFunctions().decode(content);
302     }
303 
304     /**
305      * Returns the string representation of a property from the metaData of the node or <code>null</code> if the node has no Magnolia metaData or if no matching property is found.
306      */
307     @Function
308     public static String metaData(ContentMap content, String property) {
309         return getTemplatingFunctions().metaData(content, property);
310     }
311 
312     @Function
313     public static Collection<Node> search(String workspace, String statement, String language, String returnItemType) {
314         return getTemplatingFunctions().search(workspace, statement, language, returnItemType);
315     }
316 
317     @Function
318     public static Collection<Node> simpleSearch(String workspace, String statement, String returnItemType, String startPath) {
319         return getTemplatingFunctions().simpleSearch(workspace, statement, returnItemType, startPath);
320     }
321 }