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