View Javadoc

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