View Javadoc
1   /**
2    * This file Copyright (c) 2010-2017 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.jcr.util.ContentMap;
37  import info.magnolia.objectfactory.Components;
38  import info.magnolia.rendering.template.configured.ConfiguredInheritance;
39  import info.magnolia.templating.functions.SiblingsHelper;
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     @Function
168     public static String link(ContentMap contentMap) throws RepositoryException {
169         return getTemplatingFunctions().link(contentMap);
170     }
171 
172     @Function
173     public static String link(ContentMap contentMap, String propertyName) throws RepositoryException {
174         return getTemplatingFunctions().link(contentMap);
175     }
176 
177     /**
178      * Get the language used currently.
179      *
180      * @return The language as a String.
181      */
182     @Function
183     public static String language() {
184         return getTemplatingFunctions().language();
185     }
186 
187     /**
188      * Returns an external link prepended with <code>http://</code> in case the protocol is missing or an empty String
189      * if the link does not exist.
190      *
191      * @param content The node's map representation where the link property is stored on.
192      * @param linkPropertyName The property where the link value is stored in.
193      * @return The link prepended with <code>http://</code>;
194      */
195     @Function
196     public static String externalLink(ContentMap content, String linkPropertyName) {
197         return getTemplatingFunctions().externalLink(content, linkPropertyName);
198     }
199 
200     /**
201      * Return a link title based on the @param linkTitlePropertyName. When property @param linkTitlePropertyName is
202      * empty or null, the link itself is provided as the linkTitle (prepended with <code>http://</code>).
203      *
204      * @param content The node where the link property is stored on.
205      * @param linkPropertyName The property where the link value is stored in.
206      * @param linkTitlePropertyName The property where the link title value is stored
207      * @return the resolved link title value
208      */
209     @Function
210     public static String externalLinkTitle(ContentMap content, String linkPropertyName, String linkTitlePropertyName) {
211         return getTemplatingFunctions().externalLinkTitle(content, linkPropertyName, linkTitlePropertyName);
212     }
213 
214     @Function
215     public static boolean isEditMode() {
216         return getTemplatingFunctions().isEditMode();
217     }
218 
219     @Function
220     public static boolean isPreviewMode() {
221         return getTemplatingFunctions().isPreviewMode();
222     }
223 
224     @Function
225     public static boolean isAuthorInstance() {
226         return getTemplatingFunctions().isAuthorInstance();
227     }
228 
229     @Function
230     public static boolean isPublicInstance() {
231         return getTemplatingFunctions().isPublicInstance();
232     }
233 
234     /**
235      * Util method to create html attributes <code>name="value"</code>. If the value is empty an empty string will be returned.
236      * This is mainly helpful to avoid empty attributes.
237      */
238     @Function
239     public static String createHtmlAttribute(String name, String value) {
240         return getTemplatingFunctions().createHtmlAttribute(name, value);
241     }
242 
243     /**
244      * Returns an instance of SiblingsHelper for the given contentMap.
245      */
246     @Function
247     public static SiblingsHelper siblings(ContentMap node) throws RepositoryException {
248         return getTemplatingFunctions().siblings(node);
249     }
250 
251     /**
252      * Return the Node for the Given Path
253      * from the given repository.
254      * If the repository is empty, take the default (website).
255      */
256     @Function
257     public static Node content(String path, String repository) {
258         if (StringUtils.isBlank(repository)) {
259             return getTemplatingFunctions().content(path);
260         }
261         return getTemplatingFunctions().content(repository, path);
262     }
263 
264     /**
265      * Return the Node for the given identifier
266      * from the given repository.
267      * If the repository is empty, take the default (website).
268      */
269     @Function
270     public static Node contentByIdentifier(String id, String repository) {
271         if (StringUtils.isBlank(repository)) {
272             return getTemplatingFunctions().contentByIdentifier(id);
273         }
274         return getTemplatingFunctions().contentByIdentifier(repository, id);
275     }
276 
277     @Function
278     public static List<ContentMap> asContentMapList(Collection<Node> nodeList) {
279         return getTemplatingFunctions().asContentMapList(nodeList);
280     }
281 
282     @Function
283     public static List<Node> asNodeList(Collection<ContentMap> contentMapList) {
284         return getTemplatingFunctions().asNodeList(contentMapList);
285     }
286 
287     /**
288      * Removes escaping of HTML on properties.
289      */
290     @Function
291     public static ContentMap decode(ContentMap content) {
292         return getTemplatingFunctions().decode(content);
293     }
294 
295     /**
296      * 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.
297      */
298     @Function
299     public static String metaData(ContentMap content, String property) {
300         return getTemplatingFunctions().metaData(content, property);
301     }
302 
303     @Function
304     public static Collection<Node> search(String workspace, String statement, String language, String returnItemType) {
305         return getTemplatingFunctions().search(workspace, statement, language, returnItemType);
306     }
307 
308     @Function
309     public static Collection<Node> simpleSearch(String workspace, String statement, String returnItemType, String startPath) {
310         return getTemplatingFunctions().simpleSearch(workspace, statement, returnItemType, startPath);
311     }
312 }